From f19ea5c876a81e40f8e48946e7b277c0c9ddb180 Mon Sep 17 00:00:00 2001 From: "Ansgar [Hiajen]" Date: Sat, 22 May 2021 12:48:12 +0200 Subject: [PATCH] [refactor] try to generate general structures --- src/main/java/net/saltymc/eaa/EaaMod.java | 27 ++++++ .../net/saltymc/eaa/commands/Command.java | 9 -- .../net/saltymc/eaa/commands/EaaCommand.java | 13 --- .../{commands => handler}/CommandHandler.java | 87 ++++++++----------- .../saltymc/eaa/handler/HandlerInterface.java | 8 ++ .../saltymc/eaa/handler/commands/Command.java | 14 +++ .../eaa/handler/commands/EaaCommand.java | 31 +++++++ .../net/saltymc/eaa/mixin/ExampleMixin.java | 20 +++-- .../net/saltymc/eaa/mixin/MixinInterface.java | 13 +++ .../net/saltymc/eaa/util/ResponseEntity.java | 29 +++++++ 10 files changed, 172 insertions(+), 79 deletions(-) delete mode 100644 src/main/java/net/saltymc/eaa/commands/Command.java delete mode 100644 src/main/java/net/saltymc/eaa/commands/EaaCommand.java rename src/main/java/net/saltymc/eaa/{commands => handler}/CommandHandler.java (50%) create mode 100644 src/main/java/net/saltymc/eaa/handler/HandlerInterface.java create mode 100644 src/main/java/net/saltymc/eaa/handler/commands/Command.java create mode 100644 src/main/java/net/saltymc/eaa/handler/commands/EaaCommand.java create mode 100644 src/main/java/net/saltymc/eaa/mixin/MixinInterface.java create mode 100644 src/main/java/net/saltymc/eaa/util/ResponseEntity.java diff --git a/src/main/java/net/saltymc/eaa/EaaMod.java b/src/main/java/net/saltymc/eaa/EaaMod.java index 832629c..e0ba7d2 100644 --- a/src/main/java/net/saltymc/eaa/EaaMod.java +++ b/src/main/java/net/saltymc/eaa/EaaMod.java @@ -1,8 +1,21 @@ 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; public class EaaMod implements ModInitializer { + + private final List handler; + + public EaaMod(){ + handler = new ArrayList<>(); + } + @Override public void onInitialize() { // This code runs as soon as Minecraft is in a mod-load-ready state. @@ -10,5 +23,19 @@ public class EaaMod implements ModInitializer { // Proceed with mild caution. System.out.println("EAA Mod initializing..."); + + //Init CommandHandler + handler.add(new CommandHandler()); + + } + + 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/commands/Command.java b/src/main/java/net/saltymc/eaa/commands/Command.java deleted file mode 100644 index 77b1837..0000000 --- a/src/main/java/net/saltymc/eaa/commands/Command.java +++ /dev/null @@ -1,9 +0,0 @@ -package net.saltymc.eaa.commands; - -import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; - -public interface Command { - - int run(FabricClientCommandSource cs, String[] args); - -} diff --git a/src/main/java/net/saltymc/eaa/commands/EaaCommand.java b/src/main/java/net/saltymc/eaa/commands/EaaCommand.java deleted file mode 100644 index 1193e52..0000000 --- a/src/main/java/net/saltymc/eaa/commands/EaaCommand.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.saltymc.eaa.commands; - -import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; -import net.minecraft.text.Text; - -public class EaaCommand implements Command{ - @Override - public int run(FabricClientCommandSource cs, String[] args) { - cs.sendFeedback(Text.of("command catched!")); - - return 0; - } -} \ No newline at end of file diff --git a/src/main/java/net/saltymc/eaa/commands/CommandHandler.java b/src/main/java/net/saltymc/eaa/handler/CommandHandler.java similarity index 50% rename from src/main/java/net/saltymc/eaa/commands/CommandHandler.java rename to src/main/java/net/saltymc/eaa/handler/CommandHandler.java index a9ef4e3..81679d3 100644 --- a/src/main/java/net/saltymc/eaa/commands/CommandHandler.java +++ b/src/main/java/net/saltymc/eaa/handler/CommandHandler.java @@ -1,39 +1,36 @@ -package net.saltymc.eaa.commands; +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.command.CommandException; -import net.minecraft.network.MessageType; -import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.text.Texts; import net.minecraft.text.TranslatableText; -import net.minecraft.util.Formatting; -import net.minecraft.util.Util; -import org.apache.logging.log4j.Level; +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.HashMap; +import java.util.ArrayList; +import java.util.List; -import static net.fabricmc.fabric.api.client.command.v1.ClientCommandManager.DISPATCHER; +public class CommandHandler implements HandlerInterface { -public class CommandHandler { - - private static final char PREFIX = '/'; + private static final String PREFIX = "#"; private static final Logger LOGGER = LogManager.getLogger(); - private static HashMap commands; + private static List commands; //above line stolen from net.fabricmc.fabric.impl.command.client.ClientCommandInternals; - static { - if(CommandHandler.commands == null){ - CommandHandler.commands = new HashMap<>(); - } - CommandHandler.commands.put("p", new EaaCommand()); - //TODO implement + private static FabricClientCommandSource commandSource; + + + public CommandHandler(){ + commands = new ArrayList<>(); + + CommandHandler.commands.add(new EaaCommand()); } /** @@ -42,52 +39,31 @@ public class CommandHandler { * @param message the command message * @return true if the message should not be sent to the server, false otherwise */ - public static boolean executeCommand(String message) { + public boolean executeCommand(String message) { if (message.isEmpty()) { return false; // Nothing to process } - if (message.charAt(0) != PREFIX) { + if (!message.matches("^" + PREFIX + ".*")) { return false; // Incorrect prefix, won't execute anything. } FabricClientCommandSource commandSource = (FabricClientCommandSource) MinecraftClient.getInstance().getNetworkHandler().getCommandSource(); - //commandSource.getClient().getProfiler().push(message); + // Build args + String[] args = message.replaceFirst(PREFIX,"").split(" "); // split into args and remove prefix - try { - //DISPATCHER.execute(message.substring(1), commandSource); - String command = message.replaceFirst(""+PREFIX,"").split(" ")[0]; - String[] args = message.replaceFirst(""+PREFIX,"").split(" "); //TODO remove first item from array + LOGGER.debug("Valid Prefix received"); - System.out.println("command: $"+command+"$"); - - if(CommandHandler.commands.get(command) != null) { - CommandHandler.commands.get(command).run(commandSource, args); - }else{ - return false; + for (Command command : commands){ + if (command.run(commandSource, args)) { // Try execute command + LOGGER.debug("Executed following command: " + message); + return command.intercept(); // if executed return interception flag } - - return true; - /*} catch (CommandSyntaxException e) { - boolean ignored = isIgnoredException(e.getType()); - LOGGER.log(ignored ? Level.DEBUG : Level.WARN, "Syntax exception for client-sided command '{}'", message, e); - if (ignored) { - return false; - } - commandSource.sendError(getErrorMessage(e)); - return true;*/ - } catch (CommandException e) { - LOGGER.warn("Error while executing client-sided command '{}'", message, e); - commandSource.sendError(e.getTextMessage()); - return true; - } catch (RuntimeException e) { - LOGGER.warn("Error while executing client-sided command '{}'", message, e); - commandSource.sendError(Text.of(e.getMessage())); - return true; - } finally { - //commandSource.getClient().getProfiler().pop(); } + + return false; // Default dont intercept messages + } /** @@ -115,4 +91,11 @@ public class CommandHandler { } + @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 new file mode 100644 index 0000000..c82d2fc --- /dev/null +++ b/src/main/java/net/saltymc/eaa/handler/HandlerInterface.java @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..050d01e --- /dev/null +++ b/src/main/java/net/saltymc/eaa/handler/commands/Command.java @@ -0,0 +1,14 @@ +package net.saltymc.eaa.handler.commands; + +import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; + +public interface Command { + + boolean run(final FabricClientCommandSource cs, String[] args); + + /** + * 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 new file mode 100644 index 0000000..9c3e9eb --- /dev/null +++ b/src/main/java/net/saltymc/eaa/handler/commands/EaaCommand.java @@ -0,0 +1,31 @@ +package net.saltymc.eaa.handler.commands; + +import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; +import net.minecraft.text.Text; + +public class EaaCommand implements Command { + + private static final String COMMAND = "echo"; + private static final boolean INTERCEPT = true; + + @Override + public boolean run( final FabricClientCommandSource cs, String[] args) { + 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 boolean intercept() { + return INTERCEPT; + } +} \ No newline at end of file diff --git a/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java b/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java index d514018..d78e435 100644 --- a/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java +++ b/src/main/java/net/saltymc/eaa/mixin/ExampleMixin.java @@ -1,8 +1,9 @@ package net.saltymc.eaa.mixin; -import net.minecraft.client.gui.screen.TitleScreen; import net.minecraft.client.network.ClientPlayerEntity; -import net.saltymc.eaa.commands.CommandHandler; +import net.saltymc.eaa.EaaMod; +import net.saltymc.eaa.handler.CommandHandler; +import net.saltymc.eaa.util.ResponseEntity; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -17,12 +18,21 @@ public class ExampleMixin { } */ @Mixin(ClientPlayerEntity.class) -public class ExampleMixin { //ClientPlayerEntityMixin +public class ExampleMixin extends MixinInterface { //ClientPlayerEntityMixin + + public ExampleMixin(final EaaMod eaaMod){ + super(eaaMod); + } + + @Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true) private void onSendChatMessage(String message, CallbackInfo info) { System.out.println("in onSendChatMessage"); - if (CommandHandler.executeCommand(message)) { + + ResponseEntity responseEntity = eaaMod.onEvent(message); + + if (responseEntity.isInterceptEvent()) info.cancel(); - } + } } \ No newline at end of file diff --git a/src/main/java/net/saltymc/eaa/mixin/MixinInterface.java b/src/main/java/net/saltymc/eaa/mixin/MixinInterface.java new file mode 100644 index 0000000..802d648 --- /dev/null +++ b/src/main/java/net/saltymc/eaa/mixin/MixinInterface.java @@ -0,0 +1,13 @@ +package net.saltymc.eaa.mixin; + +import net.saltymc.eaa.EaaMod; + +public abstract class MixinInterface { + + protected final EaaMod eaaMod; + + MixinInterface(EaaMod eaaMod){ + this.eaaMod = eaaMod; + } + +} diff --git a/src/main/java/net/saltymc/eaa/util/ResponseEntity.java b/src/main/java/net/saltymc/eaa/util/ResponseEntity.java new file mode 100644 index 0000000..a55f0f1 --- /dev/null +++ b/src/main/java/net/saltymc/eaa/util/ResponseEntity.java @@ -0,0 +1,29 @@ +package net.saltymc.eaa.util; + +public class ResponseEntity { + + private boolean interceptEvent; + private final boolean wasHandled; + + public ResponseEntity(boolean wasHandled){ + this.wasHandled = wasHandled; + } + + public ResponseEntity(boolean wasHandled, boolean interceptEvent){ + this.wasHandled = wasHandled; + this.interceptEvent = interceptEvent; + } + + + public boolean isInterceptEvent() { + return interceptEvent; + } + + public void setInterceptEvent(boolean interceptEvent) { + this.interceptEvent = interceptEvent; + } + + public boolean isWasHandled() { + return wasHandled; + } +}