refactor(MessageIdentifiers): rework MessageIdentifiers string based system into MessageSignatures
now we use Signature interface to get bytes for MessageSignature
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<CommandManager> 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<CommandManager> results = ctx.getSource().getDispatcher()
|
||||
.parse(command.getName(), ctx.getSource());
|
||||
@@ -47,13 +49,14 @@ public class HelpCommand extends Command {
|
||||
Map<CommandNode<CommandManager>, 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<CommandManager> ctx, int page) {
|
||||
int index = 0;
|
||||
List<Command> commands = ctx.getSource().getCommands()
|
||||
.stream().filter(Command::isShown).toList();
|
||||
|
||||
@@ -66,7 +69,7 @@ public class HelpCommand extends Command {
|
||||
List<Command> 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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
+4
-13
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package me.alpha432.oyvey.util.chat;
|
||||
|
||||
public interface Signature {
|
||||
byte[] getByteSignature();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user