From 087b05fb95c3fa6478282ce880cee6fd75b9ce0a Mon Sep 17 00:00:00 2001 From: Chipmunk <65827213+ChipmunkMC@users.noreply.github.com> Date: Wed, 15 Mar 2023 22:07:24 -0400 Subject: [PATCH] validate command i know the file is a mess i will make it more modular later mabe --- .../chipmunk/chipmunkmod/Configuration.java | 20 ++- .../chipmunkmod/command/CommandManager.java | 1 + .../chipmunkmod/commands/ValidateCommand.java | 141 ++++++++++++++++++ .../chipmunkmod/util/Hexadecimal.java | 15 ++ src/main/resources/default_config.json | 8 + 5 files changed, 183 insertions(+), 2 deletions(-) create mode 100755 src/main/java/land/chipmunk/chipmunkmod/commands/ValidateCommand.java create mode 100644 src/main/java/land/chipmunk/chipmunkmod/util/Hexadecimal.java diff --git a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java index 5ab2a1f..da1cc53 100755 --- a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java +++ b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java @@ -2,16 +2,32 @@ package land.chipmunk.chipmunkmod; import land.chipmunk.chipmunkmod.data.BlockArea; import net.minecraft.util.math.BlockPos; +import lombok.AllArgsConstructor; public class Configuration { public CommandManager commands = new CommandManager(); public CommandCore core = new CommandCore(); + public Bots bots = new Bots(); - public class CommandManager { + public static class CommandManager { public String prefix = "."; } - public class CommandCore { + public static class CommandCore { public BlockArea relativeArea = new BlockArea(new BlockPos(0, 0, 0), new BlockPos(15, 0, 15)); } + + public static class Bots { + public BotInfo hbot = new BotInfo("#", null); + public BotInfo sbot = new BotInfo(":", null); + public BotInfo chipmunk = new BotInfo("'", null); + public BotInfo chomens = new BotInfo("*", null); + public BotInfo kittycorp = new BotInfo("^", null); + } + + @AllArgsConstructor + public static class BotInfo { + public String prefix; + public String key; + } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java b/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java index fd0bbc1..70587c9 100755 --- a/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java +++ b/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java @@ -70,5 +70,6 @@ public class CommandManager { TestCommand.register(dispatcher); CoreCommand.register(dispatcher); UsernameCommand.register(dispatcher); + ValidateCommand.register(dispatcher); } } \ No newline at end of file diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/ValidateCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/ValidateCommand.java new file mode 100755 index 0000000..bd844e7 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/ValidateCommand.java @@ -0,0 +1,141 @@ +package land.chipmunk.chipmunkmod.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import static land.chipmunk.chipmunkmod.command.CommandManager.literal; +import static land.chipmunk.chipmunkmod.command.CommandManager.argument; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import net.minecraft.text.Text; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayNetworkHandler; +import java.util.Arrays; +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import land.chipmunk.chipmunkmod.ChipmunkMod; +import land.chipmunk.chipmunkmod.Configuration; +import land.chipmunk.chipmunkmod.util.Hexadecimal; + +public class ValidateCommand { + private static final SimpleCommandExceptionType UNSPECIFIED_KEY = new SimpleCommandExceptionType(Text.literal("The key of the bot is unspecified (null), did you incorrectly add it to your config?")); + + public static void register (CommandDispatcher dispatcher) { + dispatcher.register( + literal("validate") + .then(literal("hbot").then(argument("command", greedyString()).executes(c -> hbot(c)))) + .then(literal("sbot").then(argument("command", greedyString()).executes(c -> sbot(c)))) + // .then(literal("chipmunk").then(argument("command", greedyString()).executes(c -> chipmunk(c)))) + .then(literal("chomens").then(argument("command", greedyString()).executes(c -> chomens(c)))) + .then(literal("kittycorp").then(argument("command", greedyString()).executes(c -> kittycorp(c)))) + ); + } + + + public static int hbot (CommandContext context) throws CommandSyntaxException { + final Configuration.BotInfo info = ChipmunkMod.CONFIG.bots.hbot; + final String command = getString(context, "command"); + final MinecraftClient client = context.getSource().getClient(); + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); + + final String prefix = info.prefix; + final String key = info.key; + if (key == null) throw UNSPECIFIED_KEY.create(); + + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + String time = String.valueOf(System.currentTimeMillis() / 10000); + String input = command.replaceAll("&[0-9a-fklmnor]", "") + ";" + client.player.getUuidAsString() + ";" + time + ";" + key; + byte[] hash = md.digest(input.getBytes(StandardCharsets.UTF_8)); + BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4)); + String stringHash = bigInt.toString(Character.MAX_RADIX); + + networkHandler.sendChatMessage(prefix + command + " " + stringHash); + } catch (NoSuchAlgorithmException e) { + throw new SimpleCommandExceptionType(Text.literal(e.getMessage())).create(); + } + + return Command.SINGLE_SUCCESS; + } + + public static int sbot (CommandContext context) throws CommandSyntaxException { + final Configuration.BotInfo info = ChipmunkMod.CONFIG.bots.sbot; + final String command = getString(context, "command"); + final MinecraftClient client = context.getSource().getClient(); + final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler(); + + final String prefix = info.prefix; + final String key = info.key; + if (key == null) throw UNSPECIFIED_KEY.create(); + + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + String time = String.valueOf(System.currentTimeMillis() / 20000); + String input = prefix + command.replaceAll("&[0-9a-fklmnorx]", "") + ";" + client.player.getName() + ";" + time + ";" + key; + byte[] hash = md.digest(input.getBytes(StandardCharsets.UTF_8)); + BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4)); + String stringHash = bigInt.toString(Character.MAX_RADIX); + + networkHandler.sendChatMessage(prefix + command + " " + stringHash); + } catch (NoSuchAlgorithmException e) { + throw new SimpleCommandExceptionType(Text.literal(e.getMessage())).create(); + } + + return Command.SINGLE_SUCCESS; + } + + public static int chomens (CommandContext context) throws CommandSyntaxException { + final Configuration.BotInfo info = ChipmunkMod.CONFIG.bots.chomens; + final String command = getString(context, "command"); + final ClientPlayNetworkHandler networkHandler = context.getSource().getClient().getNetworkHandler(); + + final String prefix = info.prefix; + final String key = info.key; + if (key == null) throw UNSPECIFIED_KEY.create(); + + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + String time = String.valueOf(System.currentTimeMillis() / 10000); + String input = time + key; + byte[] hash = md.digest(input.getBytes(StandardCharsets.UTF_8)); + String stringHash = Hexadecimal.encode(hash).substring(0, 16); + + String[] arguments = command.split(" "); + networkHandler.sendChatMessage(prefix + arguments[0] + " " + stringHash + " " + String.join(" ", Arrays.copyOfRange(arguments, 1, arguments.length))); + } catch (NoSuchAlgorithmException e) { + throw new SimpleCommandExceptionType(Text.literal(e.getMessage())).create(); + } + + return Command.SINGLE_SUCCESS; + } + + public static int kittycorp (CommandContext context) throws CommandSyntaxException { + final Configuration.BotInfo info = ChipmunkMod.CONFIG.bots.kittycorp; + final String command = getString(context, "command"); + final ClientPlayNetworkHandler networkHandler = context.getSource().getClient().getNetworkHandler(); + + final String prefix = info.prefix; + final String key = info.key; + if (key == null) throw UNSPECIFIED_KEY.create(); + + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + String time = String.valueOf(System.currentTimeMillis() / 10000); + String input = prefix + command.replaceAll("&[0-9a-fklmnorx]", "") + ";" + time + ";" + key; + byte[] hash = md.digest(input.getBytes(StandardCharsets.UTF_8)); + BigInteger bigInt = new BigInteger(1, Arrays.copyOfRange(hash, 0, 4)); + String stringHash = bigInt.toString(Character.MAX_RADIX); + + networkHandler.sendChatMessage(prefix + command + " " + stringHash); + } catch (NoSuchAlgorithmException e) { + throw new SimpleCommandExceptionType(Text.literal(e.getMessage())).create(); + } + + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/land/chipmunk/chipmunkmod/util/Hexadecimal.java b/src/main/java/land/chipmunk/chipmunkmod/util/Hexadecimal.java new file mode 100644 index 0000000..a9b95b4 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/util/Hexadecimal.java @@ -0,0 +1,15 @@ +package land.chipmunk.chipmunkmod.util; + +public interface Hexadecimal { + static String encode (byte b) { + return "" + Character.forDigit((b >> 4) & 0xF, 16) + Character.forDigit((b & 0xF), 16); + } + + static String encode (byte[] array) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < array.length; i++) sb.append(encode(array[i])); + return sb.toString(); + } + + // TODO: Decode +} diff --git a/src/main/resources/default_config.json b/src/main/resources/default_config.json index a9179b8..fe65157 100644 --- a/src/main/resources/default_config.json +++ b/src/main/resources/default_config.json @@ -8,5 +8,13 @@ "start": { "x": 0, "y": 0, "z": 0 }, "end": { "x": 15, "y": 0, "z": 15 } } + }, + + "bots": { + "hbot": { "prefix": "#", "key": null }, + "sbot": { "prefix": ":", "key": null }, + "chipmunk": { "prefix": "'", "key": null }, + "chomens": { "prefix": "*", "key": null }, + "kittycorp": { "prefix": "^", "key": null } } } \ No newline at end of file