From a75a3562b2e36ef6e6463758c3640ad03aa5bac3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 1 Jan 2019 13:55:45 +0800 Subject: 1.2 Custom Keybinds --- src/main/java/me/shedaniel/ClientListener.java | 60 ++++++++++++------ src/main/java/me/shedaniel/Core.java | 5 +- src/main/java/me/shedaniel/gui/ConfigGui.java | 74 ++++++++++++++++++++++ src/main/java/me/shedaniel/gui/GuiItemList.java | 8 +++ .../java/me/shedaniel/gui/REIRenderHelper.java | 4 +- src/main/java/me/shedaniel/gui/RecipeGui.java | 1 + .../me/shedaniel/gui/widget/KeyBindButton.java | 59 +++++++++++++++++ .../java/me/shedaniel/library/KeyBindFunction.java | 22 +++++++ .../java/me/shedaniel/library/KeyBindManager.java | 31 --------- 9 files changed, 212 insertions(+), 52 deletions(-) create mode 100644 src/main/java/me/shedaniel/gui/ConfigGui.java create mode 100644 src/main/java/me/shedaniel/gui/widget/KeyBindButton.java create mode 100644 src/main/java/me/shedaniel/library/KeyBindFunction.java delete mode 100755 src/main/java/me/shedaniel/library/KeyBindManager.java (limited to 'src/main/java') diff --git a/src/main/java/me/shedaniel/ClientListener.java b/src/main/java/me/shedaniel/ClientListener.java index 54629ae55..abfed4b7f 100755 --- a/src/main/java/me/shedaniel/ClientListener.java +++ b/src/main/java/me/shedaniel/ClientListener.java @@ -3,10 +3,11 @@ package me.shedaniel; import me.shedaniel.api.IREIPlugin; import me.shedaniel.gui.REIRenderHelper; import me.shedaniel.impl.REIRecipeManager; -import me.shedaniel.library.KeyBindManager; +import me.shedaniel.library.KeyBindFunction; import me.shedaniel.listenerdefinitions.DoneLoading; import me.shedaniel.listenerdefinitions.RecipeLoadListener; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.api.ModInitializer; import net.minecraft.client.settings.KeyBinding; import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.EnchantmentHelper; @@ -18,19 +19,25 @@ import net.minecraft.util.DefaultedList; import net.minecraft.util.registry.Registry; import java.awt.event.KeyEvent; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; -public class ClientListener implements DoneLoading, ClientModInitializer, RecipeLoadListener { - public static KeyBinding recipeKeybind; - public static KeyBinding hideKeybind; - public static KeyBinding useKeybind; +public class ClientListener implements DoneLoading, RecipeLoadListener { + + public static KeyBindFunction recipeKeybind; + public static KeyBindFunction hideKeybind; + public static KeyBindFunction useKeybind; + public static List keyBinds = new ArrayList<>(); private List plugins; public static List stackList; + public static boolean processGuiKeybinds(int typedChar) { + for(KeyBindFunction keyBind : keyBinds) + if (keyBind.apply(typedChar)) + return true; + return false; + } + @Override public void onDoneLoading() { plugins = new ArrayList<>(); @@ -39,11 +46,32 @@ public class ClientListener implements DoneLoading, ClientModInitializer, Recipe buildItemList(); } - @Override - public void onInitializeClient() { - recipeKeybind = KeyBindManager.createKeybinding("key.rei.recipe", KeyEvent.VK_R, "key.rei.category", REIRenderHelper::recipeKeybind); - hideKeybind = KeyBindManager.createKeybinding("key.rei.hide", KeyEvent.VK_O, "key.rei.category", REIRenderHelper::hideKeybind); - useKeybind = KeyBindManager.createKeybinding("key.rei.use", KeyEvent.VK_U, "key.rei.category", REIRenderHelper::useKeybind); + public void onInitializeKeyBind() { + recipeKeybind = new KeyBindFunction(Core.config.recipeKeyBind) { + @Override + public boolean apply(int key) { + if (key == this.getKey()) + REIRenderHelper.recipeKeybind(); + return key == this.getKey(); + } + }; + hideKeybind = new KeyBindFunction(Core.config.hideKeyBind) { + @Override + public boolean apply(int key) { + if (key == this.getKey()) + REIRenderHelper.hideKeybind(); + return key == this.getKey(); + } + }; + useKeybind = new KeyBindFunction(Core.config.usageKeyBind) { + @Override + public boolean apply(int key) { + if (key == this.getKey()) + REIRenderHelper.useKeybind(); + return key == this.getKey(); + } + }; + keyBinds.addAll(Arrays.asList(recipeKeybind, hideKeybind, useKeybind)); } private void buildItemList() { @@ -67,10 +95,6 @@ public class ClientListener implements DoneLoading, ClientModInitializer, Recipe item.addStacksForDisplay(item.getItemGroup(), items); items.forEach(stackList::add); } catch (NullPointerException e) { -// if (item == Items.ENCHANTED_BOOK) { -// item.fillItemGroup(ItemGroup.TOOLS, items); -// items.forEach(stackList::add); -// } } } diff --git a/src/main/java/me/shedaniel/Core.java b/src/main/java/me/shedaniel/Core.java index 55a7c879f..f6012dc80 100755 --- a/src/main/java/me/shedaniel/Core.java +++ b/src/main/java/me/shedaniel/Core.java @@ -45,22 +45,25 @@ public class Core implements PacketAdder, ClientModInitializer { private static List events = new LinkedList<>(); public static final File configFile = new File(FabricLoader.INSTANCE.getConfigDirectory(), "rei.json"); public static REIConfig config; + public static ClientListener clientListener; @Override public void onInitializeClient() { + this.clientListener = new ClientListener(); registerEvents(); try { loadConfig(); } catch (IOException e) { e.printStackTrace(); } + this.clientListener.onInitializeKeyBind(); } private void registerEvents() { registerEvent(new DrawContainerListener()); registerEvent(new ResizeListener()); registerEvent(new VanillaPlugin()); - registerEvent(new ClientListener()); + registerEvent(clientListener); } public static void registerEvent(IEvent event) { diff --git a/src/main/java/me/shedaniel/gui/ConfigGui.java b/src/main/java/me/shedaniel/gui/ConfigGui.java new file mode 100644 index 000000000..31a1c3feb --- /dev/null +++ b/src/main/java/me/shedaniel/gui/ConfigGui.java @@ -0,0 +1,74 @@ +package me.shedaniel.gui; + +import me.shedaniel.ClientListener; +import me.shedaniel.Core; +import me.shedaniel.gui.widget.KeyBindButton; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.resource.language.I18n; + +import java.io.IOException; + +public class ConfigGui extends Gui { + + private Gui parent; + + public ConfigGui(Gui parent) { + this.parent = parent; + } + + @Override + protected void onInitialized() { + addButton(new KeyBindButton(997, parent.width / 2 - 20, 30, 80, 20, Core.config.recipeKeyBind, key -> { + Core.config.recipeKeyBind = key; + ClientListener.recipeKeybind.setKey(key); + try { + Core.saveConfig(); + } catch (IOException e) { + e.printStackTrace(); + } + })); + addButton(new KeyBindButton(997, parent.width / 2 - 20, 60, 80, 20, Core.config.usageKeyBind, key -> { + Core.config.usageKeyBind = key; + ClientListener.useKeybind.setKey(key); + try { + Core.saveConfig(); + } catch (IOException e) { + e.printStackTrace(); + } + })); + addButton(new KeyBindButton(997, parent.width / 2 - 20, 90, 80, 20, Core.config.hideKeyBind, key -> { + Core.config.hideKeyBind = key; + ClientListener.hideKeybind.setKey(key); + try { + Core.saveConfig(); + } catch (IOException e) { + e.printStackTrace(); + } + })); + } + + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + drawBackground(); + super.draw(mouseX, mouseY, partialTicks); + String text = I18n.translate("key.rei.recipe") + ": "; + drawString(MinecraftClient.getInstance().fontRenderer, text, parent.width / 2 - 40 - MinecraftClient.getInstance().fontRenderer.getStringWidth(text), 30 + 6, -1); + text = I18n.translate("key.rei.use") + ": "; + drawString(MinecraftClient.getInstance().fontRenderer, text, parent.width / 2 - 40 - MinecraftClient.getInstance().fontRenderer.getStringWidth(text), 60 + 6, -1); + text = I18n.translate("key.rei.hide") + ": "; + drawString(MinecraftClient.getInstance().fontRenderer, text, parent.width / 2 - 40 - MinecraftClient.getInstance().fontRenderer.getStringWidth(text), 90 + 6, -1); + } + + @Override + public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) { + if (p_keyPressed_1_ == 256 && this.canClose()) { + this.close(); + if (parent != null) + MinecraftClient.getInstance().openGui(parent); + return true; + } else { + return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_); + } + } +} diff --git a/src/main/java/me/shedaniel/gui/GuiItemList.java b/src/main/java/me/shedaniel/gui/GuiItemList.java index b4f913c7c..14ec8c5b3 100755 --- a/src/main/java/me/shedaniel/gui/GuiItemList.java +++ b/src/main/java/me/shedaniel/gui/GuiItemList.java @@ -11,6 +11,7 @@ import me.shedaniel.listenerdefinitions.IMixinContainerGui; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.ContainerGui; import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.resource.language.I18n; import net.minecraft.client.util.Window; import net.minecraft.item.ItemStack; import net.minecraft.text.TextComponent; @@ -37,6 +38,7 @@ public class GuiItemList extends Drawable { Button buttonLeft; Button buttonRight; Button buttonCheating; + Button buttonConfig; TextBox searchBox; private ArrayList view; private Control lastHovered; @@ -102,6 +104,12 @@ public class GuiItemList extends Drawable { controls.add(searchBox); buttonCheating = new Button(5, 5, 45, 20, getCheatModeText()); buttonCheating.onClick = this::cheatClicked; + buttonConfig = new Button(5, 28, 45, 20, I18n.translate("text.rei.config")); + buttonConfig.onClick = i -> { + MinecraftClient.getInstance().openGui(new ConfigGui(overlayedGui)); + return true; + }; + controls.add(buttonConfig); controls.add(buttonCheating); calculateSlots(); updateView(); diff --git a/src/main/java/me/shedaniel/gui/REIRenderHelper.java b/src/main/java/me/shedaniel/gui/REIRenderHelper.java index f706a4c56..a60ca45e4 100755 --- a/src/main/java/me/shedaniel/gui/REIRenderHelper.java +++ b/src/main/java/me/shedaniel/gui/REIRenderHelper.java @@ -1,11 +1,11 @@ package me.shedaniel.gui; import com.mojang.blaze3d.platform.GlStateManager; +import me.shedaniel.ClientListener; import me.shedaniel.gui.widget.Control; import me.shedaniel.gui.widget.IFocusable; import me.shedaniel.gui.widget.REISlot; import me.shedaniel.impl.REIRecipeManager; -import me.shedaniel.library.KeyBindManager; import me.shedaniel.listenerdefinitions.IMixinContainerGui; import net.fabricmc.loader.FabricLoader; import net.minecraft.client.MinecraftClient; @@ -154,7 +154,7 @@ public class REIRenderHelper { handled = true; } if (!handled) { - return KeyBindManager.processGuiKeybinds(typedChar); + return ClientListener.processGuiKeybinds(typedChar); } return handled; } diff --git a/src/main/java/me/shedaniel/gui/RecipeGui.java b/src/main/java/me/shedaniel/gui/RecipeGui.java index d4f3db8f0..9ca41e958 100755 --- a/src/main/java/me/shedaniel/gui/RecipeGui.java +++ b/src/main/java/me/shedaniel/gui/RecipeGui.java @@ -73,6 +73,7 @@ public class RecipeGui extends ContainerGui { @Override public void draw(int mouseX, int mouseY, float partialTicks) { + drawBackground(); super.draw(mouseX, mouseY, partialTicks); int y = (int) ((mainWindow.getScaledHeight() / 2 - this.guiHeight / 2)); drawStringCentered(this.fontRenderer, selectedCategory.getDisplayName(), left + guiWidth / 2, y + 11, -1); diff --git a/src/main/java/me/shedaniel/gui/widget/KeyBindButton.java b/src/main/java/me/shedaniel/gui/widget/KeyBindButton.java new file mode 100644 index 000000000..b3d2bde3c --- /dev/null +++ b/src/main/java/me/shedaniel/gui/widget/KeyBindButton.java @@ -0,0 +1,59 @@ +package me.shedaniel.gui.widget; + +import me.shedaniel.ClientListener; +import me.shedaniel.library.KeyBindFunction; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.resource.language.I18n; +import net.minecraft.text.TextFormat; + +import java.awt.event.KeyEvent; +import java.util.function.Consumer; + +public class KeyBindButton extends ButtonWidget { + + private int currentKey; + private Consumer onEditKeyBind; + private boolean editMode; + + public KeyBindButton(int buttonId, int x, int y, int widthIn, int heightIn, int currentKey, Consumer onEditKeyBind) { + super(buttonId, x, y, widthIn, heightIn, ""); + this.currentKey = currentKey; + this.onEditKeyBind = onEditKeyBind; + } + + @Override + public boolean charTyped(char p_charTyped_1_, int p_charTyped_2_) { + if (editMode) { + currentKey = KeyEvent.getExtendedKeyCodeForChar(p_charTyped_1_); + onEditKeyBind.accept(currentKey); + editMode = false; + return true; + } + return false; + } + + @Override + public void draw(int mouseX, int mouseY, float partialTicks) { + this.text = editMode ? I18n.translate("text.rei.listeningkey") : KeyEvent.getKeyText(currentKey); + if (!editMode && ClientListener.keyBinds.stream().map(KeyBindFunction::getKey).filter(integer -> integer == currentKey).count() > 1) + this.text = TextFormat.RED + this.text; + super.draw(mouseX, mouseY, partialTicks); + } + + @Override + public void setHasFocus(boolean boolean_1) { + if (boolean_1 == false) + editMode = boolean_1; + } + + @Override + public boolean hasFocus() { + return true; + } + + @Override + public void onPressed(double double_1, double double_2) { + editMode = !editMode; + } + +} diff --git a/src/main/java/me/shedaniel/library/KeyBindFunction.java b/src/main/java/me/shedaniel/library/KeyBindFunction.java new file mode 100644 index 000000000..4243d7433 --- /dev/null +++ b/src/main/java/me/shedaniel/library/KeyBindFunction.java @@ -0,0 +1,22 @@ +package me.shedaniel.library; + +import java.util.function.Function; + +public abstract class KeyBindFunction { + + public KeyBindFunction(int key) { + this.key = key; + } + + private int key; + + public void setKey(int key) { + this.key = key; + } + + public int getKey() { + return key; + } + + public abstract boolean apply(int key); +} diff --git a/src/main/java/me/shedaniel/library/KeyBindManager.java b/src/main/java/me/shedaniel/library/KeyBindManager.java deleted file mode 100755 index 5a923ad38..000000000 --- a/src/main/java/me/shedaniel/library/KeyBindManager.java +++ /dev/null @@ -1,31 +0,0 @@ -package me.shedaniel.library; - -import net.minecraft.client.settings.KeyBinding; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -/** - * Created by James on 8/7/2018. - */ -public class KeyBindManager { - - private static Map bindingFunctions = new HashMap<>(); - - public static KeyBinding createKeybinding(String bindingName, int key, String categoryName, Sink function) { - KeyBinding newBinding = new KeyBinding(bindingName, key, categoryName); - bindingFunctions.put(newBinding, function); - return newBinding; - } - - public static boolean processGuiKeybinds(int typedChar) { - Optional binding = bindingFunctions.keySet().stream().filter(f -> f.getDefaultKeyCode().getKeyCode() == typedChar).findFirst(); - if (binding.isPresent()) { - bindingFunctions.get(binding.get()).Sink(); - return true; - } - return false; - } - -} -- cgit