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 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;
|
2016-01-07 19:04:29 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
2016-05-27 18:54:17 +02:00
|
|
|
import net.minecraft.util.text.TextComponentTranslation;
|
2016-05-05 13:43:57 +02:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
|
|
import net.minecraftforge.items.CapabilityItemHandler;
|
|
|
|
import net.minecraftforge.items.wrapper.SidedInvWrapper;
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2015-03-07 02:23:31 +01:00
|
|
|
public abstract class TileEntityInventoryBase extends TileEntityBase implements ISidedInventory{
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2016-05-19 20:05:12 +02:00
|
|
|
private final SidedInvWrapper[] invWrappers = new SidedInvWrapper[6];
|
2016-06-01 00:39:35 +02:00
|
|
|
public ItemStack slots[];
|
2015-02-17 16:15:16 +01:00
|
|
|
|
|
|
|
public TileEntityInventoryBase(int slots, String name){
|
2016-05-06 23:23:29 +02:00
|
|
|
super(name);
|
|
|
|
|
2015-02-17 16:15:16 +01:00
|
|
|
this.initializeSlots(slots);
|
2016-05-05 13:43:57 +02:00
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
if(this.hasInvWrapperCapabilities()){
|
|
|
|
for(int i = 0; i < this.invWrappers.length; i++){
|
|
|
|
this.invWrappers[i] = new SidedInvWrapper(this, EnumFacing.values()[i]);
|
|
|
|
}
|
2016-05-05 13:43:57 +02:00
|
|
|
}
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
|
2016-05-10 22:19:15 +02:00
|
|
|
public static void saveSlots(ItemStack[] slots, NBTTagCompound compound){
|
|
|
|
if(slots != null && 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 static void loadSlots(ItemStack[] slots, NBTTagCompound compound){
|
|
|
|
if(slots != null && 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);
|
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
|
|
|
public void initializeSlots(int itemAmount){
|
|
|
|
this.slots = new ItemStack[itemAmount];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, boolean isForSync){
|
|
|
|
super.writeSyncableNBT(compound, isForSync);
|
|
|
|
if(!isForSync || this.shouldSyncSlots()){
|
|
|
|
saveSlots(this.slots, compound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:41:33 +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);
|
2016-01-11 22:41:33 +01:00
|
|
|
if(!isForSync || this.shouldSyncSlots()){
|
2016-05-10 22:19:15 +02:00
|
|
|
loadSlots(this.slots, compound);
|
2015-10-18 15:31:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public int[] getSlotsForFace(EnumFacing side){
|
2016-05-08 21:09:58 +02:00
|
|
|
int invSize = this.getSizeInventory();
|
|
|
|
if(invSize > 0){
|
|
|
|
int[] theInt = new int[invSize];
|
2016-02-01 17:49:55 +01:00
|
|
|
for(int i = 0; i < theInt.length; i++){
|
|
|
|
theInt[i] = i;
|
|
|
|
}
|
|
|
|
return theInt;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return new int[0];
|
|
|
|
}
|
2016-02-01 20:31:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:39:11 +01:00
|
|
|
@Override
|
2015-10-03 22:13:57 +02:00
|
|
|
public int getInventoryStackLimit(){
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:31:27 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isUseableByPlayer(EntityPlayer player){
|
2016-05-06 23:23:29 +02:00
|
|
|
return this.canPlayerUse(player);
|
2016-01-07 19:04:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public void openInventory(EntityPlayer player){
|
2016-01-07 19:04:29 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public void closeInventory(EntityPlayer player){
|
2016-01-07 19:04:29 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isItemValidForSlot(int index, ItemStack stack){
|
2016-01-07 19:04:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getField(int id){
|
|
|
|
return 0;
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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(){
|
2016-05-02 17:46:53 +02:00
|
|
|
return this.slots.length;
|
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 getStackInSlot(int i){
|
2015-05-07 16:36:29 +02:00
|
|
|
if(i < this.getSizeInventory()){
|
2016-05-02 17:46:53 +02:00
|
|
|
return this.slots[i];
|
2015-05-07 16:36:29 +02:00
|
|
|
}
|
|
|
|
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){
|
2016-05-02 17:46:53 +02:00
|
|
|
if(this.slots[i] != null){
|
2014-12-12 15:40:01 +01:00
|
|
|
ItemStack stackAt;
|
2016-05-02 17:46:53 +02:00
|
|
|
if(this.slots[i].stackSize <= j){
|
|
|
|
stackAt = this.slots[i];
|
|
|
|
this.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{
|
2016-05-02 17:46:53 +02:00
|
|
|
stackAt = this.slots[i].splitStack(j);
|
2016-05-08 21:09:58 +02:00
|
|
|
if(this.slots[i].stackSize <= 0){
|
2016-05-02 17:46:53 +02:00
|
|
|
this.slots[i] = null;
|
2015-10-03 10:16:18 +02:00
|
|
|
}
|
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-02-20 22:45:33 +01:00
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public ItemStack removeStackFromSlot(int index){
|
2016-01-07 23:42:42 +01:00
|
|
|
ItemStack stack = this.slots[index];
|
|
|
|
this.slots[index] = null;
|
|
|
|
return stack;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public String getName(){
|
|
|
|
return this.name;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
@Override
|
2016-01-07 19:04:29 +01:00
|
|
|
public boolean hasCustomName(){
|
|
|
|
return false;
|
2015-02-17 16:15:16 +01:00
|
|
|
}
|
2015-03-07 02:23:31 +01:00
|
|
|
|
2016-05-29 23:49:35 +02:00
|
|
|
|
2016-01-07 19:04:29 +01:00
|
|
|
@Override
|
2016-03-18 23:47:22 +01:00
|
|
|
public ITextComponent getDisplayName(){
|
2016-05-27 18:54:17 +02:00
|
|
|
return new TextComponentTranslation(this.getName());
|
2016-01-25 13:34:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-05 13:43:57 +02:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
|
|
|
|
if(this.hasInvWrapperCapabilities() && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){
|
2016-05-05 13:43:57 +02:00
|
|
|
return (T)this.invWrappers[facing.ordinal()];
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return super.getCapability(capability, facing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasInvWrapperCapabilities(){
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|