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 0f4fa39 commit 9f68944
Show file tree
Hide file tree
Showing 2 changed files with 11 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, PlayerEntity player, ItemStack stac
super(rows * cols);
this.player = player;
this.stack = stack;
this.loadBackpackContents();
this.loadBackpackContents(player);
}

private void loadBackpackContents()
private void loadBackpackContents(PlayerEntity player)
{
CompoundNBT compound = this.stack.getOrCreateTag();
if(compound.contains("Items", Constants.NBT.TAG_LIST))
{
InventoryHelper.loadAllItems(compound.getList("Items", Constants.NBT.TAG_COMPOUND), this);
InventoryHelper.loadAllItems(compound.getList("Items", Constants.NBT.TAG_COMPOUND), this, player);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mrcrayfish.backpacked.util;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
Expand All @@ -26,7 +28,7 @@ public static ListNBT saveAllItems(ListNBT list, Inventory inventory)
return list;
}

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

0 comments on commit 9f68944

Please sign in to comment.