mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
Improve message deletion code
This commit is contained in:
1 parent
42b19ac672
commit
2ffb9caade
4 files changed
+176
-1
No files matched your search
@@ -19,10 +19,19 @@
|
|||||||
*/
|
*/
|
||||||
package net.draycia.carbon.fabric;
|
package net.draycia.carbon.fabric;
|
||||||
|
|
||||||
|
import com.google.common.cache.Cache;
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import com.google.common.collect.MapMaker;
|
||||||
|
import com.google.common.collect.Ordering;
|
||||||
import com.google.inject.Guice;
|
import com.google.inject.Guice;
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
@@ -40,6 +49,7 @@ import net.draycia.carbon.common.util.CloudUtils;
|
|||||||
import net.draycia.carbon.common.util.ListenerUtils;
|
import net.draycia.carbon.common.util.ListenerUtils;
|
||||||
import net.draycia.carbon.common.util.PlayerUtils;
|
import net.draycia.carbon.common.util.PlayerUtils;
|
||||||
import net.draycia.carbon.fabric.callback.ChatCallback;
|
import net.draycia.carbon.fabric.callback.ChatCallback;
|
||||||
|
import net.draycia.carbon.fabric.command.DeleteMessageCommand;
|
||||||
import net.draycia.carbon.fabric.listeners.FabricChatListener;
|
import net.draycia.carbon.fabric.listeners.FabricChatListener;
|
||||||
import net.draycia.carbon.fabric.listeners.FabricChatPreviewListener;
|
import net.draycia.carbon.fabric.listeners.FabricChatPreviewListener;
|
||||||
import net.draycia.carbon.fabric.listeners.FabricPlayerJoinListener;
|
import net.draycia.carbon.fabric.listeners.FabricPlayerJoinListener;
|
||||||
@@ -56,6 +66,7 @@ import net.kyori.adventure.text.Component;
|
|||||||
import net.kyori.adventure.util.TriState;
|
import net.kyori.adventure.util.TriState;
|
||||||
import net.kyori.moonshine.message.IMessageRenderer;
|
import net.kyori.moonshine.message.IMessageRenderer;
|
||||||
import net.luckperms.api.LuckPermsProvider;
|
import net.luckperms.api.LuckPermsProvider;
|
||||||
|
import net.minecraft.network.chat.MessageSignature;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import ninja.egg82.messenger.services.PacketService;
|
import ninja.egg82.messenger.services.PacketService;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
@@ -82,6 +93,10 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
|
|||||||
private TriState luckPermsLoaded = TriState.NOT_SET;
|
private TriState luckPermsLoaded = TriState.NOT_SET;
|
||||||
private final UUID serverId = UUID.randomUUID();
|
private final UUID serverId = UUID.randomUUID();
|
||||||
|
|
||||||
|
private static final Cache<UUID, MessageSignature> messageSignatures = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(10)
|
||||||
|
.build();
|
||||||
|
|
||||||
private @MonotonicNonNull MessagingManager messagingManager = null;
|
private @MonotonicNonNull MessagingManager messagingManager = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -111,11 +126,34 @@ public final class CarbonChatFabric implements ModInitializer, CarbonChat {
|
|||||||
CloudUtils.loadCommands(this.injector);
|
CloudUtils.loadCommands(this.injector);
|
||||||
final var commandSettings = CloudUtils.loadCommandSettings(this.injector);
|
final var commandSettings = CloudUtils.loadCommandSettings(this.injector);
|
||||||
CloudUtils.registerCommands(commandSettings);
|
CloudUtils.registerCommands(commandSettings);
|
||||||
|
this.injector.getInstance(DeleteMessageCommand.class).init();
|
||||||
|
|
||||||
// Load channels
|
// Load channels
|
||||||
((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(this.carbonMessages);
|
((CarbonChannelRegistry) this.channelRegistry()).loadConfigChannels(this.carbonMessages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void addMessageSignature(final UUID uuid, final MessageSignature messageSignature) {
|
||||||
|
messageSignatures.put(uuid, messageSignature);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @Nullable MessageSignature messageSignature(final UUID uuid) {
|
||||||
|
return messageSignatures.getIfPresent(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Set<UUID> messageIds() {
|
||||||
|
return messageSignatures.asMap().keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> messageIdSuggestions() {
|
||||||
|
final List<String> suggestions = new ArrayList<>();
|
||||||
|
|
||||||
|
for (final UUID messageId : messageSignatures.asMap().keySet()) {
|
||||||
|
suggestions.add(messageId.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return suggestions;
|
||||||
|
}
|
||||||
|
|
||||||
private void registerChatListener() {
|
private void registerChatListener() {
|
||||||
ChatCallback.setup();
|
ChatCallback.setup();
|
||||||
ChatCallback.INSTANCE.registerListener(new FabricChatListener(this, this.channelRegistry));
|
ChatCallback.INSTANCE.registerListener(new FabricChatListener(this, this.channelRegistry));
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/*
|
||||||
|
* CarbonChat
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Josua Parks (Vicarious)
|
||||||
|
* Contributors
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package net.draycia.carbon.fabric.command;
|
||||||
|
|
||||||
|
import cloud.commandframework.CommandManager;
|
||||||
|
import cloud.commandframework.arguments.standard.UUIDArgument;
|
||||||
|
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import net.draycia.carbon.common.command.CarbonCommand;
|
||||||
|
import net.draycia.carbon.common.command.CommandSettings;
|
||||||
|
import net.draycia.carbon.common.command.Commander;
|
||||||
|
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
|
||||||
|
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||||
|
import net.draycia.carbon.fabric.CarbonChatFabric;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
|
import net.minecraft.network.chat.MessageSignature;
|
||||||
|
import net.minecraft.network.protocol.game.ClientboundDeleteChatPacket;
|
||||||
|
import net.minecraft.server.level.ServerPlayer;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
|
|
||||||
|
@DefaultQualifier(NonNull.class)
|
||||||
|
public class DeleteMessageCommand extends CarbonCommand {
|
||||||
|
|
||||||
|
final CarbonChatFabric carbonChat;
|
||||||
|
final CommandManager<Commander> commandManager;
|
||||||
|
final CarbonMessages messageService;
|
||||||
|
final PlayerSuggestions playerSuggestions;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public DeleteMessageCommand(
|
||||||
|
final CarbonChatFabric carbonChat,
|
||||||
|
final CommandManager<Commander> commandManager,
|
||||||
|
final CarbonMessages messageService,
|
||||||
|
final PlayerSuggestions playerSuggestions
|
||||||
|
) {
|
||||||
|
this.carbonChat = carbonChat;
|
||||||
|
this.commandManager = commandManager;
|
||||||
|
this.messageService = messageService;
|
||||||
|
this.playerSuggestions = playerSuggestions;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected CommandSettings _commandSettings() {
|
||||||
|
return new CommandSettings("deletemessage");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Key key() {
|
||||||
|
return Key.key("carbon", "deletemessage");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
|
||||||
|
.argument(UUIDArgument.<Commander>newBuilder("messageid").withSuggestionsProvider((sender, input) -> {
|
||||||
|
final List<String> messageIds = CarbonChatFabric.messageIdSuggestions();
|
||||||
|
|
||||||
|
if (input.isEmpty()) {
|
||||||
|
return messageIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<String> suggestions = new LinkedList<>();
|
||||||
|
|
||||||
|
for (final String messageId : messageIds) {
|
||||||
|
if (messageId.startsWith(input)) {
|
||||||
|
suggestions.add(messageId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return suggestions;
|
||||||
|
}).build())
|
||||||
|
.permission("carbon.updateusername")
|
||||||
|
.senderType(Commander.class)
|
||||||
|
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.messageService.commandUpdateUsernameDescription().component())
|
||||||
|
.handler(handler -> {
|
||||||
|
final @Nullable MessageSignature messageSignature = CarbonChatFabric.messageSignature(handler.get("messageid"));
|
||||||
|
|
||||||
|
if (messageSignature != null) {
|
||||||
|
for (final ServerPlayer player : this.carbonChat.minecraftServer().getPlayerList().getPlayers()) {
|
||||||
|
player.connection.send(new ClientboundDeleteChatPacket(messageSignature));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
|
||||||
|
this.commandManager.command(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package net.draycia.carbon.fabric.mixin;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
import net.draycia.carbon.fabric.CarbonChatFabric;
|
||||||
|
import net.minecraft.network.protocol.Packet;
|
||||||
|
import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket;
|
||||||
|
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.ModifyVariable;
|
||||||
|
|
||||||
|
@Mixin(ServerGamePacketListenerImpl.class)
|
||||||
|
abstract class ChatListenerMixin {
|
||||||
|
|
||||||
|
@ModifyVariable(at = @At("HEAD"), index = 1, method = "send(Lnet/minecraft/network/protocol/Packet;Lio/netty/util/concurrent/GenericFutureListener;)V", argsOnly = true)
|
||||||
|
private Packet<?> sendPacket(Packet<?> packet) {
|
||||||
|
if (packet instanceof ClientboundPlayerChatPacket chatPacket) {
|
||||||
|
final UUID uuid = UUID.nameUUIDFromBytes(chatPacket.message().headerSignature().bytes());
|
||||||
|
CarbonChatFabric.addMessageSignature(uuid, chatPacket.message().headerSignature());
|
||||||
|
}
|
||||||
|
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,7 +6,8 @@
|
|||||||
"mixins": [
|
"mixins": [
|
||||||
"ServerGamePacketListenerImplMixin",
|
"ServerGamePacketListenerImplMixin",
|
||||||
"ServerPlayerMixin",
|
"ServerPlayerMixin",
|
||||||
"PlayerListMixin"
|
"PlayerListMixin",
|
||||||
|
"ChatListenerMixin"
|
||||||
],
|
],
|
||||||
"client": [
|
"client": [
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in new issue
Block a user