From 5a29cc4a688b14b59cadadc7ff818a5e14153974 Mon Sep 17 00:00:00 2001 From: Sakura Ryoko Date: Fri, 27 Dec 2024 16:26:15 -0500 Subject: [PATCH] fix fillLimit for Bundles correctly using our own math since using the vanilla one doesn't work right. --- .../dy/masa/minihud/util/InventoryUtils.java | 72 +++++++++++++++++++ .../fi/dy/masa/minihud/util/MiscUtils.java | 4 +- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/main/java/fi/dy/masa/minihud/util/InventoryUtils.java b/src/main/java/fi/dy/masa/minihud/util/InventoryUtils.java index cc034fc5f..5fbf3d141 100644 --- a/src/main/java/fi/dy/masa/minihud/util/InventoryUtils.java +++ b/src/main/java/fi/dy/masa/minihud/util/InventoryUtils.java @@ -1,6 +1,13 @@ package fi.dy.masa.minihud.util; +import java.util.Iterator; +import java.util.List; + +import net.minecraft.block.entity.BeehiveBlockEntity; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.BundleContentsComponent; import net.minecraft.inventory.Inventory; +import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -19,4 +26,69 @@ public static Inventory getInventory(World world, BlockPos pos) return inv; } + + public static int recalculateBundleSize(BundleContentsComponent bundle, int maxCount) + { + Iterator iter = bundle.stream().iterator(); + final int vanillaMax = 64; + final int vanillaBundleAdj = 4; // Why does a nested, bundle count as 4, mojang? + int newCount = 0; + + while (iter.hasNext()) + { + ItemStack entry = iter.next(); + + if (!entry.isEmpty()) + { + List list = entry.getOrDefault(DataComponentTypes.BEES, List.of()); + + if (!list.isEmpty()) + { + return vanillaMax; + } + else if (entry.contains(DataComponentTypes.BUNDLE_CONTENTS)) + { + // Nesting Bundles... + BundleContentsComponent bundleEntry = entry.get(DataComponentTypes.BUNDLE_CONTENTS); + + if (bundleEntry != null) + { + if (bundleEntry.isEmpty()) + { + newCount += vanillaBundleAdj; + } + else + { + newCount += recalculateBundleSize(bundleEntry, maxCount) + vanillaBundleAdj; + } + } + else + { + newCount += Math.min(entry.getCount(), maxCount); + } + } + else if (entry.getMaxCount() != vanillaMax) + { + final float fraction = (float) entry.getCount() / entry.getMaxCount(); + + if (fraction != 1.0F) + { + // Needs to be based on vanillaMax. It's cool that we + // can calculate this, but no user confusion is necessary. + newCount += (int) (vanillaMax * fraction); + } + else + { + return vanillaMax; + } + } + else + { + newCount += Math.min(entry.getCount(), maxCount); + } + } + } + + return newCount; + } } diff --git a/src/main/java/fi/dy/masa/minihud/util/MiscUtils.java b/src/main/java/fi/dy/masa/minihud/util/MiscUtils.java index 6e75f3fb2..86c99fc71 100644 --- a/src/main/java/fi/dy/masa/minihud/util/MiscUtils.java +++ b/src/main/java/fi/dy/masa/minihud/util/MiscUtils.java @@ -221,7 +221,7 @@ public static void addBeeTooltip(ItemStack stack, List lines) public static void addBundleTooltip(ItemStack stack, List lines) { BundleContentsComponent bundleData = stack.get(DataComponentTypes.BUNDLE_CONTENTS); - int maxCount = Configs.Generic.BUNDLE_TOOLTIPS_FILL_LEVEL.getIntegerValue(); + final int maxCount = Configs.Generic.BUNDLE_TOOLTIPS_FILL_LEVEL.getIntegerValue(); if (bundleData != null) { @@ -231,7 +231,7 @@ public static void addBundleTooltip(ItemStack stack, List lines) if (maxCount != 64) { - count = bundleData.size(); + count = InventoryUtils.recalculateBundleSize(bundleData, maxCount); fillPercent = 100 * ((float) count / maxCount); } else