[refactor] try to generate general structures
This commit is contained in:
parent
ac28939480
commit
f19ea5c876
10 changed files with 172 additions and 79 deletions
101
src/main/java/net/saltymc/eaa/handler/CommandHandler.java
Normal file
101
src/main/java/net/saltymc/eaa/handler/CommandHandler.java
Normal file
|
@ -0,0 +1,101 @@
|
|||
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<Command> commands;
|
||||
//above line stolen from net.fabricmc.fabric.impl.command.client.ClientCommandInternals;
|
||||
|
||||
private static FabricClientCommandSource commandSource;
|
||||
|
||||
|
||||
public 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.run(commandSource, args)) { // Try execute command
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue