diff --git a/src/main/java/me/alpha432/oyvey/features/commands/Command.java b/src/main/java/me/alpha432/oyvey/features/commands/Command.java index 2e271a5..4775f28 100644 --- a/src/main/java/me/alpha432/oyvey/features/commands/Command.java +++ b/src/main/java/me/alpha432/oyvey/features/commands/Command.java @@ -6,10 +6,11 @@ import com.mojang.brigadier.builder.RequiredArgumentBuilder; import me.alpha432.oyvey.features.Feature; import me.alpha432.oyvey.manager.CommandManager; import me.alpha432.oyvey.util.TextUtil; -import me.alpha432.oyvey.util.player.ChatUtil; +import me.alpha432.oyvey.util.chat.ChatUtil; +import me.alpha432.oyvey.util.chat.Signature; import static com.mojang.brigadier.Command.SINGLE_SUCCESS; -import static me.alpha432.oyvey.features.commands.MessageIdentifiers.*; +import static me.alpha432.oyvey.features.commands.MessageSignatures.*; public abstract class Command extends Feature { public static final int NO_OP = -1; @@ -59,12 +60,12 @@ public abstract class Command extends Feature { return true; } - public static void sendMessage(String message, String identifier) { + public static void sendMessage(String message, Signature identifier) { if (message == null) return; ChatUtil.sendMessage(TextUtil.text(message), identifier); } - public static void sendMessage(String message, String identifier, Object... format) { + public static void sendMessage(String message, Signature identifier, Object... format) { if (message == null) return; ChatUtil.sendMessage(TextUtil.text(message, format), identifier); } diff --git a/src/main/java/me/alpha432/oyvey/features/commands/MessageIdentifiers.java b/src/main/java/me/alpha432/oyvey/features/commands/MessageIdentifiers.java deleted file mode 100644 index 0e072e4..0000000 --- a/src/main/java/me/alpha432/oyvey/features/commands/MessageIdentifiers.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.alpha432.oyvey.features.commands; - -public class MessageIdentifiers { - public static final String GENERAL = "general"; - public static final String SUCCESS = "success"; - public static final String FAIL = "fail"; -} \ No newline at end of file diff --git a/src/main/java/me/alpha432/oyvey/features/commands/MessageSignatures.java b/src/main/java/me/alpha432/oyvey/features/commands/MessageSignatures.java new file mode 100644 index 0000000..ca8782a --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/features/commands/MessageSignatures.java @@ -0,0 +1,10 @@ +package me.alpha432.oyvey.features.commands; + +import me.alpha432.oyvey.util.chat.Signature; +import me.alpha432.oyvey.util.chat.SimpleSignature; + +public class MessageSignatures { + public static final Signature GENERAL = SimpleSignature.fromLong(-1L); + public static final Signature SUCCESS = SimpleSignature.fromLong(0L); + public static final Signature FAIL = SimpleSignature.fromLong(1L); +} \ No newline at end of file diff --git a/src/main/java/me/alpha432/oyvey/features/commands/impl/BindCommand.java b/src/main/java/me/alpha432/oyvey/features/commands/impl/BindCommand.java index aa5fe73..e086a75 100644 --- a/src/main/java/me/alpha432/oyvey/features/commands/impl/BindCommand.java +++ b/src/main/java/me/alpha432/oyvey/features/commands/impl/BindCommand.java @@ -40,13 +40,15 @@ public class BindCommand extends Command { if (event.getKey() == GLFW_KEY_ESCAPE) { module = null; - sendMessage("Operation canceled.", "fail"); + fail("Operation canceled."); return; } - sendMessage("Bind for {green} %s {} set to {green} %s", + success("Bind for {green} %s {} set to {green} %s", module.getName(), - KeyboardUtil.getKeyName(event.getKey())); + KeyboardUtil.getKeyName(event.getKey()) + ); + module.bind.setValue(new Bind(event.getKey())); module = null; } diff --git a/src/main/java/me/alpha432/oyvey/features/commands/impl/HelpCommand.java b/src/main/java/me/alpha432/oyvey/features/commands/impl/HelpCommand.java index 42376f9..c4a3164 100644 --- a/src/main/java/me/alpha432/oyvey/features/commands/impl/HelpCommand.java +++ b/src/main/java/me/alpha432/oyvey/features/commands/impl/HelpCommand.java @@ -16,6 +16,7 @@ import static me.alpha432.oyvey.features.commands.argument.CommandArgumentType.c import static me.alpha432.oyvey.features.commands.argument.CommandArgumentType.getCommand; import static me.alpha432.oyvey.features.commands.argument.NumberArgumentType.get; import static me.alpha432.oyvey.features.commands.argument.NumberArgumentType.number; +import static me.alpha432.oyvey.util.chat.SimpleSignature.fromInt; public class HelpCommand extends Command { private static final int ITEMS_PER_PAGE = 5; @@ -35,8 +36,9 @@ public class HelpCommand extends Command { } private int helpSpecific(CommandContext ctx) { + int index = 0; Command command = getCommand(ctx, "command_name"); - sendMessage("Usages for %s:", command.getName()); + sendMessage("Usages for %s:", fromInt(index++), command.getName()); ParseResults results = ctx.getSource().getDispatcher() .parse(command.getName(), ctx.getSource()); @@ -47,13 +49,14 @@ public class HelpCommand extends Command { Map, String> smartUsages = ctx.getSource().getDispatcher().getSmartUsage( Iterables.getLast(results.getContext().getNodes()).getNode(), ctx.getSource()); for (String usage : smartUsages.values()) { - sendMessage(".%s %s", results.getReader().getString(), usage); + sendMessage(".%s %s", fromInt(index++), results.getReader().getString(), usage); } return success(); } private int helpCommands(CommandContext ctx, int page) { + int index = 0; List commands = ctx.getSource().getCommands() .stream().filter(Command::isShown).toList(); @@ -66,7 +69,7 @@ public class HelpCommand extends Command { List paginated = commands.subList((pageIndex - 1) * ITEMS_PER_PAGE, Math.min(commands.size(), pageIndex * ITEMS_PER_PAGE)); - sendMessage("Commands (%s):", "general", commands.size()); + sendMessage("Commands (%s):", fromInt(index++), commands.size()); for (Command command : paginated) { StringJoiner joiner = new StringJoiner(", "); @@ -74,7 +77,7 @@ public class HelpCommand extends Command { joiner.add(alias); } - sendMessage("{dark_gray} %s: {reset}\n %s", "general", + sendMessage("{dark_gray} %s: {reset}\n %s", fromInt(index++), joiner, command.getDescription()); } diff --git a/src/main/java/me/alpha432/oyvey/features/modules/Module.java b/src/main/java/me/alpha432/oyvey/features/modules/Module.java index 86c5942..53081ad 100644 --- a/src/main/java/me/alpha432/oyvey/features/modules/Module.java +++ b/src/main/java/me/alpha432/oyvey/features/modules/Module.java @@ -8,9 +8,11 @@ import me.alpha432.oyvey.event.impl.render.Render2DEvent; import me.alpha432.oyvey.event.impl.render.Render3DEvent; import me.alpha432.oyvey.features.Feature; import me.alpha432.oyvey.features.commands.Command; +import me.alpha432.oyvey.features.commands.MessageSignatures; import me.alpha432.oyvey.features.settings.Bind; import me.alpha432.oyvey.features.settings.Setting; import me.alpha432.oyvey.manager.ConfigManager; +import me.alpha432.oyvey.util.chat.ChatUtil; import me.alpha432.oyvey.util.traits.Jsonable; import me.alpha432.oyvey.util.traits.Toggleable; import net.minecraft.ChatFormatting; @@ -88,11 +90,13 @@ public class Module extends Feature implements Jsonable, Toggleable { Module module = OyVey.moduleManager.getModuleByDisplayName(name); Module originalModule = OyVey.moduleManager.getModuleByName(name); if (module == null && originalModule == null) { - Command.sendMessage(this.getDisplayName() + ", name: " + this.getName() + ", has been renamed to: " + name, "general"); + Command.sendMessage("%, name: %s, has been renamed to: %s", MessageSignatures.GENERAL, + getDisplayName(), getName(), name); this.displayName.setValue(name); return; } - Command.sendMessage("{red} A module of this name already exists.", "general"); + + Command.sendMessage("{red} A module of this name already exists.", MessageSignatures.GENERAL); } @Override diff --git a/src/main/java/me/alpha432/oyvey/features/modules/client/ClickGuiModule.java b/src/main/java/me/alpha432/oyvey/features/modules/client/ClickGuiModule.java index 81a2c75..ce4941a 100644 --- a/src/main/java/me/alpha432/oyvey/features/modules/client/ClickGuiModule.java +++ b/src/main/java/me/alpha432/oyvey/features/modules/client/ClickGuiModule.java @@ -4,6 +4,7 @@ import me.alpha432.oyvey.OyVey; import me.alpha432.oyvey.event.impl.ClientEvent; import me.alpha432.oyvey.event.system.Subscribe; import me.alpha432.oyvey.features.commands.Command; +import me.alpha432.oyvey.features.commands.MessageSignatures; import me.alpha432.oyvey.features.gui.OyVeyGui; import me.alpha432.oyvey.features.modules.Module; import me.alpha432.oyvey.features.settings.Setting; @@ -11,6 +12,8 @@ import org.lwjgl.glfw.GLFW; import java.awt.*; +import static me.alpha432.oyvey.features.commands.MessageSignatures.GENERAL; + public class ClickGuiModule extends Module { private static ClickGuiModule INSTANCE; @@ -36,7 +39,7 @@ public class ClickGuiModule extends Module { if (event.getType() == ClientEvent.Type.SETTING_UPDATE && event.getSetting().getFeature().equals(this)) { if (event.getSetting().equals(this.prefix)) { OyVey.commandManager.setCommandPrefix(this.prefix.getPlannedValue()); - Command.sendMessage("Prefix set to {global} %s", OyVey.commandManager.getCommandPrefix()); + Command.sendMessage("Prefix set to {global} %s", GENERAL, OyVey.commandManager.getCommandPrefix()); } if (event.getSetting().equals(this.color)) { OyVey.colorManager.setColor(this.color.getPlannedValue()); diff --git a/src/main/java/me/alpha432/oyvey/features/modules/client/NotificationsModule.java b/src/main/java/me/alpha432/oyvey/features/modules/client/NotificationsModule.java index cda421a..9c60652 100644 --- a/src/main/java/me/alpha432/oyvey/features/modules/client/NotificationsModule.java +++ b/src/main/java/me/alpha432/oyvey/features/modules/client/NotificationsModule.java @@ -5,7 +5,9 @@ import me.alpha432.oyvey.event.system.Subscribe; import me.alpha432.oyvey.features.modules.Module; import me.alpha432.oyvey.features.settings.Setting; import me.alpha432.oyvey.util.TextUtil; -import me.alpha432.oyvey.util.player.ChatUtil; +import me.alpha432.oyvey.util.chat.ChatUtil; + +import static me.alpha432.oyvey.util.chat.SimpleSignature.from; public class NotificationsModule extends Module { private static final String MODULE_FORMAT = "Toggled %s %s %s"; @@ -28,6 +30,6 @@ public class NotificationsModule extends Module { ChatUtil.sendMessage(TextUtil.text(MODULE_FORMAT, event.getFeature().getName(), moduleState ? "{green}" : "{red}", - moduleState ? "on" : "off"), event.getFeature().getName()); + moduleState ? "on" : "off"), from(event.getFeature().getName())); } } diff --git a/src/main/java/me/alpha432/oyvey/features/modules/misc/MCFModule.java b/src/main/java/me/alpha432/oyvey/features/modules/misc/MCFModule.java index b7f4b8e..4d3d012 100644 --- a/src/main/java/me/alpha432/oyvey/features/modules/misc/MCFModule.java +++ b/src/main/java/me/alpha432/oyvey/features/modules/misc/MCFModule.java @@ -7,6 +7,8 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import org.lwjgl.glfw.GLFW; +import static me.alpha432.oyvey.util.chat.SimpleSignature.from; + public class MCFModule extends Module { private boolean pressed; @@ -32,10 +34,10 @@ public class MCFModule extends Module { if (OyVey.friendManager.isFriend(name)) { OyVey.friendManager.removeFriend(name); - Command.sendMessage("{red} %s has been unfriended.", name); + Command.sendMessage("{red} %s has been unfriended.", from(getName()), name); } else { OyVey.friendManager.addFriend(name); - Command.sendMessage("{aqua} %s has been friended.", name); + Command.sendMessage("{aqua} %s has been friended.", from(getName()), name); } } } diff --git a/src/main/java/me/alpha432/oyvey/manager/CommandManager.java b/src/main/java/me/alpha432/oyvey/manager/CommandManager.java index a1906b1..d9394af 100644 --- a/src/main/java/me/alpha432/oyvey/manager/CommandManager.java +++ b/src/main/java/me/alpha432/oyvey/manager/CommandManager.java @@ -15,9 +15,9 @@ import org.apache.logging.log4j.Logger; import java.util.*; -import static com.mojang.brigadier.Command.SINGLE_SUCCESS; import static me.alpha432.oyvey.features.commands.Command.SINGLE_FAILURE; -import static me.alpha432.oyvey.features.commands.MessageIdentifiers.GENERAL; +import static me.alpha432.oyvey.features.commands.MessageSignatures.GENERAL; +import static me.alpha432.oyvey.features.commands.MessageSignatures.SUCCESS; public class CommandManager extends Feature implements Jsonable { private static final Logger LOGGER = LogManager.getLogger("Commands"); @@ -48,10 +48,8 @@ public class CommandManager extends Feature implements Jsonable { try { int result = dispatcher.execute(message.substring(commandPrefix.length()).trim(), this); - if (result == SINGLE_SUCCESS) { - Command.sendMessage("{green} Command executed successfully", GENERAL); - } else if (result == SINGLE_FAILURE) { - Command.sendMessage("{red} Failed to execute command", GENERAL); + if (result == SINGLE_FAILURE) { + Command.sendMessage("{red} Failed to execute command", SUCCESS); } } catch (CommandSyntaxException e) { LOGGER.error("Failed to execute command", e); diff --git a/src/main/java/me/alpha432/oyvey/util/player/ChatUtil.java b/src/main/java/me/alpha432/oyvey/util/chat/ChatUtil.java similarity index 71% rename from src/main/java/me/alpha432/oyvey/util/player/ChatUtil.java rename to src/main/java/me/alpha432/oyvey/util/chat/ChatUtil.java index 01964f8..08e10eb 100644 --- a/src/main/java/me/alpha432/oyvey/util/player/ChatUtil.java +++ b/src/main/java/me/alpha432/oyvey/util/chat/ChatUtil.java @@ -1,4 +1,4 @@ -package me.alpha432.oyvey.util.player; +package me.alpha432.oyvey.util.chat; import me.alpha432.oyvey.OyVey; import me.alpha432.oyvey.ducks.render.IChatComponent; @@ -10,12 +10,10 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MessageSignature; import net.minecraft.network.chat.Style; -import java.nio.charset.StandardCharsets; - import static me.alpha432.oyvey.util.traits.Util.mc; public class ChatUtil { - public static void sendMessage(Component message, String identifier) { + public static void sendMessage(Component message, Signature identifier) { sendClientSideMessage(Component.empty() .setStyle(Style.EMPTY.withColor(ChatFormatting.GRAY)) .append("<") @@ -25,11 +23,11 @@ public class ChatUtil { .append(message), identifier); } - public static void sendClientSideMessage(Component message, String identifier) { + public static void sendClientSideMessage(Component message, Signature sig) { if (Command.nullCheck()) return; IChatComponent chat = (IChatComponent) mc.gui.getChat(); - MessageSignature signature = new MessageSignature(get256Bytes(identifier)); + MessageSignature signature = new MessageSignature(sig.getByteSignature()); chat.oyvey$removeMessage(signature); chat.oyvey$addMessage(new GuiMessage(mc.gui.getGuiTicks(), message, signature, getMessageTag())); } @@ -41,11 +39,4 @@ public class ChatUtil { private static GuiMessageTag getMessageTag() { return new GuiMessageTag(OyVey.colorManager.getColorAsInt(), null, null, null); } - - private static byte[] get256Bytes(String identifier) { - byte[] bytes = new byte[256]; - byte[] identifierBytes = identifier.getBytes(StandardCharsets.UTF_8); - System.arraycopy(identifierBytes, 0, bytes, 0, Math.min(bytes.length, identifierBytes.length)); - return bytes; - } } \ No newline at end of file diff --git a/src/main/java/me/alpha432/oyvey/util/chat/Signature.java b/src/main/java/me/alpha432/oyvey/util/chat/Signature.java new file mode 100644 index 0000000..3cf50d2 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/chat/Signature.java @@ -0,0 +1,5 @@ +package me.alpha432.oyvey.util.chat; + +public interface Signature { + byte[] getByteSignature(); +} diff --git a/src/main/java/me/alpha432/oyvey/util/chat/SimpleSignature.java b/src/main/java/me/alpha432/oyvey/util/chat/SimpleSignature.java new file mode 100644 index 0000000..da2b671 --- /dev/null +++ b/src/main/java/me/alpha432/oyvey/util/chat/SimpleSignature.java @@ -0,0 +1,61 @@ +package me.alpha432.oyvey.util.chat; + +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class SimpleSignature implements Signature { + private static final MessageDigest MESSAGE_DIGEST; + + private final byte[] signature; + + SimpleSignature(byte[] signature) { + this.signature = signature; + } + + public static SimpleSignature from(String content) { + byte[] sig = allocateBuf() + .put((byte) 1) + .put(sha256(content)) + .array(); + return new SimpleSignature(sig); + } + + public static SimpleSignature fromInt(int id) { + byte[] sig = allocateBuf() + .put((byte) 2) + .putInt(id) + .array(); + return new SimpleSignature(sig); + } + + public static SimpleSignature fromLong(long id) { + byte[] sig = allocateBuf() + .put((byte) 3) + .putLong(id) + .array(); + return new SimpleSignature(sig); + } + + @Override + public byte[] getByteSignature() { + return signature; + } + + private static byte[] sha256(String content) { + return MESSAGE_DIGEST.digest(content.getBytes(StandardCharsets.UTF_8)); + } + + private static ByteBuffer allocateBuf() { + return ByteBuffer.allocate(256); + } + + static { + try { + MESSAGE_DIGEST = MessageDigest.getInstance("SHA-512"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } +}