From 4f4c849c9e9580f22a96b7db4177cf4632c09d53 Mon Sep 17 00:00:00 2001 From: Caedis Date: Fri, 12 Jan 2024 05:13:52 -0600 Subject: [PATCH] Swap to GTNHLib config --- dependencies.gradle | 4 +- repositories.gradle | 4 +- src/main/java/yalter/mousetweaks/Config.java | 90 +++++++++---------- src/main/java/yalter/mousetweaks/Main.java | 72 +++------------ .../yalter/mousetweaks/config/MTConfig.java | 42 +++++++++ .../mousetweaks/config/MTGuiConfig.java | 12 +++ .../config/MTGuiConfigFactory.java | 11 +++ .../mousetweaks/loaders/MouseTweaksForge.java | 24 ++++- 8 files changed, 147 insertions(+), 112 deletions(-) create mode 100644 src/main/java/yalter/mousetweaks/config/MTConfig.java create mode 100644 src/main/java/yalter/mousetweaks/config/MTGuiConfig.java create mode 100644 src/main/java/yalter/mousetweaks/config/MTGuiConfigFactory.java diff --git a/dependencies.gradle b/dependencies.gradle index c773c51..095faf2 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1 +1,3 @@ -dependencies {} +dependencies { + api("com.github.GTNewHorizons:GTNHLib:0.2.0-pre:dev") +} diff --git a/repositories.gradle b/repositories.gradle index 0ac475d..72461d5 100644 --- a/repositories.gradle +++ b/repositories.gradle @@ -1 +1,3 @@ -repositories {} +repositories { + mavenLocal() +} diff --git a/src/main/java/yalter/mousetweaks/Config.java b/src/main/java/yalter/mousetweaks/Config.java index 844ef90..7efc423 100644 --- a/src/main/java/yalter/mousetweaks/Config.java +++ b/src/main/java/yalter/mousetweaks/Config.java @@ -1,69 +1,61 @@ package yalter.mousetweaks; -import java.io.*; +import yalter.mousetweaks.config.MTConfig; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Objects; import java.util.Properties; public class Config { - private String fileName; - private Properties properties = new Properties(); + private File file; - public Config(String fileName) { - this.fileName = fileName; + public Config(File fileName) { + this.file = fileName; } - public boolean readConfig() { - properties.clear(); - + public boolean hasOldConfig() { try { - FileReader configReader = new FileReader(fileName); - properties.load(configReader); - configReader.close(); - - if (properties.size() == 0) return false; - - return true; - } catch (FileNotFoundException ignored) {} catch (IOException e) { - Constants.LOGGER.error("Failed to read the config file: " + fileName, e); - } - - return false; - } - - public boolean saveConfig() { - try { - File config = new File(fileName); - File parentDir = config.getParentFile(); - if (!parentDir.exists()) parentDir.mkdirs(); - - FileWriter configWriter = new FileWriter(config); - properties.store(configWriter, null); - configWriter.close(); - - return true; + FileReader reader = new FileReader(this.file); + Properties tempProps = new Properties(); + tempProps.load(reader); + return tempProps.size() > 0 && !tempProps.containsKey("general"); } catch (IOException e) { - Constants.LOGGER.error("Failed to write the config file: " + fileName, e); + return false; } - - return false; } - public String getOrCreateProperty(String name, String defaultValue) { - if (properties.containsKey(name)) return properties.getProperty(name); - else properties.setProperty(name, defaultValue); + public void importOldConfig(){ + try { + FileReader reader = new FileReader(this.file); + Properties tempProps = new Properties(); + tempProps.load(reader); - return defaultValue; - } + if (tempProps.containsKey("RMBTweak")) + MTConfig.RMBTweak = Objects.equals(tempProps.get("RMBTweak").toString(), "1"); - public int getOrCreateIntProperty(String name, int defaultValue) { - return Integer.parseInt(getOrCreateProperty(name, String.valueOf(defaultValue))); - } + if (tempProps.containsKey("LMBTweakWithItem")) + MTConfig.LMBTweakWithItem = Objects.equals(tempProps.get("LMBTweakWithItem").toString(), "1"); - public void setProperty(String name, String value) { - properties.setProperty(name, value); - } + if (tempProps.containsKey("LMBTweakWithoutItem")) + MTConfig.LMBTweakWithoutItem = Objects.equals(tempProps.get("LMBTweakWithoutItem").toString(), "1"); - public void setIntProperty(String name, int value) { - setProperty(name, String.valueOf(value)); + if (tempProps.containsKey("WheelTweak")) + MTConfig.WheelTweak = Objects.equals(tempProps.get("WheelTweak").toString(), "1"); + + if (tempProps.containsKey("WheelSearchOrder")) + MTConfig.WheelSearchOrder = Integer.parseInt(tempProps.get("WheelSearchOrder").toString()); + + if (tempProps.containsKey("WheelScrollDirection")) + MTConfig.WheelScrollDirection = Integer.parseInt(tempProps.get("WheelScrollDirection").toString()); + + if (tempProps.containsKey("ScrollItemScaling")) + MTConfig.ScrollItemScaling = Integer.parseInt(tempProps.get("ScrollItemScaling").toString()); + + + file.renameTo(new File(file.getPath() + ".bak")); + } catch (IOException ignored) {} } } diff --git a/src/main/java/yalter/mousetweaks/Main.java b/src/main/java/yalter/mousetweaks/Main.java index f7dee94..1022556 100644 --- a/src/main/java/yalter/mousetweaks/Main.java +++ b/src/main/java/yalter/mousetweaks/Main.java @@ -1,6 +1,5 @@ package yalter.mousetweaks; -import java.io.File; import java.util.List; import net.minecraft.client.Minecraft; @@ -10,20 +9,12 @@ import net.minecraft.item.ItemStack; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; +import yalter.mousetweaks.config.MTConfig; public class Main extends DeobfuscationLayer { public static boolean DisableRMBTweak = false; - public static int RMBTweak = 0; - public static int LMBTweakWithItem = 0; - public static int LMBTweakWithoutItem = 0; - public static int WheelTweak = 0; - public static int WheelSearchOrder = 1; - public static int WheelScrollDirection = 0; - public static int ScrollItemScaling = 0; - - public static Config mainConfig; private static GuiScreen oldGuiScreen = null; private static Object container = null; private static Slot oldSelectedSlot = null; @@ -35,46 +26,13 @@ public class Main extends DeobfuscationLayer { private static boolean disableWheelForThisContainer = false; private static int guiContainerID = 0; - private static boolean readConfig = false; - public static void initialise() { mc = Minecraft.getMinecraft(); - mainConfig = new Config(mc.mcDataDir + File.separator + "config" + File.separator + "MouseTweaks.cfg"); - readConfigFile(); - ModCompatibility.initialize(); Constants.LOGGER.info("Mouse Tweaks has been initialised."); } - public static void readConfigFile() { - boolean loadedConfig = mainConfig.readConfig(); - - RMBTweak = mainConfig.getOrCreateIntProperty("RMBTweak", 1); - LMBTweakWithItem = mainConfig.getOrCreateIntProperty("LMBTweakWithItem", 1); - LMBTweakWithoutItem = mainConfig.getOrCreateIntProperty("LMBTweakWithoutItem", 1); - WheelTweak = mainConfig.getOrCreateIntProperty("WheelTweak", 1); - WheelSearchOrder = mainConfig.getOrCreateIntProperty("WheelSearchOrder", 1); - WheelScrollDirection = mainConfig.getOrCreateIntProperty("WheelScrollDirection", 0); - ScrollItemScaling = mainConfig.getOrCreateIntProperty("ScrollItemScaling", 0); - - boolean savedConfig = saveConfigFile(); - - if (savedConfig && !loadedConfig) Constants.LOGGER.info("Mouse Tweaks config file was created."); - } - - public static boolean saveConfigFile() { - mainConfig.setIntProperty("RMBTweak", RMBTweak); - mainConfig.setIntProperty("LMBTweakWithItem", LMBTweakWithItem); - mainConfig.setIntProperty("LMBTweakWithoutItem", LMBTweakWithoutItem); - mainConfig.setIntProperty("WheelTweak", WheelTweak); - mainConfig.setIntProperty("WheelSearchOrder", WheelSearchOrder); - mainConfig.setIntProperty("WheelScrollDirection", WheelScrollDirection); - mainConfig.setIntProperty("ScrollItemScaling", ScrollItemScaling); - - return mainConfig.saveConfig(); - } - public static void onUpdateInGame() { GuiScreen currentScreen = getCurrentScreen(); if (currentScreen == null) { @@ -88,15 +46,9 @@ public class Main extends DeobfuscationLayer { shouldClick = true; disableForThisContainer = false; disableWheelForThisContainer = false; - readConfig = true; guiContainerID = Constants.NOTASSIGNED; } else { - if (readConfig) { - readConfig = false; - readConfigFile(); - } - if (guiContainerID == Constants.NOTASSIGNED) { guiContainerID = getGuiContainerID(currentScreen); } @@ -133,9 +85,9 @@ public class Main extends DeobfuscationLayer { if (guiContainerID == Constants.NOTGUICONTAINER) return; - if ((Main.DisableRMBTweak || (Main.RMBTweak == 0)) && (Main.LMBTweakWithoutItem == 0) - && (Main.LMBTweakWithItem == 0) - && (Main.WheelTweak == 0)) + if ((Main.DisableRMBTweak || (!MTConfig.RMBTweak)) && (!MTConfig.LMBTweakWithoutItem) + && (!MTConfig.LMBTweakWithItem) + && (!MTConfig.WheelTweak)) return; if (disableForThisContainer) return; @@ -147,7 +99,7 @@ public class Main extends DeobfuscationLayer { if (slotCount == 0) // If there are no slots, then there is nothing to do. return; - int wheel = ((Main.WheelTweak == 1) && !disableWheelForThisContainer) ? Mouse.getDWheel() : 0; + int wheel = ((MTConfig.WheelTweak) && !disableWheelForThisContainer) ? Mouse.getDWheel() : 0; if (!Mouse.isButtonDown(1)) { firstSlotClicked = false; @@ -202,7 +154,7 @@ public class Main extends DeobfuscationLayer { boolean shiftIsDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT); if (Mouse.isButtonDown(1)) { // Right mouse button - if ((Main.RMBTweak == 1) && !Main.DisableRMBTweak) { + if ((MTConfig.RMBTweak) && !Main.DisableRMBTweak) { if ((stackOnMouse != null) && areStacksCompatible(stackOnMouse, targetStack) && !isCraftingOutputSlot(currentScreen, selectedSlot)) { @@ -220,7 +172,7 @@ public class Main extends DeobfuscationLayer { } } else if (Mouse.isButtonDown(0)) { // Left mouse button if (stackOnMouse != null) { - if (Main.LMBTweakWithItem == 1) { + if (MTConfig.LMBTweakWithItem) { if ((targetStack != null) && areStacksCompatible(stackOnMouse, targetStack)) { if (shiftIsDown) { // If shift is down, we just shift-click the slot and the item gets moved @@ -242,7 +194,7 @@ public class Main extends DeobfuscationLayer { } } } - } else if (Main.LMBTweakWithoutItem == 1) { + } else if (MTConfig.LMBTweakWithoutItem) { if (targetStack != null) { if (shiftIsDown) { clickSlot(currentScreen, selectedSlot, 0, true); @@ -256,7 +208,7 @@ public class Main extends DeobfuscationLayer { if ((wheel != 0) && (selectedSlot != null)) { int numItemsToMove; - if (!ModCompatibility.isLwjgl3Loaded() && ScrollItemScaling == 0) { + if (!ModCompatibility.isLwjgl3Loaded() && MTConfig.ScrollItemScaling == 0) { numItemsToMove = Math.abs(wheel) / 120; } else { numItemsToMove = 1; @@ -277,11 +229,11 @@ public class Main extends DeobfuscationLayer { Slot applicableSlot = null; boolean pushItems = (wheel < 0); - if ((WheelScrollDirection == 2 || WheelScrollDirection == 3) + if ((MTConfig.WheelScrollDirection == 2 || MTConfig.WheelScrollDirection == 3) && otherInventoryIsAbove(selectedSlot, getSlots(asContainer(container)))) { pushItems = !pushItems; } - if (WheelScrollDirection == 1 || WheelScrollDirection == 3) { + if (MTConfig.WheelScrollDirection == 1 || MTConfig.WheelScrollDirection == 3) { pushItems = !pushItems; } @@ -292,7 +244,7 @@ public class Main extends DeobfuscationLayer { countUntil = slotCount; } - if (pushItems || (Main.WheelSearchOrder == 0)) { + if (pushItems || (MTConfig.WheelSearchOrder == 0)) { for (int i = slotCounter; i < countUntil; i++) { Slot sl = getSlotWithID(currentScreen, i); ItemStack stackSl = getSlotStack(sl); diff --git a/src/main/java/yalter/mousetweaks/config/MTConfig.java b/src/main/java/yalter/mousetweaks/config/MTConfig.java new file mode 100644 index 0000000..cde0806 --- /dev/null +++ b/src/main/java/yalter/mousetweaks/config/MTConfig.java @@ -0,0 +1,42 @@ +package yalter.mousetweaks.config; + +import com.gtnewhorizon.gtnhlib.config.Config; +import yalter.mousetweaks.Constants; + +@Config(modid = Constants.MOD_ID) +public class MTConfig { + + @Config.Comment("Very similar to the standard RMB dragging mechanic, with one difference: if you drag over a slot multiple times, an item will be put there multiple times. Replaces the standard mechanic if enabled.") + public static boolean RMBTweak = false; + + @Config.Comment("Lets you quickly pick up or move items of the same type") + public static boolean LMBTweakWithItem = true; + + @Config.Comment("Quickly move items into another inventory") + public static boolean LMBTweakWithoutItem = true; + + @Config.Comment("Scroll to quickly move items between inventories") + public static boolean WheelTweak = true; + + @Config.Comment("Wheel Inventory Slot Search Order\n" + + "0 - First to Last\n" + + "1 - Last To First") + @Config.RangeInt(min = 0, max = 1) + public static int WheelSearchOrder = 1; + + @Config.Comment("Wheel Scroll Direction\n" + + "0 - Down to Push, Up to Pull\n" + + "1 - Up to Push, Down to Pull\n" + + "2 - Inventory Position Aware\n" + + "3 - Inventory Position Aware, Inverted\n" + ) + @Config.RangeInt(min = 0, max = 3) + public static int WheelScrollDirection = 0; + + @Config.Comment("Scroll Scaling\n" + + "0 - Multiple Wheel Clicks Move Multiple Items\n" + + "1 - Always Move One Item (macOS Compatibility)") + @Config.RangeInt(min = 0, max = 1) + public static int ScrollItemScaling = 0; + +} diff --git a/src/main/java/yalter/mousetweaks/config/MTGuiConfig.java b/src/main/java/yalter/mousetweaks/config/MTGuiConfig.java new file mode 100644 index 0000000..c1cf149 --- /dev/null +++ b/src/main/java/yalter/mousetweaks/config/MTGuiConfig.java @@ -0,0 +1,12 @@ +package yalter.mousetweaks.config; + +import com.gtnewhorizon.gtnhlib.config.ConfigException; +import com.gtnewhorizon.gtnhlib.config.SimpleGuiConfig; +import net.minecraft.client.gui.GuiScreen; +import yalter.mousetweaks.Constants; + +public class MTGuiConfig extends SimpleGuiConfig { + public MTGuiConfig(GuiScreen parent) throws ConfigException { + super(parent, MTConfig.class, Constants.MOD_ID, Constants.MOD_NAME); + } +} diff --git a/src/main/java/yalter/mousetweaks/config/MTGuiConfigFactory.java b/src/main/java/yalter/mousetweaks/config/MTGuiConfigFactory.java new file mode 100644 index 0000000..1c4749b --- /dev/null +++ b/src/main/java/yalter/mousetweaks/config/MTGuiConfigFactory.java @@ -0,0 +1,11 @@ +package yalter.mousetweaks.config; + +import com.gtnewhorizon.gtnhlib.config.SimpleGuiFactory; +import net.minecraft.client.gui.GuiScreen; + +public class MTGuiConfigFactory implements SimpleGuiFactory { + @Override + public Class mainConfigGuiClass() { + return MTGuiConfig.class; + } +} diff --git a/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java b/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java index fb7ff4c..8cdd605 100644 --- a/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java +++ b/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java @@ -1,24 +1,46 @@ package yalter.mousetweaks.loaders; +import com.gtnewhorizon.gtnhlib.config.ConfigException; +import com.gtnewhorizon.gtnhlib.config.ConfigurationManager; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; +import yalter.mousetweaks.Config; import yalter.mousetweaks.Constants; import yalter.mousetweaks.Main; +import yalter.mousetweaks.config.MTConfig; @Mod( modid = Constants.MOD_ID, name = Constants.MOD_NAME, version = Constants.MOD_VERSION, + guiFactory = "yalter.mousetweaks.config.MTGuiConfigFactory", acceptedMinecraftVersions = "[1.7.10]", acceptableRemoteVersions = "*") public class MouseTweaksForge { + @Mod.EventHandler + public void preinit(FMLPreInitializationEvent event) { + if (event.getSide().isClient()) { + try { + Config oldConfig = new Config(event.getSuggestedConfigurationFile()); + if (oldConfig.hasOldConfig()){ + oldConfig.importOldConfig(); + } + ConfigurationManager.registerConfig(MTConfig.class); + ConfigurationManager.registerBus(); + } catch (ConfigException e) { + throw new RuntimeException(e); + } + } + } + @Mod.EventHandler public void init(FMLInitializationEvent event) { - if (FMLCommonHandler.instance().getSide().isClient()) { + if (event.getSide().isClient()) { Main.initialise(); FMLCommonHandler.instance().bus().register(this); }