From bcfec26e0c4ed22c8d3535424815006784b79e4d Mon Sep 17 00:00:00 2001 From: Sven <5113257+mainrs@users.noreply.github.com> Date: Sat, 28 Sep 2024 08:15:06 +0200 Subject: [PATCH] feat: add configuration option to disable empty item stack highlighting (#21) * feat: add configuration option to disable empty item stack highlighting * refactor: rename config field to match existing ones * chore: applied review suggestions * chore: change default value to true --- dependencies.gradle | 4 ++++ .../java/com/gtnh/findit/FindItConfig.java | 9 +++++++++ .../service/itemfinder/FindItemRequest.java | 20 ++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dependencies.gradle b/dependencies.gradle index 05acb38..d943a39 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -6,4 +6,8 @@ dependencies { compileOnly('com.github.GTNewHorizons:EnderIO:2.8.17:dev') compileOnly('com.github.GTNewHorizons:Draconic-Evolution:1.3.10-GTNH:dev') api('com.github.GTNewHorizons:ForestryMC:4.9.16:dev') + + // For testing in dev environment. + //runtimeOnlyNonPublishable('com.github.GTNewHorizons:StorageDrawers:1.13.5-GTNH') + //runtimeOnlyNonPublishable("com.github.GTNewHorizons:waila:1.8.1") } diff --git a/src/main/java/com/gtnh/findit/FindItConfig.java b/src/main/java/com/gtnh/findit/FindItConfig.java index fc781ec..cc6ede6 100644 --- a/src/main/java/com/gtnh/findit/FindItConfig.java +++ b/src/main/java/com/gtnh/findit/FindItConfig.java @@ -17,6 +17,7 @@ public class FindItConfig { public static int ITEM_HIGHLIGHTING_DURATION = 10; public static int BLOCK_HIGHLIGHTING_DURATION = 8; public static int ITEM_HIGHLIGHTING_COLOR = 0xFFFF8726; + public static boolean ITEM_HIGHLIGHTING_EMPTY_ITEMSTACKS = true; public static void setup(final File file) { final Configuration config = new Configuration(file); @@ -82,6 +83,14 @@ public class FindItConfig { "FFFF8726", "Item highlighting color as a hexadecimal color code. For example 0xFFFF8726").getString(), 16); + + ITEM_HIGHLIGHTING_EMPTY_ITEMSTACKS = config.get( + Configuration.CATEGORY_GENERAL, + "ItemHighlightingEmptyItemStacks", + "true", + "If true, the item stack size is ignored. If false, items are only highlighted if their stack size is greater than zero.\n" + + "This is useful when working with barrels or storage drawers.") + .getBoolean(); } catch (Exception ignore) {} finally { if (config.hasChanged()) config.save(); } 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 c7710ec..1067353 100644 --- a/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java +++ b/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java @@ -6,6 +6,7 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import com.gtnh.findit.FindIt; +import com.gtnh.findit.FindItConfig; import com.gtnh.findit.service.blockfinder.BlockFoundResponse; import com.gtnh.findit.util.ProtoUtils; import com.gtnh.findit.util.mods.ForestryUtils; @@ -82,7 +83,24 @@ public class FindItemRequest implements IMessage { } } - return StackInfo.equalItemAndNBT(targetStack, stack, true); + return StackInfo.equalItemAndNBT(targetStack, stack, true) && shouldHighlightItemStack(stack); + } + + /** + * Returns whether an {@code ItemStack} should be highlighted inside inventories. The method explicitly checks for + * empty item stacks and consults the mod's configuration to determine whether empty item stacks should be + * highlighted or not. + * + * @param itemStack The {@code ItemStack} to check. + * @return {@code true} if the {@code ItemStack} should be highlighted, {@code false} otherwise. + */ + private boolean shouldHighlightItemStack(ItemStack itemStack) { + // If the user requested to highlight empty item stacks, we early return to make sure that we do so. + if (FindItConfig.ITEM_HIGHLIGHTING_EMPTY_ITEMSTACKS) { + return true; + } + + return itemStack.stackSize > 0; } public static class Handler implements IMessageHandler {