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

161 lines
4.4 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityInventoryBase.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* <EFBFBD> 2015 Ellpeck
*/
2015-03-07 12:51:28 +01:00
package ellpeck.actuallyadditions.tile;
2014-12-12 15:40:01 +01:00
2015-04-19 01:50:02 +02:00
import ellpeck.actuallyadditions.util.ModUtil;
2014-12-12 15:40:01 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
2014-12-12 15:40:01 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
public abstract class TileEntityInventoryBase extends TileEntityBase implements ISidedInventory{
2014-12-12 15:40:01 +01:00
public ItemStack slots[];
public String name;
public TileEntityInventoryBase(int slots, String name){
this.initializeSlots(slots);
2015-10-02 16:48:01 +02:00
this.name = "container."+ModUtil.MOD_ID_LOWER+"."+name;
}
2014-12-12 15:40:01 +01:00
@Override
2014-12-12 15:40:01 +01:00
public void writeToNBT(NBTTagCompound compound){
super.writeToNBT(compound);
2015-05-20 22:39:43 +02:00
if(this.slots.length > 0){
NBTTagList tagList = new NBTTagList();
for(int currentIndex = 0; currentIndex < slots.length; currentIndex++){
if(slots[currentIndex] != null){
NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setByte("Slot", (byte)currentIndex);
slots[currentIndex].writeToNBT(tagCompound);
tagList.appendTag(tagCompound);
}
2014-12-12 15:40:01 +01:00
}
2015-05-20 22:39:43 +02:00
compound.setTag("Items", tagList);
2014-12-12 15:40:01 +01:00
}
}
@Override
2014-12-12 15:40:01 +01:00
public void readFromNBT(NBTTagCompound compound){
super.readFromNBT(compound);
2015-05-20 22:39:43 +02:00
if(this.slots.length > 0){
NBTTagList tagList = compound.getTagList("Items", 10);
for(int i = 0; i < tagList.tagCount(); i++){
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
byte slotIndex = tagCompound.getByte("Slot");
if(slotIndex >= 0 && slotIndex < slots.length){
slots[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
}
2014-12-12 15:40:01 +01:00
}
}
}
@Override
public int getInventoryStackLimit(){
2014-12-12 15:40:01 +01:00
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player){
2015-10-02 16:48:01 +02:00
return player.getDistanceSq(xCoord+0.5D, yCoord+0.5D, zCoord+0.5D) <= 64;
2014-12-12 15:40:01 +01:00
}
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
2014-12-12 15:40:01 +01:00
return false;
}
@Override
public ItemStack getStackInSlotOnClosing(int i){
2014-12-12 15:40:01 +01:00
return getStackInSlot(i);
}
@Override
2014-12-12 15:40:01 +01:00
public void setInventorySlotContents(int i, ItemStack stack){
this.slots[i] = stack;
2015-03-30 15:08:19 +02:00
this.markDirty();
2014-12-12 15:40:01 +01:00
}
@Override
public int getSizeInventory(){
2014-12-12 15:40:01 +01:00
return slots.length;
}
@Override
public ItemStack getStackInSlot(int i){
2015-05-07 16:36:29 +02:00
if(i < this.getSizeInventory()){
return slots[i];
}
return null;
2014-12-12 15:40:01 +01:00
}
@Override
public ItemStack decrStackSize(int i, int j){
2015-10-02 16:48:01 +02:00
if(slots[i] != null){
2014-12-12 15:40:01 +01:00
ItemStack stackAt;
2015-03-30 15:08:19 +02:00
if(slots[i].stackSize <= j){
2014-12-12 15:40:01 +01:00
stackAt = slots[i];
slots[i] = null;
2015-03-30 15:08:19 +02:00
this.markDirty();
2014-12-12 15:40:01 +01:00
return stackAt;
2015-03-30 15:08:19 +02:00
}
else{
2014-12-12 15:40:01 +01:00
stackAt = slots[i].splitStack(j);
2015-10-03 10:16:18 +02:00
if(slots[i].stackSize == 0){
slots[i] = null;
}
2015-03-30 15:08:19 +02:00
this.markDirty();
2014-12-12 15:40:01 +01:00
return stackAt;
}
}
return null;
}
public void initializeSlots(int itemAmount){
this.slots = new ItemStack[itemAmount];
}
@Override
public String getInventoryName(){
return this.name;
}
@Override
public boolean hasCustomInventoryName(){
return false;
}
@Override
public void openInventory(){
}
@Override
public void closeInventory(){
}
@Override
public int[] getAccessibleSlotsFromSide(int side){
2015-05-27 21:57:53 +02:00
if(this.slots.length > 0){
int[] theInt = new int[slots.length];
for(int i = 0; i < theInt.length; i++){
theInt[i] = i;
}
return theInt;
}
2015-10-02 16:48:01 +02:00
else{
return new int[0];
}
}
2014-12-12 15:40:01 +01:00
}