diff options
12 files changed, 580 insertions, 69 deletions
diff --git a/common/src/main/java/de/hype/bbsentials/common/chat/Chat.java b/common/src/main/java/de/hype/bbsentials/common/chat/Chat.java index 0d3662d..000cd70 100644 --- a/common/src/main/java/de/hype/bbsentials/common/chat/Chat.java +++ b/common/src/main/java/de/hype/bbsentials/common/chat/Chat.java @@ -191,22 +191,28 @@ public class Chat { },10, TimeUnit.SECONDS); } - public Message onEvent(Message text) { + public Message onEvent(Message text, boolean actionbar) { if (!isSpam(text.getString())) { if (BBsentials.getConfig().isDetailedDevModeEnabled()) { System.out.println("got a message: " + text.getJson()); } BBsentials.executionService.execute(() -> processThreaded(text)); - return processNotThreaded(text); + return processNotThreaded(text, actionbar); } return text; // Return the original message if it is spam } //Handle in the messages which need to be modified here - public Message processNotThreaded(Message message) { + public Message processNotThreaded(Message message, boolean actionbar) { // if (message.isFromParty()) { // message.replaceInJson("\"action\":\"run_command\",\"value\":\"/viewprofile", "\"action\":\"run_command\",\"value\":\"/bviewprofile " + messageUnformatted.split(">", 1)[1].trim()); // } + if (actionbar && !BBsentials.config.overwriteActionBar.isEmpty()) { + if (message.getUnformattedString().equals(BBsentials.config.overwriteActionBar.replaceAll("§.", ""))) { + return message; + } + return null; + } if (message.isFromReportedUser()) { sendPrivateMessageToSelfBase(Formatting.RED + "B: " + message.getUnformattedString()); return null; diff --git a/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java b/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java index 9344c4c..173d8d0 100644 --- a/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java +++ b/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java @@ -18,6 +18,7 @@ public class BBsentials { public static SplashStatusUpdateListener splashStatusUpdateListener; private static Thread bbthread; private static boolean initialised = false; + public static Chat chat = new Chat(); public static Config getConfig() { return config; @@ -78,7 +79,6 @@ public class BBsentials { config = Config.load(); executionService.scheduleAtFixedRate(EnvironmentCore.debug, 0, 20, TimeUnit.SECONDS); if (config.doGammaOverride) EnvironmentCore.mcoptions.setGamma(10); - Chat chat = new Chat(); if (Config.isBingoTime() || config.overrideBingoTime()) { connectToBBserver(); } diff --git a/common/src/main/java/de/hype/bbsentials/common/client/Config.java b/common/src/main/java/de/hype/bbsentials/common/client/Config.java index ee74855..62e5c16 100644 --- a/common/src/main/java/de/hype/bbsentials/common/client/Config.java +++ b/common/src/main/java/de/hype/bbsentials/common/client/Config.java @@ -20,7 +20,9 @@ public class Config implements Serializable { //You can change again // set automatically - private transient boolean isLeader; + private transient boolean isPartyLeader; + public transient String overwriteActionBar = ""; + public transient String alreadyReported = ""; public String[] bbsentialsRoles = {""}; public static List<String> partyMembers = new ArrayList<>(); @@ -127,11 +129,11 @@ public class Config implements Serializable { } public boolean isPartyLeader() { - return isLeader; + return isPartyLeader; } public void setIsLeader(boolean value) { - isLeader = value; + isPartyLeader = value; } public String getNickname() { @@ -193,6 +195,8 @@ public class Config implements Serializable { } public boolean hasBBRoles(String roleName) { + if (roleName == null) return true; + if (roleName.isEmpty()) return true; for (String role : bbsentialsRoles) { if (role.equalsIgnoreCase(roleName)) { return true; diff --git a/common/src/main/java/de/hype/bbsentials/common/mclibraries/MCChat.java b/common/src/main/java/de/hype/bbsentials/common/mclibraries/MCChat.java index 08cbff8..de46f84 100644 --- a/common/src/main/java/de/hype/bbsentials/common/mclibraries/MCChat.java +++ b/common/src/main/java/de/hype/bbsentials/common/mclibraries/MCChat.java @@ -1,13 +1,20 @@ package de.hype.bbsentials.common.mclibraries; import de.hype.bbsentials.common.chat.Message; +import de.hype.bbsentials.common.client.BBsentials; public interface MCChat { void init(); void sendChatMessage(String message); void sendClientSideMessage(Message message); - void sendClientSideMessage(Message message, boolean actionbar); - + default void sendClientSideMessage(Message message, boolean actionbar) { + if (BBsentials.config.swapActionBarChat && !BBsentials.config.swapOnlyNormal) { + actionbar = !actionbar; + } + else { + sendClientSideMessage(message); + } + } void showActionBar(Message message); } diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/FabricChat.java b/fabric/src/main/java/de/hype/bbsentials/fabric/FabricChat.java index e78df68..041174c 100644 --- a/fabric/src/main/java/de/hype/bbsentials/fabric/FabricChat.java +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/FabricChat.java @@ -5,7 +5,6 @@ import de.hype.bbsentials.common.chat.Message; import de.hype.bbsentials.common.client.BBsentials; import de.hype.bbsentials.common.mclibraries.MCChat; import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents; -import net.fabricmc.fabric.api.client.message.v1.ClientSendMessageEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; @@ -15,27 +14,24 @@ public class FabricChat implements MCChat { public FabricChat() { init(); } - public void init() { // Register a callback for a custom message type - ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, receptionTimestamp) -> { - chat.onEvent(new Message(Text.Serializer.toJson(message), message.getString())); - }); - ClientReceiveMessageEvents.MODIFY_GAME.register((message, actionbar) -> { - return prepareOnEvent(message, actionbar); - }); - ClientSendMessageEvents.CHAT.register(message -> { - if (message.startsWith("/")) { - System.out.println("Sent command: " + message); - } +// ClientReceiveMessageEvents.CHAT.register((message, signedMessage, sender, params, receptionTimestamp) -> { +// chat.onEvent(new Message(Text.Serializer.toJson(message), message.getString())); +// }); + ClientReceiveMessageEvents.ALLOW_GAME.register((message, isActionBar) -> { + boolean block = (BBsentials.chat.processNotThreaded(new Message(Text.Serializer.toJson(message), message.getString()), isActionBar) == null); + return !block; }); + ClientReceiveMessageEvents.MODIFY_GAME.register(this::prepareOnEvent); + } public Text prepareOnEvent(Text text, boolean actionbar) { String json = Text.Serializer.toJson(text); Message message = new Message(json, text.getString(), actionbar); if (!actionbar) { - message = chat.onEvent(message); + message = chat.onEvent(message, actionbar); } Text toReturn = Text.Serializer.fromJson(message.getJson()); if (BBsentials.config.swapActionBarChat && !BBsentials.config.swapOnlyBBsentials) { @@ -57,20 +53,11 @@ public class FabricChat implements MCChat { } } - public void sendClientSideMessage(Message message, boolean actionbar) { - if (BBsentials.config.swapActionBarChat && !BBsentials.config.swapOnlyNormal) { - actionbar = !actionbar; - } - if (actionbar) { - showActionBar(message); - } - else { - sendClientSideMessage(message); - } - } public void showActionBar(Message message) { MinecraftClient client = MinecraftClient.getInstance(); if (client.player != null) { + if (BBsentials.config.overwriteActionBar.isEmpty()) + message = Message.of(BBsentials.config.overwriteActionBar); client.player.sendMessage(Text.Serializer.fromJson(message.getJson()), true); } } diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/IntegerTextField.java b/fabric/src/main/java/de/hype/bbsentials/fabric/IntegerTextField.java new file mode 100644 index 0000000..9204900 --- /dev/null +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/IntegerTextField.java @@ -0,0 +1,34 @@ +package de.hype.bbsentials.fabric; + +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.text.Text; +import org.jetbrains.annotations.Nullable; + +public class IntegerTextField extends TextFieldWidget { + public IntegerTextField(TextRenderer textRenderer, int width, int height, Text text) { + super(textRenderer, width, height, text); + } + + public IntegerTextField(TextRenderer textRenderer, int x, int y, int width, int height, Text text) { + super(textRenderer, x, y, width, height, text); + } + + public IntegerTextField(TextRenderer textRenderer, int x, int y, int width, int height, @Nullable TextFieldWidget copyFrom, Text text) { + super(textRenderer, x, y, width, height, copyFrom, text); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + // Allow removal (backspace and delete) and specific key combinations (Ctrl+A) + if (chr == 8 || chr == 127 || (modifiers & 1) == 1) { + return super.charTyped(chr, modifiers); + } + // Allow digits (0-9) and the minus sign for negative numbers + else if ((chr >= '0' && chr <= '9') || chr == '-') { + return super.charTyped(chr, modifiers); + } + return false; // Block other characters + } + +} diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/ModInitialiser.java b/fabric/src/main/java/de/hype/bbsentials/fabric/ModInitialiser.java index 57f50f4..2a9ce01 100644 --- a/fabric/src/main/java/de/hype/bbsentials/fabric/ModInitialiser.java +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/ModInitialiser.java @@ -6,6 +6,7 @@ import de.hype.bbsentials.common.chat.Message; import de.hype.bbsentials.common.client.BBsentials; import de.hype.bbsentials.common.client.Config; import de.hype.bbsentials.common.mclibraries.EnvironmentCore; +import de.hype.bbsentials.fabric.numpad.NumPadCodes; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; @@ -25,14 +26,7 @@ import java.util.List; import static de.hype.bbsentials.common.client.BBsentials.*; public class ModInitialiser implements ClientModInitializer { - @Override - public void onInitializeClient() { - EnvironmentCore core = new EnvironmentCore(new BBUtils(),new FabricChat(),new MCUtils(),new Commands(),new Options(),new DebugThread()); - ClientPlayConnectionEvents.JOIN.register((a, b, c) -> { - BBsentials.onServerSwap(); - }); - } - + public static NumPadCodes codes; { ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> { dispatcher.register(ClientCommandManager.literal("socialoptions") @@ -189,8 +183,7 @@ public class ModInitialiser implements ClientModInitializer { ); }); //bbi - KeyBinding devKeyBind = new KeyBinding("Open Mod Menu Config", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_ADD, "BBsentials: Developing Tools"); - KeyBindingHelper.registerKeyBinding(devKeyBind); + KeyBinding devKeyBind = new KeyBinding("Open Mod Menu Config", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_MULTIPLY, "BBsentials: Developing Tools"); ClientTickEvents.END_CLIENT_TICK.register(client -> { if (devKeyBind.wasPressed()) { MinecraftClient.getInstance().setScreen(BBsentialsConfigScreemFactory.create(MinecraftClient.getInstance().currentScreen)); @@ -220,16 +213,25 @@ public class ModInitialiser implements ClientModInitializer { ClientTickEvents.END_CLIENT_TICK.register(client -> { if (petKeyBind.wasPressed()) MinecraftClient.getInstance().getNetworkHandler().sendChatMessage("/pets"); }); - for (int i = 1; i <= 9; i++) { - KeyBinding ecPageKeyBind = new KeyBinding("Ender Chest Page " + i, InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_1 + (i - 1), "BBsentials"); - KeyBindingHelper.registerKeyBinding(ecPageKeyBind); - int pageNum = i; // Capture the page number for lambda - ClientTickEvents.END_CLIENT_TICK.register(client -> { - if (ecPageKeyBind.wasPressed()) { - getConfig().sender.addImmediateSendTask("/ec " + pageNum); - } - }); - } +// for (int i = 1; i <= 9; i++) { +// KeyBinding ecPageKeyBind = new KeyBinding("Ender Chest Page " + i, InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_1 + (i - 1), "BBsentials"); +// KeyBindingHelper.registerKeyBinding(ecPageKeyBind); +// int pageNum = i; // Capture the page number for lambda +// ClientTickEvents.END_CLIENT_TICK.register(client -> { +// if (ecPageKeyBind.wasPressed()) { +// getConfig().sender.addImmediateSendTask("/ec " + pageNum); +// } +// }); +// } } // KeyBinds + @Override + public void onInitializeClient() { + EnvironmentCore core = new EnvironmentCore(new BBUtils(), new FabricChat(), new MCUtils(), new Commands(), new Options(), new DebugThread()); + codes = new NumPadCodes(); + ClientPlayConnectionEvents.JOIN.register((a, b, c) -> { + BBsentials.onServerSwap(); + }); + } + } diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCode.java b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCode.java new file mode 100644 index 0000000..1e44654 --- /dev/null +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCode.java @@ -0,0 +1,63 @@ +package de.hype.bbsentials.fabric.numpad; + +import de.hype.bbsentials.common.api.Formatting; +import de.hype.bbsentials.common.chat.Chat; +import de.hype.bbsentials.common.client.BBsentials; + +public class NumCode { + public String command; + public String code; + public boolean codeIsTransient; + public Formatting formatting; + public String requiredPermission; + public transient Runnable toPerform; + + public NumCode(String code, String command) { + this.command = command; + this.code = code; + this.codeIsTransient = false; + this.formatting = Formatting.DARK_GREEN; + requiredPermission = ""; + } + + public NumCode(String code, Formatting format, String requiredRole, Runnable toPerform) { + this.command = ""; + this.code = code; + this.formatting = format; + requiredPermission = requiredRole; + this.toPerform = toPerform; + codeIsTransient = true; + } + + public void execute() { + if (!BBsentials.config.hasBBRoles(requiredPermission)) { + Chat.sendPrivateMessageToSelfError("You don't have the required permissions to run '" + code + "' !"); + return; + } + if (!command.isEmpty()) { + BBsentials.config.sender.addImmediateSendTask(command); + } + else { + if (toPerform == null) { + Chat.sendPrivateMessageToSelfError("The specified code doesn't do anything"); + return; + } + try { + BBsentials.executionService.execute(toPerform); + } catch (Exception e) { + Chat.sendPrivateMessageToSelfError(e.getMessage()); + e.printStackTrace(); + } + } + Chat.sendPrivateMessageToSelfInfo("'" + code + "' executed."); + } + + @Override + public boolean equals(Object obj) { + if (obj.getClass().equals(this.getClass())) { + return ((NumCode) obj).code.equals(code); + } + return false; + } + +} diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCodeInputField.java b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCodeInputField.java new file mode 100644 index 0000000..2baf9b4 --- /dev/null +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCodeInputField.java @@ -0,0 +1,68 @@ +package de.hype.bbsentials.fabric.numpad; + +import de.hype.bbsentials.common.api.Formatting; +import de.hype.bbsentials.common.client.BBsentials; +import de.hype.bbsentials.fabric.IntegerTextField; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.tooltip.Tooltip; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.text.Text; +import org.jetbrains.annotations.Nullable; + +public class NumCodeInputField extends IntegerTextField { + public final NumPadConfigScreen codeConfigScreen; + + public NumCodeInputField(TextRenderer textRenderer, int width, int height, Text text, NumPadConfigScreen codeConfigScreen) { + super(textRenderer, width, height, text); + this.codeConfigScreen = codeConfigScreen; + setChangedListener(this::onChangeEvent); + } + + public NumCodeInputField(TextRenderer textRenderer, int x, int y, int width, int height, Text text, NumPadConfigScreen codeManager) { + super(textRenderer, x, y, width, height, text); + this.codeConfigScreen = codeManager; + setChangedListener(this::onChangeEvent); + } + + public NumCodeInputField(TextRenderer textRenderer, int x, int y, int width, int height, @Nullable TextFieldWidget copyFrom, Text text, NumPadConfigScreen codeManager) { + super(textRenderer, x, y, width, height, copyFrom, text); + this.codeConfigScreen = codeManager; + setChangedListener(this::onChangeEvent); + } + + @Override + public boolean charTyped(char chr, int modifiers) { + return super.charTyped(chr, modifiers); + } + + private boolean codeIsFree(String input) { + int usedAlready = 0; + for (int i1 = 0; i1 < codeConfigScreen.codes.numCodes.size(); i1++) { + if (codeConfigScreen.codes.numCodes.get(i1).code.equals(input)) { + usedAlready++; + if (usedAlready > 1) { + return false; + } + } + } + return true; + } + + public boolean forbiddenCode(String input) { + if (BBsentials.config.hasBBRoles("dev")) return false; + if (input.startsWith(String.valueOf(0))) { + return true; + } + return false; + } + + public void onChangeEvent(String newText) { + codeConfigScreen.updateCodes(); + if (!codeIsFree(newText) || forbiddenCode(newText)) { + setTooltip(Tooltip.of(Text.of(Formatting.RED + "You can not use this code.§r\nWarning: This is only updated once clicking into the field!"))); + } + else { + setTooltip(Tooltip.of(Text.of(""))); + } + } +} diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadCodes.java b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadCodes.java new file mode 100644 index 0000000..d3b3c1e --- /dev/null +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadCodes.java @@ -0,0 +1,210 @@ +package de.hype.bbsentials.fabric.numpad; + +import com.google.common.reflect.TypeToken; +import de.hype.bbsentials.common.api.Formatting; +import de.hype.bbsentials.common.chat.Message; +import de.hype.bbsentials.common.client.BBsentials; +import de.hype.bbsentials.common.mclibraries.EnvironmentCore; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBinding; +import org.lwjgl.glfw.GLFW; + +import java.io.*; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +import static de.hype.bbsentials.common.packets.PacketUtils.gson; + +public class NumPadCodes { + private static final int NUMPAD_KEY_COUNT = 10; + public List<NumCode> numCodes = new ArrayList<>(); + private KeyBinding[] numpadKeybindings = new KeyBinding[NUMPAD_KEY_COUNT]; + private String enteredCode = ""; + private boolean[] keyReleased = new boolean[NUMPAD_KEY_COUNT + 3]; + private long lastKeyPressTime = 0; + private boolean overidedActionBar = false; + + public NumPadCodes() { + loadNumCodesFromFile(); + if (numCodes == null) { + numCodes = new ArrayList<>(); + } + if (numCodes.isEmpty()) { + addDefaultCodes(true); + } + else { + addDefaultCodes(false); + } + for (int i = 0; i < NUMPAD_KEY_COUNT; i++) { + int keyCode = GLFW.GLFW_KEY_KP_0 + i; + numpadKeybindings[i] = KeyBindingHelper.registerKeyBinding(new KeyBinding( + String.valueOf(i), + keyCode, + "bbsentials.numpad" + )); + } + KeyBinding enterKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("Enter", GLFW.GLFW_KEY_KP_ENTER, "bbsentials.numpad")); + KeyBinding clear = KeyBindingHelper.registerKeyBinding(new KeyBinding("Clear", GLFW.GLFW_KEY_KP_ADD, "bbsentials.numpad")); + KeyBinding remove = KeyBindingHelper.registerKeyBinding(new KeyBinding("Remove", GLFW.GLFW_KEY_KP_DECIMAL, "bbsentials.numpad")); + Thread t = new Thread(() -> { + while (true) { + for (int i = 0; i < numpadKeybindings.length; i++) { + if (numpadKeybindings[i].isPressed() && keyReleased[i]) { + enteredCode = enteredCode + i; + keyReleased[i] = false; // Mark key as not released + lastKeyPressTime = System.currentTimeMillis(); + break; + } + else if (!numpadKeybindings[i].isPressed()) { + keyReleased[i] = true; // Mark key as released + } + } + if (enterKey.isPressed() && keyReleased[10]) { + executeCode(); + enteredCode = ""; + keyReleased[10] = false; + } + else if (!enterKey.isPressed()) { + keyReleased[10] = true; + } + if (clear.isPressed() && keyReleased[11]) { + enteredCode = ""; + keyReleased[11] = false; + } + else if (!clear.isPressed()) { + keyReleased[11] = true; + } + if (remove.isPressed() && keyReleased[12]) { + if (!enteredCode.isEmpty()) { + enteredCode = enteredCode.substring(0, enteredCode.length() - 1); + keyReleased[12] = false; + } + } + else if (!remove.isPressed()) { + keyReleased[12] = true; + } + // Reset key cooldowns after 5 seconds + if (System.currentTimeMillis() - lastKeyPressTime >= 5000) { + enteredCode = ""; + } + if (!enteredCode.isEmpty()) { + BBsentials.config.overwriteActionBar = getColorCode() + enteredCode; + overidedActionBar = true; + EnvironmentCore.chat.showActionBar(Message.of(getColorCode() + enteredCode)); + } + else if (overidedActionBar) { + BBsentials.config.overwriteActionBar = ""; + overidedActionBar = false; + EnvironmentCore.chat.showActionBar(Message.of("")); + } + + try { + Thread.sleep(50); + } catch (Exception ignored) { + } + } + }); + t.start(); + } + + public void addDefaultCodes(boolean all) { + List<NumCode> defaultCodes = new ArrayList(); + defaultCodes.add((new NumCode("042", Formatting.DARK_BLUE, "dev", () -> MinecraftClient.getInstance().execute(() -> MinecraftClient.getInstance().setScreen(new NumPadConfigScreen(this)))))); + defaultCodes.add((new NumCode("11", "/l"))); + if (all) { + numCodes.addAll(defaultCodes); + return; + } + for (int i = 0; i < defaultCodes.size(); i++) { + if (defaultCodes.get(i).codeIsTransient) { + numCodes.add(defaultCodes.get(i)); + } + } + } + + public String getColorCode() { + int index = getCodeIndex(); + boolean exists = codeExists(); + boolean devCode = enteredCode.startsWith("0"); + + if (devCode) { + if (index != -1 && !exists) { + return Formatting.DARK_BLUE.toString(); + } + else if (index != -1) { + return Formatting.DARK_AQUA.toString(); + } + else if (exists) { + return Formatting.AQUA.toString(); + } + } + else { + if (index != -1 && !exists) { + return Formatting.DARK_GREEN.toString(); + } + else if (index != -1) { + return Formatting.GREEN.toString(); + } + else if (exists) { + return Formatting.YELLOW.toString(); + } + } + return Formatting.RED.toString(); + } + + public void executeCode() { + int index = getCodeIndex(); + if (index != -1) { + numCodes.get(index).execute(); + } + } + + public int getCodeIndex() { + for (int i = 0; i < numCodes.size(); i++) { + if (numCodes.get(i).code.equals(enteredCode)) return i; + } + return -1; + } + + /** + * @return returns true if there is at least 1 code to go on. otherwise false + */ + private boolean codeExists() { + for (int i1 = 0; i1 < numCodes.size(); i1++) { + if (numCodes.get(i1).code.startsWith(enteredCode) && !numCodes.get(i1).code.equals(enteredCode)) { + return true; + } + } + return false; + } + + public void saveNumCodesToFile() { + List<NumCode> toSaveCodes = new ArrayList<>(); + for (NumCode numCode : numCodes) { + if (!numCode.codeIsTransient) { + toSaveCodes.add(numCode); + } + } + try (Writer writer = new FileWriter(new File(EnvironmentCore.mcUtils.getConfigPath(), "BBsentials_Numpad_codes.json") + )) { + gson.toJson(toSaveCodes, writer); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + public void loadNumCodesFromFile() { + try (Reader reader = new FileReader(new File(EnvironmentCore.mcUtils.getConfigPath(), "BBsentials_Numpad_codes.json"))) { + Type listType = new TypeToken<List<NumCode>>() { + }.getType(); + numCodes = gson.fromJson(reader, listType); + } catch (IOException e) { + e.printStackTrace(); + } + } + + +} diff --git a/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadConfigScreen.java b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadConfigScreen.java new file mode 100644 index 0000000..37df8ca --- /dev/null +++ b/fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadConfigScreen.java @@ -0,0 +1,145 @@ +package de.hype.bbsentials.fabric.numpad; + +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TextFieldWidget; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.List; + +public class NumPadConfigScreen extends Screen { + public List<NumCode> hiddenNumCodes = new ArrayList<>(); + ButtonWidget addButton; + ButtonWidget okButton; + NumPadCodes codes; + private List<NumCodeInputField> codeTextFields; + private List<TextFieldWidget> commandTextFields; + private List<ButtonWidget> removeButtonFields; + + protected NumPadConfigScreen(NumPadCodes codes) { + super(Text.of("NumPadConfigScreen")); + codeTextFields = new ArrayList<>(); + commandTextFields = new ArrayList<>(); + removeButtonFields = new ArrayList<>(); + this.codes = codes; + } + + @Override + protected void init() { + if (okButton == null) { + for (NumCode numCode : codes.numCodes) { + if (!numCode.codeIsTransient) { + addRow(numCode.code, numCode.command); + okButton = new ButtonWidget.Builder(Text.of("Done"), button -> done()).build(); + addButton = new ButtonWidget.Builder(Text.of("+"), button -> { + addNewRow(); + }).build(); + } + else { + hiddenNumCodes.add(numCode); + } + } + } + okButton.setY(height - okButton.getHeight()); + okButton.setX((width / 2) - okButton.getWidth() / 2); + addButton.setMessage(Text.of(("+"))); + addButton.setWidth(width / 2); + addButton.setPosition((width / 2) - addButton.getWidth() / 2, 20); + codeTextFields.forEach((this::addDrawableChild)); + commandTextFields.forEach((this::addDrawableChild)); + removeButtonFields.forEach((this::addDrawableChild)); + updateFields(); + + } + + private void addRow(String code, String command) { + NumCodeInputField textField2 = new NumCodeInputField(textRenderer, width / 3, 20, Text.of(("")), this); + textField2.setText(code); + codeTextFields.add(textField2); + TextFieldWidget textField = new TextFieldWidget(textRenderer, width / 3, 20, Text.of((""))); + textField.setText(command); + commandTextFields.add(textField); + int finalRemoveId = commandTextFields.size(); + ButtonWidget removeButton = ButtonWidget.builder(Text.of("-"), button -> removeRow(finalRemoveId)).build(); + removeButtonFields.add(removeButton); + } + + private void addNewRow() { + addRow("", ""); + updateFields(); + } + + private void removeRow(int i) { + if (!codeTextFields.isEmpty()) { + TextFieldWidget removedCodeField = codeTextFields.remove(i - 1); + TextFieldWidget removedCommandField = commandTextFields.remove(i - 1); + ButtonWidget removeButton = removeButtonFields.remove(i - 1); + remove(removeButton); + remove(removedCodeField); + remove(removedCommandField); + updateFields(); + } + } + + private void updateFields() { + int leftX = width / 9; // Start position for codeTextFields + int rightX = leftX * 2 + width / 3; // Start position for commandTextFields + + clearChildren(); + + for (int i = 0; i < codeTextFields.size(); i++) { + int hight = 60 + i * 30; + + // Set the positions for codeTextFields + codeTextFields.get(i).setX(leftX); + codeTextFields.get(i).setY(hight); + codeTextFields.get(i).setWidth(width / 3); + + // Set the positions for commandTextFields + commandTextFields.get(i).setX(rightX); + commandTextFields.get(i).setY(hight); + commandTextFields.get(i).setWidth(width / 3); + + // Set positions for removeButtonFields + removeButtonFields.get(i).setWidth(width / 12); + removeButtonFields.get(i).setX(rightX + width / 3 + width / 18 - removeButtonFields.get(i).getWidth() / 2); // Place the remove button to the right + removeButtonFields.get(i).setY(hight); + + addDrawableChild(codeTextFields.get(i)); + addDrawableChild(commandTextFields.get(i)); + addDrawableChild(removeButtonFields.get(i)); + } + + addDrawableChild(addButton); + addDrawableChild(okButton); + } + + public void updateCodes() { + List<NumCode> newCodes = new ArrayList<>(hiddenNumCodes); + for (int i = 0; i < commandTextFields.size(); i++) { + newCodes.add(new NumCode(codeTextFields.get(i).getText(), commandTextFields.get(i).getText())); + } + codes.numCodes = newCodes; + } + + public void done() { + codes.saveNumCodesToFile(); + close(); + } + + @Override + public boolean shouldCloseOnEsc() { + return false; + } + + @Override + public void close() { + updateCodes(); + codes.saveNumCodesToFile(); + super.close(); + } + + +} + diff --git a/forge/src/main/java/de/hype/bbsentials/forge/ForgeChat.java b/forge/src/main/java/de/hype/bbsentials/forge/ForgeChat.java index 7764d7b..ef5d7ed 100644 --- a/forge/src/main/java/de/hype/bbsentials/forge/ForgeChat.java +++ b/forge/src/main/java/de/hype/bbsentials/forge/ForgeChat.java @@ -2,11 +2,9 @@ package de.hype.bbsentials.forge; import de.hype.bbsentials.common.chat.Chat; import de.hype.bbsentials.common.chat.Message; -import de.hype.bbsentials.common.client.BBsentials; import de.hype.bbsentials.common.mclibraries.MCChat; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; -import net.minecraft.util.ChatStyle; import net.minecraft.util.IChatComponent; import net.minecraftforge.client.event.ClientChatReceivedEvent; import net.minecraftforge.common.MinecraftForge; @@ -29,7 +27,7 @@ public class ForgeChat implements MCChat { //2 means action bar // Chat.sendPrivateMessageToSelfDebug(String.valueOf(((int) event.type))); Message message = new Message(IChatComponent.Serializer.componentToJson(event.message), event.message.getUnformattedText()); - Message newMessage = chat.onEvent(message); + Message newMessage = chat.onEvent(message, false); if (!newMessage.equals(message)) { event.setCanceled(true); sendClientSideMessage(newMessage); @@ -46,19 +44,6 @@ public class ForgeChat implements MCChat { } } - - public void sendClientSideMessage(Message message, boolean actionbar) { - if (BBsentials.config.swapActionBarChat && !BBsentials.config.swapOnlyNormal) { - actionbar = !actionbar; - } - if (actionbar) { - showActionBar(message); - } - else { - sendClientSideMessage(message); - } - } - public void sendChatMessage(String message) { Minecraft.getMinecraft().thePlayer.sendChatMessage(message); } |