mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-22 15:18:34 +01:00
Change tile inventory saving/loading
Using the serializeNBT and deserializeNBT methods from ItemStackHandler
This commit is contained in:
parent
a117dd5340
commit
f40023cfd2
1 changed files with 2 additions and 29 deletions
|
@ -35,38 +35,11 @@ public abstract class TileEntityInventoryBase extends TileEntityBase {
|
||||||
this.inv = new TileStackHandler(slots);
|
this.inv = new TileStackHandler(slots);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void saveSlots(IItemHandler slots, CompoundTag compound, HolderLookup.Provider provider) {
|
|
||||||
if (slots != null && slots.getSlots() > 0) {
|
|
||||||
ListTag tagList = new ListTag();
|
|
||||||
for (int i = 0; i < slots.getSlots(); i++) {
|
|
||||||
ItemStack slot = slots.getStackInSlot(i);
|
|
||||||
CompoundTag tagCompound = new CompoundTag();
|
|
||||||
if (StackUtil.isValid(slot)) {
|
|
||||||
slot.save(provider, tagCompound);
|
|
||||||
}
|
|
||||||
tagList.add(tagCompound);
|
|
||||||
}
|
|
||||||
compound.put("Items", tagList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void loadSlots(IItemHandlerModifiable slots, CompoundTag compound, HolderLookup.Provider provider) {
|
|
||||||
if (slots != null && slots.getSlots() > 0) {
|
|
||||||
ListTag tagList = compound.getList("Items", 10);
|
|
||||||
for (int i = 0; i < slots.getSlots(); i++) {
|
|
||||||
CompoundTag tagCompound = tagList.getCompound(i);
|
|
||||||
slots.setStackInSlot(i, tagCompound.contains("id")
|
|
||||||
? ItemStack.parseOptional(provider, tagCompound)
|
|
||||||
: ItemStack.EMPTY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeSyncableNBT(CompoundTag compound, HolderLookup.Provider lookupProvider, NBTType type) {
|
public void writeSyncableNBT(CompoundTag compound, HolderLookup.Provider lookupProvider, NBTType type) {
|
||||||
super.writeSyncableNBT(compound, lookupProvider, type);
|
super.writeSyncableNBT(compound, lookupProvider, type);
|
||||||
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
|
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
|
||||||
saveSlots(this.inv, compound, lookupProvider);
|
compound.put("Items", this.inv.serializeNBT(lookupProvider));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +82,7 @@ public abstract class TileEntityInventoryBase extends TileEntityBase {
|
||||||
public void readSyncableNBT(CompoundTag compound, HolderLookup.Provider lookupProvider, NBTType type) {
|
public void readSyncableNBT(CompoundTag compound, HolderLookup.Provider lookupProvider, NBTType type) {
|
||||||
super.readSyncableNBT(compound, lookupProvider, type);
|
super.readSyncableNBT(compound, lookupProvider, type);
|
||||||
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
|
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
|
||||||
loadSlots(this.inv, compound, lookupProvider);
|
this.inv.deserializeNBT(lookupProvider, compound.getCompound("Items"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue