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

135 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
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 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.ItemStackHandlerCustom;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2014-12-12 15:40:01 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
public abstract class TileEntityInventoryBase extends TileEntityBase{
2014-12-12 15:40:01 +01:00
public final ItemStackHandlerCustom slots;
public TileEntityInventoryBase(int slots, String name){
super(name);
this.slots = new ItemStackHandlerCustom(slots){
@Override
public boolean canInsert(ItemStack stack, int slot){
return TileEntityInventoryBase.this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtract(ItemStack stack, int slot){
return TileEntityInventoryBase.this.canExtractItem(slot, stack);
}
@Override
protected int getStackLimit(int slot, ItemStack stack){
return TileEntityInventoryBase.this.getMaxStackSizePerSlot(slot, stack);
}
@Override
protected void onContentsChanged(int slot){
super.onContentsChanged(slot);
TileEntityInventoryBase.this.markDirty();
}
};
}
public static void saveSlots(IItemHandler slots, NBTTagCompound compound){
if(slots != null && slots.getSlots() > 0){
NBTTagList tagList = new NBTTagList();
for(int i = 0; i < slots.getSlots(); i++){
ItemStack slot = slots.getStackInSlot(i);
NBTTagCompound tagCompound = new NBTTagCompound();
if(StackUtil.isValid(slot)){
slot.writeToNBT(tagCompound);
}
tagList.appendTag(tagCompound);
}
compound.setTag("Items", tagList);
}
}
public static void loadSlots(IItemHandlerModifiable slots, NBTTagCompound compound){
if(slots != null && slots.getSlots() > 0){
NBTTagList tagList = compound.getTagList("Items", 10);
for(int i = 0; i < slots.getSlots(); i++){
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
slots.setStackInSlot(i, tagCompound != null && tagCompound.hasKey("id") ? new ItemStack(tagCompound) : StackUtil.getNull());
}
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
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
if(type == NBTType.SAVE_TILE || (type == NBTType.SYNC && this.shouldSyncSlots())){
saveSlots(this.slots, compound);
2016-05-13 21:57:53 +02:00
}
}
@Override
public IItemHandler getItemHandler(EnumFacing facing){
return this.slots;
2014-12-12 15:40:01 +01:00
}
public boolean isItemValidForSlot(int slot, ItemStack stack){
return true;
}
public boolean canExtractItem(int slot, ItemStack stack){
return true;
}
public int getMaxStackSizePerSlot(int slot, ItemStack stack){
return stack.getMaxStackSize();
2014-12-12 15:40:01 +01:00
}
public boolean shouldSyncSlots(){
return false;
2014-12-12 15:40:01 +01:00
}
@Override
public int getComparatorStrength(){
int i = 0;
float f = 0;
2014-12-12 15:40:01 +01:00
for(int j = 0; j < this.slots.getSlots(); ++j){
ItemStack stack = this.slots.getStackInSlot(j);
2014-12-12 15:40:01 +01:00
if(StackUtil.isValid(stack)){
f += (float)StackUtil.getStackSize(stack)/(float)Math.min(this.getMaxStackSizePerSlot(j, stack), stack.getMaxStackSize());
i++;
2014-12-12 15:40:01 +01:00
}
}
f = f/(float)this.slots.getSlots();
return MathHelper.floor(f*14.0F)+(i > 0 ? 1 : 0);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
if(type == NBTType.SAVE_TILE || (type == NBTType.SYNC && this.shouldSyncSlots())){
loadSlots(this.slots, compound);
}
}
2014-12-12 15:40:01 +01:00
}