diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a13335..6e52f15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,3 +4,4 @@ - Updated bundle handling - Added Ukrainian translation (ttrafford7) +- Added support for ItemLocks diff --git a/common/build.gradle b/common/build.gradle index 219b96b..6000f04 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -9,6 +9,9 @@ dependencies { // Cloth Config API api("me.shedaniel.cloth:cloth-config-neoforge:${clothconfig_version}") + + // ItemLocks + implementation("maven.modrinth:tJzrFuyy:${itemlocks_version}") } neoForge { diff --git a/common/src/main/java/dev/terminalmc/clientsort/compat/itemlocks/ItemLocks.java b/common/src/main/java/dev/terminalmc/clientsort/compat/itemlocks/ItemLocks.java new file mode 100644 index 0000000..2ef3262 --- /dev/null +++ b/common/src/main/java/dev/terminalmc/clientsort/compat/itemlocks/ItemLocks.java @@ -0,0 +1,47 @@ +/* + * Copyright 2024 TerminalMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.terminalmc.clientsort.compat.itemlocks; + +import com.kirdow.itemlocks.client.LockManager; +import dev.terminalmc.clientsort.util.inject.ISlot; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.Slot; + +import static com.kirdow.itemlocks.client.input.KeyBindings.isBypass; +import static com.kirdow.itemlocks.proxy.Components.getComponent; + +public class ItemLocks { + static boolean isLocked(Slot slot) { + if (!(slot.container instanceof Inventory)) return false; + int index = adjustForInventory(((ISlot) slot).mouseWheelie_getIndexInInv()); + return getComponent(LockManager.class).isLockedSlotRaw(index) && !isBypass(); + } + + /** + * Moves the hotbar from 0-8 to 27-35. + */ + private static int adjustForInventory(int slot) { + // + if (0 <= slot && slot <= 8) { + return slot + 27; + } else if (9 <= slot && slot <= 35) { + return slot - 9; + } else { + return slot; + } + } +} diff --git a/common/src/main/java/dev/terminalmc/clientsort/compat/itemlocks/ItemLocksWrapper.java b/common/src/main/java/dev/terminalmc/clientsort/compat/itemlocks/ItemLocksWrapper.java new file mode 100644 index 0000000..463b88b --- /dev/null +++ b/common/src/main/java/dev/terminalmc/clientsort/compat/itemlocks/ItemLocksWrapper.java @@ -0,0 +1,33 @@ +/* + * Copyright 2024 TerminalMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dev.terminalmc.clientsort.compat.itemlocks; + +import net.minecraft.world.inventory.Slot; + +public class ItemLocksWrapper { + private static boolean hasFailed = false; + + public static boolean isLocked(Slot slot) { + if (hasFailed) return false; + try { + return ItemLocks.isLocked(slot); + } catch (NoClassDefFoundError | NoSuchMethodError ignored) { + hasFailed = true; + return false; + } + } +} diff --git a/common/src/main/java/dev/terminalmc/clientsort/inventory/sort/InventorySorter.java b/common/src/main/java/dev/terminalmc/clientsort/inventory/sort/InventorySorter.java index ffed63d..1b41a48 100644 --- a/common/src/main/java/dev/terminalmc/clientsort/inventory/sort/InventorySorter.java +++ b/common/src/main/java/dev/terminalmc/clientsort/inventory/sort/InventorySorter.java @@ -17,6 +17,7 @@ package dev.terminalmc.clientsort.inventory.sort; +import dev.terminalmc.clientsort.compat.itemlocks.ItemLocksWrapper; import dev.terminalmc.clientsort.config.Config; import dev.terminalmc.clientsort.inventory.ContainerScreenHelper; import dev.terminalmc.clientsort.network.InteractionManager; @@ -57,7 +58,9 @@ private void collectSlots(Slot originSlot) { ArrayList slotsInScope = new ArrayList<>(); for (Slot slot : containerScreen.getMenu().slots) { if (originScope == screenHelper.getScope(slot, true)) { - slotsInScope.add(slot); + if (!ItemLocksWrapper.isLocked(slot)) { + slotsInScope.add(slot); + } } } this.inventorySlots = slotsInScope.toArray(new Slot[0]); diff --git a/fabric/build.gradle b/fabric/build.gradle index 8bc364c..74ee429 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -24,6 +24,9 @@ dependencies { modApi("me.shedaniel.cloth:cloth-config-fabric:${clothconfig_version}") { exclude(group: "net.fabricmc.fabric-api") } + + // ItemLocks + modImplementation("maven.modrinth:tJzrFuyy:${itemlocks_version}") } loom { diff --git a/gradle.properties b/gradle.properties index dd0ef1d..95a3c7b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -54,6 +54,9 @@ clothconfig_versions_neoforge=[15,) modmenu_version=11.0.2 modmenu_versions_fabric=>10 +# https://modrinth.com/mod/tJzrFuyy/versions +itemlocks_version=1.21-1.3.9 + # GitHub, Modrinth, CurseForge releases # Plural properties expect CSV lists github_repo_owner=TerminalMC diff --git a/neoforge/build.gradle b/neoforge/build.gradle index 1573d00..1994790 100644 --- a/neoforge/build.gradle +++ b/neoforge/build.gradle @@ -9,6 +9,9 @@ dependencies { // Cloth Config API api("me.shedaniel.cloth:cloth-config-neoforge:${clothconfig_version}") + + // ItemLocks + implementation("maven.modrinth:tJzrFuyy:${itemlocks_version}") } neoForge {