validate command

i know the file is a mess i will make it more modular later mabe
This commit is contained in:
Chipmunk 2023-03-15 22:07:24 -04:00
parent 88dc82eb1e
commit 087b05fb95
5 changed files with 183 additions and 2 deletions

View file

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

View file

@ -70,5 +70,6 @@ public class CommandManager {
TestCommand.register(dispatcher);
CoreCommand.register(dispatcher);
UsernameCommand.register(dispatcher);
ValidateCommand.register(dispatcher);
}
}

View file

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

View file

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

View file

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