Swap to GTNHLib config (#5)

* Swap to GTNHLib config

* Spotless apply for branch newConfig for #5 (#6)

Co-authored-by: GitHub GTNH Actions <>

* Update dependencies.gradle

* add required dep

* Fix old config detection

* Remove unneeded lines in mcmod.info

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
CaedisGitHub GTNH Actionsgithub-actions[bot]
authored and GitHub committed 2024-01-14 10:23:08 +01:00
1 parent a766732334
commit b5afd44ec3
9 files changed
+151 -117

No files matched your search

+3 -1
View File
@@ -1 +1,3 @@
dependencies {} dependencies {
api("com.github.GTNewHorizons:GTNHLib:0.2.0:dev")
}
+3 -1
View File
@@ -1 +1,3 @@
repositories {} repositories {
mavenLocal()
}
+40 -49
View File
@@ -1,69 +1,60 @@
package yalter.mousetweaks; package yalter.mousetweaks;
import java.io.*; import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import yalter.mousetweaks.config.MTConfig;
public class Config { public class Config {
private String fileName; private File file;
private Properties properties = new Properties();
public Config(String fileName) { public Config(File fileName) {
this.fileName = fileName; this.file = fileName;
} }
public boolean readConfig() { public boolean hasOldConfig() {
properties.clear();
try { try {
FileReader configReader = new FileReader(fileName); FileReader reader = new FileReader(this.file);
properties.load(configReader); Properties tempProps = new Properties();
configReader.close(); tempProps.load(reader);
return !tempProps.containsKey("general");
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;
} catch (IOException e) { } catch (IOException e) {
Constants.LOGGER.error("Failed to write the config file: " + fileName, e); return true; // assume yes if it failed for whatever reason
} }
return false;
} }
public String getOrCreateProperty(String name, String defaultValue) { public void importOldConfig() {
if (properties.containsKey(name)) return properties.getProperty(name); try {
else properties.setProperty(name, defaultValue); 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) { if (tempProps.containsKey("LMBTweakWithItem"))
return Integer.parseInt(getOrCreateProperty(name, String.valueOf(defaultValue))); MTConfig.LMBTweakWithItem = Objects.equals(tempProps.get("LMBTweakWithItem").toString(), "1");
}
public void setProperty(String name, String value) { if (tempProps.containsKey("LMBTweakWithoutItem"))
properties.setProperty(name, value); MTConfig.LMBTweakWithoutItem = Objects.equals(tempProps.get("LMBTweakWithoutItem").toString(), "1");
}
public void setIntProperty(String name, int value) { if (tempProps.containsKey("WheelTweak"))
setProperty(name, String.valueOf(value)); 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) {}
} }
} }
+13 -60
View File
@@ -1,6 +1,5 @@
package yalter.mousetweaks; package yalter.mousetweaks;
import java.io.File;
import java.util.List; import java.util.List;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@@ -11,19 +10,12 @@ import net.minecraft.item.ItemStack;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse; import org.lwjgl.input.Mouse;
import yalter.mousetweaks.config.MTConfig;
public class Main extends DeobfuscationLayer { public class Main extends DeobfuscationLayer {
public static boolean DisableRMBTweak = false; 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 GuiScreen oldGuiScreen = null;
private static Object container = null; private static Object container = null;
private static Slot oldSelectedSlot = null; private static Slot oldSelectedSlot = null;
@@ -35,46 +27,13 @@ public class Main extends DeobfuscationLayer {
private static boolean disableWheelForThisContainer = false; private static boolean disableWheelForThisContainer = false;
private static int guiContainerID = 0; private static int guiContainerID = 0;
private static boolean readConfig = false;
public static void initialise() { public static void initialise() {
mc = Minecraft.getMinecraft(); mc = Minecraft.getMinecraft();
mainConfig = new Config(mc.mcDataDir + File.separator + "config" + File.separator + "MouseTweaks.cfg");
readConfigFile();
ModCompatibility.initialize(); ModCompatibility.initialize();
Constants.LOGGER.info("Mouse Tweaks has been initialised."); 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() { public static void onUpdateInGame() {
GuiScreen currentScreen = getCurrentScreen(); GuiScreen currentScreen = getCurrentScreen();
if (currentScreen == null) { if (currentScreen == null) {
@@ -88,15 +47,9 @@ public class Main extends DeobfuscationLayer {
shouldClick = true; shouldClick = true;
disableForThisContainer = false; disableForThisContainer = false;
disableWheelForThisContainer = false; disableWheelForThisContainer = false;
readConfig = true;
guiContainerID = Constants.NOTASSIGNED; guiContainerID = Constants.NOTASSIGNED;
} else { } else {
if (readConfig) {
readConfig = false;
readConfigFile();
}
if (guiContainerID == Constants.NOTASSIGNED) { if (guiContainerID == Constants.NOTASSIGNED) {
guiContainerID = getGuiContainerID(currentScreen); guiContainerID = getGuiContainerID(currentScreen);
} }
@@ -133,9 +86,9 @@ public class Main extends DeobfuscationLayer {
if (guiContainerID == Constants.NOTGUICONTAINER) return; if (guiContainerID == Constants.NOTGUICONTAINER) return;
if ((Main.DisableRMBTweak || (Main.RMBTweak == 0)) && (Main.LMBTweakWithoutItem == 0) if ((Main.DisableRMBTweak || (!MTConfig.RMBTweak)) && (!MTConfig.LMBTweakWithoutItem)
&& (Main.LMBTweakWithItem == 0) && (!MTConfig.LMBTweakWithItem)
&& (Main.WheelTweak == 0)) && (!MTConfig.WheelTweak))
return; return;
if (disableForThisContainer) return; if (disableForThisContainer) return;
@@ -147,7 +100,7 @@ public class Main extends DeobfuscationLayer {
if (slotCount == 0) // If there are no slots, then there is nothing to do. if (slotCount == 0) // If there are no slots, then there is nothing to do.
return; return;
int wheel = ((Main.WheelTweak == 1) && !disableWheelForThisContainer) ? Mouse.getDWheel() : 0; int wheel = ((MTConfig.WheelTweak) && !disableWheelForThisContainer) ? Mouse.getDWheel() : 0;
if (!Mouse.isButtonDown(1)) { if (!Mouse.isButtonDown(1)) {
firstSlotClicked = false; firstSlotClicked = false;
@@ -202,7 +155,7 @@ public class Main extends DeobfuscationLayer {
boolean shiftIsDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT); boolean shiftIsDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT);
if (Mouse.isButtonDown(1)) { // Right mouse button if (Mouse.isButtonDown(1)) { // Right mouse button
if ((Main.RMBTweak == 1) && !Main.DisableRMBTweak) { if ((MTConfig.RMBTweak) && !Main.DisableRMBTweak) {
if ((stackOnMouse != null) && areStacksCompatible(stackOnMouse, targetStack) if ((stackOnMouse != null) && areStacksCompatible(stackOnMouse, targetStack)
&& !isCraftingOutputSlot(currentScreen, selectedSlot)) { && !isCraftingOutputSlot(currentScreen, selectedSlot)) {
@@ -220,7 +173,7 @@ public class Main extends DeobfuscationLayer {
} }
} else if (Mouse.isButtonDown(0)) { // Left mouse button } else if (Mouse.isButtonDown(0)) { // Left mouse button
if (stackOnMouse != null) { if (stackOnMouse != null) {
if (Main.LMBTweakWithItem == 1) { if (MTConfig.LMBTweakWithItem) {
if ((targetStack != null) && areStacksCompatible(stackOnMouse, targetStack)) { if ((targetStack != null) && areStacksCompatible(stackOnMouse, targetStack)) {
if (shiftIsDown) { // If shift is down, we just shift-click the slot and the item gets moved if (shiftIsDown) { // If shift is down, we just shift-click the slot and the item gets moved
@@ -242,7 +195,7 @@ public class Main extends DeobfuscationLayer {
} }
} }
} }
} else if (Main.LMBTweakWithoutItem == 1) { } else if (MTConfig.LMBTweakWithoutItem) {
if (targetStack != null) { if (targetStack != null) {
if (shiftIsDown) { if (shiftIsDown) {
clickSlot(currentScreen, selectedSlot, 0, true); clickSlot(currentScreen, selectedSlot, 0, true);
@@ -256,7 +209,7 @@ public class Main extends DeobfuscationLayer {
if ((wheel != 0) && (selectedSlot != null)) { if ((wheel != 0) && (selectedSlot != null)) {
int numItemsToMove; int numItemsToMove;
if (!ModCompatibility.isLwjgl3Loaded() && ScrollItemScaling == 0) { if (!ModCompatibility.isLwjgl3Loaded() && MTConfig.ScrollItemScaling == 0) {
numItemsToMove = Math.abs(wheel) / 120; numItemsToMove = Math.abs(wheel) / 120;
} else { } else {
numItemsToMove = 1; numItemsToMove = 1;
@@ -277,11 +230,11 @@ public class Main extends DeobfuscationLayer {
Slot applicableSlot = null; Slot applicableSlot = null;
boolean pushItems = (wheel < 0); boolean pushItems = (wheel < 0);
if ((WheelScrollDirection == 2 || WheelScrollDirection == 3) if ((MTConfig.WheelScrollDirection == 2 || MTConfig.WheelScrollDirection == 3)
&& otherInventoryIsAbove(selectedSlot, getSlots(asContainer(container)))) { && otherInventoryIsAbove(selectedSlot, getSlots(asContainer(container)))) {
pushItems = !pushItems; pushItems = !pushItems;
} }
if (WheelScrollDirection == 1 || WheelScrollDirection == 3) { if (MTConfig.WheelScrollDirection == 1 || MTConfig.WheelScrollDirection == 3) {
pushItems = !pushItems; pushItems = !pushItems;
} }
@@ -292,7 +245,7 @@ public class Main extends DeobfuscationLayer {
countUntil = slotCount; countUntil = slotCount;
} }
if (pushItems || (Main.WheelSearchOrder == 0)) { if (pushItems || (MTConfig.WheelSearchOrder == 0)) {
for (int i = slotCounter; i < countUntil; i++) { for (int i = slotCounter; i < countUntil; i++) {
Slot sl = getSlotWithID(currentScreen, i); Slot sl = getSlotWithID(currentScreen, i);
ItemStack stackSl = getSlotStack(sl); ItemStack stackSl = getSlotStack(sl);
@@ -0,0 +1,38 @@
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;
}
@@ -0,0 +1,15 @@
package yalter.mousetweaks.config;
import net.minecraft.client.gui.GuiScreen;
import com.gtnewhorizon.gtnhlib.config.ConfigException;
import com.gtnewhorizon.gtnhlib.config.SimpleGuiConfig;
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);
}
}
@@ -0,0 +1,13 @@
package yalter.mousetweaks.config;
import net.minecraft.client.gui.GuiScreen;
import com.gtnewhorizon.gtnhlib.config.SimpleGuiFactory;
public class MTGuiConfigFactory implements SimpleGuiFactory {
@Override
public Class<? extends GuiScreen> mainConfigGuiClass() {
return MTGuiConfig.class;
}
}
@@ -1,24 +1,48 @@
package yalter.mousetweaks.loaders; 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.FMLCommonHandler;
import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent; 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.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent; import cpw.mods.fml.common.gameevent.TickEvent;
import yalter.mousetweaks.Config;
import yalter.mousetweaks.Constants; import yalter.mousetweaks.Constants;
import yalter.mousetweaks.Main; import yalter.mousetweaks.Main;
import yalter.mousetweaks.config.MTConfig;
@Mod( @Mod(
modid = Constants.MOD_ID, modid = Constants.MOD_ID,
name = Constants.MOD_NAME, name = Constants.MOD_NAME,
version = Constants.MOD_VERSION, version = Constants.MOD_VERSION,
guiFactory = "yalter.mousetweaks.config.MTGuiConfigFactory",
dependencies = "required-after:gtnhlib@[0.2.0,);",
acceptedMinecraftVersions = "[1.7.10]", acceptedMinecraftVersions = "[1.7.10]",
acceptableRemoteVersions = "*") acceptableRemoteVersions = "*")
public class MouseTweaksForge { 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 @Mod.EventHandler
public void init(FMLInitializationEvent event) { public void init(FMLInitializationEvent event) {
if (FMLCommonHandler.instance().getSide().isClient()) { if (event.getSide().isClient()) {
Main.initialise(); Main.initialise();
FMLCommonHandler.instance().bus().register(this); FMLCommonHandler.instance().bus().register(this);
} }
+1 -5
View File
@@ -12,10 +12,6 @@
"credits": "Various open-source mods from where I learned.", "credits": "Various open-source mods from where I learned.",
"logoFile": "mousetweaks_logo.png", "logoFile": "mousetweaks_logo.png",
"screenshots": [], "screenshots": [],
"parent": "", "parent": ""
"requiredMods": [],
"dependencies": [],
"dependants": [],
"useDependencyInformation": true
}] }]
} }