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
|
|
|
|
2018-06-23 01:39:30 +02: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;
|
2016-11-16 16:59:00 +01:00
|
|
|
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;
|
2021-02-27 13:24:45 +01:00
|
|
|
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
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public abstract class TileEntityInventoryBase extends TileEntityBase {
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public final ItemStackHandlerAA inv;
|
2021-02-27 13:24:45 +01:00
|
|
|
public final LazyOptional<IItemHandler> lazyInv;
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
public TileEntityInventoryBase(TileEntityType<?> type, int slots) {
|
|
|
|
super(type);
|
2019-02-27 19:53:05 +01:00
|
|
|
this.inv = new TileStackHandler(slots);
|
2021-02-27 13:24:45 +01:00
|
|
|
this.lazyInv = LazyOptional.of(() -> this.inv);
|
2016-10-01 23:35:39 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public static void saveSlots(IItemHandler slots, CompoundNBT compound) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (slots != null && slots.getSlots() > 0) {
|
2021-02-26 22:15:48 +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);
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT tagCompound = new CompoundNBT();
|
2018-06-23 01:39:30 +02:00
|
|
|
if (StackUtil.isValid(slot)) {
|
2021-08-22 17:09:06 +02:00
|
|
|
slot.save(tagCompound);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
2021-02-27 13:24:45 +01:00
|
|
|
tagList.add(tagCompound);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
2021-02-27 13:24:45 +01:00
|
|
|
compound.put("Items", tagList);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
public static void loadSlots(IItemHandlerModifiable slots, CompoundNBT compound) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (slots != null && slots.getSlots() > 0) {
|
2021-02-26 22:15:48 +01:00
|
|
|
ListNBT tagList = compound.getList("Items", 10);
|
2018-06-23 01:39:30 +02:00
|
|
|
for (int i = 0; i < slots.getSlots(); i++) {
|
2021-02-26 22:15:48 +01:00
|
|
|
CompoundNBT tagCompound = tagList.getCompound(i);
|
2021-02-27 13:24:45 +01:00
|
|
|
slots.setStackInSlot(i, tagCompound.contains("id")
|
2021-08-22 17:09:06 +02:00
|
|
|
? ItemStack.of(tagCompound)
|
2022-06-24 21:38:07 +02:00
|
|
|
: ItemStack.EMPTY);
|
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
|
2021-02-26 22:15:48 +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
|
2021-02-27 13:24:45 +01:00
|
|
|
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;
|
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
|
2021-08-22 17:09:06 +02:00
|
|
|
public void setChanged() {
|
|
|
|
super.setChanged();
|
2016-12-18 17:28:29 +01:00
|
|
|
|
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
|
2021-02-26 22:15:48 +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);
|
2021-08-22 17:09:06 +02:00
|
|
|
TileEntityInventoryBase.this.setChanged();
|
2018-06-23 01:39:30 +02:00
|
|
|
}
|
2021-02-26 22:15:48 +01:00
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|