diff options
| author | Unknown <shekwancheung0528@gmail.com> | 2019-01-01 13:24:38 +0800 |
|---|---|---|
| committer | Unknown <shekwancheung0528@gmail.com> | 2019-01-01 13:24:38 +0800 |
| commit | 2d39c9e131136ceba87284a96d4b42afa0e297f7 (patch) | |
| tree | b3546175258f0ab041b096147db4564f57450aed /src/main/java | |
| parent | 50da7926d439166a1e68631e31523c604c337517 (diff) | |
| download | RoughlyEnoughItems-2d39c9e131136ceba87284a96d4b42afa0e297f7.tar.gz RoughlyEnoughItems-2d39c9e131136ceba87284a96d4b42afa0e297f7.tar.bz2 RoughlyEnoughItems-2d39c9e131136ceba87284a96d4b42afa0e297f7.zip | |
Custom Keybinds
Diffstat (limited to 'src/main/java')
| -rwxr-xr-x | src/main/java/me/shedaniel/ClientListener.java | 52 | ||||
| -rwxr-xr-x | src/main/java/me/shedaniel/Core.java | 16 | ||||
| -rw-r--r-- | src/main/java/me/shedaniel/config/REIConfig.java | 2 | ||||
| -rw-r--r-- | src/main/java/me/shedaniel/gui/ConfigGui.java | 75 | ||||
| -rwxr-xr-x | src/main/java/me/shedaniel/gui/GuiItemList.java | 8 | ||||
| -rwxr-xr-x | src/main/java/me/shedaniel/gui/REIRenderHelper.java | 4 | ||||
| -rwxr-xr-x | src/main/java/me/shedaniel/gui/RecipeGui.java | 1 | ||||
| -rw-r--r-- | src/main/java/me/shedaniel/gui/widget/KeyBindButton.java | 60 | ||||
| -rw-r--r-- | src/main/java/me/shedaniel/library/KeyBindFunction.java | 22 | ||||
| -rwxr-xr-x | src/main/java/me/shedaniel/library/KeyBindManager.java | 32 |
10 files changed, 221 insertions, 51 deletions
diff --git a/src/main/java/me/shedaniel/ClientListener.java b/src/main/java/me/shedaniel/ClientListener.java index 00be6f7ae..8ec8f27cc 100755 --- a/src/main/java/me/shedaniel/ClientListener.java +++ b/src/main/java/me/shedaniel/ClientListener.java @@ -3,10 +3,9 @@ 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.minecraft.client.settings.KeyBinding; import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.init.Items; @@ -15,28 +14,55 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; import net.minecraft.util.registry.IRegistry; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class ClientListener implements DoneLoading, RecipeLoadListener { - public static KeyBinding recipeKeybind; - public static KeyBinding hideKeybind; - public static KeyBinding useKeybind; + + public static KeyBindFunction recipeKeybind; + public static KeyBindFunction hideKeybind; + public static KeyBindFunction useKeybind; + public static List<KeyBindFunction> keyBinds = new ArrayList<>(); private List<IREIPlugin> plugins; public static List<ItemStack> 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<>(); stackList = new ArrayList<>(); - recipeKeybind = KeyBindManager.createKeybinding("key.rei.recipe", Core.config.recipeKeyBind, REIRenderHelper::recipeKeybind); - hideKeybind = KeyBindManager.createKeybinding("key.rei.hide", Core.config.hideKeyBind, REIRenderHelper::hideKeybind); - useKeybind = KeyBindManager.createKeybinding("key.rei.use", Core.config.usageKeyBind, REIRenderHelper::useKeybind); - + 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)); buildItemList(); } diff --git a/src/main/java/me/shedaniel/Core.java b/src/main/java/me/shedaniel/Core.java index 495274d03..695f032dc 100755 --- a/src/main/java/me/shedaniel/Core.java +++ b/src/main/java/me/shedaniel/Core.java @@ -55,8 +55,18 @@ public class Core implements PacketAdder, InitializationListener { public static void loadConfig() throws IOException { if (!configFile.exists()) loadDefaultConfig(); - InputStream in = Files.newInputStream(configFile.toPath()); - config = REIConfig.GSON.fromJson(new InputStreamReader(in), REIConfig.class); + boolean failed = false; + try { + InputStream in = Files.newInputStream(configFile.toPath()); + config = REIConfig.GSON.fromJson(new InputStreamReader(in), REIConfig.class); + } catch (Exception e){ + failed = true; + } + if (failed || config == null) { + System.out.println("[REI] Failed to load config! Overwriting with default config."); + config = new REIConfig(); + } + saveConfig(); } public static void loadDefaultConfig() throws IOException { @@ -68,7 +78,7 @@ public class Core implements PacketAdder, InitializationListener { configFile.getParentFile().mkdirs(); if (configFile.exists()) configFile.delete(); - try (Writer writer = new FileWriter(configFile)) { + try (PrintWriter writer = new PrintWriter(configFile)) { REIConfig.GSON.toJson(config, writer); writer.close(); } diff --git a/src/main/java/me/shedaniel/config/REIConfig.java b/src/main/java/me/shedaniel/config/REIConfig.java index 791c9f1a9..4acaa102a 100644 --- a/src/main/java/me/shedaniel/config/REIConfig.java +++ b/src/main/java/me/shedaniel/config/REIConfig.java @@ -7,7 +7,7 @@ import java.awt.event.KeyEvent; public class REIConfig { - public static Gson GSON = new GsonBuilder().create(); + public static Gson GSON = new GsonBuilder().setPrettyPrinting().create(); public int recipeKeyBind = KeyEvent.VK_R; public int usageKeyBind = KeyEvent.VK_U; 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..f075ca903 --- /dev/null +++ b/src/main/java/me/shedaniel/gui/ConfigGui.java @@ -0,0 +1,75 @@ +package me.shedaniel.gui; + +import me.shedaniel.ClientListener; +import me.shedaniel.Core; +import me.shedaniel.gui.widget.KeyBindButton; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.resources.I18n; +import net.minecraft.client.util.InputMappings; + +import java.io.IOException; + +public class ConfigGui extends GuiScreen { + + private GuiScreen parent; + + public ConfigGui(GuiScreen parent) { + this.parent = parent; + } + + @Override + protected void initGui() { + 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 render(int mouseX, int mouseY, float partialTicks) { + drawDefaultBackground(); + super.render(mouseX, mouseY, partialTicks); + String text = I18n.format("key.rei.recipe") + ": "; + drawString(Minecraft.getInstance().fontRenderer, text, parent.width / 2 - 40 - Minecraft.getInstance().fontRenderer.getStringWidth(text), 30 + 6, -1); + text = I18n.format("key.rei.use") + ": "; + drawString(Minecraft.getInstance().fontRenderer, text, parent.width / 2 - 40 - Minecraft.getInstance().fontRenderer.getStringWidth(text), 60 + 6, -1); + text = I18n.format("key.rei.hide") + ": "; + drawString(Minecraft.getInstance().fontRenderer, text, parent.width / 2 - 40 - Minecraft.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.allowCloseWithEscape()) { + this.close(); + if (parent != null) + Minecraft.getInstance().displayGuiScreen(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 f23ab9f85..b3219ee29 100755 --- a/src/main/java/me/shedaniel/gui/GuiItemList.java +++ b/src/main/java/me/shedaniel/gui/GuiItemList.java @@ -10,6 +10,7 @@ import net.minecraft.client.MainWindow; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; @@ -35,6 +36,7 @@ public class GuiItemList extends Drawable { Button buttonLeft; Button buttonRight; Button buttonCheating; + Button buttonConfig; TextBox searchBox; private ArrayList<ItemStack> view; private Control lastHovered; @@ -101,6 +103,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.format("text.rei.config")); + buttonConfig.onClick = i -> { + Minecraft.getInstance().displayGuiScreen(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 74f8c1b02..08f937234 100755 --- a/src/main/java/me/shedaniel/gui/REIRenderHelper.java +++ b/src/main/java/me/shedaniel/gui/REIRenderHelper.java @@ -1,10 +1,10 @@ package me.shedaniel.gui; +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.IMixinGuiContainer; import net.minecraft.client.MainWindow; import net.minecraft.client.Minecraft; @@ -151,7 +151,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 437717d4c..467a1ee86 100755 --- a/src/main/java/me/shedaniel/gui/RecipeGui.java +++ b/src/main/java/me/shedaniel/gui/RecipeGui.java @@ -74,6 +74,7 @@ public class RecipeGui extends GuiContainer { @Override public void render(int mouseX, int mouseY, float partialTicks) { + drawDefaultBackground(); super.render(mouseX, mouseY, partialTicks); int y = (int) ((mainWindow.getScaledHeight() / 2 - this.guiHeight / 2)); drawCenteredString(this.fontRenderer, selectedCategory.getDisplayName(), guiLeft + 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..25cf0f475 --- /dev/null +++ b/src/main/java/me/shedaniel/gui/widget/KeyBindButton.java @@ -0,0 +1,60 @@ +package me.shedaniel.gui.widget; + +import me.shedaniel.ClientListener; +import me.shedaniel.library.KeyBindFunction; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.text.TextFormatting; + +import java.awt.event.KeyEvent; +import java.util.function.Consumer; + +public class KeyBindButton extends GuiButton { + + private int currentKey; + private Consumer<Integer> onEditKeyBind; + private boolean editMode; + + public KeyBindButton(int buttonId, int x, int y, int widthIn, int heightIn, int currentKey, Consumer<Integer> 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 render(int mouseX, int mouseY, float partialTicks) { + this.displayString = editMode ? I18n.format("text.rei.listeningkey") : KeyEvent.getKeyText(currentKey); + if (!editMode && ClientListener.keyBinds.stream().map(KeyBindFunction::getKey).filter(integer -> integer == currentKey).count() > 1) + this.displayString = TextFormatting.RED + this.displayString; + System.out.println(displayString); + super.render(mouseX, mouseY, partialTicks); + } + + @Override + public void focusChanged(boolean focused) { + if (focused == false) + editMode = focused; + } + + @Override + public void onClick(double mouseX, double mouseY) { + editMode = !editMode; + } + + @Override + public boolean canFocus() { + return true; + } + +} 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 de4ea3dbc..000000000 --- a/src/main/java/me/shedaniel/library/KeyBindManager.java +++ /dev/null @@ -1,32 +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<KeyBinding, Sink> bindingFunctions = new HashMap<>(); - - public static KeyBinding createKeybinding(String bindingName, int key, Sink function) { - KeyBinding newBinding; - newBinding = new KeyBinding(bindingName, key, ""); - bindingFunctions.put(newBinding, function); - return newBinding; - } - - public static boolean processGuiKeybinds(int typedChar) { - Optional<KeyBinding> binding = bindingFunctions.keySet().stream().filter(f -> f.getDefault().getKeyCode() == typedChar).findFirst(); - if (binding.isPresent()) { - bindingFunctions.get(binding.get()).Sink(); - return true; - } - return false; - } - -} |
