diff --git a/Mouse Tweaks.iml b/Mouse Tweaks.iml new file mode 100644 index 0000000..9ab7daa --- /dev/null +++ b/Mouse Tweaks.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/ant/build_mousetweaks.xml b/ant/build_mousetweaks.xml new file mode 100644 index 0000000..e1fb4a9 --- /dev/null +++ b/ant/build_mousetweaks.xml @@ -0,0 +1,317 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ant/buildnumber.txt b/ant/buildnumber.txt new file mode 100644 index 0000000..c166b44 --- /dev/null +++ b/ant/buildnumber.txt @@ -0,0 +1,3 @@ +#Build Number for ANT. Do not edit! +#Thu Jun 05 17:05:06 MSK 2014 +build.number=146 diff --git a/ant/litemod.template.json b/ant/litemod.template.json new file mode 100644 index 0000000..b6b4212 --- /dev/null +++ b/ant/litemod.template.json @@ -0,0 +1,7 @@ +{ + "name": "${ant.project.name}", + "mcversion": "${litemodversion}", + "revision": "${build.number}", + "author": "${author}", + "description": "A mod that enhances the inventory management by adding various additional functions to the usual mouse buttons." +} diff --git a/src/main/resources/mcmod.info b/ant/mcmod.template.info similarity index 88% rename from src/main/resources/mcmod.info rename to ant/mcmod.template.info index 9a632a7..0d314bd 100644 --- a/src/main/resources/mcmod.info +++ b/ant/mcmod.template.info @@ -1,7 +1,7 @@ [ { - "modid": "MouseTweaks", - "name": "Mouse Tweaks", + "modid": "${modid}", + "name": "${ant.project.name}", "description": "A mod that enhances the inventory management by adding various additional functions to the usual mouse buttons.", "version": "${version}", "mcversion": "${mcversion}", @@ -13,4 +13,4 @@ "screenshots": [ ], "dependencies": [ ] } -] \ No newline at end of file +] diff --git a/build.gradle b/build.gradle deleted file mode 100644 index 04c65f7..0000000 --- a/build.gradle +++ /dev/null @@ -1,74 +0,0 @@ -buildscript { - repositories { - mavenCentral() - maven { - name = "forge" - url = "http://files.minecraftforge.net/maven" - } - maven { - name = "sonatype" - url = "https://oss.sonatype.org/content/repositories/snapshots/" - } - } - dependencies { - classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT' - } -} - -apply plugin: 'forge' - -version = "2.4.2" -group = "yalter.mousetweaks" // http://maven.apache.org/guides/mini/guide-naming-conventions.html -archivesBaseName = "MouseTweaks" - -minecraft { - version = "1.7.2-10.12.1.1110" - assetDir = "eclipse/assets" -} - -processResources -{ - // this will ensure that this task is redone when the versions change. - inputs.property "version", project.version - inputs.property "mcversion", project.minecraft.version - - from(sourceSets.main.resources.srcDirs) { - include 'mcmod.info' - - // replace version and mcversion - expand 'version':project.version, 'mcversion':project.minecraft.version - } - - // copy everything else, thats not the mcmod.info - from(sourceSets.main.resources.srcDirs) { - exclude 'mcmod.info' - } - - def propertiesFile = new File(destinationDir, 'version.properties') - def properties = new Properties() - properties['MouseTweaks.version'] = project.version - properties.store(propertiesFile.newWriter(), null) -} - -jar { - exclude 'com/**' - version = "${project.minecraft.version}-${project.version}" -} - -task sourceJar(type: Jar) { - from sourceSets.main.allSource - exclude 'com/**' - classifier = 'src' - version = "${project.minecraft.version}-${project.version}" -} - -task deobfJar(type: Jar) { - from sourceSets.main.output - exclude 'com/**' - classifier = 'deobf' - version = "${project.minecraft.version}-${project.version}" -} - -artifacts { - archives sourceJar, deobfJar -} diff --git a/forge/forgeSrc.jar b/forge/forgeSrc.jar new file mode 100644 index 0000000..e42fd5f Binary files /dev/null and b/forge/forgeSrc.jar differ diff --git a/src/main/java/yalter/mousetweaks/MouseTweaksForge.java b/forge/yalter/mousetweaks/loaders/MouseTweaksForge.java similarity index 56% rename from src/main/java/yalter/mousetweaks/MouseTweaksForge.java rename to forge/yalter/mousetweaks/loaders/MouseTweaksForge.java index 8652aa2..663ca73 100644 --- a/src/main/java/yalter/mousetweaks/MouseTweaksForge.java +++ b/forge/yalter/mousetweaks/loaders/MouseTweaksForge.java @@ -1,4 +1,4 @@ -package yalter.mousetweaks; +package yalter.mousetweaks.loaders; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; @@ -6,12 +6,15 @@ import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; -@Mod(modid = "MouseTweaks", useMetadata = true) +import yalter.mousetweaks.Constants; +import yalter.mousetweaks.Main; + +@Mod(modid = Constants.MOD_ID, name = Constants.MOD_NAME, version = Constants.MOD_VERSION, useMetadata = true) public class MouseTweaksForge { @Mod.EventHandler public void init(FMLInitializationEvent event) { - Main.initialise(); + Main.initialise(Constants.EntryPoint.FORGE); if (Main.useForge) { @@ -22,7 +25,8 @@ public class MouseTweaksForge { @SubscribeEvent public void onRenderTick(TickEvent.RenderTickEvent event) { - if (event.phase == TickEvent.Phase.START) + // The useForge check is redundant here since the OnTick method cannot change in the runtime (at the time of writing this comment). + if (Main.useForge && event.phase == TickEvent.Phase.START) { Main.onUpdateInGame(); } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar deleted file mode 100644 index 0087cd3..0000000 Binary files a/gradle/wrapper/gradle-wrapper.jar and /dev/null differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties deleted file mode 100644 index 5a4e10a..0000000 --- a/gradle/wrapper/gradle-wrapper.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Tue Jun 03 23:08:53 MSK 2014 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip diff --git a/gradlew b/gradlew deleted file mode 100644 index 91a7e26..0000000 --- a/gradlew +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env bash - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn ( ) { - echo "$*" -} - -die ( ) { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; -esac - -# For Cygwin, ensure paths are in UNIX format before anything is touched. -if $cygwin ; then - [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` -fi - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >&- -APP_HOME="`pwd -P`" -cd "$SAVED" >&- - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=$((i+1)) - done - case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") -} -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" - -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat deleted file mode 100644 index 8a0b282..0000000 --- a/gradlew.bat +++ /dev/null @@ -1,90 +0,0 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windowz variants - -if not "%OS%" == "Windows_NT" goto win9xME_args -if "%@eval[2+2]" == "4" goto 4NT_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* -goto execute - -:4NT_args -@rem Get arguments from the 4NT Shell from JP Software -set CMD_LINE_ARGS=%$ - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/java/yalter/mousetweaks/Config.java b/java/yalter/mousetweaks/Config.java new file mode 100644 index 0000000..0e76268 --- /dev/null +++ b/java/yalter/mousetweaks/Config.java @@ -0,0 +1,73 @@ +package yalter.mousetweaks; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; + +public class Config { + private String fileName; + private Properties properties = new Properties(); + + public Config(String fileName) { + this.fileName = fileName; + } + + public boolean readConfig() { + properties.clear(); + + 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) { + Logger.Log("Failed to read the config file: " + fileName); + e.printStackTrace(); + } + + return false; + } + + public boolean saveConfig() { + try { + FileWriter configWriter = new FileWriter(fileName); + properties.store(configWriter, null); + configWriter.close(); + + return true; + } catch (IOException e) { + Logger.Log("Failed to write the config file: " + fileName); + e.printStackTrace(); + } + + return false; + } + + public String getOrCreateProperty(String name, String defaultValue) { + if (properties.containsKey(name)) + return properties.getProperty(name); + else + properties.setProperty(name, defaultValue); + + return defaultValue; + } + + public int getOrCreateIntProperty(String name, int defaultValue) { + return Integer.parseInt(getOrCreateProperty(name, String.valueOf(defaultValue))); + } + + public void setProperty(String name, String value) { + properties.setProperty(name, value); + } + + public void setIntProperty(String name, int value) { + setProperty(name, String.valueOf(value)); + } +} diff --git a/java/yalter/mousetweaks/Constants.java b/java/yalter/mousetweaks/Constants.java new file mode 100644 index 0000000..7c6d91b --- /dev/null +++ b/java/yalter/mousetweaks/Constants.java @@ -0,0 +1,45 @@ +package yalter.mousetweaks; + +public class Constants { + public static final String MOD_NAME = "@MODNAME@"; + public static final String MOD_ID = "@MODID@"; + public static final String MOD_VERSION = "@VERSION@"; + + // Obfuscated names + public static final String ISMOUSEOVERSLOT_NAME = "a"; + public static final String FIELDE_NAME = "H"; + public static final String FIELDq_NAME = "t"; + public static final String MCPROFILER_NAME = "A"; + + // Forge obfuscated names + public static final String ISMOUSEOVERSLOT_FORGE_NAME = "func_146981_a"; + public static final String FIELDE_FORGE_NAME = "field_146995_H"; + public static final String FIELDq_FORGE_NAME = "field_147007_t"; + public static final String MCPROFILER_FORGE_NAME = "field_71424_I"; + + // MCP names - these are the same as Forge ones for the most part + public static final String MCPROFILER_MCP_NAME = "mcProfiler"; + + // OnTick methods + public static final String ONTICKMETHOD_FORGE_NAME = "Forge"; + public static final String ONTICKMETHOD_LITELOADER_NAME = "LiteLoader"; + public static final String ONTICKMETHOD_PROFILER_NAME = "Minecraft"; + + // Inventory-related stuff + public static final int INVENTORY_SIZE = 36; // Size of the player inventory + + // Mod GUI container IDs + public static final int NOTASSIGNED = -1; // When we haven't determined it yet. + public static final int NOTGUICONTAINER = 0; // This is not a container GUI. + public static final int MINECRAFT = 1; // Containers that should be compatible with vanilla Minecraft ones. + public static final int MTMODGUICONTAINER = 2; // Containers that implement the IMTModGuiContainer interface. + public static final int FORESTRY = 3; // Forestry containers. + public static final int CODECHICKENCORE = 4; // CodeChickenCore containers. + public static final int NEI = 5; // NotEnoughItems containers (like the crafting menu). + + public enum EntryPoint { + UNDEFINED, + FORGE, + LITELOADER + } +} diff --git a/java/yalter/mousetweaks/DeobfuscationLayer.java b/java/yalter/mousetweaks/DeobfuscationLayer.java new file mode 100644 index 0000000..4ab2229 --- /dev/null +++ b/java/yalter/mousetweaks/DeobfuscationLayer.java @@ -0,0 +1,188 @@ +package yalter.mousetweaks; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.gui.inventory.GuiContainerCreative; +import net.minecraft.client.multiplayer.PlayerControllerMP; +import net.minecraft.client.settings.GameSettings; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.*; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import org.lwjgl.input.Mouse; + +import java.util.List; + +public class DeobfuscationLayer { + + protected static Minecraft mc; + + protected static GuiScreen getCurrentScreen() { + return mc.currentScreen; + } + + protected static boolean isGuiContainer(GuiScreen guiScreen) { + return (guiScreen != null) && (guiScreen instanceof GuiContainer); + } + + protected static boolean isValidGuiContainer(GuiScreen guiScreen) { + return (guiScreen != null) + && !(guiScreen.getClass().getSimpleName().contains("CJB_GuiCrafting")) + && !(guiScreen.getClass().equals(GuiContainerCreative.class)); + } + + protected static boolean isVanillaCraftingOutputSlot(Container container, Slot slot) { + return ((container instanceof ContainerWorkbench) && (getSlotNumber(slot) == 0)) + || ((container instanceof ContainerPlayer) && (getSlotNumber(slot) == 0)) + || ((container instanceof ContainerFurnace) && (getSlotNumber(slot) == 2)) + || ((container instanceof ContainerRepair) && (getSlotNumber(slot) == 2)); + } + + protected static GuiContainer asGuiContainer(GuiScreen guiScreen) { + return (GuiContainer) guiScreen; + } + + protected static Container asContainer(Object obj) { + return (Container) obj; + } + + protected static Slot asSlot(Object obj) { + return (Slot) obj; + } + + protected static Container getContainer(GuiContainer guiContainer) { + return guiContainer.field_147002_h; + } + + protected static List getSlots(Container container) { + return container.inventorySlots; + } + + protected static Slot getSlot(Container container, int index) { + return (Slot) (getSlots(container).get(index)); + } + + protected static ItemStack getSlotStack(Slot slot) { + return (slot == null) ? null : slot.getStack(); + } + + protected static int getItemStackID(ItemStack itemStack) { + return Item.getIdFromItem(itemStack.getItem()); + } + + protected static boolean hasSubtypesItemStack(ItemStack itemStack) { + return itemStack.getHasSubtypes(); + } + + protected static int getItemStackItemDamage(ItemStack itemStack) { + return itemStack.getItemDamage(); + } + + protected static int getWindowId(Container container) { + return container.windowId; + } + + protected static void windowClick(int windowId, int slotNumber, int mouseButton, + int shiftPressed) { + // if (slotNumber != -1) { + getPlayerController().windowClick(windowId, slotNumber, mouseButton, shiftPressed, + getThePlayer()); + // } + } + + protected static EntityClientPlayerMP getThePlayer() { + return mc.thePlayer; + } + + protected static InventoryPlayer getInventoryPlayer() { + return getThePlayer().inventory; + } + + protected static GameSettings getGameSettings() { + return mc.gameSettings; + } + + protected static int getDisplayWidth() { + return mc.displayWidth; + } + + protected static int getDisplayHeight() { + return mc.displayHeight; + } + + protected static ItemStack getStackOnMouse() { + return getInventoryPlayer().getItemStack(); + } + + protected static PlayerControllerMP getPlayerController() { + return mc.playerController; + } + + protected static int getSlotNumber(Slot slot) { + return slot.slotNumber; + } + + protected static int getItemStackSize(ItemStack itemStack) { + return itemStack.stackSize; + } + + protected static int getMaxItemStackSize(ItemStack itemStack) { + return itemStack.getMaxStackSize(); + } + + protected static ItemStack copyItemStack(ItemStack itemStack) { + return (itemStack == null) ? null : itemStack.copy(); + } + + protected static boolean areStacksCompatible(ItemStack itemStack1, ItemStack itemStack2) { + return ((itemStack1 == null) || (itemStack2 == null)) + || ((getItemStackID(itemStack1) == getItemStackID(itemStack2)) + && (!hasSubtypesItemStack(itemStack1) + || (getItemStackItemDamage(itemStack1) == getItemStackItemDamage(itemStack2)))); + } + + protected static boolean isMouseOverSlot(GuiContainer guiContainer, Slot slot) { + return (Boolean) Reflection.guiContainerClass.invokeMethod(guiContainer, "isMouseOverSlot", slot, getRequiredMouseX(), getRequiredMouseY()); + } + + protected static Slot getSelectedSlot(GuiContainer guiContainer, Container container, int slotCount) { + for (int i = 0; i < slotCount; i++) { + Slot slot = getSlot(container, i); + if (isMouseOverSlot(guiContainer, slot)) + return slot; + } + + return null; + } + + /** + * Disables the vanilla RMB drag mechanic in the given GuiContainer. If your guiContainer is based on the vanilla GuiContainer, you can use this method to disable the RMB drag. + * + * @param guiContainer The guiContainer to disable RMB drag in. + */ + public static void disableVanillaRMBDrag(GuiContainer guiContainer) { + Reflection.guiContainerClass.setFieldValue(guiContainer, Constants.FIELDE_FORGE_NAME, true); + Reflection.guiContainerClass.setFieldValue(guiContainer, Constants.FIELDq_FORGE_NAME, false); + } + + protected static int getRequiredMouseX() { + ScaledResolution var8 = new ScaledResolution(getGameSettings(), getDisplayWidth(), + getDisplayHeight()); + int var9 = var8.getScaledWidth(); + return (Mouse.getX() * var9) / getDisplayWidth(); + } + + protected static int getRequiredMouseY() { + ScaledResolution var8 = new ScaledResolution(getGameSettings(), getDisplayWidth(), + getDisplayHeight()); + int var10 = var8.getScaledHeight(); + return var10 - ((Mouse.getY() * var10) / getDisplayHeight()) - 1; + } + + protected static void sendClientMessage(String message) { + getThePlayer().sendChatMessage(message); + } +} diff --git a/java/yalter/mousetweaks/Logger.java b/java/yalter/mousetweaks/Logger.java new file mode 100644 index 0000000..b09719c --- /dev/null +++ b/java/yalter/mousetweaks/Logger.java @@ -0,0 +1,9 @@ +package yalter.mousetweaks; + +public class Logger { + + public static void Log(String textToLog) { + System.out.println("[Mouse Tweaks] " + textToLog); + } + +} diff --git a/java/yalter/mousetweaks/Main.java b/java/yalter/mousetweaks/Main.java new file mode 100644 index 0000000..1229e1f --- /dev/null +++ b/java/yalter/mousetweaks/Main.java @@ -0,0 +1,621 @@ +package yalter.mousetweaks; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import org.lwjgl.input.Keyboard; +import org.lwjgl.input.Mouse; + +import java.io.File; +import java.util.LinkedList; +import java.util.List; + +public class Main extends DeobfuscationLayer { + + public static boolean liteLoader = false; + public static boolean minecraftForge = false; + public static boolean optifine = false; + + public static boolean useForge = false; + public static boolean useLiteLoader = false; + public static boolean useProfiler = 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 enum OnTickMethod {PROFILER, FORGE, LITELOADER} + public static List onTickMethodOrder = new LinkedList(); + + public static int Debug = 0; + + public static Config mainConfig; + private static GuiScreen oldGuiScreen = null; + private static Object container = null; + private static Slot oldSelectedSlot = null; + private static Slot firstSlot = null; + private static ItemStack oldStackOnMouse = null; + private static boolean firstSlotClicked = false; + private static boolean shouldClick = true; + private static boolean disableForThisContainer = false; + private static boolean disableWheelForThisContainer = false; + private static int guiContainerID = 0; + + private static boolean readConfig = false; + private static boolean initialised = false; + private static boolean disabled = false; + + public static boolean initialise() { + return initialise(Constants.EntryPoint.UNDEFINED); + } + + public static boolean initialise(Constants.EntryPoint entryPoint) { + Logger.Log("A call to initialise, entry point: " + entryPoint.toString() + "."); + + if (disabled) + return false; + + if (initialised) + return true; + + initialised = true; + + if (!Reflection.reflectGuiContainer()) { + disabled = true; + return false; + } + + mc = Minecraft.getMinecraft(); + + mainConfig = new Config(mc.mcDataDir + File.separator + "config" + File.separator + "MouseTweaks.cfg"); + readConfigFile(); + + minecraftForge = Reflection.doesClassExist("net.minecraftforge.client.MinecraftForgeClient"); + if (minecraftForge) { + Logger.Log("Minecraft Forge is installed."); + } else { + Logger.Log("Minecraft Forge is not installed."); + } + + liteLoader = ((entryPoint == Constants.EntryPoint.LITELOADER) // If we're initialising from LiteModMouseTweaks, the check is not necessary. + || Reflection.doesClassExist("com.mumfrey.liteloader.core.LiteLoader")); + if (liteLoader) { + Logger.Log("LiteLoader is installed."); + } else { + Logger.Log("LiteLoader is not installed."); + } + + // Find the first suitable OnTick method. + methodLoop: + for (OnTickMethod method : onTickMethodOrder) { + switch (method) { + case FORGE: + if (minecraftForge) { + useForge = true; + Logger.Log("Using Forge for the mod operation."); + break methodLoop; + } + break; + + case LITELOADER: + if (liteLoader) { + useLiteLoader = ((entryPoint == Constants.EntryPoint.LITELOADER) + || Reflection.registerLiteModListenerInLiteLoader()); + if (useLiteLoader) { + Logger.Log("Using LiteLoader for the mod operation."); + break methodLoop; + } + } + break; + + case PROFILER: + if (Reflection.replaceProfiler()) { + useProfiler = true; + Logger.Log("Using the Minecraft profiler for the mod operation."); + + optifine = Reflection.reflectOptifine(); + + break methodLoop; + } + break; + } + } + + if (!useForge && !useLiteLoader && !useProfiler) { + // Nothing worked at all. + disabled = true; + return false; + } + + ModCompatibility.initialize(); + Logger.Log("Mouse Tweaks has been initialised."); + + return true; + } + + 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); + Debug = mainConfig.getOrCreateIntProperty("Debug", 0); + + // Load the OnTick method order. + String onTickMethodString = mainConfig.getOrCreateProperty("OnTickMethodOrder", + Constants.ONTICKMETHOD_FORGE_NAME + ", " + + Constants.ONTICKMETHOD_LITELOADER_NAME + ", " + + Constants.ONTICKMETHOD_PROFILER_NAME); + + onTickMethodString = onTickMethodString.trim(); + String onTickMethods[] = onTickMethodString.split("[\\s]*,[\\s]*"); + for (String method : onTickMethods) { + if (Constants.ONTICKMETHOD_FORGE_NAME.equalsIgnoreCase(method) && !onTickMethodOrder.contains(OnTickMethod.FORGE)) + onTickMethodOrder.add(OnTickMethod.FORGE); + else if (Constants.ONTICKMETHOD_LITELOADER_NAME.equalsIgnoreCase(method) && !onTickMethodOrder.contains(OnTickMethod.LITELOADER)) + onTickMethodOrder.add(OnTickMethod.LITELOADER); + else if (Constants.ONTICKMETHOD_PROFILER_NAME.equalsIgnoreCase(method) && !onTickMethodOrder.contains(OnTickMethod.PROFILER)) + onTickMethodOrder.add(OnTickMethod.PROFILER); + } + + // Make sure we have one of each. + if (!onTickMethodOrder.contains(OnTickMethod.FORGE)) + onTickMethodOrder.add(OnTickMethod.FORGE); + if (!onTickMethodOrder.contains(OnTickMethod.LITELOADER)) + onTickMethodOrder.add(OnTickMethod.LITELOADER); + if (!onTickMethodOrder.contains(OnTickMethod.PROFILER)) + onTickMethodOrder.add(OnTickMethod.PROFILER); + + boolean savedConfig = saveConfigFile(); + + if (savedConfig && !loadedConfig) + Logger.Log("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("Debug", Debug); + + String onTickMethodString = ""; + for (OnTickMethod method : onTickMethodOrder) { + if (!onTickMethodString.isEmpty()) + onTickMethodString += ", "; + + switch (method) { + case FORGE: + onTickMethodString += Constants.ONTICKMETHOD_FORGE_NAME; + break; + + case LITELOADER: + onTickMethodString += Constants.ONTICKMETHOD_LITELOADER_NAME; + break; + + case PROFILER: + onTickMethodString += Constants.ONTICKMETHOD_PROFILER_NAME; + break; + } + } + mainConfig.setProperty("OnTickMethodOrder", onTickMethodString); + + return mainConfig.saveConfig(); + } + + public static void onUpdateInGame() { + GuiScreen currentScreen = getCurrentScreen(); + if (currentScreen == null) { + // Reset stuff + oldGuiScreen = null; + container = null; + oldSelectedSlot = null; + firstSlot = null; + oldStackOnMouse = null; + firstSlotClicked = false; + shouldClick = true; + disableForThisContainer = false; + disableWheelForThisContainer = false; + readConfig = true; + + guiContainerID = Constants.NOTASSIGNED; + } else { + if (readConfig) { + readConfig = false; + readConfigFile(); + } + + if (guiContainerID == Constants.NOTASSIGNED) { + guiContainerID = getGuiContainerID(currentScreen); + } + + onUpdateInGui(currentScreen); + } + } + + public static void onUpdateInGui(GuiScreen currentScreen) { + + if (oldGuiScreen != currentScreen) { + oldGuiScreen = currentScreen; + + // If we opened an inventory from another inventory (for example, NEI's options menu). + guiContainerID = getGuiContainerID(currentScreen); + if (guiContainerID == Constants.NOTGUICONTAINER) + return; + + container = getContainerWithID(currentScreen); + disableForThisContainer = isDisabledForThisContainer(currentScreen); + + if (Debug == 1) { + Logger.Log(new StringBuilder().append("You have just opened a ").append(getGuiContainerNameFromID(currentScreen)).append(" container (").append(currentScreen.getClass().getSimpleName()).append((container == null) ? "" : "; ").append((container == null) ? "" : container.getClass().getSimpleName()).append("), which has ").append(getSlotCountWithID(currentScreen)).append(" slots!").toString()); + } + + disableWheelForThisContainer = isWheelDisabledForThisContainer(currentScreen); + } + + if (guiContainerID == Constants.NOTGUICONTAINER) + return; + + if ((Main.DisableRMBTweak || (Main.RMBTweak == 0)) && (Main.LMBTweakWithoutItem == 0) && (Main.LMBTweakWithItem == 0) + && (Main.WheelTweak == 0)) + return; + + if (disableForThisContainer) + return; + + int slotCount = getSlotCountWithID(currentScreen); // It's better to have this here, because there are some inventories that change slot count during + // runtime (for example NEI's crafting recipe GUI). + if (slotCount == 0) // If there are no slots, then there is nothing to do. + return; + + int wheel = ((Main.WheelTweak == 1) && !disableWheelForThisContainer) ? Mouse.getDWheel() / 120 : 0; + + if (!Mouse.isButtonDown(1)) { + firstSlotClicked = false; + firstSlot = null; + shouldClick = true; + } + + Slot selectedSlot = getSelectedSlotWithID(currentScreen, slotCount); + + // Copy the stacks, so that they don't change while we do our stuff. + ItemStack stackOnMouse = copyItemStack(getStackOnMouse()); + ItemStack targetStack = copyItemStack(getSlotStack(selectedSlot)); + + // To correctly determine, when and how the default RMB drag needs to be disabled, we need a bunch of conditions... + if (Mouse.isButtonDown(1) && (oldStackOnMouse != stackOnMouse) && (oldStackOnMouse == null)) { + shouldClick = false; + } + + if (oldSelectedSlot != selectedSlot) { + // ...and some more conditions. + if (Mouse.isButtonDown(1) && !firstSlotClicked && (firstSlot == null) && (oldSelectedSlot != null)) { + if (!areStacksCompatible(stackOnMouse, getSlotStack(oldSelectedSlot))) { + shouldClick = false; + } + + firstSlot = oldSelectedSlot; + } + + if (Mouse.isButtonDown(1) && (oldSelectedSlot == null) && !firstSlotClicked && (firstSlot == null)) { + shouldClick = false; + } + + if (selectedSlot == null) { + oldSelectedSlot = selectedSlot; + + if ((firstSlot != null) && !firstSlotClicked) { + firstSlotClicked = true; + disableRMBDragWithID(currentScreen); + firstSlot = null; + } + + return; + } + + if (Debug == 1) { + Logger.Log(new StringBuilder().append("You have selected a new slot, it's slot number is ").append(getSlotNumber(selectedSlot)).toString()); + } + + 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 ((stackOnMouse != null) && areStacksCompatible(stackOnMouse, targetStack)) { + if ((firstSlot != null) && !firstSlotClicked) { + firstSlotClicked = true; + disableRMBDragWithID(currentScreen); + firstSlot = null; + } else { + shouldClick = false; + disableRMBDragWithID(currentScreen); + } + + clickSlot(currentScreen, selectedSlot, 1, false); + } + + } + } else if (Mouse.isButtonDown(0)) { // Left mouse button + if (stackOnMouse != null) { + if (Main.LMBTweakWithItem == 1) { + if ((targetStack != null) && areStacksCompatible(stackOnMouse, targetStack)) { + + if (shiftIsDown) { // If shift is down, we just shift-click the slot and the item gets moved into another inventory. + clickSlot(currentScreen, selectedSlot, 0, true); + } else { // If shift is not down, we need to merge the item stack on the mouse with the one in the slot. + if ((getItemStackSize(stackOnMouse) + getItemStackSize(targetStack)) <= getMaxItemStackSize(stackOnMouse)) { + clickSlot(currentScreen, selectedSlot, 0, false); // We need to click on the slot so that our item stack gets merged with + clickSlot(currentScreen, selectedSlot, 0, false); // it, and then click again to return the stack to the mouse. + } + } + } + + } + } else if (Main.LMBTweakWithoutItem == 1) { + if (targetStack != null) { + if (shiftIsDown) { + clickSlot(currentScreen, selectedSlot, 0, true); + } + } + } + } + + oldSelectedSlot = selectedSlot; + } + + if ((wheel != 0) && (selectedSlot != null)) { + int numItemsToMove = Math.abs(wheel); + if (Debug == 1) { + Logger.Log("numItemsToMove: " + numItemsToMove); + } + + if (slotCount > Constants.INVENTORY_SIZE) { + ItemStack originalStack = getSlotStack(selectedSlot); + boolean isCraftingOutput = isCraftingOutputSlot(currentScreen, selectedSlot); + + if ((originalStack != null) + && ((stackOnMouse == null) || (isCraftingOutput ? areStacksCompatible(originalStack, stackOnMouse) : !areStacksCompatible(originalStack, stackOnMouse)))) { + do { + Slot applicableSlot = null; + + int slotCounter = 0; + int countUntil = slotCount + - Constants.INVENTORY_SIZE; + if (getSlotNumber(selectedSlot) < countUntil) { + slotCounter = countUntil; + countUntil = slotCount; + } + + if ((wheel < 0) || (Main.WheelSearchOrder == 0)) { + for (int i = slotCounter; i < countUntil; i++) { + Slot sl = getSlotWithID(currentScreen, i); + ItemStack stackSl = getSlotStack(sl); + + if (stackSl == null) { + if ((applicableSlot == null) + && (wheel < 0) + && sl.isItemValid(originalStack) + && !isCraftingOutputSlot(currentScreen, sl)) { + applicableSlot = sl; + } + } else if (areStacksCompatible(originalStack, stackSl)) { + if ((wheel < 0) + && (stackSl.stackSize < stackSl.getMaxStackSize())) { + applicableSlot = sl; + break; + } else if (wheel > 0) { + applicableSlot = sl; + break; + } + } + } + } else { + for (int i = countUntil - 1; i >= slotCounter; i--) { + Slot sl = getSlotWithID(currentScreen, i); + ItemStack stackSl = getSlotStack(sl); + + if (stackSl == null) { + if ((applicableSlot == null) + && (wheel < 0) + && sl.isItemValid(originalStack)) { + applicableSlot = sl; + } + } else if (areStacksCompatible(originalStack, stackSl)) { + if ((wheel < 0) + && (stackSl.stackSize < stackSl.getMaxStackSize())) { + applicableSlot = sl; + break; + } else if (wheel > 0) { + applicableSlot = sl; + break; + } + } + } + } + + if (isCraftingOutput) { + if (wheel < 0) { + boolean mouseWasEmpty = stackOnMouse == null; + + for (int i = 0; i < numItemsToMove; i++) { + clickSlot(currentScreen, selectedSlot, 0, false); + } + + if ((applicableSlot != null) + && mouseWasEmpty) { + clickSlot(currentScreen, applicableSlot, 0, false); + } + } + + break; + } + + if (applicableSlot != null) { + Slot slotTo = (wheel < 0) ? applicableSlot : selectedSlot; + Slot slotFrom = (wheel < 0) ? selectedSlot : applicableSlot; + ItemStack stackTo = (getSlotStack(slotTo) != null) ? copyItemStack(getSlotStack(slotTo)) : null; + ItemStack stackFrom = copyItemStack(getSlotStack(slotFrom)); + + if (wheel < 0) { + numItemsToMove = Math.min(numItemsToMove, getItemStackSize(stackFrom)); + + if ((stackTo != null) + && ((getMaxItemStackSize(stackTo) - getItemStackSize(stackTo)) <= numItemsToMove)) { + clickSlot(currentScreen, slotFrom, 0, false); + clickSlot(currentScreen, slotTo, 0, false); + clickSlot(currentScreen, slotFrom, 0, false); + + numItemsToMove -= getMaxItemStackSize(stackTo) - getItemStackSize(stackTo); + } else { + clickSlot(currentScreen, slotFrom, 0, false); + + if (getItemStackSize(stackFrom) <= numItemsToMove) { + clickSlot(currentScreen, slotTo, 0, false); + } else { + for (int i = 0; i < numItemsToMove; i++) { + clickSlot(currentScreen, slotTo, 1, false); + } + } + + clickSlot(currentScreen, slotFrom, 0, false); + + numItemsToMove = 0; + } + } else { + if ((getMaxItemStackSize(stackTo) - getItemStackSize(stackTo)) <= numItemsToMove) { + clickSlot(currentScreen, slotFrom, 0, false); + clickSlot(currentScreen, slotTo, 0, false); + clickSlot(currentScreen, slotFrom, 0, false); + } else { + clickSlot(currentScreen, slotFrom, 0, false); + + if (getItemStackSize(stackFrom) <= numItemsToMove) { + clickSlot(currentScreen, slotTo, 0, false); + numItemsToMove -= getMaxItemStackSize(stackFrom); + } else { + for (int i = 0; i < numItemsToMove; i++) { + clickSlot(currentScreen, slotTo, 1, false); + } + + numItemsToMove = 0; + } + + clickSlot(currentScreen, slotFrom, 0, false); + } + + if (getMaxItemStackSize(stackTo) == getMaxItemStackSize(stackTo)) { + numItemsToMove = 0; + } + } + } else { + break; + } + } + while (numItemsToMove != 0); + } + } + } + + oldStackOnMouse = stackOnMouse; + } + + public static int getGuiContainerID(GuiScreen currentScreen) { + int containerID = ModCompatibility.getModGuiContainerID(currentScreen); // This first because a lot of mod containers extend the vanilla Minecraft one. + if (containerID == Constants.NOTGUICONTAINER) + return (isGuiContainer(currentScreen) && isValidGuiContainer(currentScreen)) ? Constants.MINECRAFT : Constants.NOTGUICONTAINER; + else + return containerID; + } + + public static Object getContainerWithID(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) + return getContainer(asGuiContainer(currentScreen)); + else + return ModCompatibility.getModContainer(guiContainerID, currentScreen); + } + + public static int getSlotCountWithID(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) + return getSlots(asContainer(container)).size(); + else + return ModCompatibility.getModSlotCount(guiContainerID, currentScreen, container); + } + + public static String getGuiContainerNameFromID(GuiScreen currentScreen) { + switch (guiContainerID) { + case Constants.NOTASSIGNED: + return "Unknown"; + case Constants.NOTGUICONTAINER: + return "Wrong"; + case Constants.MINECRAFT: + return "Vanilla Minecraft"; + + default: + return ModCompatibility.getModNameFromModGuiContainerID(guiContainerID, currentScreen); + } + } + + public static boolean isDisabledForThisContainer(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) + return false; + else + return ModCompatibility.isDisabledForThisModContainer(guiContainerID, currentScreen, container); + } + + public static boolean isWheelDisabledForThisContainer(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) + return false; + else + return ModCompatibility.isWheelDisabledForThisModContainer(guiContainerID, currentScreen); + } + + public static Slot getSelectedSlotWithID(GuiScreen currentScreen, int slotCount) { + if (guiContainerID == Constants.MINECRAFT) + return getSelectedSlot(asGuiContainer(currentScreen), asContainer(container), slotCount); + else + return ModCompatibility.getModSelectedSlot(guiContainerID, currentScreen, container, slotCount); + } + + public static void clickSlot(GuiScreen currentScreen, Slot targetSlot, int mouseButton, boolean shiftPressed) { + if (guiContainerID == Constants.MINECRAFT) { + windowClick(getWindowId(asContainer(container)), getSlotNumber(targetSlot), mouseButton, shiftPressed ? 1 : 0); + } else { + ModCompatibility.modClickSlot(guiContainerID, currentScreen, container, targetSlot, mouseButton, shiftPressed); + } + } + + public static boolean isCraftingOutputSlot(GuiScreen currentScreen, Slot targetSlot) { + if (guiContainerID == Constants.MINECRAFT) + return isVanillaCraftingOutputSlot(asContainer(container), targetSlot); + else + return ModCompatibility.modIsCraftingOutputSlot(guiContainerID, currentScreen, container, targetSlot); + } + + public static Slot getSlotWithID(GuiScreen currentScreen, int slotNumber) { + if (guiContainerID == Constants.MINECRAFT) + return getSlot(asContainer(container), slotNumber); + else + return ModCompatibility.modGetSlot(guiContainerID, currentScreen, container, slotNumber); + } + + public static void disableRMBDragWithID(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) { + disableVanillaRMBDrag(asGuiContainer(currentScreen)); + + if (shouldClick) { + clickSlot(currentScreen, firstSlot, 1, false); + } + } else { + ModCompatibility.disableRMBDragIfRequired(guiContainerID, currentScreen, container, firstSlot, shouldClick); + } + } +} diff --git a/java/yalter/mousetweaks/ModCompatibility.java b/java/yalter/mousetweaks/ModCompatibility.java new file mode 100644 index 0000000..a90b94e --- /dev/null +++ b/java/yalter/mousetweaks/ModCompatibility.java @@ -0,0 +1,213 @@ +package yalter.mousetweaks; + +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.inventory.Slot; +import yalter.mousetweaks.api.IMTModGuiContainer; + +public class ModCompatibility extends DeobfuscationLayer { + private static boolean initialized = false; + + private static boolean forestryInstalled = false; + private static boolean codechickencoreInstalled = false; + private static boolean NEIInstalled = false; + + public static void initialize() { + if (initialized) + return; + + forestryInstalled = Reflection.reflectForestry(); + if (forestryInstalled) { + Logger.Log("Successfully reflected Forestry!"); + } + + codechickencoreInstalled = Reflection.reflectCodeChickenCore(); + if (codechickencoreInstalled) { + Logger.Log("Successfully reflected CodeChickenCore!"); + } + + NEIInstalled = Reflection.reflectNEI(); + if (NEIInstalled) { + Logger.Log("Successfully reflected NEI!"); + } + + initialized = true; + } + + public static int getModGuiContainerID(GuiScreen guiScreen) { + if (guiScreen instanceof IMTModGuiContainer) + return Constants.MTMODGUICONTAINER; + + if (forestryInstalled && Reflection.forestry.isInstance("GuiForestry", guiScreen)) + return Constants.FORESTRY; + + if (codechickencoreInstalled && Reflection.codechickencore.isInstance("GuiContainerWidget", guiScreen)) + return Constants.CODECHICKENCORE; + + if ((NEIInstalled + && (Reflection.NEI.isInstance("GuiRecipe", guiScreen))) + || Reflection.NEI.isInstance("GuiEnchantmentModifier", guiScreen)) + return Constants.NEI; + + return Constants.NOTGUICONTAINER; + } + + public static String getModNameFromModGuiContainerID(int id, GuiScreen guiScreen) { + switch (id) { + case Constants.MTMODGUICONTAINER: + return ((IMTModGuiContainer) guiScreen).getModName(); + + case Constants.FORESTRY: + return "Forestry"; + case Constants.CODECHICKENCORE: + return "CodeChickenCore"; + case Constants.NEI: + return "NotEnoughItems"; + + default: + return "Unknown"; + } + } + + public static boolean isWheelDisabledForThisModContainer(int modGuiContainerID, GuiScreen guiScreen) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + return ((IMTModGuiContainer) guiScreen).isWheelTweakDisabled(); + + default: + return false; + } + } + + public static Slot modGetSlot(int modGuiContainerID, GuiScreen guiScreen, Object modContainer, int slotNumber) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + return asSlot(((IMTModGuiContainer) guiScreen).getModSlot(modContainer, slotNumber)); + + case Constants.FORESTRY: + return getSlot(asContainer(modContainer), slotNumber); + case Constants.NEI: + return getSlot(asContainer(modContainer), slotNumber); + + default: + return null; + } + } + + public static boolean modIsCraftingOutputSlot(int modGuiContainerID, GuiScreen guiScreen, Object modContainer, Slot selectedSlot) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + return ((IMTModGuiContainer) guiScreen).isCraftingOutputSlot(modContainer, selectedSlot); + + case Constants.FORESTRY: + return (Reflection.is(modContainer, "ContainerSqueezer") && ((getSlotNumber(selectedSlot) == 9) || (getSlotNumber(selectedSlot) == 11))) + || (Reflection.is(modContainer, "ContainerMoistener") && (getSlotNumber(selectedSlot) == 9)) + || (Reflection.is(modContainer, "ContainerCentrifuge") && ((getSlotNumber(selectedSlot) >= 1) && (getSlotNumber(selectedSlot) <= 9))) + || (Reflection.is(modContainer, "ContainerCarpenter") && (getSlotNumber(selectedSlot) == 21)) + || (Reflection.is(modContainer, "ContainerFabricator") && (getSlotNumber(selectedSlot) == 20)) + || (Reflection.is(modContainer, "ContainerWorktable") && (getSlotNumber(selectedSlot) == 27)); + + default: + return false; + } + } + + public static void modClickSlot(int modGuiContainerID, GuiScreen guiScreen, Object modContainer, Slot targetSlot, int mouseButton, boolean shiftPressed) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + ((IMTModGuiContainer) guiScreen).clickModSlot(modContainer, targetSlot, mouseButton, shiftPressed); + return; + + case Constants.FORESTRY: + Reflection.forestry.invokeMethod(guiScreen, "handleMouseClick", targetSlot, 0, mouseButton, shiftPressed ? 1 : 0); + return; + + case Constants.NEI: + windowClick(getWindowId(asContainer(modContainer)), getSlotNumber(targetSlot), mouseButton, shiftPressed ? 1 : 0); + return; + + default: + return; + } + } + + public static Slot getModSelectedSlot(int modGuiContainerID, GuiScreen guiScreen, Object modContainer, int slotCount) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + return asSlot(((IMTModGuiContainer) guiScreen).getModSelectedSlot(modContainer, slotCount)); + + case Constants.FORESTRY: + return asSlot(Reflection.forestry.invokeMethod(guiScreen, "getSlotAtPosition", getRequiredMouseX(), getRequiredMouseY())); + case Constants.NEI: + return getSelectedSlot(asGuiContainer(guiScreen), asContainer(modContainer), slotCount); + + default: + return null; + } + } + + public static int getModSlotCount(int modGuiContainerID, GuiScreen guiScreen, Object modContainer) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + return ((IMTModGuiContainer) guiScreen).getModSlotCount(modContainer); + + case Constants.FORESTRY: + return getSlots(asContainer(modContainer)).size(); + case Constants.NEI: + return getSlots(asContainer(modContainer)).size(); + + default: + return 0; + } + } + + public static Object getModContainer(int modGuiContainerID, GuiScreen guiScreen) { + switch (modGuiContainerID) { + case Constants.MTMODGUICONTAINER: + return ((IMTModGuiContainer) guiScreen).getModContainer(); + + case Constants.FORESTRY: + return Reflection.forestry.getFieldValue("inventorySlots", guiScreen); + case Constants.NEI: + return getContainer(asGuiContainer(guiScreen)); + + default: + return null; + } + } + + public static void disableRMBDragIfRequired(int guiContainerID, GuiScreen guiScreen, Object modContainer, Slot firstSlot, boolean shouldClick) { + switch (guiContainerID) { + case Constants.MTMODGUICONTAINER: + ((IMTModGuiContainer) guiScreen).disableRMBDragIfRequired(modContainer, firstSlot, shouldClick); + + return; + + case Constants.NEI: + disableVanillaRMBDrag(asGuiContainer(guiScreen)); + + if (shouldClick) { + modClickSlot(guiContainerID, guiScreen, modContainer, firstSlot, 1, false); + } + + return; + + default: + return; + } + } + + public static boolean isDisabledForThisModContainer(int guiContainerID, GuiScreen guiScreen, Object container) { + switch (guiContainerID) { + case Constants.MTMODGUICONTAINER: + return ((IMTModGuiContainer) guiScreen).isMouseTweaksDisabled(); + + case Constants.CODECHICKENCORE: + return true; + case Constants.NEI: + return !Reflection.NEI.isInstance("GuiEnchantmentModifier", guiScreen); + + default: + return false; + } + } +} diff --git a/java/yalter/mousetweaks/ProfilerCustom.java b/java/yalter/mousetweaks/ProfilerCustom.java new file mode 100644 index 0000000..f90f14a --- /dev/null +++ b/java/yalter/mousetweaks/ProfilerCustom.java @@ -0,0 +1,28 @@ +package yalter.mousetweaks; + +import net.minecraft.client.Minecraft; +import net.minecraft.profiler.Profiler; + +public class ProfilerCustom extends Profiler { + private Minecraft minecraft; + + public ProfilerCustom() { + minecraft = Minecraft.getMinecraft(); + } + + @Override + public void startSection(String sectionName) { + // The useProfiler check is redundant here since the OnTick method cannot change in the runtime (at the time of writing this comment). + if (Main.useProfiler && "gameRenderer".equals(sectionName)) { + Main.onUpdateInGame(); + } + + super.startSection(sectionName); + + if (Main.optifine) { + if (!Reflection.gameSettings.setFieldValue(minecraft.gameSettings, "ofProfiler", true)) { + Main.optifine = false; + } + } + } +} diff --git a/java/yalter/mousetweaks/Reflection.java b/java/yalter/mousetweaks/Reflection.java new file mode 100644 index 0000000..4d806e7 --- /dev/null +++ b/java/yalter/mousetweaks/Reflection.java @@ -0,0 +1,389 @@ +package yalter.mousetweaks; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.settings.GameSettings; +import net.minecraft.inventory.Slot; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +public class Reflection { + public static ReflectionCache guiContainerClass; + public static ReflectionCache liteLoaderClass; + public static ReflectionCache liteModMouseTweaks; + public static ReflectionCache gameSettings; + public static ReflectionCache minecraft; + + public static ReflectionCache forestry; + public static ReflectionCache codechickencore; + public static ReflectionCache NEI; + + public enum Obfuscation {MCP, FORGE, VANILLA} + private static Obfuscation obfuscation; + private static boolean checkObfuscation = true; + + public static boolean reflectGuiContainer() { + guiContainerClass = new ReflectionCache(); + guiContainerClass.storeClass("GuiContainer", GuiContainer.class); + + Method isMouseOverSlot = getMethod(GuiContainer.class, + getObfuscatedName(Constants.ISMOUSEOVERSLOT_FORGE_NAME, Constants.ISMOUSEOVERSLOT_FORGE_NAME, Constants.ISMOUSEOVERSLOT_NAME), Slot.class, int.class, + int.class); + + if (isMouseOverSlot == null) { + Logger.Log("Failed to get isMouseOverSlot method, quitting"); + return false; + } + + guiContainerClass.storeMethod("isMouseOverSlot", isMouseOverSlot); + + Field field; + field = getField(GuiContainer.class, + getObfuscatedName(Constants.FIELDE_FORGE_NAME, Constants.FIELDE_FORGE_NAME, Constants.FIELDE_NAME)); + + if (field == null) { + Logger.Log("Failed to retrieve the E field, disabling RMBTweak"); + Main.DisableRMBTweak = true; + } else { + guiContainerClass.storeField(Constants.FIELDE_FORGE_NAME, field); + + field = getField(GuiContainer.class, + getObfuscatedName(Constants.FIELDq_FORGE_NAME, Constants.FIELDq_FORGE_NAME, Constants.FIELDq_NAME)); + + if (field == null) { + Logger.Log("Failed to retreive the q field, disabling RMBTweak"); + Main.DisableRMBTweak = true; + } else { + guiContainerClass.storeField(Constants.FIELDq_FORGE_NAME, field); + } + } + + return true; + } + + public static boolean registerLiteModListenerInLiteLoader() { + liteLoaderClass = new ReflectionCache(); + + Class lL = getClass("com.mumfrey.liteloader.core.LiteLoader"); + if (lL != null) { + liteLoaderClass.storeClass("LiteLoader", lL); + + liteLoaderClass.available = true; + + Class llEvents = getClass("com.mumfrey.liteloader.core.Events"); + if (llEvents != null) { + liteLoaderClass.storeClass("Events", llEvents); + + Field llEventsField = getField(lL, "events"); + if (llEventsField != null) { + liteLoaderClass.storeField("events", llEventsField); + + Class lM = getClass("com.mumfrey.liteloader.LiteMod"); + if (lM != null) { + liteLoaderClass.storeClass("LiteMod", lM); + + Method addListener = getMethod(llEvents, "addListener", lM); + if (addListener != null) { + liteLoaderClass.storeMethod("addListener", addListener); + + Method getInstance = getMethod(lL, "getInstance"); + if (getInstance != null) { + liteLoaderClass.storeMethod("getInstance", getInstance); + + Object liteLoaderInstance = liteLoaderClass.invokeStaticMethod("LiteLoader", "getInstance"); + if (liteLoaderInstance != null) { + Object llEventsInstance = liteLoaderClass.getFieldValue("events", liteLoaderInstance); + if (llEventsInstance != null) { + Class mouseTweaks = getClass("yalter.mousetweaks.loaders.LiteModMouseTweaks"); + if (mouseTweaks != null) { + liteModMouseTweaks = new ReflectionCache(); + liteModMouseTweaks.storeClass("LiteModMouseTweaks", mouseTweaks); + + Method getLMMTInstance = getMethod(mouseTweaks, "getInstance"); + if (getLMMTInstance != null) { + liteModMouseTweaks.storeMethod("getInstance", getLMMTInstance); + + Object LMMTInstance = liteModMouseTweaks.invokeStaticMethod("LiteModMouseTweaks", "getInstance"); + if (LMMTInstance != null) { + liteLoaderClass.invokeMethod(llEventsInstance, "addListener", LMMTInstance); + + liteLoaderClass.compatible = true; + Logger.Log("Registered the LiteModMouseTweaks' listener in LiteLoader."); + + return true; + } else { + Logger.Log("Could not get the instance from the getInstance method of LiteModMouseTweaks! Have you modified the class?"); + } + } else { + Logger.Log("Could not get the getInstance method from LiteModMouseTweaks! Have you modified the class?"); + } + } else { + Logger.Log("Could not get the LiteModMouseTweaks class! Have you deleted it accidentally?"); + } + } + } else { + Logger.Log("Failed to retrieve the LiteLoader instance!"); + } + } + } + } + } + } + } + + return false; + } + + public static boolean reflectOptifine() { + gameSettings = new ReflectionCache(); + gameSettings.storeClass("GameSettings", GameSettings.class); + + Field optifineProfilerEnabled = getField(GameSettings.class, "ofProfiler"); + if (optifineProfilerEnabled != null) { + gameSettings.storeField("ofProfiler", optifineProfilerEnabled); + + Logger.Log("Optifine is installed."); + return true; + } + + Logger.Log("Optifine is not installed."); + return false; + } + + public static boolean replaceProfiler() { + minecraft = new ReflectionCache(); + minecraft.storeClass("Minecraft", Minecraft.class); + + Field profilerField = getFinalField(Minecraft.class, getObfuscatedName(Constants.MCPROFILER_MCP_NAME, Constants.MCPROFILER_FORGE_NAME, Constants.MCPROFILER_NAME)); + if (profilerField != null) { + minecraft.storeField("mcProfiler", profilerField); + return minecraft.setFieldValue(Minecraft.getMinecraft(), "mcProfiler", new ProfilerCustom()); + } + + Logger.Log("Failed to get the Minecraft profiler field!"); + return false; + } + + public static boolean reflectForestry() { + forestry = new ReflectionCache(); + + Class guiForestryClass = getClass("forestry.core.gui.GuiForestry"); + if (guiForestryClass != null) { + forestry.storeClass("GuiForestry", guiForestryClass); + forestry.available = true; + + Field inventorySlotsField = getField(guiForestryClass, "inventorySlots"); + if (inventorySlotsField != null) { + forestry.storeField("inventorySlots", inventorySlotsField); + + Method getSlotAtPositionMethod = getMethod(guiForestryClass, "getSlotAtPosition", int.class, int.class); + if (getSlotAtPositionMethod != null) { + forestry.storeMethod("getSlotAtPosition", getSlotAtPositionMethod); + + Method handleMouseClickMethod = getMethod(guiForestryClass, "handleMouseClick", Slot.class, int.class, int.class, int.class); + if (handleMouseClickMethod != null) { + forestry.storeMethod("handleMouseClick", handleMouseClickMethod); + + forestry.compatible = true; + + // Class containerForestryClass = getClass( "forestry.core.gui.ContainerForestry" ); + // if ( containerForestryClass != null ) { + // forestry.storeClass( "ContainerForestry", containerForestryClass ); + // + // Field slotCountField = getField( containerForestryClass, "slotCount" ); + // if ( slotCountField != null ) { + // forestry.storeField( "slotCount", slotCountField ); + // + // forestry.compatible = true; + // } + // } + } + } + } + } + + return forestry.compatible; + } + + public static boolean reflectCodeChickenCore() { + codechickencore = new ReflectionCache(); + + Class guiContainerWidgetClass = getClass("codechicken.core.inventory.GuiContainerWidget"); + if (guiContainerWidgetClass != null) { + codechickencore.storeClass("GuiContainerWidget", guiContainerWidgetClass); + + codechickencore.available = true; + codechickencore.compatible = true; + } + + return codechickencore.compatible; + } + + public static boolean reflectNEI() { + NEI = new ReflectionCache(); + + Class guiRecipeClass = getClass("codechicken.nei.recipe.GuiRecipe"); + if (guiRecipeClass != null) { + NEI.storeClass("GuiRecipe", guiRecipeClass); + NEI.available = true; + + Class guiEnchantmentModifierClass = getClass("codechicken.nei.GuiEnchantmentModifier"); + if (guiEnchantmentModifierClass != null) { + NEI.storeClass("GuiEnchantmentModifier", guiEnchantmentModifierClass); + + NEI.compatible = true; + } + } + + return NEI.compatible; + } + + public static boolean is(Object object, String name) { + return object.getClass().getSimpleName() == name; + } + + public static boolean doesClassExist(String name) { + Class clazz = getClass(name); + return clazz != null; + } + + public static Class getClass(String name) { + try { + return Class.forName(name); + } catch (ClassNotFoundException e) { + ; + } + + return null; + } + + public static Field getField(Class clazz, String name) { + try { + Field field = null; + + try { + field = clazz.getField(name); + } catch (Exception e) { + field = null; + } + + if (field == null) { + field = clazz.getDeclaredField(name); + } + + field.setAccessible(true); + return field; + } catch (Exception e) { + if (name != "ofProfiler") { + Logger.Log("Could not retrieve field \"" + name + "\" from class \"" + clazz.getName() + + "\""); + + if (Main.Debug == 1) { + e.printStackTrace(); + } + } + } + + return null; + } + + public static Field getFinalField(Class clazz, String name) { + try { + Field field = null; + + try { + field = clazz.getField(name); + } catch (Exception e) { + field = null; + } + + if (field == null) { + field = clazz.getDeclaredField(name); + } + + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + + modifiers.set(field, field.getModifiers() & ~Modifier.FINAL); + + field.setAccessible(true); + return field; + } catch (Exception e) { + Logger.Log("Could not retrieve field \"" + name + "\" from class \"" + clazz.getName() + + "\""); + + if (Main.Debug == 1) { + e.printStackTrace(); + } + } + + return null; + } + + public static Method getMethod(Class clazz, String name, Class... args) { + try { + Method method = null; + + try { + method = clazz.getMethod(name, args); + } catch (Exception e) { + method = null; + } + + if (method == null) { + if ((args != null) & (args.length != 0)) { + method = clazz.getDeclaredMethod(name, args); + } else { + method = clazz.getDeclaredMethod(name, new Class[0]); + } + } + + method.setAccessible(true); + return method; + } catch (Exception e) { + Logger.Log("Could not retrieve method \"" + name + "\" from class \"" + clazz.getName() + + "\""); + + if (Main.Debug == 1) { + e.printStackTrace(); + } + } + + return null; + } + + public static String methodToString(Method method) { + return Modifier.toString(method.getModifiers()) + " " + ((method.getReturnType() != null) ? method + .getReturnType().getName() : "void") + " " + method.getName(); + } + + public static String getObfuscatedName(String mcpName, String forgeName, String originalName) { + if (checkObfuscation) { + checkObfuscation(); + } + + if (obfuscation == Obfuscation.MCP) + return mcpName; + else if (obfuscation == Obfuscation.FORGE) + return forgeName; + else + return originalName; + } + + public static void checkObfuscation() { + checkObfuscation = false; + + Logger.Log("Obfuscation check, please ignore the following errors (if they come up)."); + + if (getField(Minecraft.class, Constants.MCPROFILER_MCP_NAME) != null) + obfuscation = Obfuscation.MCP; + else if (getField(Minecraft.class, Constants.MCPROFILER_FORGE_NAME) != null) + obfuscation = Obfuscation.FORGE; + else + obfuscation = Obfuscation.VANILLA; + + Logger.Log("Obfuscation check completed."); + } +} diff --git a/java/yalter/mousetweaks/ReflectionCache.java b/java/yalter/mousetweaks/ReflectionCache.java new file mode 100644 index 0000000..b6e9d20 --- /dev/null +++ b/java/yalter/mousetweaks/ReflectionCache.java @@ -0,0 +1,101 @@ +package yalter.mousetweaks; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; + +public class ReflectionCache { + public boolean available = false; + public boolean compatible = false; + public HashMap classes = new HashMap(); + public HashMap methods = new HashMap(); + public HashMap fields = new HashMap(); + + public ReflectionCache() { + } + + public Object getFieldValue(String name, Object obj) { + try { + if (fields.containsKey(name)) { + Field field = fields.get(name); + return field.get(obj); + } + + Logger.Log("No such field: " + name); + } catch (Exception e) { + e.printStackTrace(); + Logger.Log("Failed to get a value of field: " + name); + } + + return null; + } + + public boolean setFieldValue(Object obj, String name, Object value) { + try { + if (fields.containsKey(name)) { + Field field = fields.get(name); + field.set(obj, value); + + return true; + } + + Logger.Log("No such field: " + name); + } catch (Exception e) { + e.printStackTrace(); + Logger.Log("Failed to set a value of field: " + name); + } + + return false; + } + + public Object invokeMethod(Object obj, String name, Object... args) { + try { + if (methods.containsKey(name)) { + Method method = methods.get(name); + + if (args != null) + return method.invoke(obj, args); + + return method.invoke(obj, new Object[0]); + } + + Logger.Log("No such method: " + name); + } catch (Exception e) { + e.printStackTrace(); + Logger.Log("Failed to invoke method: " + name); + } + + return null; + } + + public Object invokeStaticMethod(String className, String name, Object... args) { + if (classes.containsKey(className)) { + Class clazz = classes.get(className); + + return invokeMethod(clazz, name, args); + } + + return null; + } + + public boolean isInstance(String className, Object obj) { + if (classes.containsKey(className)) { + Class clazz = classes.get(className); + return clazz.isInstance(obj); + } + + return false; + } + + public void storeClass(String name, Class clazz) { + classes.put(name, clazz); + } + + public void storeMethod(String name, Method method) { + methods.put(name, method); + } + + public void storeField(String name, Field field) { + fields.put(name, field); + } +} diff --git a/java/yalter/mousetweaks/api/IMTModGuiContainer.java b/java/yalter/mousetweaks/api/IMTModGuiContainer.java new file mode 100644 index 0000000..e16fd67 --- /dev/null +++ b/java/yalter/mousetweaks/api/IMTModGuiContainer.java @@ -0,0 +1,92 @@ +package yalter.mousetweaks.api; + +public interface IMTModGuiContainer { + /** + * Returns the API version. + * + * @return The API version. Should return 1 for this version. + */ + public int getAPIVersion(); + + /** + * Returns the mod name to use in log messages. + * + * @return The name of the mod. + */ + public String getModName(); + + /** + * Returns whether or not Mouse Tweaks should be disabled for this GuiContainer. + * + * @return True if Mouse Tweaks should be disabled, false otherwise. + */ + public boolean isMouseTweaksDisabled(); + + /** + * Returns whether or not wheelTweak should be disabled for this GuiContainer. + * + * @return True if wheelTweak should be disabled, false otherwise. + */ + public boolean isWheelTweakDisabled(); + + /** + * Returns whether or not the given slot is a crafting output slot. + * + * @param modContainer The container of the given slot. + * @param slot The slot to check. + * @return True if the given slot is a crafting output slot, false otherwise. + */ + public boolean isCraftingOutputSlot(Object modContainer, Object slot); + + /** + * Returns the container of this GuiContainer. If multiple containers are used, returns the one that is used at the moment of the method execution. + * + * @return The container that is currently used. + */ + public Object getModContainer(); + + /** + * Returns the number of slots present in the given container (for containers with variable slot number - the number of slots at the moment of the method execution). + * + * @param modContainer The container to retrieve the number of slots from. + * @return The number of slots in the given container. + */ + public int getModSlotCount(Object modContainer); + + /** + * Returns a slot from this container with the given slot number. + * + * @param modContainer The container from which to retrieve the slot. + * @param slotNumber The number of the slot to be retrieved. + * @return The slot from the given container with the given slot number. + */ + public Object getModSlot(Object modContainer, int slotNumber); + + /** + * Returns the currently selected slot (the one under the player's mouse cursor) of the given container, which has the given number of slots. + * + * @param modContainer The container to check. + * @param slotCount The number of slots that this container has. + * @return The slot that is currently selected. Null if none are. + */ + public Object getModSelectedSlot(Object modContainer, int slotCount); + + /** + * The method that clicks the given slot of the given container with the given mouse button and shift key state. + * + * @param modContainer The container of the given slot. + * @param slot The slot to click. + * @param mouseButton The mouse button. 0 is the left one, 1 is the right one. + * @param shiftPressed True if the shift key is pressed, false otherwise. + */ + public void clickModSlot(Object modContainer, Object slot, int mouseButton, boolean shiftPressed); + + /** + * The method that disables the built-in RMB drag mechanic, if required, in this GuiContainer, with the given first slot in the given container to click if shouldClick is set to true (in vanilla circumstances - your RMB dragging mechanic might work differently). + * + * @param modContainer The container of the first slot. + * @param firstSlot The first slot. + * @param shouldClick True, if the first slot is to be clicked. + */ + public void disableRMBDragIfRequired(Object modContainer, Object firstSlot, boolean shouldClick); +} diff --git a/java/yalter/mousetweaks/loaders/LiteModMouseTweaks.java b/java/yalter/mousetweaks/loaders/LiteModMouseTweaks.java new file mode 100644 index 0000000..91fc583 --- /dev/null +++ b/java/yalter/mousetweaks/loaders/LiteModMouseTweaks.java @@ -0,0 +1,54 @@ +package yalter.mousetweaks.loaders; + +import com.mumfrey.liteloader.RenderListener; +import net.minecraft.client.gui.GuiScreen; +import yalter.mousetweaks.Constants; +import yalter.mousetweaks.Main; + +import java.io.File; + +public class LiteModMouseTweaks implements RenderListener { + private static LiteModMouseTweaks instance; + + public static LiteModMouseTweaks getInstance() { + if (instance == null) + instance = new LiteModMouseTweaks(); + + return instance; + } + + public LiteModMouseTweaks() { } + + @Override + public void init(File file) { + Main.initialise(Constants.EntryPoint.LITELOADER); + } + + @Override + public void onRender() { + if (Main.useLiteLoader) + Main.onUpdateInGame(); + } + + @Override + public String getName() { + return Constants.MOD_NAME; + } + + @Override + public String getVersion() { + return Constants.MOD_VERSION; + } + + @Override + public void onRenderGui(GuiScreen guiScreen) { } + + @Override + public void onRenderWorld() { } + + @Override + public void onSetupCameraTransform() { } + + @Override + public void upgradeSettings(String s, File file, File file2) { } +} diff --git a/src/main/resources/mousetweaks_logo.png b/resources/mousetweaks_logo.png similarity index 100% rename from src/main/resources/mousetweaks_logo.png rename to resources/mousetweaks_logo.png diff --git a/src/main/java/yalter/mousetweaks/Config.java b/src/main/java/yalter/mousetweaks/Config.java deleted file mode 100644 index 830cebe..0000000 --- a/src/main/java/yalter/mousetweaks/Config.java +++ /dev/null @@ -1,132 +0,0 @@ -package yalter.mousetweaks; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.util.HashMap; -import java.util.Iterator; - -public class Config -{ - private String fileName; - private HashMap Properties = new HashMap(); - - public Config( String fileName ) - { - this.fileName = fileName; - } - - public boolean readConfig() - { - Properties.clear(); - - try - { - File config = new File( fileName ); - if ( !config.exists() ) - return false; - - FileReader fr = new FileReader( config ); - BufferedReader br = new BufferedReader( fr ); - String strLine = ""; - - while ( ( strLine = br.readLine() ) != null ) - { - String[] strKeyValue = strLine.split( "=" ); - - int l = strKeyValue.length; - - if ( l < 2 ) - { - continue; - } - - strKeyValue[0] = strKeyValue[0].trim(); - strKeyValue[1] = strKeyValue[1].trim(); - - if ( Properties.containsKey( strKeyValue[0] ) ) - { - Logger.Log( "Duplicate property \"" + strKeyValue[0] + "\" in the config file, using the last value present" ); - } - - Properties.put( strKeyValue[0], Integer.parseInt( strKeyValue[1] ) ); - } - - br.close(); - fr.close(); - - int size = Properties.size(); - if ( size == 0 ) - return false; - - return true; - } - catch ( Exception e ) - { - e.printStackTrace(); - } - - return false; - } - - public void saveConfig() - { - try - { - File config = new File( fileName ); - if ( !config.exists() ) - { - config.getParentFile().mkdirs(); - config.createNewFile(); - } - - int size = Properties.size(); - if ( size == 0 ) - return; - - FileWriter fw = new FileWriter( config ); - BufferedWriter bw = new BufferedWriter( fw ); - - Iterator i = Properties.keySet().iterator(); - while ( i.hasNext() ) - { - String key = ( String ) i.next(); - bw.write( key + " = " + Properties.get( key ) + "\n" ); - } - - bw.close(); - fw.close(); - } - catch ( Exception e ) - { - Logger.Log( "Failed to write the config file: " + fileName ); - e.printStackTrace(); - } - } - - public int getOrCreatePropertyValue( String name, int defaultValue ) - { - if ( Properties.containsKey( name ) ) - return ( Integer ) Properties.get( name ); - else - { - setPropertyValue( name, defaultValue ); - return defaultValue; - } - } - - public int getPropertyValue( String name ) - { - if ( Properties.containsKey( name ) ) - return ( Integer ) Properties.get( name ); - else - return 0; - } - - public void setPropertyValue( String name, int value ) - { - Properties.put( name, value ); - } -} diff --git a/src/main/java/yalter/mousetweaks/Constants.java b/src/main/java/yalter/mousetweaks/Constants.java deleted file mode 100644 index 59f1068..0000000 --- a/src/main/java/yalter/mousetweaks/Constants.java +++ /dev/null @@ -1,29 +0,0 @@ -package yalter.mousetweaks; - -public class Constants -{ - // Obfuscated names - public static final String ISMOUSEOVERSLOT_NAME = "a"; - public static final String FIELDE_NAME = "H"; - public static final String FIELDq_NAME = "t"; - public static final String MCPROFILER_NAME = "A"; - - // Forge obfuscated names - public static final String ISMOUSEOVERSLOT_FORGE_NAME = "func_146981_a"; - public static final String FIELDE_FORGE_NAME = "field_146995_H"; - public static final String FIELDq_FORGE_NAME = "field_147007_t"; - public static final String MCPROFILER_FORGE_NAME = "field_71424_I"; - - // Inventory-related stuff - public static final int INVENTORY_SIZE = 36; // Size of the player inventory - - // Mod GUI container IDs - public static final int NOTASSIGNED = -1; // When we haven't determined it yet. - public static final int NOTGUICONTAINER = 0; // This is not a container GUI. - public static final int MINECRAFT = 1; // Containers that should be compatible with vanilla Minecraft ones. - public static final int MTMODGUICONTAINER = 2; // Containers that implement the IMTModGuiContainer interface. - public static final int FORESTRY = 3; // Forestry containers. - public static final int CODECHICKENCORE = 4; // CodeChickenCore containers. - public static final int NEI = 5; // NotEnoughItems containers (like the crafting menu). - -} diff --git a/src/main/java/yalter/mousetweaks/DeobfuscationLayer.java b/src/main/java/yalter/mousetweaks/DeobfuscationLayer.java deleted file mode 100644 index 19fd02c..0000000 --- a/src/main/java/yalter/mousetweaks/DeobfuscationLayer.java +++ /dev/null @@ -1,231 +0,0 @@ -package yalter.mousetweaks; - -import java.util.List; - -import net.minecraft.inventory.Container; -import net.minecraft.inventory.ContainerFurnace; -import net.minecraft.inventory.ContainerPlayer; -import net.minecraft.inventory.ContainerRepair; -import net.minecraft.inventory.ContainerWorkbench; -import net.minecraft.client.entity.EntityClientPlayerMP; -import net.minecraft.client.settings.GameSettings; -import net.minecraft.client.gui.inventory.GuiContainer; -import net.minecraft.client.gui.inventory.GuiContainerCreative; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.PlayerControllerMP; -import net.minecraft.client.gui.ScaledResolution; -import net.minecraft.inventory.Slot; - -import org.lwjgl.input.Mouse; - -public class DeobfuscationLayer -{ - - protected static Minecraft mc; - - // protected MouseTweaksModCompatibility MTMC; - - protected static GuiScreen getCurrentScreen() - { - return mc.currentScreen; - } - - protected static boolean isGuiContainer( GuiScreen guiScreen ) - { - return ( guiScreen != null ) && ( guiScreen instanceof GuiContainer ); - } - - protected static boolean isValidGuiContainer( GuiScreen guiScreen ) - { - return ( guiScreen != null ) - && !( guiScreen.getClass().getSimpleName().contains( "CJB_GuiCrafting" ) ) - && !( guiScreen.getClass().equals( GuiContainerCreative.class ) ); - } - - protected static boolean isVanillaCraftingOutputSlot( Container container, Slot slot ) - { - return ( ( container instanceof ContainerWorkbench ) && ( getSlotNumber( slot ) == 0 ) ) - || ( ( container instanceof ContainerPlayer ) && ( getSlotNumber( slot ) == 0 ) ) - || ( ( container instanceof ContainerFurnace ) && ( getSlotNumber( slot ) == 2 ) ) - || ( ( container instanceof ContainerRepair ) && ( getSlotNumber( slot ) == 2 ) ); - } - - protected static GuiContainer asGuiContainer( GuiScreen guiScreen ) - { - return ( GuiContainer ) guiScreen; - } - - protected static Container asContainer( Object obj ) - { - return ( Container ) obj; - } - - protected static Slot asSlot( Object obj ) - { - return ( Slot ) obj; - } - - protected static Container getContainer( GuiContainer guiContainer ) - { - return guiContainer.inventorySlots; - } - - protected static List< ? > getSlots( Container container ) - { - return container.inventorySlots; - } - - protected static Slot getSlot( Container container, int index ) - { - return ( Slot ) ( getSlots( container ).get( index ) ); - } - - protected static ItemStack getSlotStack( Slot slot ) - { - return ( slot == null ) ? null : slot.getStack(); - } - - protected static int getItemStackID( ItemStack itemStack ) - { - return Item.getIdFromItem( itemStack.getItem() ); - } - - protected static boolean hasSubtypesItemStack( ItemStack itemStack ) - { - return itemStack.getHasSubtypes(); - } - - protected static int getItemStackItemDamage( ItemStack itemStack ) - { - return itemStack.getItemDamage(); - } - - protected static int getWindowId( Container container ) - { - return container.windowId; - } - - protected static void windowClick( int windowId, int slotNumber, int mouseButton, - int shiftPressed ) - { - // if (slotNumber != -1) { - getPlayerController().windowClick( windowId, slotNumber, mouseButton, shiftPressed, - getThePlayer() ); - // } - } - - protected static EntityClientPlayerMP getThePlayer() - { - return mc.thePlayer; - } - - protected static InventoryPlayer getInventoryPlayer() - { - return getThePlayer().inventory; - } - - protected static GameSettings getGameSettings() - { - return mc.gameSettings; - } - - protected static int getDisplayWidth() - { - return mc.displayWidth; - } - - protected static int getDisplayHeight() - { - return mc.displayHeight; - } - - protected static ItemStack getStackOnMouse() - { - return getInventoryPlayer().getItemStack(); - } - - protected static PlayerControllerMP getPlayerController() - { - return mc.playerController; - } - - protected static int getSlotNumber( Slot slot ) - { - return slot.slotNumber; - } - - protected static int getItemStackSize( ItemStack itemStack ) - { - return itemStack.stackSize; - } - - protected static int getMaxItemStackSize( ItemStack itemStack ) - { - return itemStack.getMaxStackSize(); - } - - protected static ItemStack copyItemStack( ItemStack itemStack ) - { - return ( itemStack == null ) ? null : itemStack.copy(); - } - - protected static boolean areStacksCompatible( ItemStack itemStack1, ItemStack itemStack2 ) - { - return ( ( itemStack1 == null ) || ( itemStack2 == null ) ) - || ( ( getItemStackID( itemStack1 ) == getItemStackID( itemStack2 ) ) && ( !hasSubtypesItemStack( itemStack1 ) || ( getItemStackItemDamage( itemStack1 ) == getItemStackItemDamage( itemStack2 ) ) ) ); - } - - protected static boolean isMouseOverSlot( GuiContainer guiContainer, Slot slot ) - { - return ( Boolean ) Reflection.guiContainerClass.invokeMethod( guiContainer, - "isMouseOverSlot", slot, getRequiredMouseX(), getRequiredMouseY() ); - } - - protected static Slot getSelectedSlot( GuiContainer guiContainer, Container container, int slotCount ) - { - for ( int i = 0; i < slotCount; i++ ) - { - Slot slot = getSlot( container, i ); - if ( isMouseOverSlot( guiContainer, slot ) ) - return slot; - } - - return null; - } - - /** - * Disables the vanilla RMB drag mechanic in the given GuiContainer. If your guiContainer is based on the vanilla GuiContainer, you can use this method to disable the RMB drag. - * - * @param guiContainer The guiContainer to disable RMB drag in. - */ - public static void disableVanillaRMBDrag( GuiContainer guiContainer ) - { - Reflection.guiContainerClass.setFieldValue( guiContainer, Constants.FIELDE_FORGE_NAME, true ); - Reflection.guiContainerClass.setFieldValue( guiContainer, Constants.FIELDq_FORGE_NAME, false ); - } - - protected static int getRequiredMouseX() - { - ScaledResolution var8 = new ScaledResolution( getGameSettings(), getDisplayWidth(), - getDisplayHeight() ); - int var9 = var8.getScaledWidth(); - return ( Mouse.getX() * var9 ) / getDisplayWidth(); - } - - protected static int getRequiredMouseY() - { - ScaledResolution var8 = new ScaledResolution( getGameSettings(), getDisplayWidth(), - getDisplayHeight() ); - int var10 = var8.getScaledHeight(); - return var10 - ( ( Mouse.getY() * var10 ) / getDisplayHeight() ) - 1; - } - - protected static void sendClientMessage( String message ) - { - getThePlayer().sendChatMessage( message ); - } -} diff --git a/src/main/java/yalter/mousetweaks/Logger.java b/src/main/java/yalter/mousetweaks/Logger.java deleted file mode 100644 index 167e9b7..0000000 --- a/src/main/java/yalter/mousetweaks/Logger.java +++ /dev/null @@ -1,11 +0,0 @@ -package yalter.mousetweaks; - -public class Logger -{ - - public static void Log( String textToLog ) - { - System.out.println( "[Mouse Tweaks] " + textToLog ); - } - -} diff --git a/src/main/java/yalter/mousetweaks/Main.java b/src/main/java/yalter/mousetweaks/Main.java deleted file mode 100644 index 1445199..0000000 --- a/src/main/java/yalter/mousetweaks/Main.java +++ /dev/null @@ -1,686 +0,0 @@ -package yalter.mousetweaks; - -import java.io.File; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.item.ItemStack; -import net.minecraft.inventory.Slot; - -import org.lwjgl.input.Keyboard; -import org.lwjgl.input.Mouse; - -public class Main extends DeobfuscationLayer -{ - - private static GuiScreen oldGuiScreen = null; - private static Object container = null; - private static Slot oldSelectedSlot = null; - private static Slot firstSlot = null; - private static ItemStack oldStackOnMouse = null; - private static boolean firstSlotClicked = false; - private static boolean shouldClick = true; - private static boolean disableForThisContainer = false; - private static boolean disableWheelForThisContainer = false; - - private static int guiContainerID = 0; - - private static boolean readConfig = false; - private static boolean initialised = false; - private static boolean disabled = false; - - public static boolean liteLoader = false; - public static boolean minecraftForge = false; - public static boolean optifine = false; -// public static boolean modLoader = false; - public static boolean useForge = 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 ForceForgeOnRenderTick = 0; - public static int WheelSearchOrder = 1; - - public static int Debug = 0; - - public static Config mainConfig; - - public static boolean initialise() - { - if ( disabled ) - return false; - - if ( initialised ) - return true; - - initialised = true; - - mc = Minecraft.getMinecraft(); - - mainConfig = new Config( mc.mcDataDir + File.separator + "config" - + File.separator + "MouseTweaks.cfg" ); - readConfigFile(); - -// modLoader = Reflection.doesClassExist( "net.minecraft.src.ModLoader" ); - minecraftForge = Reflection - .doesClassExist( "net.minecraftforge.client.MinecraftForgeClient" ); - - if ( minecraftForge ) - { - Logger.Log( "Minecraft Forge is installed" ); - } - else - { - Logger.Log( "Minecraft Forge is not installed" ); - } - - if ( ForceForgeOnRenderTick == 1 ) - { - if ( minecraftForge ) - { - useForge = true; - Logger.Log( "ForceForgeOnRenderTick is set to 1, using Forge's RenderTickEvent for mod operation..." ); - } - else - { - Logger.Log( "ForceForgeOnRenderTick is set to 1, but Forge's main class is not present, quitting..." ); - disabled = true; - return false; - } - } - else - { - liteLoader = Reflection.reflectLiteLoader(); - optifine = Reflection.reflectOptifine(); - } - - if ( !liteLoader && ( ForceForgeOnRenderTick == 0 ) ) - { - useForge = !Reflection.reflectMinecraft(); - if ( !useForge ) - { - if ( !Reflection.replaceProfiler() ) - { - if ( minecraftForge ) - { - useForge = true; - Logger.Log( "Using Forge's RenderTickEvent for mod operation." ); - } - else - { - Logger.Log( "Failed to replace the Minecraft profiler, quitting..." ); - - disabled = true; - return false; - } - } - } - else - { - Logger.Log( "Using Forge's RenderTickEvent for mod operation" ); - } - } - - boolean returnValue = Reflection.reflectGuiContainer(); - - if ( returnValue ) - { - ModCompatibility.initialize(); - Logger.Log( "Mouse Tweaks has been initialised" ); - } - else - { - disabled = true; - } - - return returnValue; - } - - public static void readConfigFile() - { - if ( !mainConfig.readConfig() ) - { - mainConfig.setPropertyValue( "RMBTweak", 1 ); - mainConfig.setPropertyValue( "LMBTweakWithItem", 1 ); - mainConfig.setPropertyValue( "LMBTweakWithoutItem", 1 ); - mainConfig.setPropertyValue( "WheelTweak", 1 ); - mainConfig.setPropertyValue( "WheelSearchOrder", 1 ); - mainConfig.setPropertyValue( "ForceForgeOnRenderTick", 0 ); - mainConfig.setPropertyValue( "Debug", 0 ); - - mainConfig.saveConfig(); - - Logger.Log( "Mouse Tweaks config file was created." ); - } - - RMBTweak = mainConfig.getOrCreatePropertyValue( "RMBTweak", 1 ); - LMBTweakWithItem = mainConfig.getOrCreatePropertyValue( "LMBTweakWithItem", 1 ); - LMBTweakWithoutItem = mainConfig.getOrCreatePropertyValue( "LMBTweakWithoutItem", 1 ); - WheelTweak = mainConfig.getOrCreatePropertyValue( "WheelTweak", 1 ); - WheelSearchOrder = mainConfig.getOrCreatePropertyValue( "WheelSearchOrder", 1 ); - ForceForgeOnRenderTick = mainConfig.getOrCreatePropertyValue( "ForceForgeOnRenderTick", 0 ); - Debug = mainConfig.getOrCreatePropertyValue( "Debug", 0 ); - - mainConfig.saveConfig(); - } - - public static void onUpdateInGame() - { - GuiScreen currentScreen = getCurrentScreen(); - if ( currentScreen == null ) - { - // Reset stuff - oldGuiScreen = null; - container = null; - oldSelectedSlot = null; - firstSlot = null; - oldStackOnMouse = null; - firstSlotClicked = false; - shouldClick = true; - disableForThisContainer = false; - disableWheelForThisContainer = false; - readConfig = true; - - guiContainerID = Constants.NOTASSIGNED; - } - else - { - - if ( readConfig ) - { - readConfig = false; - readConfigFile(); - } - - if ( guiContainerID == Constants.NOTASSIGNED ) - { - guiContainerID = getGuiContainerID( currentScreen ); - } - - onUpdateInGui( currentScreen ); - - } - } - - public static void onUpdateInGui( GuiScreen currentScreen ) - { - - if ( oldGuiScreen != currentScreen ) - { - oldGuiScreen = currentScreen; - - // If we opened an inventory from another inventory (for example, NEI's options menu). - guiContainerID = getGuiContainerID( currentScreen ); - if ( guiContainerID == Constants.NOTGUICONTAINER ) - return; - - container = getContainerWithID( currentScreen ); - disableForThisContainer = isDisabledForThisContainer( currentScreen ); - - if ( Debug == 1 ) - { - Logger.Log( new StringBuilder().append( "You have just opened a " ).append( getGuiContainerNameFromID( currentScreen ) ).append( " container (" ).append( currentScreen.getClass().getSimpleName() ).append( ( container == null ) ? "" : "; " ).append( ( container == null ) ? "" : container.getClass().getSimpleName() ).append( "), which has " ).append( getSlotCountWithID( currentScreen ) ).append( " slots!" ).toString() ); - } - - disableWheelForThisContainer = isWheelDisabledForThisContainer( currentScreen ); - } - - if ( guiContainerID == Constants.NOTGUICONTAINER ) - return; - - if ( ( Main.DisableRMBTweak || ( Main.RMBTweak == 0 ) ) && ( Main.LMBTweakWithoutItem == 0 ) && ( Main.LMBTweakWithItem == 0 ) - && ( Main.WheelTweak == 0 ) ) - return; - - if ( disableForThisContainer ) - return; - - int slotCount = getSlotCountWithID( currentScreen ); // It's better to have this here, because there are some inventories that change slot count during - // runtime (for example NEI's crafting recipe GUI). - if ( slotCount == 0 ) // If there are no slots, then there is nothing to do. - return; - - int wheel = ( ( Main.WheelTweak == 1 ) && !disableWheelForThisContainer ) ? Mouse.getDWheel() / 120 : 0; - - if ( !Mouse.isButtonDown( 1 ) ) - { - firstSlotClicked = false; - firstSlot = null; - shouldClick = true; - } - - Slot selectedSlot = getSelectedSlotWithID( currentScreen, slotCount ); - - // Copy the stacks, so that they don't change while we do our stuff. - ItemStack stackOnMouse = copyItemStack( getStackOnMouse() ); - ItemStack targetStack = copyItemStack( getSlotStack( selectedSlot ) ); - - // To correctly determine, when and how the default RMB drag needs to be disabled, we need a bunch of conditions... - if ( Mouse.isButtonDown( 1 ) && ( oldStackOnMouse != stackOnMouse ) && ( oldStackOnMouse == null ) ) - { - shouldClick = false; - } - - if ( oldSelectedSlot != selectedSlot ) - { - // ...and some more conditions. - if ( Mouse.isButtonDown( 1 ) && !firstSlotClicked && ( firstSlot == null ) && ( oldSelectedSlot != null ) ) - { - if ( !areStacksCompatible( stackOnMouse, getSlotStack( oldSelectedSlot ) ) ) - { - shouldClick = false; - } - - firstSlot = oldSelectedSlot; - } - - if ( Mouse.isButtonDown( 1 ) && ( oldSelectedSlot == null ) && !firstSlotClicked && ( firstSlot == null ) ) - { - shouldClick = false; - } - - if ( selectedSlot == null ) - { - oldSelectedSlot = selectedSlot; - - if ( ( firstSlot != null ) && !firstSlotClicked ) - { - firstSlotClicked = true; - disableRMBDragWithID( currentScreen ); - firstSlot = null; - } - - return; - } - - if ( Debug == 1 ) - { - Logger.Log( new StringBuilder().append( "You have selected a new slot, it's slot number is " ).append( getSlotNumber( selectedSlot ) ).toString() ); - } - - 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 ( ( stackOnMouse != null ) && areStacksCompatible( stackOnMouse, targetStack ) ) - { - if ( ( firstSlot != null ) && !firstSlotClicked ) - { - firstSlotClicked = true; - disableRMBDragWithID( currentScreen ); - firstSlot = null; - } - else - { - shouldClick = false; - disableRMBDragWithID( currentScreen ); - } - - clickSlot( currentScreen, selectedSlot, 1, false ); - } - - } - } - else if ( Mouse.isButtonDown( 0 ) ) - { // Left mouse button - if ( stackOnMouse != null ) - { - if ( Main.LMBTweakWithItem == 1 ) - { - if ( ( targetStack != null ) && areStacksCompatible( stackOnMouse, targetStack ) ) - { - - if ( shiftIsDown ) - { // If shift is down, we just shift-click the slot and the item gets moved into another inventory. - clickSlot( currentScreen, selectedSlot, 0, true ); - } - else - { // If shift is not down, we need to merge the item stack on the mouse with the one in the slot. - if ( ( getItemStackSize( stackOnMouse ) + getItemStackSize( targetStack ) ) <= getMaxItemStackSize( stackOnMouse ) ) - { - clickSlot( currentScreen, selectedSlot, 0, false ); // We need to click on the slot so that our item stack gets merged with - clickSlot( currentScreen, selectedSlot, 0, false ); // it, and then click again to return the stack to the mouse. - } - } - } - - } - } - else if ( Main.LMBTweakWithoutItem == 1 ) - { - if ( targetStack != null ) - { - if ( shiftIsDown ) - { - clickSlot( currentScreen, selectedSlot, 0, true ); - } - } - } - } - - oldSelectedSlot = selectedSlot; - } - - if ( ( wheel != 0 ) && ( selectedSlot != null ) ) - { - int numItemsToMove = Math.abs( wheel ); - if ( Debug == 1 ) - { - Logger.Log("numItemsToMove: " + numItemsToMove); - } - - if ( slotCount > Constants.INVENTORY_SIZE ) - { - ItemStack originalStack = getSlotStack( selectedSlot ); - boolean isCraftingOutput = isCraftingOutputSlot( currentScreen, selectedSlot ); - - if ( ( originalStack != null ) - && ( ( stackOnMouse == null ) || ( isCraftingOutput ? areStacksCompatible( originalStack, stackOnMouse ) : !areStacksCompatible( originalStack, stackOnMouse ) ) ) ) - { - do - { - Slot applicableSlot = null; - - int slotCounter = 0; - int countUntil = slotCount - - Constants.INVENTORY_SIZE; - if ( getSlotNumber( selectedSlot ) < countUntil ) - { - slotCounter = countUntil; - countUntil = slotCount; - } - - if ( ( wheel < 0 ) || ( Main.WheelSearchOrder == 0 ) ) - { - for ( int i = slotCounter; i < countUntil; i++ ) - { - Slot sl = getSlotWithID( currentScreen, i ); - ItemStack stackSl = getSlotStack( sl ); - - if ( stackSl == null ) - { - if ( ( applicableSlot == null ) - && ( wheel < 0 ) - && sl.isItemValid( originalStack ) - && !isCraftingOutputSlot( currentScreen, sl ) ) - { - applicableSlot = sl; - } - } - else if ( areStacksCompatible( originalStack, stackSl ) ) - { - if ( ( wheel < 0 ) - && ( stackSl.stackSize < stackSl.getMaxStackSize() ) ) - { - applicableSlot = sl; - break; - } - else if ( wheel > 0 ) - { - applicableSlot = sl; - break; - } - } - } - } - else - { - for ( int i = countUntil - 1; i >= slotCounter; i-- ) - { - Slot sl = getSlotWithID( currentScreen, i ); - ItemStack stackSl = getSlotStack( sl ); - - if ( stackSl == null ) - { - if ( ( applicableSlot == null ) - && ( wheel < 0 ) - && sl.isItemValid( originalStack ) ) - { - applicableSlot = sl; - } - } - else if ( areStacksCompatible( originalStack, stackSl ) ) - { - if ( ( wheel < 0 ) - && ( stackSl.stackSize < stackSl.getMaxStackSize() ) ) - { - applicableSlot = sl; - break; - } - else if ( wheel > 0 ) - { - applicableSlot = sl; - break; - } - } - } - } - - if ( isCraftingOutput ) - { - if ( wheel < 0 ) - { - boolean mouseWasEmpty = stackOnMouse == null; - - for ( int i = 0; i < numItemsToMove; i++ ) - { - clickSlot( currentScreen, selectedSlot, 0, false ); - } - - if ( ( applicableSlot != null ) - && mouseWasEmpty ) - { - clickSlot( currentScreen, applicableSlot, 0, false ); - } - } - - break; - } - - if ( applicableSlot != null ) - { - Slot slotTo = ( wheel < 0 ) ? applicableSlot : selectedSlot; - Slot slotFrom = ( wheel < 0 ) ? selectedSlot : applicableSlot; - ItemStack stackTo = ( getSlotStack( slotTo ) != null ) ? copyItemStack( getSlotStack( slotTo ) ) : null; - ItemStack stackFrom = copyItemStack( getSlotStack( slotFrom ) ); - - if ( wheel < 0 ) - { - numItemsToMove = Math.min(numItemsToMove, getItemStackSize(stackFrom)); - - if ( ( stackTo != null ) - && ( ( getMaxItemStackSize( stackTo ) - getItemStackSize( stackTo ) ) <= numItemsToMove ) ) - { - clickSlot( currentScreen, slotFrom, 0, false ); - clickSlot( currentScreen, slotTo, 0, false ); - clickSlot( currentScreen, slotFrom, 0, false ); - - numItemsToMove -= getMaxItemStackSize( stackTo ) - getItemStackSize( stackTo ); - } - else - { - clickSlot( currentScreen, slotFrom, 0, false ); - - if ( getItemStackSize( stackFrom ) <= numItemsToMove ) - { - clickSlot( currentScreen, slotTo, 0, false ); - } - else - { - for ( int i = 0; i < numItemsToMove; i++ ) - { - clickSlot( currentScreen, slotTo, 1, false ); - } - } - - clickSlot( currentScreen, slotFrom, 0, false ); - - numItemsToMove = 0; - } - } - else - { - if ( ( getMaxItemStackSize( stackTo ) - getItemStackSize( stackTo ) ) <= numItemsToMove ) - { - clickSlot( currentScreen, slotFrom, 0, false ); - clickSlot( currentScreen, slotTo, 0, false ); - clickSlot( currentScreen, slotFrom, 0, false ); - } - else - { - clickSlot( currentScreen, slotFrom, 0, false ); - - if ( getItemStackSize( stackFrom ) <= numItemsToMove ) - { - clickSlot( currentScreen, slotTo, 0, false ); - numItemsToMove -= getMaxItemStackSize( stackFrom ); - } - else - { - for ( int i = 0; i < numItemsToMove; i++ ) - { - clickSlot( currentScreen, slotTo, 1, false ); - } - - numItemsToMove = 0; - } - - clickSlot( currentScreen, slotFrom, 0, false ); - } - - if ( getMaxItemStackSize( stackTo ) == getMaxItemStackSize( stackTo ) ) - { - numItemsToMove = 0; - } - } - } - else - { - break; - } - } - while ( numItemsToMove != 0 ); - } - } - } - - oldStackOnMouse = stackOnMouse; - } - - public static int getGuiContainerID( GuiScreen currentScreen ) - { - int containerID = ModCompatibility.getModGuiContainerID( currentScreen ); // This first because a lot of mod containers extend the vanilla Minecraft - // one. - if ( containerID == Constants.NOTGUICONTAINER ) - return ( isGuiContainer( currentScreen ) && isValidGuiContainer( currentScreen ) ) ? Constants.MINECRAFT : Constants.NOTGUICONTAINER; - else - return containerID; - } - - public static Object getContainerWithID( GuiScreen currentScreen ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return getContainer( asGuiContainer( currentScreen ) ); - else - return ModCompatibility.getModContainer( guiContainerID, currentScreen ); - } - - public static int getSlotCountWithID( GuiScreen currentScreen ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return getSlots( asContainer( container ) ).size(); - else - return ModCompatibility.getModSlotCount( guiContainerID, currentScreen, container ); - } - - public static String getGuiContainerNameFromID( GuiScreen currentScreen ) - { - switch ( guiContainerID ) - { - case Constants.NOTASSIGNED: - return "Unknown"; - case Constants.NOTGUICONTAINER: - return "Wrong"; - case Constants.MINECRAFT: - return "Vanilla Minecraft"; - - default: - return ModCompatibility.getModNameFromModGuiContainerID( guiContainerID, currentScreen ); - } - } - - public static boolean isDisabledForThisContainer( GuiScreen currentScreen ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return false; - else - return ModCompatibility.isDisabledForThisModContainer( guiContainerID, currentScreen, container ); - } - - public static boolean isWheelDisabledForThisContainer( GuiScreen currentScreen ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return false; - else - return ModCompatibility.isWheelDisabledForThisModContainer( guiContainerID, currentScreen ); - } - - public static Slot getSelectedSlotWithID( GuiScreen currentScreen, int slotCount ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return getSelectedSlot( asGuiContainer( currentScreen ), asContainer( container ), slotCount ); - else - return ModCompatibility.getModSelectedSlot( guiContainerID, currentScreen, container, slotCount ); - } - - public static void clickSlot( GuiScreen currentScreen, Slot targetSlot, int mouseButton, boolean shiftPressed ) - { - if ( guiContainerID == Constants.MINECRAFT ) - { - windowClick( getWindowId( asContainer( container ) ), getSlotNumber( targetSlot ), mouseButton, shiftPressed ? 1 : 0 ); - } - else - { - ModCompatibility.modClickSlot( guiContainerID, currentScreen, container, targetSlot, mouseButton, shiftPressed ); - } - } - - public static boolean isCraftingOutputSlot( GuiScreen currentScreen, Slot targetSlot ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return isVanillaCraftingOutputSlot( asContainer( container ), targetSlot ); - else - return ModCompatibility.modIsCraftingOutputSlot( guiContainerID, currentScreen, container, targetSlot ); - } - - public static Slot getSlotWithID( GuiScreen currentScreen, int slotNumber ) - { - if ( guiContainerID == Constants.MINECRAFT ) - return getSlot( asContainer( container ), slotNumber ); - else - return ModCompatibility.modGetSlot( guiContainerID, currentScreen, container, slotNumber ); - } - - public static void disableRMBDragWithID( GuiScreen currentScreen ) - { - if ( guiContainerID == Constants.MINECRAFT ) - { - disableVanillaRMBDrag( asGuiContainer( currentScreen ) ); - - if ( shouldClick ) - { - clickSlot( currentScreen, firstSlot, 1, false ); - } - } - else - { - ModCompatibility.disableRMBDragIfRequired( guiContainerID, currentScreen, container, firstSlot, shouldClick ); - } - } -} diff --git a/src/main/java/yalter/mousetweaks/ModCompatibility.java b/src/main/java/yalter/mousetweaks/ModCompatibility.java deleted file mode 100644 index acc5b85..0000000 --- a/src/main/java/yalter/mousetweaks/ModCompatibility.java +++ /dev/null @@ -1,242 +0,0 @@ -package yalter.mousetweaks; - -import yalter.mousetweaks.api.IMTModGuiContainer; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.inventory.Slot; - -public class ModCompatibility extends DeobfuscationLayer -{ - - private static boolean initialized = false; - - private static boolean forestryInstalled = false; - private static boolean codechickencoreInstalled = false; - private static boolean NEIInstalled = false; - - public static void initialize() - { - if ( initialized ) - return; - - forestryInstalled = Reflection.reflectForestry(); - if ( forestryInstalled ) - { - Logger.Log( "Successfully reflected Forestry!" ); - } - - codechickencoreInstalled = Reflection.reflectCodeChickenCore(); - if ( codechickencoreInstalled ) - { - Logger.Log( "Successfully reflected CodeChickenCore!" ); - } - - NEIInstalled = Reflection.reflectNEI(); - if ( NEIInstalled ) - { - Logger.Log( "Successfully reflected NEI!" ); - } - - initialized = true; - } - - public static int getModGuiContainerID( GuiScreen guiScreen ) - { - if ( guiScreen instanceof IMTModGuiContainer ) - return Constants.MTMODGUICONTAINER; - - if ( forestryInstalled && Reflection.forestry.isInstance( "GuiForestry", guiScreen ) ) - return Constants.FORESTRY; - - if ( codechickencoreInstalled && Reflection.codechickencore.isInstance( "GuiContainerWidget", guiScreen ) ) - return Constants.CODECHICKENCORE; - - if ( ( NEIInstalled - && ( Reflection.NEI.isInstance( "GuiRecipe", guiScreen ) ) ) - || Reflection.NEI.isInstance( "GuiEnchantmentModifier", guiScreen ) ) - return Constants.NEI; - - return Constants.NOTGUICONTAINER; - } - - public static String getModNameFromModGuiContainerID( int id, GuiScreen guiScreen ) - { - switch ( id ) - { - case Constants.MTMODGUICONTAINER: - return ((IMTModGuiContainer) guiScreen).getModName(); - - case Constants.FORESTRY: - return "Forestry"; - case Constants.CODECHICKENCORE: - return "CodeChickenCore"; - case Constants.NEI: - return "NotEnoughItems"; - - default: - return "Unknown"; - } - } - - public static boolean isWheelDisabledForThisModContainer( int modGuiContainerID, GuiScreen guiScreen ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return ((IMTModGuiContainer) guiScreen).isWheelTweakDisabled(); - - default: - return false; - } - } - - public static Slot modGetSlot( int modGuiContainerID, GuiScreen guiScreen, Object modContainer, int slotNumber ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return asSlot( ((IMTModGuiContainer) guiScreen).getModSlot( modContainer, slotNumber ) ); - - case Constants.FORESTRY: - return getSlot( asContainer( modContainer ), slotNumber ); - case Constants.NEI: - return getSlot( asContainer( modContainer ), slotNumber ); - - default: - return null; - } - } - - public static boolean modIsCraftingOutputSlot( int modGuiContainerID, GuiScreen guiScreen, Object modContainer, Slot selectedSlot ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return ((IMTModGuiContainer) guiScreen).isCraftingOutputSlot( modContainer, selectedSlot ); - - case Constants.FORESTRY: - return ( Reflection.is( modContainer, "ContainerSqueezer" ) && ( ( getSlotNumber( selectedSlot ) == 9 ) || ( getSlotNumber( selectedSlot ) == 11 ) ) ) - || ( Reflection.is( modContainer, "ContainerMoistener" ) && ( getSlotNumber( selectedSlot ) == 9 ) ) - || ( Reflection.is( modContainer, "ContainerCentrifuge" ) && ( ( getSlotNumber( selectedSlot ) >= 1 ) && ( getSlotNumber( selectedSlot ) <= 9 ) ) ) - || ( Reflection.is( modContainer, "ContainerCarpenter" ) && ( getSlotNumber( selectedSlot ) == 21 ) ) - || ( Reflection.is( modContainer, "ContainerFabricator" ) && ( getSlotNumber( selectedSlot ) == 20 ) ) - || ( Reflection.is( modContainer, "ContainerWorktable" ) && ( getSlotNumber( selectedSlot ) == 27 ) ); - - default: - return false; - } - } - - public static void modClickSlot( int modGuiContainerID, GuiScreen guiScreen, Object modContainer, Slot targetSlot, int mouseButton, boolean shiftPressed ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - ((IMTModGuiContainer) guiScreen).clickModSlot( modContainer, targetSlot, mouseButton, shiftPressed ); - return; - - case Constants.FORESTRY: - Reflection.forestry.invokeMethod( guiScreen, "handleMouseClick", targetSlot, 0, mouseButton, shiftPressed ? 1 : 0 ); - return; - - case Constants.NEI: - windowClick( getWindowId( asContainer( modContainer ) ), getSlotNumber( targetSlot ), mouseButton, shiftPressed ? 1 : 0 ); - return; - - default: - return; - } - } - - public static Slot getModSelectedSlot( int modGuiContainerID, GuiScreen guiScreen, Object modContainer, int slotCount ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return asSlot( ((IMTModGuiContainer) guiScreen).getModSelectedSlot( modContainer, slotCount ) ); - - case Constants.FORESTRY: - return asSlot( Reflection.forestry.invokeMethod( guiScreen, "getSlotAtPosition", getRequiredMouseX(), getRequiredMouseY() ) ); - case Constants.NEI: - return getSelectedSlot( asGuiContainer( guiScreen ), asContainer( modContainer ), slotCount ); - - default: - return null; - } - } - - public static int getModSlotCount( int modGuiContainerID, GuiScreen guiScreen, Object modContainer ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return ((IMTModGuiContainer) guiScreen).getModSlotCount( modContainer ); - - case Constants.FORESTRY: - return getSlots( asContainer( modContainer ) ).size(); - case Constants.NEI: - return getSlots( asContainer( modContainer ) ).size(); - - default: - return 0; - } - } - - public static Object getModContainer( int modGuiContainerID, GuiScreen guiScreen ) - { - switch ( modGuiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return ((IMTModGuiContainer) guiScreen).getModContainer(); - - case Constants.FORESTRY: - return Reflection.forestry.getFieldValue( "inventorySlots", guiScreen ); - case Constants.NEI: - return getContainer( asGuiContainer( guiScreen ) ); - - default: - return null; - } - } - - public static void disableRMBDragIfRequired( int guiContainerID, GuiScreen guiScreen, Object modContainer, Slot firstSlot, boolean shouldClick ) - { - switch ( guiContainerID ) - { - case Constants.MTMODGUICONTAINER: - ((IMTModGuiContainer) guiScreen).disableRMBDragIfRequired( modContainer, firstSlot, shouldClick ); - - return; - - case Constants.NEI: - disableVanillaRMBDrag( asGuiContainer( guiScreen ) ); - - if ( shouldClick ) - { - modClickSlot( guiContainerID, guiScreen, modContainer, firstSlot, 1, false ); - } - - return; - - default: - return; - } - } - - public static boolean isDisabledForThisModContainer( int guiContainerID, GuiScreen guiScreen, Object container ) - { - switch ( guiContainerID ) - { - case Constants.MTMODGUICONTAINER: - return ((IMTModGuiContainer) guiScreen).isMouseTweaksDisabled(); - - case Constants.CODECHICKENCORE: - return true; - case Constants.NEI: - return !Reflection.NEI.isInstance( "GuiEnchantmentModifier", guiScreen ); - - default: - return false; - } - } - -} diff --git a/src/main/java/yalter/mousetweaks/ProfilerCustom.java b/src/main/java/yalter/mousetweaks/ProfilerCustom.java deleted file mode 100644 index 71ddcf0..0000000 --- a/src/main/java/yalter/mousetweaks/ProfilerCustom.java +++ /dev/null @@ -1,33 +0,0 @@ -package yalter.mousetweaks; - -import net.minecraft.client.Minecraft; -import net.minecraft.profiler.Profiler; - -public class ProfilerCustom extends Profiler -{ - private Minecraft minecraft; - - public ProfilerCustom() - { - minecraft = Minecraft.getMinecraft(); - } - - @Override - public void startSection( String sectionName ) - { - if ( "gameRenderer".equals( sectionName ) ) - { - Main.onUpdateInGame(); - } - - super.startSection( sectionName ); - - if ( Main.optifine ) - { - if ( !Reflection.gameSettings.setFieldValue( minecraft.gameSettings, "ofProfiler", true ) ) - { - Main.optifine = false; - } - } - } -} diff --git a/src/main/java/yalter/mousetweaks/Reflection.java b/src/main/java/yalter/mousetweaks/Reflection.java deleted file mode 100644 index a407c07..0000000 --- a/src/main/java/yalter/mousetweaks/Reflection.java +++ /dev/null @@ -1,467 +0,0 @@ -package yalter.mousetweaks; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.settings.GameSettings; -import net.minecraft.client.gui.inventory.GuiContainer; -import net.minecraft.inventory.Slot; -import net.minecraft.client.renderer.Tessellator; - -public class Reflection -{ - public static ReflectionCache guiContainerClass; - public static ReflectionCache liteLoaderClass; - public static ReflectionCache liteModMouseTweaks; - public static ReflectionCache gameSettings; - public static ReflectionCache minecraft; - - public static ReflectionCache forestry; - public static ReflectionCache codechickencore; - public static ReflectionCache NEI; - - private static boolean isObfuscated = false; - private static boolean checkObfuscation = true; - - public static boolean reflectGuiContainer() - { - guiContainerClass = new ReflectionCache(); - guiContainerClass.storeClass( "GuiContainer", GuiContainer.class ); - - Method isMouseOverSlot = getMethod( GuiContainer.class, - getObfuscatedName( "isMouseOverSlot", Constants.ISMOUSEOVERSLOT_FORGE_NAME, Constants.ISMOUSEOVERSLOT_NAME ), Slot.class, int.class, - int.class ); - - if ( isMouseOverSlot == null ) - { - Logger.Log( "Failed to get isMouseOverSlot method, quitting" ); - return false; - } - - guiContainerClass.storeMethod( "isMouseOverSlot", isMouseOverSlot ); - - Field field; - field = getField( GuiContainer.class, - getObfuscatedName( Constants.FIELDE_FORGE_NAME, Constants.FIELDE_FORGE_NAME, Constants.FIELDE_NAME ) ); - - if ( field == null ) - { - Logger.Log( "Failed to retrieve the E field, disabling RMBTweak" ); - Main.DisableRMBTweak = true; - } - else - { - guiContainerClass.storeField( Constants.FIELDE_FORGE_NAME, field ); - - field = getField( GuiContainer.class, - getObfuscatedName( Constants.FIELDq_FORGE_NAME, Constants.FIELDq_FORGE_NAME, Constants.FIELDq_NAME ) ); - - if ( field == null ) - { - Logger.Log( "Failed to retreive the q field, disabling RMBTweak" ); - Main.DisableRMBTweak = true; - } - else - { - guiContainerClass.storeField( Constants.FIELDq_FORGE_NAME, field ); - } - } - - return true; - } - - public static boolean reflectLiteLoader() - { - liteLoaderClass = new ReflectionCache(); - - Class lL = getClass( "com.mumfrey.liteloader.core.LiteLoader" ); - if ( lL != null ) - { - liteLoaderClass.storeClass( "LiteLoader", lL ); - - liteLoaderClass.available = true; - - Class llEvents = getClass( "com.mumfrey.liteloader.core.Events" ); - if ( llEvents != null ) - { - liteLoaderClass.storeClass( "Events", llEvents ); - - Field llEventsField = getField( lL, "events" ); - if ( llEventsField != null ) - { - liteLoaderClass.storeField( "events", llEventsField ); - - Class lM = getClass( "com.mumfrey.liteloader.LiteMod" ); - if ( lM != null ) - { - liteLoaderClass.storeClass( "LiteMod", lM ); - - Method addListener = getMethod( llEvents, "addListener", lM ); - if ( addListener != null ) - { - liteLoaderClass.storeMethod( "addListener", addListener ); - - Method getInstance = getMethod( lL, "getInstance" ); - if ( getInstance != null ) - { - liteLoaderClass.storeMethod( "getInstance", getInstance ); - - Object liteLoaderInstance = liteLoaderClass.invokeStaticMethod( "LiteLoader", "getInstance" ); - if ( liteLoaderInstance != null ) - { - Object llEventsInstance = liteLoaderClass.getFieldValue( "events", liteLoaderInstance ); - if ( llEventsInstance != null ) - { - Class mouseTweaks = getClass( "yalter.mousetweaks.LiteModMouseTweaks" ); - if ( mouseTweaks != null ) - { - liteModMouseTweaks = new ReflectionCache(); - liteModMouseTweaks.storeClass( "LiteModMouseTweaks", mouseTweaks ); - - Method getLMMTInstance = getMethod( mouseTweaks, "getInstance" ); - if ( getLMMTInstance != null ) - { - liteModMouseTweaks.storeMethod( "getInstance", getLMMTInstance ); - - Object LMMTInstance = liteModMouseTweaks.invokeStaticMethod( "LiteModMouseTweaks", "getInstance" ); - if ( LMMTInstance != null ) - { - liteLoaderClass.invokeMethod( llEventsInstance, "addListener", LMMTInstance ); - - liteLoaderClass.compatible = true; - Logger.Log( "LiteLoader is installed and compatible" ); - - return true; - } - else - { - Logger.Log( "Could not get instance from getInstance method of LiteModMouseTweaks class! Have you modified the class?" ); - } - } - else - { - Logger.Log( "Could not get getInstance method from LiteModMouseTweaks class! Have you modified the class?" ); - } - } - else - { - Logger.Log( "Could not get LiteModMouseTweaks class! Have you deleted it accidentally?" ); - } - } - } - else - { - Logger.Log( "Failed to retrieve LiteLoader instance!" ); - } - } - } - } - } - } - } - - Logger.Log( ( !liteLoaderClass.available ) ? "LiteLoader is not installed" : "LiteLoader is installed, but not compatible" ); - return false; - } - - public static boolean reflectOptifine() - { - gameSettings = new ReflectionCache(); - gameSettings.storeClass( "GameSetting", GameSettings.class ); - - Field optifineProfilerEnabled = getField( GameSettings.class, "ofProfiler" ); - if ( optifineProfilerEnabled != null ) - { - gameSettings.storeField( "ofProfiler", optifineProfilerEnabled ); - - Logger.Log( "Optifine is installed" ); - return true; - } - - Logger.Log( "Optifine is not installed" ); - return false; - } - - public static boolean reflectMinecraft() - { - minecraft = new ReflectionCache(); - minecraft.storeClass( "Minecraft", Minecraft.class ); - - Field profilerField = getFinalField( Minecraft.class, getObfuscatedName( "mcProfiler", Constants.MCPROFILER_FORGE_NAME, Constants.MCPROFILER_NAME ) ); - if ( profilerField != null ) - { - minecraft.storeField( "mcProfiler", profilerField ); - return true; - } - - Logger.Log( "Failed to get the Minecraft profiler field!" ); - return false; - } - - public static boolean replaceProfiler() - { - return minecraft.setFieldValue( Minecraft.getMinecraft(), "mcProfiler", new ProfilerCustom() ); - } - - public static boolean reflectForestry() - { - forestry = new ReflectionCache(); - - Class guiForestryClass = getClass( "forestry.core.gui.GuiForestry" ); - if ( guiForestryClass != null ) - { - forestry.storeClass( "GuiForestry", guiForestryClass ); - forestry.available = true; - - Field inventorySlotsField = getField( guiForestryClass, "inventorySlots" ); - if ( inventorySlotsField != null ) - { - forestry.storeField( "inventorySlots", inventorySlotsField ); - - Method getSlotAtPositionMethod = getMethod( guiForestryClass, "getSlotAtPosition", int.class, int.class ); - if ( getSlotAtPositionMethod != null ) - { - forestry.storeMethod( "getSlotAtPosition", getSlotAtPositionMethod ); - - Method handleMouseClickMethod = getMethod( guiForestryClass, "handleMouseClick", Slot.class, int.class, int.class, int.class ); - if ( handleMouseClickMethod != null ) - { - forestry.storeMethod( "handleMouseClick", handleMouseClickMethod ); - - forestry.compatible = true; - - // Class containerForestryClass = getClass( "forestry.core.gui.ContainerForestry" ); - // if ( containerForestryClass != null ) { - // forestry.storeClass( "ContainerForestry", containerForestryClass ); - // - // Field slotCountField = getField( containerForestryClass, "slotCount" ); - // if ( slotCountField != null ) { - // forestry.storeField( "slotCount", slotCountField ); - // - // forestry.compatible = true; - // } - // } - } - } - } - } - - return forestry.compatible; - } - - public static boolean reflectCodeChickenCore() - { - codechickencore = new ReflectionCache(); - - Class guiContainerWidgetClass = getClass( "codechicken.core.inventory.GuiContainerWidget" ); - if ( guiContainerWidgetClass != null ) - { - codechickencore.storeClass( "GuiContainerWidget", guiContainerWidgetClass ); - - codechickencore.available = true; - codechickencore.compatible = true; - } - - return codechickencore.compatible; - } - - public static boolean reflectNEI() - { - NEI = new ReflectionCache(); - - Class guiRecipeClass = getClass( "codechicken.nei.recipe.GuiRecipe" ); - if ( guiRecipeClass != null ) - { - NEI.storeClass( "GuiRecipe", guiRecipeClass ); - NEI.available = true; - - Class guiEnchantmentModifierClass = getClass( "codechicken.nei.GuiEnchantmentModifier" ); - if ( guiEnchantmentModifierClass != null ) - { - NEI.storeClass( "GuiEnchantmentModifier", guiEnchantmentModifierClass ); - - NEI.compatible = true; - } - } - - return NEI.compatible; - } - - public static boolean is( Object object, String name ) - { - return object.getClass().getSimpleName() == name; - } - - public static boolean doesClassExist( String name ) - { - Class clazz = getClass( name ); - return clazz != null; - } - - public static Class getClass( String name ) - { - try - { - return Class.forName( name ); - } - catch ( ClassNotFoundException e ) - { - ; - } - - return null; - } - - public static Field getField( Class clazz, String name ) - { - try - { - Field field = null; - - try - { - field = clazz.getField( name ); - } - catch ( Exception e ) - { - field = null; - } - - if ( field == null ) - { - field = clazz.getDeclaredField( name ); - } - - field.setAccessible( true ); - return field; - } - catch ( Exception e ) - { - if ( name != "ofProfiler" ) - { - Logger.Log( "Could not retrieve field \"" + name + "\" from class \"" + clazz.getName() - + "\"" ); - - if ( Main.Debug == 1 ) - { - e.printStackTrace(); - } - } - } - - return null; - } - - public static Field getFinalField( Class clazz, String name ) - { - try - { - Field field = null; - - try - { - field = clazz.getField( name ); - } - catch ( Exception e ) - { - field = null; - } - - if ( field == null ) - { - field = clazz.getDeclaredField( name ); - } - - Field modifiers = Field.class.getDeclaredField( "modifiers" ); - modifiers.setAccessible( true ); - - modifiers.set( field, field.getModifiers() & ~Modifier.FINAL ); - - field.setAccessible( true ); - return field; - } - catch ( Exception e ) - { - Logger.Log( "Could not retrieve field \"" + name + "\" from class \"" + clazz.getName() - + "\"" ); - - if ( Main.Debug == 1 ) - { - e.printStackTrace(); - } - } - - return null; - } - - public static Method getMethod( Class clazz, String name, Class... args ) - { - try - { - Method method = null; - - try - { - method = clazz.getMethod( name, args ); - } - catch ( Exception e ) - { - method = null; - } - - if ( method == null ) - { - if ( ( args != null ) & ( args.length != 0 ) ) - { - method = clazz.getDeclaredMethod( name, args ); - } - else - { - method = clazz.getDeclaredMethod( name, new Class[0] ); - } - } - - method.setAccessible( true ); - return method; - } - catch ( Exception e ) - { - Logger.Log( "Could not retrieve method \"" + name + "\" from class \"" + clazz.getName() - + "\"" ); - - if ( Main.Debug == 1 ) - { - e.printStackTrace(); - } - } - - return null; - } - - public static String methodToString( Method method ) - { - return Modifier.toString( method.getModifiers() ) + " " + ( ( method.getReturnType() != null ) ? method - .getReturnType().getName() : "void" ) + " " + method.getName(); - } - - public static String getObfuscatedName( String mcpName, String forgeName, String originalName ) - { - if (checkObfuscation) - { - checkObfuscation(); - } - - return isObfuscated ? ( Main.minecraftForge ? forgeName : originalName ) : mcpName; -} - - public static void checkObfuscation() - { - checkObfuscation = false; - - Logger.Log("Obfuscation check, please ignore the following isMouseOverSlot error (if it comes up)."); - isObfuscated = getMethod( GuiContainer.class, "isMouseOverSlot", Slot.class, int.class, int.class ) == null; - Logger.Log("Obfuscation check completed."); - } -} diff --git a/src/main/java/yalter/mousetweaks/ReflectionCache.java b/src/main/java/yalter/mousetweaks/ReflectionCache.java deleted file mode 100644 index 74e0382..0000000 --- a/src/main/java/yalter/mousetweaks/ReflectionCache.java +++ /dev/null @@ -1,125 +0,0 @@ -package yalter.mousetweaks; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.HashMap; - -public class ReflectionCache -{ - public boolean available = false; - public boolean compatible = false; - public HashMap classes = new HashMap(); - public HashMap methods = new HashMap(); - public HashMap fields = new HashMap(); - - public ReflectionCache() - { - } - - public Object getFieldValue( String name, Object obj ) - { - try - { - if ( fields.containsKey( name ) ) - { - Field field = ( Field ) fields.get( name ); - return field.get( obj ); - } - - Logger.Log( "No such field: " + name ); - } - catch ( Exception e ) - { - e.printStackTrace(); - Logger.Log( "Failed to get a value of field: " + name ); - } - - return null; - } - - public boolean setFieldValue( Object obj, String name, Object value ) - { - try - { - if ( fields.containsKey( name ) ) - { - Field field = ( Field ) fields.get( name ); - field.set( obj, value ); - - return true; - } - - Logger.Log( "No such field: " + name ); - } - catch ( Exception e ) - { - e.printStackTrace(); - Logger.Log( "Failed to set a value of field: " + name ); - } - - return false; - } - - public Object invokeMethod( Object obj, String name, Object... args ) - { - try - { - if ( methods.containsKey( name ) ) - { - Method method = ( Method ) methods.get( name ); - - if ( args != null ) - return method.invoke( obj, args ); - - return method.invoke( obj, new Object[0] ); - } - - Logger.Log( "No such method: " + name ); - } - catch ( Exception e ) - { - e.printStackTrace(); - Logger.Log( "Failed to invoke method: " + name ); - } - - return null; - } - - public Object invokeStaticMethod( String className, String name, Object... args ) - { - if ( classes.containsKey( className ) ) - { - Class clazz = ( Class ) classes.get( className ); - - return invokeMethod( clazz, name, args ); - } - - return null; - } - - public boolean isInstance( String className, Object obj ) - { - if ( classes.containsKey( className ) ) - { - Class clazz = ( Class ) classes.get( className ); - return clazz.isInstance( obj ); - } - - return false; - } - - public void storeClass( String name, Class clazz ) - { - classes.put( name, clazz ); - } - - public void storeMethod( String name, Method method ) - { - methods.put( name, method ); - } - - public void storeField( String name, Field field ) - { - fields.put( name, field ); - } -} diff --git a/src/main/java/yalter/mousetweaks/api/IMTModGuiContainer.java b/src/main/java/yalter/mousetweaks/api/IMTModGuiContainer.java deleted file mode 100644 index a310556..0000000 --- a/src/main/java/yalter/mousetweaks/api/IMTModGuiContainer.java +++ /dev/null @@ -1,94 +0,0 @@ -package yalter.mousetweaks.api; - -public interface IMTModGuiContainer { - - /** - * Returns the API version. - * - * @return API version. Should return 1 for this version. - */ - public int getAPIVersion(); - - /** - * Returns a mod name to use in log messages. - * - * @return The name of the mod. - */ - public String getModName(); - - /** - * Returns, whether or not Mouse Tweaks should be disabled for this GuiContainer. - * - * @return True, if Mouse Tweaks should be disabled, false otherwise. - */ - public boolean isMouseTweaksDisabled(); - - /** - * Returns, whether or not wheelTweak should be disabled for this GuiContainer. - * - * @return True, if wheelTweak should be disabled, false otherwise. - */ - public boolean isWheelTweakDisabled(); - - /** - * Returns, whether or not the given slot is a crafting output slot. - * - * @param modContainer The container of the given slot. - * @param slot The slot to check. - * @return True, if the given slot is a crafting output slot, false otherwise. - */ - public boolean isCraftingOutputSlot(Object modContainer, Object slot); - - /** - * Returns the container of this GuiContainer. If multiple containers are used, returns the one that is used at the moment of the method execution. - * - * @return The container that is currently used. - */ - public Object getModContainer(); - - /** - * Returns the number of slots present in the given container (for containers with variable slot number - the number of slots at the moment of the method execution). - * - * @param modContainer The container to retrieve the number of slots from. - * @return The number of slots in the given container. - */ - public int getModSlotCount(Object modContainer); - - /** - * Returns a slot from the container of this GuiContainer with the given slot number. - * - * @param modContainer The container from which to retrieve the slot. - * @param slotNumber The number of the slot to be retrieved. - * @return The slot from the given container with the given slot number. - */ - public Object getModSlot(Object modContainer, int slotNumber); - - /** - * Returns the currently selected slot (the one under the player's mouse cursor) of the given container, which has the given number of slots. - * - * @param modContainer The container to check. - * @param slotCount The number of slots that this container has. - * @return The slot that is currently selected. Null, if none are. - */ - public Object getModSelectedSlot(Object modContainer, int slotCount); - - /** - * The method that clicks the given slot of the given container with the given mouse button and shift key state. - * - * @param modContainer The container of the given slot. - * @param slot The slot to click. - * @param mouseButton The mouse button. 0 is the left one, 1 is the right one. - * @param shiftPressed True if the shift key is pressed, false otherwise. - */ - public void clickModSlot(Object modContainer, Object slot, int mouseButton, boolean shiftPressed); - - /** - * The method that disables the built-in RMB drag mechanic, if required, in this GuiContainer, with the given first slot in the given container to click if shouldClick is set to true (in vanilla circumstances - your RMB dragging mechanic might work differently). - * - * @param modContainer The container of the first slot. - * @param firstSlot The first slot. - * @param shouldClick True, if the first slot is to be clicked. - */ - public void disableRMBDragIfRequired(Object modContainer, Object firstSlot, boolean shouldClick); - -}