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

144 lines
4.5 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityInventoryBase.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2014-12-12 15:40:01 +01:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2014-12-12 15:40:01 +01:00
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
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;
public abstract class TileEntityInventoryBase extends TileEntityBase {
2014-12-12 15:40:01 +01:00
public final ItemStackHandlerAA inv;
public final LazyOptional<IItemHandler> lazyInv;
public TileEntityInventoryBase(TileEntityType<?> type, int slots) {
super(type);
2019-02-27 19:53:05 +01:00
this.inv = new TileStackHandler(slots);
this.lazyInv = LazyOptional.of(() -> this.inv);
}
2021-02-26 22:15:48 +01:00
public static void saveSlots(IItemHandler slots, CompoundNBT compound) {
if (slots != null && slots.getSlots() > 0) {
2021-02-26 22:15:48 +01:00
ListNBT tagList = new ListNBT();
for (int i = 0; i < slots.getSlots(); i++) {
ItemStack slot = slots.getStackInSlot(i);
2021-02-26 22:15:48 +01:00
CompoundNBT tagCompound = new CompoundNBT();
if (StackUtil.isValid(slot)) {
slot.save(tagCompound);
}
tagList.add(tagCompound);
}
compound.put("Items", tagList);
}
}
2021-02-26 22:15:48 +01:00
public static void loadSlots(IItemHandlerModifiable slots, CompoundNBT compound) {
if (slots != null && slots.getSlots() > 0) {
2021-02-26 22:15:48 +01:00
ListNBT tagList = compound.getList("Items", 10);
for (int i = 0; i < slots.getSlots(); i++) {
2021-02-26 22:15:48 +01:00
CompoundNBT tagCompound = tagList.getCompound(i);
slots.setStackInSlot(i, tagCompound.contains("id")
? ItemStack.of(tagCompound)
2022-06-24 21:38:07 +02:00
: ItemStack.EMPTY);
}
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
2021-02-26 22:15:48 +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
public LazyOptional<IItemHandler> 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 setChanged() {
super.setChanged();
if (this.shouldSyncSlots()) {
this.sendUpdate();
}
}
@Override
public int getComparatorStrength() {
return ItemHandlerHelper.calcRedstoneFromInventory(this.inv);
}
@Override
2021-02-26 22:15:48 +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.setChanged();
}
2021-02-26 22:15:48 +01:00
}
2014-12-12 15:40:01 +01:00
}