[refactor] try to generate general structures

This commit is contained in:
Hiajen Hiajen 2021-05-22 12:48:12 +02:00
parent ac28939480
commit f19ea5c876
10 changed files with 172 additions and 79 deletions

View file

@ -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<HandlerInterface> 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);
}
}

View file

@ -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);
}

View file

@ -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;
}
}

View file

@ -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<String, Command> commands;
private static List<Command> 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
}
}

View file

@ -0,0 +1,8 @@
package net.saltymc.eaa.handler;
import net.saltymc.eaa.util.ResponseEntity;
public interface HandlerInterface {
ResponseEntity handle(Object object);
}

View file

@ -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();
}

View file

@ -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;
}
}

View file

@ -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();
}
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}