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