Skip to content

Commit

Permalink
📦 Items no longer disappear if backpack size was changed
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCrayfish committed Jan 7, 2022
1 parent b00dd24 commit 65c8578
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public BackpackInventory(int cols, int rows, Player player, ItemStack stack)
super(rows * cols);
this.player = player;
this.stack = stack;
this.loadBackpackContents();
this.loadBackpackContents(player);
}

private void loadBackpackContents()
private void loadBackpackContents(Player player)
{
CompoundTag compound = this.stack.getOrCreateTag();
if(compound.contains("Items", Tag.TAG_LIST))
{
InventoryHelper.loadAllItems(compound.getList("Items", Tag.TAG_COMPOUND), this);
InventoryHelper.loadAllItems(compound.getList("Items", Tag.TAG_COMPOUND), this, player);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

/**
Expand All @@ -26,7 +29,7 @@ public static ListTag saveAllItems(ListTag list, SimpleContainer inventory)
return list;
}

public static void loadAllItems(ListTag list, SimpleContainer inventory)
public static void loadAllItems(ListTag list, SimpleContainer inventory, Player player)
{
for(int i = 0; i < list.size(); i++)
{
Expand All @@ -36,6 +39,11 @@ public static void loadAllItems(ListTag list, SimpleContainer inventory)
{
inventory.setItem(slot, ItemStack.of(compound));
}
else if(player instanceof ServerPlayer)
{
ItemStack stack = ItemStack.of(compound);
player.spawnAtLocation(inventory.addItem(stack));
}
}
}
}

0 comments on commit 65c8578

Please sign in to comment.