From c3e1fbd398b3981a15d1b682976bdb1829866b71 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 31 Jan 2019 20:02:34 +0200 Subject: [PATCH] Fixed item weight calculation in the Bag of Holding --- .../inventory/ContainerOfHolding.java | 1 - .../inventory/InventoryOfHolding.java | 50 +++++++++++++++---- .../unicopia/player/PlayerAttributes.java | 10 +--- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/inventory/ContainerOfHolding.java b/src/main/java/com/minelittlepony/unicopia/inventory/ContainerOfHolding.java index a9e2058a..a723f7a0 100644 --- a/src/main/java/com/minelittlepony/unicopia/inventory/ContainerOfHolding.java +++ b/src/main/java/com/minelittlepony/unicopia/inventory/ContainerOfHolding.java @@ -55,7 +55,6 @@ public class ContainerOfHolding extends Container implements IWorldNameable { inventory.writeTostack(sourceStack); inventory.closeInventory(player); - super.onContainerClosed(player); } diff --git a/src/main/java/com/minelittlepony/unicopia/inventory/InventoryOfHolding.java b/src/main/java/com/minelittlepony/unicopia/inventory/InventoryOfHolding.java index 50653903..3e599f2a 100644 --- a/src/main/java/com/minelittlepony/unicopia/inventory/InventoryOfHolding.java +++ b/src/main/java/com/minelittlepony/unicopia/inventory/InventoryOfHolding.java @@ -96,7 +96,7 @@ public class InventoryOfHolding extends InventoryBasic implements InbtSerialisab } } - encodeStackWeight(blockStack, getContentsTotalWorth(blockInventory)); + encodeStackWeight(blockStack, getContentsTotalWorth(blockInventory, true), true); world.removeTileEntity(pos); world.setBlockState(pos, Blocks.AIR.getDefaultState()); @@ -165,30 +165,36 @@ public class InventoryOfHolding extends InventoryBasic implements InbtSerialisab } public double getContentsTotalWorth() { - return getContentsTotalWorth(this); + return getContentsTotalWorth(this, true); } public void writeTostack(ItemStack stack) { writeToNBT(stack.getOrCreateSubCompound("inventory")); } - public static double getContentsTotalWorth(IInventory inventory) { + public static double getContentsTotalWorth(IInventory inventory, boolean deep) { double total = 0; for (int i = 0; i < inventory.getSizeInventory(); i++) { ItemStack stack = inventory.getStackInSlot(i); - total += stack.getCount(); - total += decodeStackWeight(stack); + double weightOfOne = decodeStackWeight(stack, deep); + + if (weightOfOne == 0) { + total += stack.getCount(); + } else { + total += weightOfOne * stack.getCount(); + } } return total; } - public static void encodeStackWeight(ItemStack stack, double weight) { + public static void encodeStackWeight(ItemStack stack, double weight, boolean deep) { NBTTagCompound compound = stack.getSubCompound("inventory"); if (weight == 0 && compound != null) { compound.removeTag("weight"); + compound.removeTag("deep"); if (compound.isEmpty()) { stack.removeSubCompound("inventory"); } @@ -199,14 +205,40 @@ public class InventoryOfHolding extends InventoryBasic implements InbtSerialisab } compound.setDouble("weight", weight); + if (deep) { + compound.setBoolean("deep", deep); + } } } } - public static double decodeStackWeight(ItemStack stack) { - if (!stack.isEmpty()) { + public static double decodeStackWeight(ItemStack stack, boolean deep) { + if (!stack.isEmpty() && stack.hasTagCompound()) { + NBTTagCompound bet = stack.getSubCompound("BlockEntityTag"); NBTTagCompound compound = stack.getSubCompound("inventory"); - if (compound != null && compound.hasKey("weight")) { + + boolean hasWeight = compound != null && compound.hasKey("weight"); + + if (deep) { + if (!hasWeight && bet != null) { + Block b = Block.getBlockFromItem(stack.getItem()); + TileEntity te = b.createTileEntity(null, b.getDefaultState()); + + double weight = 0; + + if (te instanceof IInventory) { + te.readFromNBT(bet); + + weight = getContentsTotalWorth((IInventory)te, deep); + } + + encodeStackWeight(stack, weight, deep); + + return weight; + } + } + + if (hasWeight && (deep || !compound.hasKey("deep"))) { return compound.getDouble("weight"); } } diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerAttributes.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerAttributes.java index 54b4c0f1..aa0d2fca 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerAttributes.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerAttributes.java @@ -10,7 +10,6 @@ import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.entity.ai.attributes.IAttribute; import net.minecraft.entity.ai.attributes.IAttributeInstance; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; class PlayerAttributes { public static final int ADD = 0; @@ -31,14 +30,7 @@ class PlayerAttributes { private final WalkSpeed walker = new WalkSpeed(); public void applyAttributes(EntityPlayer entity, Race race) { - loadStrength = 0; - - for (ItemStack item : entity.inventory.mainInventory) { - loadStrength += InventoryOfHolding.decodeStackWeight(item); - } - for (ItemStack item : entity.inventory.armorInventory) { - loadStrength += InventoryOfHolding.decodeStackWeight(item); - } + loadStrength = InventoryOfHolding.getContentsTotalWorth(entity.inventory, false); walker.setPlayerWalkSpeed(entity.capabilities, 0.1F - (float)(loadStrength / 100000));