Fixed item weight calculation in the Bag of Holding

This commit is contained in:
Sollace 2019-01-31 20:02:34 +02:00
parent 0ec870cd9f
commit c3e1fbd398
3 changed files with 42 additions and 19 deletions

View file

@ -55,7 +55,6 @@ public class ContainerOfHolding extends Container implements IWorldNameable {
inventory.writeTostack(sourceStack); inventory.writeTostack(sourceStack);
inventory.closeInventory(player); inventory.closeInventory(player);
super.onContainerClosed(player); super.onContainerClosed(player);
} }

View file

@ -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.removeTileEntity(pos);
world.setBlockState(pos, Blocks.AIR.getDefaultState()); world.setBlockState(pos, Blocks.AIR.getDefaultState());
@ -165,30 +165,36 @@ public class InventoryOfHolding extends InventoryBasic implements InbtSerialisab
} }
public double getContentsTotalWorth() { public double getContentsTotalWorth() {
return getContentsTotalWorth(this); return getContentsTotalWorth(this, true);
} }
public void writeTostack(ItemStack stack) { public void writeTostack(ItemStack stack) {
writeToNBT(stack.getOrCreateSubCompound("inventory")); writeToNBT(stack.getOrCreateSubCompound("inventory"));
} }
public static double getContentsTotalWorth(IInventory inventory) { public static double getContentsTotalWorth(IInventory inventory, boolean deep) {
double total = 0; double total = 0;
for (int i = 0; i < inventory.getSizeInventory(); i++) { for (int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack stack = inventory.getStackInSlot(i); ItemStack stack = inventory.getStackInSlot(i);
total += stack.getCount(); double weightOfOne = decodeStackWeight(stack, deep);
total += decodeStackWeight(stack);
if (weightOfOne == 0) {
total += stack.getCount();
} else {
total += weightOfOne * stack.getCount();
}
} }
return total; 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"); NBTTagCompound compound = stack.getSubCompound("inventory");
if (weight == 0 && compound != null) { if (weight == 0 && compound != null) {
compound.removeTag("weight"); compound.removeTag("weight");
compound.removeTag("deep");
if (compound.isEmpty()) { if (compound.isEmpty()) {
stack.removeSubCompound("inventory"); stack.removeSubCompound("inventory");
} }
@ -199,14 +205,40 @@ public class InventoryOfHolding extends InventoryBasic implements InbtSerialisab
} }
compound.setDouble("weight", weight); compound.setDouble("weight", weight);
if (deep) {
compound.setBoolean("deep", deep);
}
} }
} }
} }
public static double decodeStackWeight(ItemStack stack) { public static double decodeStackWeight(ItemStack stack, boolean deep) {
if (!stack.isEmpty()) { if (!stack.isEmpty() && stack.hasTagCompound()) {
NBTTagCompound bet = stack.getSubCompound("BlockEntityTag");
NBTTagCompound compound = stack.getSubCompound("inventory"); 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"); return compound.getDouble("weight");
} }
} }

View file

@ -10,7 +10,6 @@ import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.IAttribute; import net.minecraft.entity.ai.attributes.IAttribute;
import net.minecraft.entity.ai.attributes.IAttributeInstance; import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
class PlayerAttributes { class PlayerAttributes {
public static final int ADD = 0; public static final int ADD = 0;
@ -31,14 +30,7 @@ class PlayerAttributes {
private final WalkSpeed walker = new WalkSpeed(); private final WalkSpeed walker = new WalkSpeed();
public void applyAttributes(EntityPlayer entity, Race race) { public void applyAttributes(EntityPlayer entity, Race race) {
loadStrength = 0; loadStrength = InventoryOfHolding.getContentsTotalWorth(entity.inventory, false);
for (ItemStack item : entity.inventory.mainInventory) {
loadStrength += InventoryOfHolding.decodeStackWeight(item);
}
for (ItemStack item : entity.inventory.armorInventory) {
loadStrength += InventoryOfHolding.decodeStackWeight(item);
}
walker.setPlayerWalkSpeed(entity.capabilities, 0.1F - (float)(loadStrength / 100000)); walker.setPlayerWalkSpeed(entity.capabilities, 0.1F - (float)(loadStrength / 100000));