aboutsummaryrefslogtreecommitdiff
path: root/fabric
diff options
context:
space:
mode:
Diffstat (limited to 'fabric')
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/FabricChat.java35
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/IntegerTextField.java34
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/ModInitialiser.java42
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCode.java63
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumCodeInputField.java68
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadCodes.java210
-rw-r--r--fabric/src/main/java/de/hype/bbsentials/fabric/numpad/NumPadConfigScreen.java145
7 files changed, 553 insertions, 44 deletions
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();
+ }
+
+
+}
+