Explorar el Código

1.2 Custom Keybinds

Unknown hace 6 años
padre
commit
f6b8a2d460

+ 42 - 18
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<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<>();
@@ -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);
-//            }
         }
     }
     

+ 4 - 1
src/main/java/me/shedaniel/Core.java

@@ -45,22 +45,25 @@ public class Core implements PacketAdder, ClientModInitializer {
     private static List<IEvent> 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) {

+ 74 - 0
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_);
+        }
+    }
+}

+ 8 - 0
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<ItemStack> 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();

+ 2 - 2
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;
     }

+ 1 - 0
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);

+ 59 - 0
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<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 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;
+    }
+    
+}

+ 22 - 0
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);
+}

+ 0 - 31
src/main/java/me/shedaniel/library/KeyBindManager.java

@@ -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<KeyBinding, Sink> 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<KeyBinding> binding = bindingFunctions.keySet().stream().filter(f -> f.getDefaultKeyCode().getKeyCode() == typedChar).findFirst();
-        if (binding.isPresent()) {
-            bindingFunctions.get(binding.get()).Sink();
-            return true;
-        }
-        return false;
-    }
-    
-}

+ 9 - 7
src/main/resources/assets/roughlyenoughitems/lang/en_us.json

@@ -1,10 +1,10 @@
 {
-  "key.rei.category":"Roughly Enough Items",
-  "key.rei.recipe":"Show Recipe",
-  "key.rei.hide":"Hide/Show REI",
-  "key.rei.use":"Show Uses",
-  "text.rei.cheat":"Cheat",
-  "text.rei.nocheat":"§4§mCheat",
+  "key.rei.category": "Roughly Enough Items",
+  "key.rei.recipe": "Show Recipe",
+  "key.rei.hide": "Hide/Show REI",
+  "key.rei.use": "Show Uses",
+  "text.rei.cheat": "Cheat",
+  "text.rei.nocheat": "§4§mCheat",
   "text.rei.mod": "§9§o%s",
   "category.rei.crafting": "Crafting",
   "category.rei.smelting": "Smelting",
@@ -14,5 +14,7 @@
   "category.rei.brewing": "Brewing",
   "category.rei.brewing.input": "§eOriginal Potion",
   "category.rei.brewing.reactant": "§eIngredient",
-  "category.rei.brewing.result": "§eResulted Potion"
+  "category.rei.brewing.result": "§eResulted Potion",
+  "text.rei.config": "Config",
+  "text.rei.listeningkey": "Listening Key"
 }

+ 6 - 6
src/main/resources/assets/roughlyenoughitems/lang/fr_fr.json

@@ -1,8 +1,8 @@
 {
-  "key.rei.category":"Roughly Enough Items",
-  "key.rei.recipe":"Afficher la recette",
-  "key.rei.hide":"Masquer/afficher REI",
-  "key.rei.use":"Afficher les utilisations",
-  "text.rei.cheat":"Triche",
-  "text.rei.nocheat":"§4§mTriche"
+  "key.rei.category": "Roughly Enough Items",
+  "key.rei.recipe": "Afficher la recette",
+  "key.rei.hide": "Masquer/afficher REI",
+  "key.rei.use": "Afficher les utilisations",
+  "text.rei.cheat": "Triche",
+  "text.rei.nocheat": "§4§mTriche"
 }

+ 16 - 0
src/main/resources/assets/roughlyenoughitems/lang/zh_tw.json

@@ -0,0 +1,16 @@
+{
+  "key.rei.category": "大致足夠的物品(REI)",
+  "key.rei.recipe": "顯示配方",
+  "key.rei.hide": "隱藏/顯示REI",
+  "key.rei.use": "顯示用途",
+  "text.rei.cheat": "作弊",
+  "text.rei.nocheat": "§4§m作弊",
+  "text.rei.mod": "§9§o%s",
+  "category.rei.crafting": "合成",
+  "category.rei.smelting": "冶煉",
+  "category.rei.smelting.fuel": "§e燃料",
+  "category.rei.brewing": "釀造",
+  "category.rei.brewing.input": "§e輸入藥水",
+  "category.rei.brewing.reactant": "§e材料",
+  "category.rei.brewing.result": "§e輸出藥水"
+}

+ 1 - 2
src/main/resources/fabric.mod.json

@@ -8,8 +8,7 @@
     "Danielshe"
   ],
   "initializers": [
-    "me.shedaniel.Core",
-    "me.shedaniel.ClientListener"
+    "me.shedaniel.Core"
   ],
   "requires": {