ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityInventoryBase.java

133 lines
4.2 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.common.tile;
2014-12-12 15:40:01 +01:00
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA;
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.common.util.StackUtil;
2014-12-12 15:40:01 +01:00
import net.minecraft.item.ItemStack;
2020-10-31 21:42:32 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
2016-12-05 17:10:23 +01:00
import net.minecraftforge.items.ItemHandlerHelper;
2020-10-31 21:42:32 +01:00
public abstract class TileEntityInventoryBase<T extends TileEntity> extends TileEntityBase {
2014-12-12 15:40:01 +01:00
public final ItemStackHandlerAA inv;
2020-10-31 21:42:32 +01:00
private final LazyOptional<ItemStackHandlerAA> lazyInv;
2020-10-31 21:42:32 +01:00
public TileEntityInventoryBase(TileEntityType<T> type, int slots, String name) {
super(type, name);
2019-02-27 19:53:05 +01:00
this.inv = new TileStackHandler(slots);
2020-10-31 21:42:32 +01:00
this.lazyInv = LazyOptional.of(() -> this.inv);
}
2020-10-31 21:42:32 +01:00
public static void saveSlots(IItemHandler slots, CompoundNBT compound) {
if (slots != null && slots.getSlots() > 0) {
2020-10-31 21:42:32 +01:00
ListNBT tagList = new ListNBT();
for (int i = 0; i < slots.getSlots(); i++) {
ItemStack slot = slots.getStackInSlot(i);
2020-10-31 21:42:32 +01:00
CompoundNBT tagCompound = new CompoundNBT();
if (StackUtil.isValid(slot)) {
2020-10-31 21:42:32 +01:00
slot.write(tagCompound);
}
2020-10-31 21:42:32 +01:00
tagList.add(tagCompound);
}
2020-10-31 21:42:32 +01:00
compound.put("Items", tagList);
}
}
2020-10-31 21:42:32 +01:00
public static void loadSlots(IItemHandlerModifiable slots, CompoundNBT compound) {
if (slots != null && slots.getSlots() > 0) {
2020-10-31 21:42:32 +01:00
ListNBT tagList = compound.getList("Items", 10);
for (int i = 0; i < slots.getSlots(); i++) {
2020-10-31 21:42:32 +01:00
CompoundNBT tagCompound = tagList.getCompound(i);
slots.setStackInSlot(i, tagCompound.contains("id") ? ItemStack.read(tagCompound) : StackUtil.getEmpty());
}
2015-10-03 10:19:40 +02:00
}
2014-12-12 15:40:01 +01:00
}
2016-05-13 21:57:53 +02:00
@Override
2020-10-31 21:42:32 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
2019-02-27 19:53:05 +01:00
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
saveSlots(this.inv, compound);
2016-05-13 21:57:53 +02:00
}
}
@Override
2020-10-31 21:42:32 +01:00
public LazyOptional<ItemStackHandlerAA> getItemHandler(Direction facing) {
return this.lazyInv;
2014-12-12 15:40:01 +01:00
}
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return ItemStackHandlerAA.ACCEPT_TRUE;
}
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
return ItemStackHandlerAA.REMOVE_TRUE;
}
public int getMaxStackSize(int slot) {
2016-12-05 17:18:00 +01:00
return 64;
2014-12-12 15:40:01 +01:00
}
public boolean shouldSyncSlots() {
return false;
2014-12-12 15:40:01 +01:00
}
@Override
public void markDirty() {
super.markDirty();
if (this.shouldSyncSlots()) {
this.sendUpdate();
}
}
@Override
public int getComparatorStrength() {
return ItemHandlerHelper.calcRedstoneFromInventory(this.inv);
}
@Override
2020-10-31 21:42:32 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
2019-02-27 19:53:05 +01:00
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
loadSlots(this.inv, compound);
}
}
protected class TileStackHandler extends ItemStackHandlerAA {
protected TileStackHandler(int slots) {
super(slots);
}
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return TileEntityInventoryBase.this.getAcceptor();
}
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
return TileEntityInventoryBase.this.getRemover();
}
@Override
public int getSlotLimit(int slot) {
return TileEntityInventoryBase.this.getMaxStackSize(slot);
}
@Override
protected void onContentsChanged(int slot) {
super.onContentsChanged(slot);
TileEntityInventoryBase.this.markDirty();
}
};
2014-12-12 15:40:01 +01:00
}