diff --git a/api/src/main/java/me/bymartrixx/playerevents/api/mixin/ServerPlayNetworkHandlerMixin.java b/api/src/main/java/me/bymartrixx/playerevents/api/mixin/ServerPlayNetworkHandlerMixin.java index 5241dba..9c7893a 100644 --- a/api/src/main/java/me/bymartrixx/playerevents/api/mixin/ServerPlayNetworkHandlerMixin.java +++ b/api/src/main/java/me/bymartrixx/playerevents/api/mixin/ServerPlayNetworkHandlerMixin.java @@ -2,46 +2,30 @@ import me.bymartrixx.playerevents.api.event.CommandExecutionCallback; import me.bymartrixx.playerevents.api.event.PlayerLeaveCallback; +import net.minecraft.network.message.MessageSignatureList; import net.minecraft.network.packet.c2s.play.ChatCommandC2SPacket; -import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; -import net.minecraft.unmapped.C_mofgmouu; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import java.util.Optional; @Mixin(ServerPlayNetworkHandler.class) -@SuppressWarnings({"InvalidInjectorMethodSignature", "MixinAnnotationTarget", "UnresolvedMixinReference"}) +@SuppressWarnings({"MixinAnnotationTarget", "UnresolvedMixinReference"}) public class ServerPlayNetworkHandlerMixin { @Shadow public ServerPlayerEntity player; // Just after the command is executed - // 1.19 - @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;checkForSpam()V"), - method = "onChatCommand", locals = LocalCapture.CAPTURE_FAILSOFT, require = 0) - private void onCommandExecuted(ChatCommandC2SPacket packet, CallbackInfo ci, ServerCommandSource source) { - CommandExecutionCallback.EVENT.invoker().onExecuted(packet.command(), source); - } - - // 1.19.1/2 - @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;checkForSpam()V"), - method = "method_44356(Lnet/minecraft/class_7472;)V", require = 0) - private void onCommandExecuted(ChatCommandC2SPacket packet, CallbackInfo ci) { - CommandExecutionCallback.EVENT.invoker().onExecuted(packet.command(), this.player.getCommandSource()); - } - - // 1.19.3 + // 1.20 @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;checkForSpam()V"), method = "method_44356(Lnet/minecraft/class_7472;Ljava/util/Optional;)V", require = 0) - private void onCommandExecuted(ChatCommandC2SPacket packet, Optional optional, CallbackInfo ci) { + private void onCommandExecuted(ChatCommandC2SPacket packet, Optional optional, CallbackInfo ci) { CommandExecutionCallback.EVENT.invoker().onExecuted(packet.command(), this.player.getCommandSource()); } diff --git a/src/main/java/me/bymartrixx/playerevents/command/ReloadCommand.java b/src/main/java/me/bymartrixx/playerevents/command/ReloadCommand.java index f27d599..bb762cb 100644 --- a/src/main/java/me/bymartrixx/playerevents/command/ReloadCommand.java +++ b/src/main/java/me/bymartrixx/playerevents/command/ReloadCommand.java @@ -3,16 +3,20 @@ import com.mojang.brigadier.tree.LiteralCommandNode; import me.bymartrixx.playerevents.config.PlayerEventsConfig; import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.MutableText; import net.minecraft.text.Text; import static net.minecraft.server.command.CommandManager.literal; public class ReloadCommand { + + private static final MutableText RELOADING_TEXT = Text.literal("Reloading Player Events config..."); + public static LiteralCommandNode getNode() { return literal("reload") .executes(context -> { ServerCommandSource source = context.getSource(); - source.sendFeedback(Text.literal("Reloading Player Events config..."), true); + source.sendFeedback(() -> RELOADING_TEXT, true); PlayerEventsConfig.Manager.loadConfig(); return 1; diff --git a/src/main/java/me/bymartrixx/playerevents/config/PlayerEventsConfig.java b/src/main/java/me/bymartrixx/playerevents/config/PlayerEventsConfig.java index c0174f2..c9deb9a 100644 --- a/src/main/java/me/bymartrixx/playerevents/config/PlayerEventsConfig.java +++ b/src/main/java/me/bymartrixx/playerevents/config/PlayerEventsConfig.java @@ -17,6 +17,7 @@ import net.minecraft.server.function.CommandFunction; import net.minecraft.server.function.CommandFunctionManager; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.MutableText; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; @@ -34,6 +35,9 @@ import static me.bymartrixx.playerevents.util.PlaceholderReplacingUtil.lazyResolver; public class PlayerEventsConfig { + private static final String TEST_TEXT_PREFIX = String.valueOf(Formatting.GRAY) + Formatting.ITALIC; + private static final MutableText PLAYER_ONLY_COMMAND_ERROR_TEXT = Text.literal("This command can only be executed by players"); + public static class Manager { private static File configFile; @@ -174,7 +178,9 @@ public static void testSimpleAction(ActionList actionList, ServerCommandSource s if (actionList.pickMessageRandomly()) { message = message + " [Message picked randomly]"; } - source.sendFeedback(Text.literal("" + Formatting.GRAY + Formatting.ITALIC + message), false); + + String finalMessage = message; + source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + finalMessage), false); Map placeholderArgs = playerPlaceholder(source); @@ -191,9 +197,9 @@ public static void testAction(String action, ServerCommandSource source, if (action.startsWith("/")) { String command = message.getString(); - source.sendFeedback(Text.literal("[COMMAND] " + command), false); + source.sendFeedback(() -> Text.literal("[COMMAND] " + command), false); } else { - source.sendFeedback(message, false); + source.sendFeedback(() -> message, false); } } @@ -301,11 +307,7 @@ public void doCustomCommandsActions(String command, ServerCommandSource source) // Run actions from the event to allow editing the actions without restarting the server for (CustomCommandActionList customCommand : this.customCommands) { if (command.trim().equals(customCommand.getCommand())) { - try { - doSimpleAction(customCommand, source.getPlayer()); - } catch (CommandSyntaxException e) { - PlayerEvents.LOGGER.error("This should not happen, please report it to the mod author, attaching the logs", e); - } + doSimpleAction(customCommand, source.getPlayer()); } } } @@ -332,7 +334,7 @@ public void testLeaveActions(ServerCommandSource source) { public void testKillEntityActions(ServerCommandSource source) { String message = String.format("Kill entity actions (%s):", this.killEntity.doBroadcastToEveryone() ? "Send to everyone" : "Send only to the player"); - source.sendFeedback(Text.literal("" + Formatting.GRAY + Formatting.ITALIC + message), false); + source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + message), false); Map placeholderArgs = playerPlaceholder(source); placeholderArgs.put("killedEntity", lazyResolver((subKey) -> switch (subKey) { @@ -350,7 +352,7 @@ public void testKillEntityActions(ServerCommandSource source) { public void testKillPlayerActions(ServerCommandSource source) { String message = String.format("Kill player actions (%s):", this.killPlayer.doBroadcastToEveryone() ? "Send to everyone" : "Send only to the player"); - source.sendFeedback(Text.literal("" + Formatting.GRAY + Formatting.ITALIC + message), false); + source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + message), false); Map placeholderArgs = playerPlaceholder(source); placeholderArgs.put("killedPlayer", source); @@ -365,7 +367,7 @@ public void testCustomCommandsActions(ServerCommandSource source) { for (CustomCommandActionList actionList : this.customCommands) { String message = String.format("'%s' actions ('%s'):", actionList.getCommandStr(), actionList.doBroadcastToEveryone() ? "Send to everyone" : "Send only to the player"); - source.sendFeedback(Text.literal("§7§o" + message), false); + source.sendFeedback(() -> Text.literal(TEST_TEXT_PREFIX + message), false); for (String action : actionList.actions) { testAction(action, source, placeholderArgs); @@ -397,7 +399,7 @@ public void registerCustomCommands(CommandDispatcher dispat private int executeCommand(CommandContext ctx) { ServerCommandSource source = ctx.getSource(); if (!(source.getEntity() instanceof ServerPlayerEntity)) { - source.sendFeedback(Text.literal("This command can only be executed by players"), false); + source.sendFeedback(() -> PLAYER_ONLY_COMMAND_ERROR_TEXT, false); } return 1;