From 865abd7c29b9e0233fdd04731f371ef814ce7956 Mon Sep 17 00:00:00 2001 From: Marcel Kempf Date: Sun, 23 May 2021 20:30:11 +0200 Subject: [PATCH] rework lol --- src/main/java/net/saltymc/eaa/EaaMod.java | 38 +------ .../saltymc/eaa/handler/CommandHandler.java | 103 ------------------ .../saltymc/eaa/handler/HandlerInterface.java | 8 -- .../saltymc/eaa/handler/commands/Command.java | 22 ---- .../eaa/handler/commands/EaaCommand.java | 60 ---------- .../eaa/handler/commands/EaaModCommand.java | 21 ++++ .../eaa/handler/commands/EchoCommand.java | 39 +++++++ .../net/saltymc/eaa/mixin/ExampleMixin.java | 25 +---- 8 files changed, 65 insertions(+), 251 deletions(-) delete mode 100644 src/main/java/net/saltymc/eaa/handler/CommandHandler.java delete mode 100644 src/main/java/net/saltymc/eaa/handler/HandlerInterface.java delete mode 100644 src/main/java/net/saltymc/eaa/handler/commands/Command.java delete mode 100644 src/main/java/net/saltymc/eaa/handler/commands/EaaCommand.java create mode 100644 src/main/java/net/saltymc/eaa/handler/commands/EaaModCommand.java create mode 100644 src/main/java/net/saltymc/eaa/handler/commands/EchoCommand.java diff --git a/src/main/java/net/saltymc/eaa/EaaMod.java b/src/main/java/net/saltymc/eaa/EaaMod.java index a956c01..bb3c6cc 100644 --- a/src/main/java/net/saltymc/eaa/EaaMod.java +++ b/src/main/java/net/saltymc/eaa/EaaMod.java @@ -1,32 +1,10 @@ package net.saltymc.eaa; import net.fabricmc.api.ModInitializer; -import net.saltymc.eaa.handler.CommandHandler; -import net.saltymc.eaa.handler.HandlerInterface; -import net.saltymc.eaa.util.ResponseEntity; - -import java.util.ArrayList; -import java.util.List; +import net.saltymc.eaa.handler.commands.EchoCommand; public class EaaMod implements ModInitializer { - private static EaaMod instance; - private final List handler; - - public EaaMod(){ - instance = this; - handler = new ArrayList<>(); - } - - public static EaaMod getInstance(){ - if (instance == null) { - instance = new EaaMod(); - instance.onInitialize(); - } - - return instance; - } - @Override public void onInitialize() { // This code runs as soon as Minecraft is in a mod-load-ready state. @@ -35,19 +13,7 @@ public class EaaMod implements ModInitializer { System.out.println("EAA Mod initializing..."); - //Init CommandHandler - handler.add(new CommandHandler()); + new EchoCommand(); } - - public ResponseEntity onEvent(Object object){ - for (HandlerInterface hi : handler) { - ResponseEntity handlerResponse = hi.handle(object); - if (handlerResponse.isWasHandled()) { - return handlerResponse; // Events are exclusive yet - } - } - - return new ResponseEntity(false); - } } diff --git a/src/main/java/net/saltymc/eaa/handler/CommandHandler.java b/src/main/java/net/saltymc/eaa/handler/CommandHandler.java deleted file mode 100644 index 30a2e3d..0000000 --- a/src/main/java/net/saltymc/eaa/handler/CommandHandler.java +++ /dev/null @@ -1,103 +0,0 @@ -package net.saltymc.eaa.handler; - -import com.mojang.brigadier.exceptions.BuiltInExceptionProvider; -import com.mojang.brigadier.exceptions.CommandExceptionType; -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; -import net.minecraft.client.MinecraftClient; -import net.minecraft.text.Text; -import net.minecraft.text.Texts; -import net.minecraft.text.TranslatableText; -import net.saltymc.eaa.handler.commands.Command; -import net.saltymc.eaa.handler.commands.EaaCommand; -import net.saltymc.eaa.util.ResponseEntity; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.ArrayList; -import java.util.List; - -public class CommandHandler implements HandlerInterface { - - private static final String PREFIX = "#"; - private static final Logger LOGGER = LogManager.getLogger(); - private static List commands; - //above line stolen from net.fabricmc.fabric.impl.command.client.ClientCommandInternals; - - private static FabricClientCommandSource commandSource; - - - public CommandHandler(){ - LOGGER.info("init CommandHandler"); - commands = new ArrayList<>(); - - CommandHandler.commands.add(new EaaCommand()); - } - - /** - * Executes a client-sided command from a message. - * - * @param message the command message - * @return true if the message should not be sent to the server, false otherwise - */ - public boolean executeCommand(String message) { - if (message.isEmpty()) { - return false; // Nothing to process - } - - if (!message.matches("^" + PREFIX + ".*")) { - return false; // Incorrect prefix, won't execute anything. - } - - FabricClientCommandSource commandSource = (FabricClientCommandSource) MinecraftClient.getInstance().getNetworkHandler().getCommandSource(); - - // Build args - String[] args = message.replaceFirst(PREFIX,"").split(" "); // split into args and remove prefix - - LOGGER.debug("Valid Prefix received"); - - for (Command command : commands){ - if (command.acceptInput(args[0])) { // Try execute command - command.run(commandSource, args); - LOGGER.debug("Executed following command: " + message); - return command.intercept(); // if executed return interception flag - } - } - - return false; // Default dont intercept messages - - } - - /** - * Tests whether a command syntax exception with the type - * should be ignored and the message sent to the server. - * - * @param type the exception type - * @return true if ignored, false otherwise - */ - private static boolean isIgnoredException(CommandExceptionType type) { - BuiltInExceptionProvider builtins = CommandSyntaxException.BUILT_IN_EXCEPTIONS; - - // Only ignore unknown commands and node parse exceptions. - // The argument-related dispatcher exceptions are not ignored because - // they will only happen if the user enters a correct command. - return type == builtins.dispatcherUnknownCommand() || type == builtins.dispatcherParseException(); - } - - // See CommandSuggestor.method_30505. That cannot be used directly as it returns an OrderedText instead of a Text. - private static Text getErrorMessage(CommandSyntaxException e) { - Text message = Texts.toText(e.getRawMessage()); - String context = e.getContext(); - - return context != null ? new TranslatableText("command.context.parse_error", message, context) : message; - } - - - @Override - public ResponseEntity handle(Object object) { - if (object instanceof String) - return new ResponseEntity(true, executeCommand((String) object)); // object matches handle event and return positive respondentsenity - - return new ResponseEntity(false); // object not matching return false - } -} diff --git a/src/main/java/net/saltymc/eaa/handler/HandlerInterface.java b/src/main/java/net/saltymc/eaa/handler/HandlerInterface.java deleted file mode 100644 index c82d2fc..0000000 --- a/src/main/java/net/saltymc/eaa/handler/HandlerInterface.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.saltymc.eaa.handler; - -import net.saltymc.eaa.util.ResponseEntity; - -public interface HandlerInterface { - - ResponseEntity handle(Object object); -} diff --git a/src/main/java/net/saltymc/eaa/handler/commands/Command.java b/src/main/java/net/saltymc/eaa/handler/commands/Command.java deleted file mode 100644 index 30c324b..0000000 --- a/src/main/java/net/saltymc/eaa/handler/commands/Command.java +++ /dev/null @@ -1,22 +0,0 @@ -package net.saltymc.eaa.handler.commands; - -import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; - -import java.util.Set; - -public interface Command { - - boolean run(final FabricClientCommandSource cs, String[] args); - - String getCommand(); - - Set getAlias(); - - boolean acceptInput(String command); - - /** - * should message get intercepted and not send to Server? - */ - boolean intercept(); - -} diff --git a/src/main/java/net/saltymc/eaa/handler/commands/EaaCommand.java b/src/main/java/net/saltymc/eaa/handler/commands/EaaCommand.java deleted file mode 100644 index f903755..0000000 --- a/src/main/java/net/saltymc/eaa/handler/commands/EaaCommand.java +++ /dev/null @@ -1,60 +0,0 @@ -package net.saltymc.eaa.handler.commands; - -import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; -import net.minecraft.text.Text; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -public class EaaCommand implements Command { - - private static final String COMMAND = "echo"; - private static final Set ALIAS = new HashSet<>(Arrays.asList("e")); - private static final boolean INTERCEPT = true; - private static final Logger LOGGER = LogManager.getLogger(); - - public EaaCommand(){ - LOGGER.info("Init EaaCommand"); - } - - @Override - public boolean run( final FabricClientCommandSource cs, String[] args) { - System.out.println("on Command: " + args[0]); - - if (!args[0].equalsIgnoreCase(COMMAND)) { - return false; - } - - StringBuilder sb = new StringBuilder(); - for (String arg : args) - sb.append(arg); - - cs.sendFeedback(Text.of(sb.toString().replaceFirst(args[0], ""))); - - - return true; - } - - @Override - public String getCommand() { - return COMMAND; - } - - @Override - public Set getAlias() { - return ALIAS; - } - - @Override - public boolean acceptInput(String command) { - return (command.equalsIgnoreCase(getCommand()) || getAlias().contains(command.toLowerCase())); - } - - @Override - public boolean intercept() { - return INTERCEPT; - } -} \ No newline at end of file diff --git a/src/main/java/net/saltymc/eaa/handler/commands/EaaModCommand.java b/src/main/java/net/saltymc/eaa/handler/commands/EaaModCommand.java new file mode 100644 index 0000000..f7ca365 --- /dev/null +++ b/src/main/java/net/saltymc/eaa/handler/commands/EaaModCommand.java @@ -0,0 +1,21 @@ +package net.saltymc.eaa.handler.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; + +import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; + +public abstract class EaaModCommand implements Command { + + public EaaModCommand() { + this.register(); + } + + public void register(){ + ClientCommandManager.DISPATCHER.register(getCommandSpecification().executes(this)); + } + + public abstract LiteralArgumentBuilder getCommandSpecification(); + +} diff --git a/src/main/java/net/saltymc/eaa/handler/commands/EchoCommand.java b/src/main/java/net/saltymc/eaa/handler/commands/EchoCommand.java new file mode 100644 index 0000000..3c2de3b --- /dev/null +++ b/src/main/java/net/saltymc/eaa/handler/commands/EchoCommand.java @@ -0,0 +1,39 @@ +package net.saltymc.eaa.handler.commands; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.fabricmc.fabric.api.client.command.v1.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; +import net.minecraft.client.toast.SystemToast; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; + + +public class EchoCommand extends EaaModCommand { + @Override + public int run(CommandContext context) throws CommandSyntaxException { + FabricClientCommandSource source = (FabricClientCommandSource) (context.getSource()); + + SystemToast.add(source.getClient().getToastManager(), SystemToast.Type.TUTORIAL_HINT, Text.of("ECHO"), Text.of(context.getInput())); + + source.sendFeedback( + new LiteralText("You said input: ") + .formatted(Formatting.RED) + .append(new LiteralText(getString(context, "message")) + .formatted(Formatting.WHITE, Formatting.ITALIC) + ) + ); + return SINGLE_SUCCESS; + } + + @Override + public LiteralArgumentBuilder getCommandSpecification() { + return ClientCommandManager.literal("echo") + .then(ClientCommandManager.argument("message", StringArgumentType.greedyString())); + } +} diff --git a/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java b/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java index 869803c..ed1fb6e 100644 --- a/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java +++ b/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java @@ -1,34 +1,15 @@ package net.saltymc.eaa.mixin; -import net.minecraft.client.network.ClientPlayerEntity; -import net.saltymc.eaa.EaaMod; -import net.saltymc.eaa.util.ResponseEntity; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import net.minecraft.client.gui.screen.TitleScreen; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -/* + @Mixin(TitleScreen.class) public class ExampleMixin { @Inject(at = @At("HEAD"), method = "init()V") private void init(CallbackInfo info) { - System.out.println("This line is printed by an example mod mixin!"); - } -} -*/ -@Mixin(ClientPlayerEntity.class) -public class ExampleMixin { //ClientPlayerEntityMixin - - private static final Logger LOGGER = LogManager.getLogger(); - - @Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true) - private void onSendChatMessage(String message, CallbackInfo info) { - ResponseEntity responseEntity = EaaMod.getInstance().onEvent(message); - - if (responseEntity.isInterceptEvent()) - info.cancel(); - + System.out.println("This line is printed by EAA MOD!"); } } \ No newline at end of file