2020-09-09 16:48:43 +02:00
|
|
|
package de.ellpeck.actuallyadditions.common.tile;
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2020-09-09 16:48:43 +02: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;
|
2016-12-04 00:10:52 +01:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
2016-12-05 17:10:23 +01:00
|
|
|
import net.minecraftforge.items.ItemHandlerHelper;
|
2016-11-16 16:59:00 +01:00
|
|
|
|
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
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public final ItemStackHandlerAA inv;
|
2020-10-31 21:42:32 +01:00
|
|
|
private final LazyOptional<ItemStackHandlerAA> lazyInv;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
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);
|
2016-10-01 23:35:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-31 21:42:32 +01:00
|
|
|
public static void saveSlots(IItemHandler slots, CompoundNBT compound) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (slots != null && slots.getSlots() > 0) {
|
2020-10-31 21:42:32 +01:00
|
|
|
ListNBT tagList = new ListNBT();
|
2018-06-23 01:39:30 +02:00
|
|
|
for (int i = 0; i < slots.getSlots(); i++) {
|
2016-11-27 11:37:55 +01:00
|
|
|
ItemStack slot = slots.getStackInSlot(i);
|
2020-10-31 21:42:32 +01:00
|
|
|
CompoundNBT tagCompound = new CompoundNBT();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (StackUtil.isValid(slot)) {
|
2020-10-31 21:42:32 +01:00
|
|
|
slot.write(tagCompound);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
2020-10-31 21:42:32 +01:00
|
|
|
tagList.add(tagCompound);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
2020-10-31 21:42:32 +01:00
|
|
|
compound.put("Items", tagList);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:42:32 +01:00
|
|
|
public static void loadSlots(IItemHandlerModifiable slots, CompoundNBT compound) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (slots != null && slots.getSlots() > 0) {
|
2020-10-31 21:42:32 +01:00
|
|
|
ListNBT tagList = compound.getList("Items", 10);
|
2018-06-23 01:39:30 +02:00
|
|
|
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());
|
2016-01-11 22:41:33 +01:00
|
|
|
}
|
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) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2019-02-27 19:53:05 +01:00
|
|
|
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
|
2018-06-23 01:39:30 +02:00
|
|
|
saveSlots(this.inv, compound);
|
2016-05-13 21:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:04:29 +01: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;
|
2016-01-07 19:04:29 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:04:07 +02:00
|
|
|
public IRemover getRemover() {
|
|
|
|
return ItemStackHandlerAA.REMOVE_TRUE;
|
2016-01-07 19:04:29 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getMaxStackSize(int slot) {
|
2016-12-05 17:18:00 +01:00
|
|
|
return 64;
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public boolean shouldSyncSlots() {
|
2016-12-04 00:10:52 +01:00
|
|
|
return false;
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-18 17:28:29 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public void markDirty() {
|
2016-12-18 17:28:29 +01:00
|
|
|
super.markDirty();
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.shouldSyncSlots()) {
|
2016-12-18 17:28:29 +01:00
|
|
|
this.sendUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2018-06-23 01:39:30 +02:00
|
|
|
public int getComparatorStrength() {
|
|
|
|
return ItemHandlerHelper.calcRedstoneFromInventory(this.inv);
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-05-05 13:43:57 +02:00
|
|
|
@Override
|
2020-10-31 21:42:32 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
|
2016-12-04 00:10:52 +01:00
|
|
|
super.readSyncableNBT(compound, type);
|
2019-02-27 19:53:05 +01:00
|
|
|
if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) {
|
2018-06-23 01:39:30 +02:00
|
|
|
loadSlots(this.inv, compound);
|
2016-05-05 13:43:57 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
|
|
|
|
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();
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IRemover getRemover() {
|
|
|
|
return TileEntityInventoryBase.this.getRemover();
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|