Skip to content

Commit

Permalink
fix fillLimit for Bundles correctly using our own math since using th…
Browse files Browse the repository at this point in the history
…e vanilla one doesn't work right.
  • Loading branch information
sakura-ryoko committed Dec 27, 2024
1 parent 7e37956 commit 5a29cc4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
72 changes: 72 additions & 0 deletions src/main/java/fi/dy/masa/minihud/util/InventoryUtils.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,4 +26,69 @@ public static Inventory getInventory(World world, BlockPos pos)

return inv;
}

public static int recalculateBundleSize(BundleContentsComponent bundle, int maxCount)
{
Iterator<ItemStack> 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<BeehiveBlockEntity.BeeData> 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;
}
}
4 changes: 2 additions & 2 deletions src/main/java/fi/dy/masa/minihud/util/MiscUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static void addBeeTooltip(ItemStack stack, List<Text> lines)
public static void addBundleTooltip(ItemStack stack, List<Text> 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)
{
Expand All @@ -231,7 +231,7 @@ public static void addBundleTooltip(ItemStack stack, List<Text> lines)

if (maxCount != 64)
{
count = bundleData.size();
count = InventoryUtils.recalculateBundleSize(bundleData, maxCount);
fillPercent = 100 * ((float) count / maxCount);
}
else
Expand Down

0 comments on commit 5a29cc4

Please sign in to comment.