diff --git a/.gitignore b/.gitignore index 48c525b..be880e0 100644 --- a/.gitignore +++ b/.gitignore @@ -26,7 +26,12 @@ whitelist.json *.iml *.ipr *.iws -src/main/resources/mixins.*.json *.bat *.DS_Store !gradlew.bat +.factorypath +addon.local.gradle +addon.local.gradle.kts +addon.late.local.gradle +addon.late.local.gradle.kts +layout.json diff --git a/dependencies.gradle b/dependencies.gradle index 125fb75..cb6eb93 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,4 +1,4 @@ dependencies { - api("com.github.GTNewHorizons:GTNHLib:0.5.21:dev") - implementation('com.github.GTNewHorizons:NotEnoughItems:2.6.51-GTNH:dev') + api("com.github.GTNewHorizons:GTNHLib:0.9.27:dev") + implementation('com.github.GTNewHorizons:NotEnoughItems:2.8.70-GTNH:dev') } diff --git a/gradle.properties b/gradle.properties index a8e07f8..c146a46 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,7 +14,7 @@ modId = MouseTweaks modGroup = yalter.mousetweaks # Whether to use modGroup as the maven publishing group. -# Due to a history of using JitPack, the default is com.github.GTNewHorizons for all mods. +# When false, com.github.GTNewHorizons is used. useModGroupForPublishing = false # Updates your build.gradle and settings.gradle automatically whenever an update is available. @@ -39,10 +39,34 @@ remoteMappings = https\://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/co # `./gradlew runClient --username=AnotherPlayer`, or configuring this command in your IDE. developmentEnvironmentUserName = Developer -# Enables using modern Java syntax (up to version 17) via Jabel, while still targeting JVM 8. -# See https://github.com/bsideup/jabel for details on how this works. +# Enables modern Java syntax support. Valid values: +# - false: No modern syntax, Java 8 only +# - jabel: Jabel syntax-only support, compiles to J8 bytecode +# - jvmDowngrader: Full modern Java via JVM Downgrader (syntax + stdlib APIs) +# - modern: Native modern Java bytecode, no downgrading enableModernJavaSyntax = false +# If set, ignores the above setting and compiles with the given toolchain. This may cause unexpected issues, +# and should *not* be used in most situations. -1 disables this. +# forceToolchainVersion = -1 + +# Target JVM version for JVM Downgrader bytecode downgrading. +# Only used when enableModernJavaSyntax = jvmDowngrader +# downgradeTargetVersion = 8 + +# Comma-separated list of Java versions for multi-release jar support (JVM Downgrader only). +# Classes will be available in META-INF/versions/N/ for each version N in this list. +# Default: "21,25" (J25+ gets native classes, J21-24 gets partial downgrade, J8-20 gets full downgrade). +# jvmDowngraderMultiReleaseVersions = 21,25 + +# Specifies how JVM Downgrader API stubs are provided. Options: +# - shade: Shade minimized stubs into the jar +# - gtnhlib: GTNHLib provides stubs at runtime (adds version constraint) +# - external: Another dependency provides stubs (no constraint, no warning) +# - (empty): Warning reminding you to configure stubs +# Note: 'shade' option requires you to verify license compliance, see: https://github.com/unimined/JvmDowngrader/blob/main/LICENSE.md +# jvmDowngraderStubsProvider = + # Enables injecting missing generics into the decompiled source code for a better coding experience. # Turns most publicly visible List, Map, etc. into proper List, Map types. enableGenericInjection = false @@ -96,7 +120,9 @@ usesMixinDebug = false # Specify the location of your implementation of IMixinConfigPlugin. Leave it empty otherwise. mixinPlugin = -# Specify the package that contains all of your Mixins. You may only place Mixins in this package or the build will fail! +# Specify the package that contains all of your Mixins. The package must exist or +# the build will fail. If you have a package property defined in your mixins..json, +# it must match with this or the build will fail. mixinsPackage = # Specify the core mod entry class if you use a core mod. This class must implement IFMLLoadingPlugin! @@ -151,7 +177,7 @@ modrinthProjectId = # type can be one of [project, version], # and the name is the Modrinth project or version slug/id of the other mod. # Example: required-project:fplib;optional-project:gasstation;incompatible-project:gregtech -# Note: GTNH Mixins is automatically set as a required dependency if usesMixins = true +# Note: UniMixins is automatically set as a required dependency if usesMixins = true. modrinthRelations = # Publishing to CurseForge requires you to set the CURSEFORGE_TOKEN environment variable to one of your CurseForge API tokens. @@ -172,6 +198,12 @@ curseForgeRelations = # projects. New projects should not use this parameter. # customArchiveBaseName = +# Optional parameter to customize the default working directory used by the runClient* tasks. Relative to the project directory. +# runClientWorkingDirectory = run/client + +# Optional parameter to customize the default working directory used by the runServer* tasks. Relative to the project directory. +# runServerWorkingDirectory = run/server + # Optional parameter to have the build automatically fail if an illegal version is used. # This can be useful if you e.g. only want to allow versions in the form of '1.1.xxx'. # The check is ONLY performed if the version is a git tag. @@ -201,3 +233,6 @@ disableCheckstyle = true # This is meant to be set in $HOME/.gradle/gradle.properties. # ideaCheckSpotlessOnBuild = true +# Non-GTNH properties +org.gradle.configuration-cache = true +org.gradle.parallel = true diff --git a/gradle/gradle-daemon-jvm.properties b/gradle/gradle-daemon-jvm.properties new file mode 100644 index 0000000..67c3bd1 --- /dev/null +++ b/gradle/gradle-daemon-jvm.properties @@ -0,0 +1,12 @@ +#This file is generated by updateDaemonJvm +toolchainUrl.FREE_BSD.AARCH64=https\://api.foojay.io/disco/v3.0/ids/df211d3c3eefdc408b462041881bc575/redirect +toolchainUrl.FREE_BSD.X86_64=https\://api.foojay.io/disco/v3.0/ids/93aeea858331bd6bb00ba94759830234/redirect +toolchainUrl.LINUX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/df211d3c3eefdc408b462041881bc575/redirect +toolchainUrl.LINUX.X86_64=https\://api.foojay.io/disco/v3.0/ids/93aeea858331bd6bb00ba94759830234/redirect +toolchainUrl.MAC_OS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/3426ffcaa54c3f62406beb1f1ab8b179/redirect +toolchainUrl.MAC_OS.X86_64=https\://api.foojay.io/disco/v3.0/ids/f636c800fdb3f9ae33f019dfa048ba72/redirect +toolchainUrl.UNIX.AARCH64=https\://api.foojay.io/disco/v3.0/ids/df211d3c3eefdc408b462041881bc575/redirect +toolchainUrl.UNIX.X86_64=https\://api.foojay.io/disco/v3.0/ids/93aeea858331bd6bb00ba94759830234/redirect +toolchainUrl.WINDOWS.AARCH64=https\://api.foojay.io/disco/v3.0/ids/3cd7045fca9a72cd9bc7d14a385e594c/redirect +toolchainUrl.WINDOWS.X86_64=https\://api.foojay.io/disco/v3.0/ids/552c7bffe0370c66410a51c55985b511/redirect +toolchainVersion=25 diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 2c35211..61285a6 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 09523c0..37f78a6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.1-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index f5feea6..adff685 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ #!/bin/sh # -# Copyright © 2015-2021 the original authors. +# Copyright © 2015 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -86,8 +86,7 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s -' "$PWD" ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -115,7 +114,6 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -173,7 +171,6 @@ fi # For Cygwin or MSYS, switch paths to Windows format before running java if "$cygwin" || "$msys" ; then APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) JAVACMD=$( cygpath --unix "$JAVACMD" ) @@ -206,15 +203,14 @@ fi DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Collect all arguments for the java command: -# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, # and any embedded shellness will be escaped. # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be # treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ - -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" # Stop when "xargs" is not available. diff --git a/gradlew.bat b/gradlew.bat index 9d21a21..c4bdd3a 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -70,11 +70,10 @@ goto fail :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 %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell diff --git a/settings.gradle b/settings.gradle index aa612ad..d1af453 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,7 +17,7 @@ pluginManagement { } plugins { - id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.29' + id 'com.gtnewhorizons.gtnhsettingsconvention' version '2.0.20' } diff --git a/src/main/java/yalter/mousetweaks/Config.java b/src/main/java/yalter/mousetweaks/Config.java index 4643de6..4895166 100644 --- a/src/main/java/yalter/mousetweaks/Config.java +++ b/src/main/java/yalter/mousetweaks/Config.java @@ -3,15 +3,16 @@ package yalter.mousetweaks; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.util.stream.Stream; public class Config { public static void handleOldConfig(File file) { if (!file.exists()) return; - try { - if (Files.readAllLines(file.toPath()).stream().noneMatch(str -> str.contains("general"))) { - file.delete(); + try (Stream lines = Files.lines(file.toPath())) { + if (lines.noneMatch(str -> str.contains("general")) && !file.delete()) { + Constants.LOGGER.warn("Failed to delete outdated config file {}", file.getAbsolutePath()); } } catch (IOException e) { Constants.LOGGER.error(e); diff --git a/src/main/java/yalter/mousetweaks/Main.java b/src/main/java/yalter/mousetweaks/Main.java index 0924009..41ee79f 100644 --- a/src/main/java/yalter/mousetweaks/Main.java +++ b/src/main/java/yalter/mousetweaks/Main.java @@ -1,494 +1,22 @@ package yalter.mousetweaks; -import java.util.List; - -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 cpw.mods.fml.common.Loader; -import yalter.mousetweaks.config.MTConfig; - -public class Main extends DeobfuscationLayer { +public final class Main { public static boolean DisableRMBTweak = false; - 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 Main() {} public static void initialise() { - mc = Minecraft.getMinecraft(); - - ModCompatibility.initialize(); - Constants.LOGGER.info("Mouse Tweaks has been initialised."); + Runtime.initialise(); } 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; - guiContainerID = Constants.NOTASSIGNED; - } else { - if (guiContainerID == Constants.NOTASSIGNED) { - guiContainerID = getGuiContainerID(currentScreen); - } - onUpdateInGui(currentScreen); - } + Runtime.onUpdateInGame(); } 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 (Constants.DEV_ENV) { - Constants.LOGGER.debug( - 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 (isMouseWheelTransferActive() && !disableWheelForThisContainer) { - Mouse.getDWheel(); // reset the mouse wheel delta - } - } - - if (guiContainerID == Constants.NOTGUICONTAINER) return; - - if ((Main.DisableRMBTweak || (!MTConfig.RMBTweak)) && !MTConfig.LMBTweakWithoutItem - && !MTConfig.LMBTweakWithItem - && !isMouseWheelTransferActive()) { - return; - } - - if (disableForThisContainer) return; - - // 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). - int slotCount = getSlotCountWithID(currentScreen); - - if (slotCount == 0) { - // If there are no slots, then there is nothing to do. - return; - } - - int wheel = isMouseWheelTransferActive() && !disableWheelForThisContainer ? Mouse.getDWheel() : 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 (Constants.DEV_ENV) { - Constants.LOGGER - .debug("You have selected a new slot, it's slot number is " + getSlotNumber(selectedSlot)); - } - - boolean shiftIsDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT); - - if (Mouse.isButtonDown(1)) { // Right mouse button - if ((MTConfig.RMBTweak) && !Main.DisableRMBTweak) { - - if ((stackOnMouse != null) && areStacksCompatible(stackOnMouse, targetStack) - && !isCraftingOutputSlot(currentScreen, selectedSlot)) { - 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 (MTConfig.LMBTweakWithItem) { - if ((targetStack != null) && areStacksCompatible(stackOnMouse, targetStack)) { - - if (shiftIsDown) { // If shift is down, we just shift-click the slot and the item gets moved - // 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)) { - // We need to click on the slot so that our item stack gets merged with it, - // and then click again to return the stack to the mouse. - // However, if the slot is crafting output, then the item is added to the mouse - // stack - // on the first click and we don't need to click the second time. - clickSlot(currentScreen, selectedSlot, 0, false); - if (!isCraftingOutputSlot(currentScreen, selectedSlot)) - clickSlot(currentScreen, selectedSlot, 0, false); - } - } - } - } - } else if (MTConfig.LMBTweakWithoutItem) { - if (targetStack != null) { - if (shiftIsDown) { - clickSlot(currentScreen, selectedSlot, 0, true); - } - } - } - } - - oldSelectedSlot = selectedSlot; - } - - if ((wheel != 0) && (selectedSlot != null)) { - int numItemsToMove; - if (!ModCompatibility.isLwjgl3Loaded() && MTConfig.ScrollItemScaling == 0) { - numItemsToMove = Math.abs(wheel) / 120; - } else { - numItemsToMove = 1; - } - - if (Constants.DEV_ENV) { - Constants.LOGGER.debug("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; - - boolean pushItems = (wheel < 0); - if ((MTConfig.WheelScrollDirection == 2 || MTConfig.WheelScrollDirection == 3) - && otherInventoryIsAbove(selectedSlot, getSlots(asContainer(container)))) { - pushItems = !pushItems; - } - if (MTConfig.WheelScrollDirection == 1 || MTConfig.WheelScrollDirection == 3) { - pushItems = !pushItems; - } - - int slotCounter = 0; - int countUntil = slotCount - Constants.INVENTORY_SIZE; - if (getSlotNumber(selectedSlot) < countUntil) { - slotCounter = countUntil; - countUntil = slotCount; - } - - if (pushItems || (MTConfig.WheelSearchOrder == 0)) { - for (int i = slotCounter; i < countUntil; i++) { - Slot sl = getSlotWithID(currentScreen, i); - ItemStack stackSl = getSlotStack(sl); - - if (stackSl == null) { - if ((applicableSlot == null) && pushItems - && sl.isItemValid(originalStack) - && !isCraftingOutputSlot(currentScreen, sl)) { - applicableSlot = sl; - } - } else if (areStacksCompatible(originalStack, stackSl)) { - if (pushItems && (stackSl.stackSize < stackSl.getMaxStackSize())) { - applicableSlot = sl; - break; - } else if (!pushItems) { - 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) && pushItems && sl.isItemValid(originalStack)) { - applicableSlot = sl; - } - } else if (areStacksCompatible(originalStack, stackSl)) { - if (pushItems && (stackSl.stackSize < stackSl.getMaxStackSize())) { - applicableSlot = sl; - break; - } else if (!pushItems) { - applicableSlot = sl; - break; - } - } - } - } - - if (isCraftingOutput) { - if (pushItems) { - 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 = pushItems ? applicableSlot : selectedSlot; - Slot slotFrom = pushItems ? selectedSlot : applicableSlot; - ItemStack stackTo = (getSlotStack(slotTo) != null) ? copyItemStack(getSlotStack(slotTo)) - : null; - ItemStack stackFrom = copyItemStack(getSlotStack(slotFrom)); - - if (pushItems) { - 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; - } - - private static final boolean isNEIPresent = Loader.isModLoaded("NotEnoughItems"); - - private static boolean isMouseWheelTransferActive() { - if (isNEIPresent) { - return MTConfig.WheelTweak && !codechicken.nei.NEIClientConfig.isMouseScrollTransferEnabled(); - } - return MTConfig.WheelTweak; - } - - // Returns true if the other inventory is above the selected slot inventory. - // - // This is used for the inventory position aware scroll direction. To prevent any surprises, this should have the - // same logic for what constitutes the "other" inventory as elsewhere. - private static boolean otherInventoryIsAbove(Slot selectedSlot, List slots) { - boolean selectedIsInPlayerInventory = (getSlotInventory(selectedSlot) == getInventoryPlayer()); - - // Count the number of "other inventory" slots below and above the selected slot. - int otherInventorySlotsBelow = 0, otherInventorySlotsAbove = 0; - for (Slot slot : slots) { - if ((getSlotInventory(slot) == getInventoryPlayer()) != selectedIsInPlayerInventory) { - if (getSlotYPosition(slot) < getSlotYPosition(selectedSlot)) otherInventorySlotsAbove++; - else otherInventorySlotsBelow++; - } - } - - // If there are more "other inventory" slots above the selected slot, consider the other inventory above. - return (otherInventorySlotsAbove > otherInventorySlotsBelow); - } - - public static int getGuiContainerID(GuiScreen currentScreen) { - // This first because a lot of mod extend the vanilla Minecraft one. - int containerID = ModCompatibility.getModGuiContainerID(currentScreen); - 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); - } + Runtime.onUpdateInGui(currentScreen); } } diff --git a/src/main/java/yalter/mousetweaks/Reflection.java b/src/main/java/yalter/mousetweaks/Reflection.java index 5997143..bc8d99c 100644 --- a/src/main/java/yalter/mousetweaks/Reflection.java +++ b/src/main/java/yalter/mousetweaks/Reflection.java @@ -94,7 +94,7 @@ public class Reflection { } public static boolean is(Object object, String name) { - return object.getClass().getSimpleName() == name; + return (object != null) && (name != null) && name.equals(object.getClass().getSimpleName()); } public static boolean doesClassExist(String name) { @@ -105,9 +105,7 @@ public class Reflection { public static Class getClass(String name) { try { return Class.forName(name); - } catch (ClassNotFoundException e) { - ; - } + } catch (ClassNotFoundException ignored) {} return null; } @@ -179,10 +177,10 @@ public class Reflection { } if (method == null) { - if ((args != null) & (args.length != 0)) { + if ((args != null) && (args.length != 0)) { method = clazz.getDeclaredMethod(name, args); } else { - method = clazz.getDeclaredMethod(name, new Class[0]); + method = clazz.getDeclaredMethod(name, new Class[0]); } } diff --git a/src/main/java/yalter/mousetweaks/ReflectionCache.java b/src/main/java/yalter/mousetweaks/ReflectionCache.java index 4c9560a..b74981b 100644 --- a/src/main/java/yalter/mousetweaks/ReflectionCache.java +++ b/src/main/java/yalter/mousetweaks/ReflectionCache.java @@ -2,29 +2,30 @@ package yalter.mousetweaks; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.HashMap; + +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; 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<>(); + private final Object2ObjectMap> classes = new Object2ObjectOpenHashMap<>(); + private final Object2ObjectMap methods = new Object2ObjectOpenHashMap<>(); + private final Object2ObjectMap fields = new Object2ObjectOpenHashMap<>(); public ReflectionCache() {} public Object getFieldValue(String name, Object obj) { try { - if (fields.containsKey(name)) { - Field field = fields.get(name); + Field field = fields.get(name); + if (field != null) { return field.get(obj); } Constants.LOGGER.error("No such field: " + name); } catch (Exception e) { - e.printStackTrace(); - Constants.LOGGER.error("Failed to get a value of field: " + name); + Constants.LOGGER.error("Failed to get a value of field: " + name, e); } return null; @@ -32,8 +33,8 @@ public class ReflectionCache { public boolean setFieldValue(Object obj, String name, Object value) { try { - if (fields.containsKey(name)) { - Field field = fields.get(name); + Field field = fields.get(name); + if (field != null) { field.set(obj, value); return true; @@ -41,8 +42,7 @@ public class ReflectionCache { Constants.LOGGER.error("No such field: " + name); } catch (Exception e) { - e.printStackTrace(); - Constants.LOGGER.error("Failed to set a value of field: " + name); + Constants.LOGGER.error("Failed to set a value of field: " + name, e); } return false; @@ -50,8 +50,8 @@ public class ReflectionCache { public Object invokeMethod(Object obj, String name, Object... args) { try { - if (methods.containsKey(name)) { - Method method = methods.get(name); + Method method = methods.get(name); + if (method != null) { if (args != null) return method.invoke(obj, args); @@ -60,16 +60,15 @@ public class ReflectionCache { Constants.LOGGER.error("No such method: " + name); } catch (Exception e) { - e.printStackTrace(); - Constants.LOGGER.error("Failed to invoke method: " + name); + Constants.LOGGER.error("Failed to invoke method: " + name, e); } return null; } public Object invokeStaticMethod(String className, String name, Object... args) { - if (classes.containsKey(className)) { - Class clazz = classes.get(className); + Class clazz = classes.get(className); + if (clazz != null) { return invokeMethod(clazz, name, args); } @@ -78,8 +77,8 @@ public class ReflectionCache { } public boolean isInstance(String className, Object obj) { - if (classes.containsKey(className)) { - Class clazz = classes.get(className); + Class clazz = classes.get(className); + if (clazz != null) { return clazz.isInstance(obj); } diff --git a/src/main/java/yalter/mousetweaks/Runtime.java b/src/main/java/yalter/mousetweaks/Runtime.java new file mode 100644 index 0000000..e245d3f --- /dev/null +++ b/src/main/java/yalter/mousetweaks/Runtime.java @@ -0,0 +1,90 @@ +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.Mouse; + +import cpw.mods.fml.common.Loader; +import yalter.mousetweaks.config.MTConfig; +import yalter.mousetweaks.handlers.ClickHandler; +import yalter.mousetweaks.handlers.ContainerContext; +import yalter.mousetweaks.handlers.WheelHandler; + +public class Runtime extends DeobfuscationLayer { + + private static final ContainerContext context = new ContainerContext(); + private static final ClickHandler clickHandler = new ClickHandler(); + + private static final boolean isNEIPresent = Loader.isModLoaded("NotEnoughItems"); + + public static void initialise() { + mc = Minecraft.getMinecraft(); + + ModCompatibility.initialize(); + Constants.LOGGER.info("Mouse Tweaks has been initialised."); + } + + public static void onUpdateInGame() { + GuiScreen currentScreen = getCurrentScreen(); + if (currentScreen == null) { + context.reset(); + clickHandler.reset(); + return; + } + + onUpdateInGui(currentScreen); + } + + public static void onUpdateInGui(GuiScreen currentScreen) { + boolean wheelTransferActive = isMouseWheelTransferActive(); + + if (!context.refresh(currentScreen, wheelTransferActive)) return; + if (context.getCurrentGuiContainerID() == Constants.NOTGUICONTAINER) return; + if (!anyTweaksEnabled()) return; + if (context.isDisabledForCurrentContainer()) return; + + // 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). + int slotCount = context.getSlotCount(currentScreen); + + if (slotCount == 0) { + // If there are no slots, then there is nothing to do. + return; + } + + int wheel = wheelTransferActive && !context.isWheelDisabledForCurrentContainer() ? Mouse.getDWheel() : 0; + clickHandler.resetDragStateOnRightButtonRelease(); + + Slot selectedSlot = context.getSelectedSlot(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)); + + clickHandler.updateShouldClickFromRmbState(stackOnMouse); + + if (!clickHandler.handleSlotChangeAndButtons(currentScreen, selectedSlot, stackOnMouse, targetStack, context)) { + return; + } + + WheelHandler.handleWheelTweak(currentScreen, selectedSlot, stackOnMouse, slotCount, wheel, context); + clickHandler.setOldStackOnMouse(stackOnMouse); + } + + private static boolean anyTweaksEnabled() { + return (MTConfig.RMBTweak && !Main.DisableRMBTweak) || MTConfig.LMBTweakWithoutItem + || MTConfig.LMBTweakWithItem + || isMouseWheelTransferActive(); + } + + private static boolean isMouseWheelTransferActive() { + if (isNEIPresent) { + return MTConfig.WheelTweak && !codechicken.nei.NEIClientConfig.isMouseScrollTransferEnabled(); + } + return MTConfig.WheelTweak; + } +} diff --git a/src/main/java/yalter/mousetweaks/handlers/ClickHandler.java b/src/main/java/yalter/mousetweaks/handlers/ClickHandler.java new file mode 100644 index 0000000..ac9533c --- /dev/null +++ b/src/main/java/yalter/mousetweaks/handlers/ClickHandler.java @@ -0,0 +1,136 @@ +package yalter.mousetweaks.handlers; + +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 yalter.mousetweaks.Constants; +import yalter.mousetweaks.DeobfuscationLayer; +import yalter.mousetweaks.Main; +import yalter.mousetweaks.config.MTConfig; + +public class ClickHandler extends DeobfuscationLayer { + + private Slot oldSelectedSlot = null; + private Slot firstSlot = null; + private ItemStack oldStackOnMouse = null; + private boolean firstSlotClicked = false; + private boolean shouldClick = true; + + public void reset() { + oldSelectedSlot = null; + firstSlot = null; + oldStackOnMouse = null; + firstSlotClicked = false; + shouldClick = true; + } + + public void resetDragStateOnRightButtonRelease() { + if (!Mouse.isButtonDown(1)) { + firstSlotClicked = false; + firstSlot = null; + shouldClick = true; + } + } + + public void updateShouldClickFromRmbState(ItemStack stackOnMouse) { + // To correctly determine when and how the default RMB drag needs to be disabled, + // we need to observe the state transition from empty mouse stack. + if (Mouse.isButtonDown(1) && (oldStackOnMouse != stackOnMouse) && (oldStackOnMouse == null)) { + shouldClick = false; + } + } + + public boolean handleSlotChangeAndButtons(GuiScreen currentScreen, Slot selectedSlot, ItemStack stackOnMouse, + ItemStack targetStack, ContainerContext context) { + if (oldSelectedSlot == selectedSlot) return true; + + 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 = null; + + if ((firstSlot != null) && !firstSlotClicked) { + firstSlotClicked = true; + context.disableRMBDragIfRequired(currentScreen, firstSlot, shouldClick); + firstSlot = null; + } + + return false; + } + + if (Constants.DEV_ENV) { + Constants.LOGGER.debug("You have selected a new slot, its slot number is " + getSlotNumber(selectedSlot)); + } + + boolean shiftIsDown = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT); + + if (Mouse.isButtonDown(1)) { // Right mouse button + handleRMBTweak(currentScreen, selectedSlot, stackOnMouse, targetStack, context); + } else if (Mouse.isButtonDown(0)) { // Left mouse button + handleLMBTweak(currentScreen, selectedSlot, stackOnMouse, targetStack, shiftIsDown, context); + } + + oldSelectedSlot = selectedSlot; + return true; + } + + public void setOldStackOnMouse(ItemStack stackOnMouse) { + oldStackOnMouse = stackOnMouse; + } + + private void handleRMBTweak(GuiScreen currentScreen, Slot selectedSlot, ItemStack stackOnMouse, + ItemStack targetStack, ContainerContext context) { + if (!MTConfig.RMBTweak || Main.DisableRMBTweak) return; + + if ((stackOnMouse != null) && areStacksCompatible(stackOnMouse, targetStack) + && !context.isCraftingOutputSlot(currentScreen, selectedSlot)) { + if ((firstSlot != null) && !firstSlotClicked) { + firstSlotClicked = true; + context.disableRMBDragIfRequired(currentScreen, firstSlot, shouldClick); + firstSlot = null; + } else { + shouldClick = false; + Slot fallbackSlot = (firstSlot != null) ? firstSlot : selectedSlot; + context.disableRMBDragIfRequired(currentScreen, fallbackSlot, shouldClick); + } + + context.clickSlot(currentScreen, selectedSlot, 1, false); + } + } + + private void handleLMBTweak(GuiScreen currentScreen, Slot selectedSlot, ItemStack stackOnMouse, + ItemStack targetStack, boolean shiftIsDown, ContainerContext context) { + if (stackOnMouse != null) { + if (MTConfig.LMBTweakWithItem && (targetStack != null) && areStacksCompatible(stackOnMouse, targetStack)) { + boolean isCraftingOutput = context.isCraftingOutputSlot(currentScreen, selectedSlot); + if (shiftIsDown) { + if (!isCraftingOutput) { + context.clickSlot(currentScreen, selectedSlot, 0, true); + } + } else if ((getItemStackSize(stackOnMouse) + getItemStackSize(targetStack)) + <= getMaxItemStackSize(stackOnMouse)) { + context.clickSlot(currentScreen, selectedSlot, 0, false); + if (!isCraftingOutput) { + context.clickSlot(currentScreen, selectedSlot, 0, false); + } + } + } + } else if (MTConfig.LMBTweakWithoutItem && (targetStack != null) && shiftIsDown) { + context.clickSlot(currentScreen, selectedSlot, 0, true); + } + } +} diff --git a/src/main/java/yalter/mousetweaks/handlers/ContainerContext.java b/src/main/java/yalter/mousetweaks/handlers/ContainerContext.java new file mode 100644 index 0000000..c6e4599 --- /dev/null +++ b/src/main/java/yalter/mousetweaks/handlers/ContainerContext.java @@ -0,0 +1,160 @@ +package yalter.mousetweaks.handlers; + +import java.util.List; + +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.inventory.Slot; + +import org.lwjgl.input.Mouse; + +import yalter.mousetweaks.Constants; +import yalter.mousetweaks.DeobfuscationLayer; +import yalter.mousetweaks.ModCompatibility; + +public class ContainerContext extends DeobfuscationLayer { + + private GuiScreen oldGuiScreen = null; + private Object container = null; + private boolean disableForThisContainer = false; + private boolean disableWheelForThisContainer = false; + private int guiContainerID = Constants.NOTASSIGNED; + + public void reset() { + oldGuiScreen = null; + container = null; + disableForThisContainer = false; + disableWheelForThisContainer = false; + guiContainerID = Constants.NOTASSIGNED; + } + + public boolean refresh(GuiScreen currentScreen, boolean wheelTransferActive) { + if (oldGuiScreen == currentScreen) return true; + oldGuiScreen = currentScreen; + + // If we opened an inventory from another inventory (for example, NEI's options menu). + guiContainerID = resolveGuiContainerID(currentScreen); + if (guiContainerID == Constants.NOTGUICONTAINER) return false; + + container = getContainerWithID(currentScreen); + disableForThisContainer = isDisabledForThisContainer(currentScreen); + + if (Constants.DEV_ENV) { + Constants.LOGGER.debug( + 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(getSlotCount(currentScreen)).append(" slots!").toString()); + } + + disableWheelForThisContainer = isWheelDisabledForThisContainer(currentScreen); + if (wheelTransferActive && !disableWheelForThisContainer) { + Mouse.getDWheel(); // reset the mouse wheel delta + } + + return true; + } + + public int getCurrentGuiContainerID() { + return guiContainerID; + } + + public boolean isDisabledForCurrentContainer() { + return disableForThisContainer; + } + + public boolean isWheelDisabledForCurrentContainer() { + return disableWheelForThisContainer; + } + + public int getSlotCount(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) return getSlots(asContainer(container)).size(); + return ModCompatibility.getModSlotCount(guiContainerID, currentScreen, container); + } + + public Slot getSelectedSlot(GuiScreen currentScreen, int slotCount) { + if (guiContainerID == Constants.MINECRAFT) { + return getSelectedSlot(asGuiContainer(currentScreen), asContainer(container), slotCount); + } + return ModCompatibility.getModSelectedSlot(guiContainerID, currentScreen, container, slotCount); + } + + public Slot getSlot(GuiScreen currentScreen, int slotNumber) { + if (guiContainerID == Constants.MINECRAFT) return getSlot(asContainer(container), slotNumber); + return ModCompatibility.modGetSlot(guiContainerID, currentScreen, container, slotNumber); + } + + public 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 boolean isCraftingOutputSlot(GuiScreen currentScreen, Slot targetSlot) { + if (guiContainerID == Constants.MINECRAFT) { + return isVanillaCraftingOutputSlot(asContainer(container), targetSlot); + } + return ModCompatibility.modIsCraftingOutputSlot(guiContainerID, currentScreen, container, targetSlot); + } + + public void disableRMBDragIfRequired(GuiScreen currentScreen, Slot firstSlot, boolean shouldClick) { + if (guiContainerID == Constants.MINECRAFT) { + disableVanillaRMBDrag(asGuiContainer(currentScreen)); + + if (shouldClick) { + clickSlot(currentScreen, firstSlot, 1, false); + } + } else { + ModCompatibility.disableRMBDragIfRequired(guiContainerID, currentScreen, container, firstSlot, shouldClick); + } + } + + public List getContainerSlots() { + return getSlots(asContainer(container)); + } + + private int resolveGuiContainerID(GuiScreen currentScreen) { + // This first because a lot of mods extend the vanilla Minecraft one. + int containerID = ModCompatibility.getModGuiContainerID(currentScreen); + if (containerID == Constants.NOTGUICONTAINER) { + return (isGuiContainer(currentScreen) && isValidGuiContainer(currentScreen)) ? Constants.MINECRAFT + : Constants.NOTGUICONTAINER; + } + return containerID; + } + + private Object getContainerWithID(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) return getContainer(asGuiContainer(currentScreen)); + return ModCompatibility.getModContainer(guiContainerID, currentScreen); + } + + private 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); + } + } + + private boolean isDisabledForThisContainer(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) return false; + return ModCompatibility.isDisabledForThisModContainer(guiContainerID, currentScreen, container); + } + + private boolean isWheelDisabledForThisContainer(GuiScreen currentScreen) { + if (guiContainerID == Constants.MINECRAFT) return false; + return ModCompatibility.isWheelDisabledForThisModContainer(guiContainerID, currentScreen); + } +} diff --git a/src/main/java/yalter/mousetweaks/handlers/WheelHandler.java b/src/main/java/yalter/mousetweaks/handlers/WheelHandler.java new file mode 100644 index 0000000..45a1f4a --- /dev/null +++ b/src/main/java/yalter/mousetweaks/handlers/WheelHandler.java @@ -0,0 +1,207 @@ +package yalter.mousetweaks.handlers; + +import java.util.List; + +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; + +import yalter.mousetweaks.Constants; +import yalter.mousetweaks.DeobfuscationLayer; +import yalter.mousetweaks.ModCompatibility; +import yalter.mousetweaks.config.MTConfig; + +public final class WheelHandler extends DeobfuscationLayer { + + private WheelHandler() {} + + public static void handleWheelTweak(GuiScreen currentScreen, Slot selectedSlot, ItemStack stackOnMouse, + int slotCount, int wheel, ContainerContext context) { + if ((wheel == 0) || (selectedSlot == null)) return; + + int numItemsToMove = getNumItemsToMoveFromWheel(wheel); + if (Constants.DEV_ENV) { + Constants.LOGGER.debug("numItemsToMove: " + numItemsToMove); + } + + if (slotCount <= Constants.INVENTORY_SIZE) return; + + ItemStack originalStack = getSlotStack(selectedSlot); + boolean isCraftingOutput = context.isCraftingOutputSlot(currentScreen, selectedSlot); + if ((originalStack == null) + || ((stackOnMouse != null) && (isCraftingOutput ? !areStacksCompatible(originalStack, stackOnMouse) + : areStacksCompatible(originalStack, stackOnMouse)))) { + return; + } + + do { + Slot applicableSlot = null; + boolean pushItems = shouldPushItems(wheel, selectedSlot, context); + + int slotCounter = 0; + int countUntil = slotCount - Constants.INVENTORY_SIZE; + if (getSlotNumber(selectedSlot) < countUntil) { + slotCounter = countUntil; + countUntil = slotCount; + } + + if (pushItems || (MTConfig.WheelSearchOrder == 0)) { + for (int i = slotCounter; i < countUntil; i++) { + Slot sl = context.getSlot(currentScreen, i); + ItemStack stackSl = getSlotStack(sl); + + if (stackSl == null) { + if ((applicableSlot == null) && pushItems + && sl.isItemValid(originalStack) + && !context.isCraftingOutputSlot(currentScreen, sl)) { + applicableSlot = sl; + } + } else if (areStacksCompatible(originalStack, stackSl)) { + if (pushItems && (stackSl.stackSize < stackSl.getMaxStackSize())) { + applicableSlot = sl; + break; + } else if (!pushItems) { + applicableSlot = sl; + break; + } + } + } + } else { + for (int i = countUntil - 1; i >= slotCounter; i--) { + Slot sl = context.getSlot(currentScreen, i); + ItemStack stackSl = getSlotStack(sl); + + if (stackSl == null) { + if ((applicableSlot == null) && pushItems && sl.isItemValid(originalStack)) { + applicableSlot = sl; + } + } else if (areStacksCompatible(originalStack, stackSl)) { + if (pushItems && (stackSl.stackSize < stackSl.getMaxStackSize())) { + applicableSlot = sl; + break; + } else if (!pushItems) { + applicableSlot = sl; + break; + } + } + } + } + + if (isCraftingOutput) { + if (pushItems) { + boolean mouseWasEmpty = stackOnMouse == null; + + for (int i = 0; i < numItemsToMove; i++) { + context.clickSlot(currentScreen, selectedSlot, 0, false); + } + + if ((applicableSlot != null) && mouseWasEmpty) { + context.clickSlot(currentScreen, applicableSlot, 0, false); + } + } + + break; + } + + if (applicableSlot == null) break; + + Slot slotTo = pushItems ? applicableSlot : selectedSlot; + Slot slotFrom = pushItems ? selectedSlot : applicableSlot; + ItemStack stackTo = (getSlotStack(slotTo) != null) ? copyItemStack(getSlotStack(slotTo)) : null; + ItemStack stackFrom = copyItemStack(getSlotStack(slotFrom)); + + if (pushItems) { + numItemsToMove = Math.min(numItemsToMove, getItemStackSize(stackFrom)); + + if ((stackTo != null) + && ((getMaxItemStackSize(stackTo) - getItemStackSize(stackTo)) <= numItemsToMove)) { + context.clickSlot(currentScreen, slotFrom, 0, false); + context.clickSlot(currentScreen, slotTo, 0, false); + context.clickSlot(currentScreen, slotFrom, 0, false); + + numItemsToMove -= getMaxItemStackSize(stackTo) - getItemStackSize(stackTo); + } else { + context.clickSlot(currentScreen, slotFrom, 0, false); + + if (getItemStackSize(stackFrom) <= numItemsToMove) { + context.clickSlot(currentScreen, slotTo, 0, false); + } else { + for (int i = 0; i < numItemsToMove; i++) { + context.clickSlot(currentScreen, slotTo, 1, false); + } + } + + context.clickSlot(currentScreen, slotFrom, 0, false); + numItemsToMove = 0; + } + } else { + int availableInTarget = getMaxItemStackSize(stackTo) - getItemStackSize(stackTo); + if (availableInTarget <= 0) { + break; + } + + if (availableInTarget <= numItemsToMove) { + context.clickSlot(currentScreen, slotFrom, 0, false); + context.clickSlot(currentScreen, slotTo, 0, false); + context.clickSlot(currentScreen, slotFrom, 0, false); + + numItemsToMove -= availableInTarget; + } else { + context.clickSlot(currentScreen, slotFrom, 0, false); + + if (getItemStackSize(stackFrom) <= numItemsToMove) { + context.clickSlot(currentScreen, slotTo, 0, false); + numItemsToMove -= getItemStackSize(stackFrom); + } else { + for (int i = 0; i < numItemsToMove; i++) { + context.clickSlot(currentScreen, slotTo, 1, false); + } + + numItemsToMove = 0; + } + + context.clickSlot(currentScreen, slotFrom, 0, false); + } + } + } while (numItemsToMove != 0); + } + + private static int getNumItemsToMoveFromWheel(int wheel) { + if (!ModCompatibility.isLwjgl3Loaded() && MTConfig.ScrollItemScaling == 0) { + return Math.abs(wheel) / 120; + } + return 1; + } + + private static boolean shouldPushItems(int wheel, Slot selectedSlot, ContainerContext context) { + boolean pushItems = (wheel < 0); + if ((MTConfig.WheelScrollDirection == 2 || MTConfig.WheelScrollDirection == 3) + && otherInventoryIsAbove(selectedSlot, context.getContainerSlots())) { + pushItems = !pushItems; + } + if (MTConfig.WheelScrollDirection == 1 || MTConfig.WheelScrollDirection == 3) { + pushItems = !pushItems; + } + return pushItems; + } + + // Returns true if the other inventory is above the selected slot inventory. + // + // This is used for the inventory position aware scroll direction. To prevent any surprises, this should have the + // same logic for what constitutes the "other" inventory as elsewhere. + private static boolean otherInventoryIsAbove(Slot selectedSlot, List slots) { + boolean selectedIsInPlayerInventory = (getSlotInventory(selectedSlot) == getInventoryPlayer()); + + // Count the number of "other inventory" slots below and above the selected slot. + int otherInventorySlotsBelow = 0, otherInventorySlotsAbove = 0; + for (Slot slot : slots) { + if ((getSlotInventory(slot) == getInventoryPlayer()) != selectedIsInPlayerInventory) { + if (getSlotYPosition(slot) < getSlotYPosition(selectedSlot)) otherInventorySlotsAbove++; + else otherInventorySlotsBelow++; + } + } + + // If there are more "other inventory" slots above the selected slot, consider the other inventory above. + return (otherInventorySlotsAbove > otherInventorySlotsBelow); + } +} diff --git a/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java b/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java index c15f6b8..a1d4435 100644 --- a/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java +++ b/src/main/java/yalter/mousetweaks/loaders/MouseTweaksForge.java @@ -19,7 +19,7 @@ import yalter.mousetweaks.config.MTConfig; name = Constants.MOD_NAME, version = Constants.MOD_VERSION, guiFactory = "yalter.mousetweaks.config.MTGuiConfigFactory", - dependencies = "required-after:gtnhlib@[0.2.0,);", + dependencies = "required-after:gtnhlib@[0.9.27,);", acceptedMinecraftVersions = "[1.7.10]", acceptableRemoteVersions = "*") public class MouseTweaksForge {