Merge pull request #2 from Hiajen/dev

Dev into master
This commit is contained in:
Marcel Marcel 2021-05-23 14:41:50 +02:00
commit e66327e3fb
12 changed files with 265 additions and 79 deletions

View file

@ -1,8 +1,31 @@
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.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(){
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.
@ -10,5 +33,20 @@ public class EaaMod implements ModInitializer {
// Proceed with mild caution. // Proceed with mild caution.
System.out.println("EAA Mod initializing..."); 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,37 @@
package net.saltymc.eaa.commands; package net.saltymc.eaa.handler;
import com.mojang.brigadier.exceptions.BuiltInExceptionProvider; import com.mojang.brigadier.exceptions.BuiltInExceptionProvider;
import com.mojang.brigadier.exceptions.CommandExceptionType; import com.mojang.brigadier.exceptions.CommandExceptionType;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource; import net.fabricmc.fabric.api.client.command.v1.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient; 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.Text;
import net.minecraft.text.Texts; import net.minecraft.text.Texts;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting; import net.saltymc.eaa.handler.commands.Command;
import net.minecraft.util.Util; import net.saltymc.eaa.handler.commands.EaaCommand;
import org.apache.logging.log4j.Level; import net.saltymc.eaa.util.ResponseEntity;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; 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 String PREFIX = "#";
private static final char PREFIX = '/';
private static final Logger LOGGER = LogManager.getLogger(); 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; //above line stolen from net.fabricmc.fabric.impl.command.client.ClientCommandInternals;
static { private static FabricClientCommandSource commandSource;
if(CommandHandler.commands == null){
CommandHandler.commands = new HashMap<>();
} public CommandHandler(){
CommandHandler.commands.put("p", new EaaCommand()); LOGGER.info("init CommandHandler");
//TODO implement commands = new ArrayList<>();
CommandHandler.commands.add(new EaaCommand());
} }
/** /**
@ -42,52 +40,31 @@ public class CommandHandler {
* @param message the command message * @param message the command message
* @return true if the message should not be sent to the server, false otherwise * @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()) { if (message.isEmpty()) {
return false; // Nothing to process return false; // Nothing to process
} }
if (message.charAt(0) != PREFIX) { if (!message.matches("^" + PREFIX + ".*")) {
return false; // Incorrect prefix, won't execute anything. return false; // Incorrect prefix, won't execute anything.
} }
FabricClientCommandSource commandSource = (FabricClientCommandSource) MinecraftClient.getInstance().getNetworkHandler().getCommandSource(); 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 { LOGGER.debug("Valid Prefix received");
//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
System.out.println("command: $"+command+"$"); for (Command command : commands){
if (command.run(commandSource, args)) { // Try execute command
if(CommandHandler.commands.get(command) != null) { LOGGER.debug("Executed following command: " + message);
CommandHandler.commands.get(command).run(commandSource, args); return command.intercept(); // if executed return interception flag
}else{
return false;
} }
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 +92,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,42 @@
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;
public class EaaCommand implements Command {
private static final String COMMAND = "echo";
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 boolean intercept() {
return INTERCEPT;
}
}

View file

@ -1,8 +1,11 @@
package net.saltymc.eaa.mixin; package net.saltymc.eaa.mixin;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.network.ClientPlayerEntity; 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.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;
@ -18,11 +21,15 @@ public class ExampleMixin {
*/ */
@Mixin(ClientPlayerEntity.class) @Mixin(ClientPlayerEntity.class)
public class ExampleMixin { //ClientPlayerEntityMixin public class ExampleMixin { //ClientPlayerEntityMixin
private static final Logger LOGGER = LogManager.getLogger();
@Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true) @Inject(method = "sendChatMessage", at = @At("HEAD"), cancellable = true)
private void onSendChatMessage(String message, CallbackInfo info) { private void onSendChatMessage(String message, CallbackInfo info) {
System.out.println("in onSendChatMessage"); ResponseEntity responseEntity = EaaMod.getInstance().onEvent(message);
if (CommandHandler.executeCommand(message)) {
if (responseEntity.isInterceptEvent())
info.cancel(); info.cancel();
}
} }
} }

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

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -0,0 +1,86 @@
-- MySQL Script generated by MySQL Workbench
-- Sun May 23 00:03:01 2021
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Player`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Player` (
`id` INT NOT NULL,
`name` VARCHAR(45) NULL,
`UUID` VARCHAR(36) NOT NULL,
`tag` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `UUID_UNIQUE` (`UUID` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Report`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Report` (
`Player_id` INT NOT NULL,
`id` INT NOT NULL,
`date` DATE NOT NULL,
`reason` VARCHAR(255) NULL,
`grade` INT NULL,
INDEX `fk_Report_Player_idx` (`Player_id` ASC),
PRIMARY KEY (`Player_id`, `id`),
CONSTRAINT `fk_Report_Player`
FOREIGN KEY (`Player_id`)
REFERENCES `mydb`.`Player` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Map`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Map` (
`id` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
`Color` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Server_Reports`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Server_Reports` (
`id` INT NOT NULL,
`date` DATE NOT NULL,
`appeals` VARCHAR(255) NOT NULL,
`grade` INT NULL,
`Server_Reportscol` VARCHAR(45) NULL,
`Player_id` INT NOT NULL,
PRIMARY KEY (`id`, `Player_id`),
INDEX `fk_Server_Reports_Player1_idx` (`Player_id` ASC),
CONSTRAINT `fk_Server_Reports_Player1`
FOREIGN KEY (`Player_id`)
REFERENCES `mydb`.`Player` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;