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
|
|
|
|
*
|
2015-11-02 20:55:19 +01:00
|
|
|
* © 2015 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-30 22:02:15 +01:00
|
|
|
package de.ellpeck.actuallyadditions.tile;
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2015-12-30 22:02:15 +01:00
|
|
|
import de.ellpeck.actuallyadditions.util.ModUtil;
|
2014-12-12 15:40:01 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2015-03-07 02:23:31 +01:00
|
|
|
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;
|
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
public abstract class TileEntityInventoryBase extends TileEntityBase implements ISidedInventory{
|
2014-12-12 15:40:01 +01:00
|
|
|
|
|
|
|
public ItemStack slots[];
|
2015-02-17 16:15:16 +01:00
|
|
|
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;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
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 int[] getAccessibleSlotsFromSide(int 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 void updateEntity(){
|
|
|
|
super.updateEntity();
|
2015-11-29 13:17:45 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-10-18 15:28:06 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
|
2015-12-21 22:18:33 +01:00
|
|
|
super.writeSyncableNBT(compound, isForSync);
|
2015-11-29 13:17:45 +01:00
|
|
|
if(!isForSync || this.shouldSyncSlots()){
|
2015-10-18 15:28:06 +02:00
|
|
|
if(this.slots.length > 0){
|
|
|
|
NBTTagList tagList = new NBTTagList();
|
|
|
|
for(int currentIndex = 0; currentIndex < slots.length; currentIndex++){
|
2015-11-29 13:17:45 +01:00
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
|
|
|
tagCompound.setByte("Slot", (byte)currentIndex);
|
2015-10-18 15:28:06 +02:00
|
|
|
if(slots[currentIndex] != null){
|
|
|
|
slots[currentIndex].writeToNBT(tagCompound);
|
|
|
|
}
|
2015-11-29 13:17:45 +01:00
|
|
|
tagList.appendTag(tagCompound);
|
2015-10-03 10:19:40 +02:00
|
|
|
}
|
2015-10-18 15:28:06 +02:00
|
|
|
compound.setTag("Items", tagList);
|
2015-10-03 10:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 19:48:09 +01:00
|
|
|
public boolean shouldSyncSlots(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 15:31:01 +02:00
|
|
|
@Override
|
|
|
|
public void readSyncableNBT(NBTTagCompound compound, boolean isForSync){
|
2015-12-21 22:18:33 +01:00
|
|
|
super.readSyncableNBT(compound, isForSync);
|
2015-11-29 13:17:45 +01:00
|
|
|
if(!isForSync || this.shouldSyncSlots()){
|
2015-10-18 15:31:01 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 19:55:28 +02:00
|
|
|
@Override
|
2015-10-03 22:13:57 +02:00
|
|
|
public int getInventoryStackLimit(){
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public ItemStack getStackInSlotOnClosing(int i){
|
2014-12-12 15:40:01 +01:00
|
|
|
return getStackInSlot(i);
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +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
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public int getSizeInventory(){
|
2014-12-12 15:40:01 +01:00
|
|
|
return slots.length;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public String getInventoryName(){
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public boolean hasCustomInventoryName(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public void openInventory(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2015-02-17 16:15:16 +01:00
|
|
|
public void closeInventory(){
|
|
|
|
|
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2015-10-03 10:19:40 +02:00
|
|
|
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|