rework lol
This commit is contained in:
parent
59420948d3
commit
865abd7c29
8 changed files with 65 additions and 251 deletions
|
@ -1,32 +1,10 @@
|
||||||
package net.saltymc.eaa;
|
package net.saltymc.eaa;
|
||||||
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import net.saltymc.eaa.handler.CommandHandler;
|
import net.saltymc.eaa.handler.commands.EchoCommand;
|
||||||
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 {
|
public class EaaMod implements ModInitializer {
|
||||||
|
|
||||||
private static EaaMod instance;
|
|
||||||
private final List<HandlerInterface> handler;
|
|
||||||
|
|
||||||
public EaaMod(){
|
|
||||||
instance = this;
|
|
||||||
handler = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static EaaMod getInstance(){
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new EaaMod();
|
|
||||||
instance.onInitialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
// This code runs as soon as Minecraft is in a mod-load-ready state.
|
// 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...");
|
System.out.println("EAA Mod initializing...");
|
||||||
|
|
||||||
//Init CommandHandler
|
new EchoCommand();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<Command> 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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +0,0 @@
|
||||||
package net.saltymc.eaa.handler;
|
|
||||||
|
|
||||||
import net.saltymc.eaa.util.ResponseEntity;
|
|
||||||
|
|
||||||
public interface HandlerInterface {
|
|
||||||
|
|
||||||
ResponseEntity handle(Object object);
|
|
||||||
}
|
|
|
@ -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<String> getAlias();
|
|
||||||
|
|
||||||
boolean acceptInput(String command);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* should message get intercepted and not send to Server?
|
|
||||||
*/
|
|
||||||
boolean intercept();
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<String> 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<String> getAlias() {
|
|
||||||
return ALIAS;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean acceptInput(String command) {
|
|
||||||
return (command.equalsIgnoreCase(getCommand()) || getAlias().contains(command.toLowerCase()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean intercept() {
|
|
||||||
return INTERCEPT;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<FabricClientCommandSource> {
|
||||||
|
|
||||||
|
public EaaModCommand() {
|
||||||
|
this.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(){
|
||||||
|
ClientCommandManager.DISPATCHER.register(getCommandSpecification().executes(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract LiteralArgumentBuilder<FabricClientCommandSource> getCommandSpecification();
|
||||||
|
|
||||||
|
}
|
|
@ -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<FabricClientCommandSource> getCommandSpecification() {
|
||||||
|
return ClientCommandManager.literal("echo")
|
||||||
|
.then(ClientCommandManager.argument("message", StringArgumentType.greedyString()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,34 +1,15 @@
|
||||||
package net.saltymc.eaa.mixin;
|
package net.saltymc.eaa.mixin;
|
||||||
|
|
||||||
import net.minecraft.client.network.ClientPlayerEntity;
|
import net.minecraft.client.gui.screen.TitleScreen;
|
||||||
import net.saltymc.eaa.EaaMod;
|
|
||||||
import net.saltymc.eaa.util.ResponseEntity;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.spongepowered.asm.mixin.Mixin;
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
/*
|
|
||||||
@Mixin(TitleScreen.class)
|
@Mixin(TitleScreen.class)
|
||||||
public class ExampleMixin {
|
public class ExampleMixin {
|
||||||
@Inject(at = @At("HEAD"), method = "init()V")
|
@Inject(at = @At("HEAD"), method = "init()V")
|
||||||
private void init(CallbackInfo info) {
|
private void init(CallbackInfo info) {
|
||||||
System.out.println("This line is printed by an example mod mixin!");
|
System.out.println("This line is printed by EAA MOD!");
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue