From 689e786c36ea5c9ec6c2d826d56881c5a204f74c Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Thu, 29 Jun 2023 05:16:26 +0200 Subject: [PATCH] basic keybinds --- .../github/sebseb7/autotrade/AutoTrade.java | 5 +- .../github/sebseb7/autotrade/InitHandler.java | 20 ++++++ .../sebseb7/autotrade/event/InputHandler.java | 28 ++++++++ .../autotrade/event/KeybindCallbacks.java | 69 +++++++++++++++++++ .../assets/autotrade/lang/en_us.json | 5 +- 5 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/github/sebseb7/autotrade/InitHandler.java create mode 100644 src/main/java/com/github/sebseb7/autotrade/event/InputHandler.java create mode 100644 src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java diff --git a/src/main/java/com/github/sebseb7/autotrade/AutoTrade.java b/src/main/java/com/github/sebseb7/autotrade/AutoTrade.java index 9d4ed8c..2048a09 100644 --- a/src/main/java/com/github/sebseb7/autotrade/AutoTrade.java +++ b/src/main/java/com/github/sebseb7/autotrade/AutoTrade.java @@ -1,5 +1,6 @@ package com.github.sebseb7.autotrade; +import fi.dy.masa.malilib.event.InitializationHandler; import net.fabricmc.api.ModInitializer; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -8,5 +9,7 @@ public class AutoTrade implements ModInitializer { public static final Logger logger = LogManager.getLogger(Reference.MOD_ID); @Override - public void onInitialize() {} + public void onInitialize() { + InitializationHandler.getInstance().registerInitializationHandler(new InitHandler()); + } } diff --git a/src/main/java/com/github/sebseb7/autotrade/InitHandler.java b/src/main/java/com/github/sebseb7/autotrade/InitHandler.java new file mode 100644 index 0000000..b901428 --- /dev/null +++ b/src/main/java/com/github/sebseb7/autotrade/InitHandler.java @@ -0,0 +1,20 @@ +package com.github.sebseb7.autotrade; + +import com.github.sebseb7.autotrade.config.Configs; +import com.github.sebseb7.autotrade.event.InputHandler; +import com.github.sebseb7.autotrade.event.KeybindCallbacks; +import fi.dy.masa.malilib.config.ConfigManager; +import fi.dy.masa.malilib.event.InputEventHandler; +import fi.dy.masa.malilib.interfaces.IInitializationHandler; + +public class InitHandler implements IInitializationHandler { + @Override + public void registerModHandlers() { + ConfigManager.getInstance().registerConfigHandler(Reference.MOD_ID, new Configs()); + + InputHandler handler = new InputHandler(); + InputEventHandler.getKeybindManager().registerKeybindProvider(handler); + + KeybindCallbacks.getInstance().setCallbacks(); + } +} diff --git a/src/main/java/com/github/sebseb7/autotrade/event/InputHandler.java b/src/main/java/com/github/sebseb7/autotrade/event/InputHandler.java new file mode 100644 index 0000000..92f98c6 --- /dev/null +++ b/src/main/java/com/github/sebseb7/autotrade/event/InputHandler.java @@ -0,0 +1,28 @@ +package com.github.sebseb7.autotrade.event; + +import com.github.sebseb7.autotrade.Reference; +import com.github.sebseb7.autotrade.config.Hotkeys; +import fi.dy.masa.malilib.hotkeys.IHotkey; +import fi.dy.masa.malilib.hotkeys.IKeybindManager; +import fi.dy.masa.malilib.hotkeys.IKeybindProvider; + +public class InputHandler implements IKeybindProvider { + private final KeybindCallbacks callbacks; + + public InputHandler() { + this.callbacks = KeybindCallbacks.getInstance(); + } + + @Override + public void addKeysToMap(IKeybindManager manager) { + for (IHotkey hotkey : Hotkeys.HOTKEY_LIST) { + manager.addKeybindToMap(hotkey.getKeybind()); + } + } + + @Override + public void addHotkeys(IKeybindManager manager) { + manager.addHotkeysForCategory( + Reference.MOD_NAME, "autotrade.hotkeys.category.hotkeys", Hotkeys.HOTKEY_LIST); + } +} diff --git a/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java new file mode 100644 index 0000000..59cf289 --- /dev/null +++ b/src/main/java/com/github/sebseb7/autotrade/event/KeybindCallbacks.java @@ -0,0 +1,69 @@ +package com.github.sebseb7.autotrade.event; + +import com.github.sebseb7.autotrade.config.Configs; +import com.github.sebseb7.autotrade.config.Hotkeys; +import com.github.sebseb7.autotrade.gui.GuiConfigs; +import fi.dy.masa.malilib.config.options.ConfigHotkey; +import fi.dy.masa.malilib.gui.GuiBase; +import fi.dy.masa.malilib.gui.Message; +import fi.dy.masa.malilib.hotkeys.IHotkeyCallback; +import fi.dy.masa.malilib.hotkeys.IKeybind; +import fi.dy.masa.malilib.hotkeys.KeyAction; +import fi.dy.masa.malilib.util.GuiUtils; +import fi.dy.masa.malilib.util.InfoUtils; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.ingame.HandledScreen; + +public class KeybindCallbacks implements IHotkeyCallback { + private static final KeybindCallbacks INSTANCE = new KeybindCallbacks(); + + public static KeybindCallbacks getInstance() { + return INSTANCE; + } + + private KeybindCallbacks() {} + + public void setCallbacks() { + for (ConfigHotkey hotkey : Hotkeys.HOTKEY_LIST) { + hotkey.getKeybind().setCallback(this); + } + } + + public boolean functionalityEnabled() { + return Configs.Generic.ENABLED.getBooleanValue(); + } + + @Override + public boolean onKeyAction(KeyAction action, IKeybind key) { + boolean cancel = this.onKeyActionImpl(action, key); + return cancel; + } + + private boolean onKeyActionImpl(KeyAction action, IKeybind key) { + MinecraftClient mc = MinecraftClient.getInstance(); + + if (mc.player == null || mc.world == null) { + return false; + } + + if (key == Hotkeys.TOGGLE_KEY.getKeybind()) { + Configs.Generic.ENABLED.toggleBooleanValue(); + String msg = + this.functionalityEnabled() + ? "autotrade.message.toggled_mod_on" + : "autotrade.message.toggled_mod_off"; + InfoUtils.showGuiOrInGameMessage(Message.MessageType.INFO, msg); + return true; + } else if (key == Hotkeys.OPEN_GUI_SETTINGS.getKeybind()) { + GuiBase.openGui(new GuiConfigs()); + return true; + } + + if (this.functionalityEnabled() == false + || (GuiUtils.getCurrentScreen() instanceof HandledScreen) == false) { + return false; + } + + return false; + } +} diff --git a/src/main/resources/assets/autotrade/lang/en_us.json b/src/main/resources/assets/autotrade/lang/en_us.json index f1d4786..8da8191 100644 --- a/src/main/resources/assets/autotrade/lang/en_us.json +++ b/src/main/resources/assets/autotrade/lang/en_us.json @@ -3,5 +3,8 @@ "autotrade.gui.button.config_gui.hotkeys": "Hotkeys", "autotrade.gui.button.config_gui.toggles": "Toggles", - "autotrade.gui.title.configs": "AutoTrade" + "autotrade.gui.title.configs": "AutoTrade", + + "autotrade.message.toggled_mod_off": "Toggled Auto Trade §cOFF", + "autotrade.message.toggled_mod_on": "Toggled Auto Trade §aON" }