From 9a54f7c6b29b693a0ed1f41bbd12d7da5647244e Mon Sep 17 00:00:00 2001 From: Sven <5113257+mainrs@users.noreply.github.com> Date: Wed, 25 Sep 2024 22:36:20 +0200 Subject: [PATCH] feat: add support for Forestry backpacks (#19) --- dependencies.gradle | 1 + src/main/java/com/gtnh/findit/FindIt.java | 31 +++++++++------- .../itemfinder/ClientItemFindService.java | 34 ++++++++++++----- .../service/itemfinder/FindItemRequest.java | 22 +++++++++++ .../gtnh/findit/util/mods/ForestryUtils.java | 37 +++++++++++++++++++ 5 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/gtnh/findit/util/mods/ForestryUtils.java diff --git a/dependencies.gradle b/dependencies.gradle index 9ba3af6..76c7f9e 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -5,4 +5,5 @@ dependencies { compileOnly('com.github.GTNewHorizons:GT5-Unofficial:5.09.49.05:dev') compileOnly('com.github.GTNewHorizons:EnderIO:2.8.17:dev') compileOnly('com.github.GTNewHorizons:Draconic-Evolution:1.3.6-GTNH:dev') + api('com.github.GTNewHorizons:ForestryMC:4.9.16:dev') } diff --git a/src/main/java/com/gtnh/findit/FindIt.java b/src/main/java/com/gtnh/findit/FindIt.java index 14b46b0..61a5688 100644 --- a/src/main/java/com/gtnh/findit/FindIt.java +++ b/src/main/java/com/gtnh/findit/FindIt.java @@ -28,11 +28,11 @@ public class FindIt { @Mod.Instance(MOD_ID) public static FindIt INSTANCE; - private boolean extraUtilitiesLoaded; - private boolean gregTechloaded; - private boolean enderIOloaded; - - private boolean draconicEvolutionLoaded; + private boolean isDraconicEvolutionLoaded; + private boolean isEnderIOLoaded; + private boolean isExtraUtilitiesLoaded; + private boolean isForestryLoaded; + private boolean isGregTechLoaded; private SearchCooldownService cooldownService; private BlockFindService blockFindService; @@ -40,10 +40,11 @@ public class FindIt { @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { - this.extraUtilitiesLoaded = Loader.isModLoaded("ExtraUtilities"); - this.gregTechloaded = Loader.isModLoaded("gregtech"); - this.enderIOloaded = Loader.isModLoaded("EnderIO"); - this.draconicEvolutionLoaded = Loader.isModLoaded("DraconicEvolution"); + this.isExtraUtilitiesLoaded = Loader.isModLoaded("ExtraUtilities"); + this.isGregTechLoaded = Loader.isModLoaded("gregtech"); + this.isEnderIOLoaded = Loader.isModLoaded("EnderIO"); + this.isDraconicEvolutionLoaded = Loader.isModLoaded("DraconicEvolution"); + this.isForestryLoaded = Loader.isModLoaded("Forestry"); FindItConfig.setup(event.getSuggestedConfigurationFile()); boolean isClient = event.getSide() == Side.CLIENT; @@ -66,18 +67,22 @@ public class FindIt { } public static boolean isExtraUtilitiesLoaded() { - return INSTANCE.extraUtilitiesLoaded; + return INSTANCE.isExtraUtilitiesLoaded; } public static boolean isGregTechLoaded() { - return INSTANCE.gregTechloaded; + return INSTANCE.isGregTechLoaded; } public static boolean isEnderIOLoaded() { - return INSTANCE.enderIOloaded; + return INSTANCE.isEnderIOLoaded; } public static boolean isDraconicEvolutionLoaded() { - return INSTANCE.draconicEvolutionLoaded; + return INSTANCE.isDraconicEvolutionLoaded; + } + + public static boolean isForestryLoaded() { + return INSTANCE.isForestryLoaded; } } diff --git a/src/main/java/com/gtnh/findit/service/itemfinder/ClientItemFindService.java b/src/main/java/com/gtnh/findit/service/itemfinder/ClientItemFindService.java index cff76b8..e36088a 100644 --- a/src/main/java/com/gtnh/findit/service/itemfinder/ClientItemFindService.java +++ b/src/main/java/com/gtnh/findit/service/itemfinder/ClientItemFindService.java @@ -95,10 +95,14 @@ public class ClientItemFindService extends ItemFindService { return; } + // `WorldClient` is only available on the client-side, thus effectively checking if the game is running on + // the client. We are only interested in highlighting slots when the player is in a GUI; the operation is + // bound client-side. if (Minecraft.getMinecraft().theWorld == null) { return; } + // We are only interested in GUIs that contain some kind of inventory. final GuiScreen screen = Minecraft.getMinecraft().currentScreen; if (!(screen instanceof GuiContainer)) { return; @@ -107,21 +111,26 @@ public class ClientItemFindService extends ItemFindService { final GuiContainer gui = (GuiContainer) screen; final HashSet highlightedSlots = new HashSet<>(); + // If the expiration time has passed, we reset the found item. This is done to prevent the item from being + // highlighted indefinitely. if (System.currentTimeMillis() > expirationTime) { foundItem = null; } + if (foundItem == null) { + return; + } - if (foundItem != null) { - @SuppressWarnings("unchecked") - List slots = gui.inventorySlots.inventorySlots; + // We continue to iterate over each slot of the given GUI's inventory and checking if the slot contains the + // item that we are looking for. + @SuppressWarnings("unchecked") + List slots = gui.inventorySlots.inventorySlots; - for (Slot slot : slots) { - if (!(slot.inventory instanceof InventoryPlayer) && foundItem.isStackSatisfies(slot.getStack())) { - highlightedSlots.add(slot); + for (Slot slot : slots) { + if (!(slot.inventory instanceof InventoryPlayer) && foundItem.isStackSatisfies(slot.getStack())) { + highlightedSlots.add(slot); - if (highlightedSlots.size() > 256) { - break; - } + if (highlightedSlots.size() > 256) { + break; } } } @@ -152,6 +161,10 @@ public class ClientItemFindService extends ItemFindService { } } + /** + * This class is responsible for removing the NEI ping handler from the list of input handlers. This is done to + * prevent the NEI ping handler from interfering with the FindIt keybind. + */ public static class NEIEventListener { @SubscribeEvent @@ -164,6 +177,9 @@ public class ClientItemFindService extends ItemFindService { } } + /** + * Returns the instance of the client item find service. + */ public static ClientItemFindService getInstance() { return (ClientItemFindService) FindIt.getItemFindService(); } diff --git a/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java b/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java index de38e20..c7710ec 100644 --- a/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java +++ b/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java @@ -1,11 +1,14 @@ package com.gtnh.findit.service.itemfinder; +import java.util.Optional; + import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import com.gtnh.findit.FindIt; import com.gtnh.findit.service.blockfinder.BlockFoundResponse; import com.gtnh.findit.util.ProtoUtils; +import com.gtnh.findit.util.mods.ForestryUtils; import codechicken.nei.recipe.StackInfo; import cpw.mods.fml.common.network.simpleimpl.IMessage; @@ -60,6 +63,25 @@ public class FindItemRequest implements IMessage { return targetFluidStack.isFluidEqual(StackInfo.getFluid(stack)); } + // Additionally check for backpacks that might contain the item itself. + if (FindIt.isForestryLoaded()) { + Optional result = ForestryUtils.getInventoryOfPotentialStorageItem(stack).map(inventory -> { + for (int i = 0; i < inventory.getSizeInventory(); i++) { + // This does a recursive call, but backpacks cannot store other backpacks, so we won't run into + // StackOverflowExceptions. + if (inventory.getStackInSlot(i) != null && isStackSatisfies(inventory.getStackInSlot(i))) { + return true; + } + } + + // None of the inventory slots contained the item we were looking for. + return false; + }); + if (result.isPresent()) { + return result.get(); + } + } + return StackInfo.equalItemAndNBT(targetStack, stack, true); } diff --git a/src/main/java/com/gtnh/findit/util/mods/ForestryUtils.java b/src/main/java/com/gtnh/findit/util/mods/ForestryUtils.java new file mode 100644 index 0000000..92ebfb8 --- /dev/null +++ b/src/main/java/com/gtnh/findit/util/mods/ForestryUtils.java @@ -0,0 +1,37 @@ +package com.gtnh.findit.util.mods; + +import java.util.Optional; + +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; + +import forestry.storage.inventory.ItemInventoryBackpack; +import forestry.storage.items.ItemBackpack; + +public class ForestryUtils { + + private ForestryUtils() {} + + public static Optional getInventoryOfPotentialStorageItem(ItemStack potentialBackpackItemStack) { + // Checks for Forestry backpacks. + Item item = potentialBackpackItemStack.getItem(); + if (item instanceof ItemBackpack) { + ItemBackpack backpack = (ItemBackpack) item; + + // We're running the GUI code client-side only, thus the only player interacting with the GUI is the + // player that the backpack's inventory is checked against. + EntityPlayer player = Minecraft.getMinecraft().thePlayer; + + ItemInventoryBackpack inventory = new ItemInventoryBackpack( + player, + backpack.getBackpackSize(), + potentialBackpackItemStack); + return Optional.of(inventory); + } + + return Optional.empty(); + } +}