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

208 lines
5.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
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 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
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.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;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.IChatComponent;
2014-12-12 15:40:01 +01:00
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
2015-10-03 10:19:40 +02:00
public void initializeSlots(int itemAmount){
this.slots = new ItemStack[itemAmount];
2014-12-12 15:40:01 +01:00
}
2015-12-01 19:48:09 +01:00
@Override
public void updateEntity(){
super.updateEntity();
2015-11-29 13:17:45 +01:00
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
super.writeSyncableNBT(compound, isForSync);
if(!isForSync){
this.writeSlotsToCompound(compound);
2015-10-03 10:19:40 +02:00
}
2014-12-12 15:40:01 +01:00
}
2015-10-18 15:31:01 +02:00
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
super.readSyncableNBT(compound, isForSync);
if(!isForSync){
this.readSlotsFromCompound(compound);
}
}
public void writeSlotsToCompound(NBTTagCompound compound){
if(this.slots.length > 0){
NBTTagList tagList = new NBTTagList();
for(int currentIndex = 0; currentIndex < slots.length; currentIndex++){
NBTTagCompound tagCompound = new NBTTagCompound();
tagCompound.setByte("Slot", (byte)currentIndex);
if(slots[currentIndex] != null){
slots[currentIndex].writeToNBT(tagCompound);
}
tagList.appendTag(tagCompound);
}
compound.setTag("Items", tagList);
}
}
public void readSlotsFromCompound(NBTTagCompound compound){
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);
2015-10-18 15:31:01 +02:00
}
}
}
}
@Override
2015-10-03 22:13:57 +02:00
public int getInventoryStackLimit(){
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player){
return player.getDistanceSq(this.getPos().getX()+0.5D, this.pos.getY()+0.5D, this.pos.getZ()+0.5D) <= 64 && !this.isInvalid() && this.worldObj.getTileEntity(this.pos) == this;
}
@Override
public void openInventory(EntityPlayer player){
}
@Override
public void closeInventory(EntityPlayer player){
}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack){
return false;
}
@Override
public int getField(int id){
return 0;
2014-12-12 15:40:01 +01:00
}
@Override
public void setField(int id, int value){
}
@Override
public int getFieldCount(){
return 0;
}
@Override
public void clear(){
this.initializeSlots(this.slots.length);
2014-12-12 15:40:01 +01:00
}
@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;
}
@Override
public ItemStack removeStackFromSlot(int index){
ItemStack stack = this.slots[index];
this.slots[index] = null;
return stack;
}
@Override
public int[] getSlotsForFace(EnumFacing side){
if(this.slots.length > 0){
int[] theInt = new int[slots.length];
for(int i = 0; i < theInt.length; i++){
theInt[i] = i;
}
return theInt;
}
else{
return new int[0];
}
}
@Override
public String getName(){
return this.name;
}
@Override
public boolean hasCustomName(){
return false;
}
@Override
public IChatComponent getDisplayName(){
return null;
}
2014-12-12 15:40:01 +01:00
}