basic keybinds
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
20
src/main/java/com/github/sebseb7/autotrade/InitHandler.java
Normal file
20
src/main/java/com/github/sebseb7/autotrade/InitHandler.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user