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
|
|
|
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2014-12-12 15:40:01 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2016-11-28 12:51:45 +01:00
|
|
|
import net.minecraft.inventory.Container;
|
2016-11-27 11:37:55 +01:00
|
|
|
import net.minecraft.inventory.IInventory;
|
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-11-20 20:13:26 +01:00
|
|
|
import net.minecraft.util.NonNullList;
|
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;
|
2016-11-16 16:59:00 +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-11-20 20:13:26 +01:00
|
|
|
public NonNullList<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);
|
|
|
|
|
2016-11-20 20:13:26 +01:00
|
|
|
this.slots = StackUtil.createSlots(slots);
|
2016-05-05 13:43:57 +02:00
|
|
|
|
2016-05-08 21:09:58 +02:00
|
|
|
if(this.hasInvWrapperCapabilities()){
|
2016-10-01 23:35:39 +02:00
|
|
|
this.getInvWrappers(this.invWrappers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:37:55 +01:00
|
|
|
public static void saveSlots(IInventory slots, NBTTagCompound compound){
|
|
|
|
if(slots != null && slots.getSizeInventory() > 0){
|
2016-05-10 22:19:15 +02:00
|
|
|
NBTTagList tagList = new NBTTagList();
|
2016-11-27 11:37:55 +01:00
|
|
|
for(int i = 0; i < slots.getSizeInventory(); i++){
|
|
|
|
ItemStack slot = slots.getStackInSlot(i);
|
2016-05-10 22:19:15 +02:00
|
|
|
NBTTagCompound tagCompound = new NBTTagCompound();
|
2016-11-16 16:59:00 +01:00
|
|
|
if(StackUtil.isValid(slot)){
|
2016-06-17 19:14:59 +02:00
|
|
|
slot.writeToNBT(tagCompound);
|
2016-05-10 22:19:15 +02:00
|
|
|
}
|
|
|
|
tagList.appendTag(tagCompound);
|
|
|
|
}
|
|
|
|
compound.setTag("Items", tagList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:37:55 +01:00
|
|
|
public static void loadSlots(IInventory slots, NBTTagCompound compound){
|
|
|
|
if(slots != null && slots.getSizeInventory() > 0){
|
2016-05-10 22:19:15 +02:00
|
|
|
NBTTagList tagList = compound.getTagList("Items", 10);
|
2016-11-27 11:37:55 +01:00
|
|
|
for(int i = 0; i < slots.getSizeInventory(); i++){
|
2016-05-10 22:19:15 +02:00
|
|
|
NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
|
2016-11-27 11:37:55 +01:00
|
|
|
slots.setInventorySlotContents(i, tagCompound != null && tagCompound.hasKey("id") ? new ItemStack(tagCompound) : StackUtil.getNull());
|
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-10-31 19:05:29 +01:00
|
|
|
protected void getInvWrappers(SidedInvWrapper[] wrappers){
|
|
|
|
for(int i = 0; i < wrappers.length; i++){
|
|
|
|
wrappers[i] = new SidedInvWrapper(this, EnumFacing.values()[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 21:57:53 +02:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
if(type == NBTType.SAVE_TILE || (type == NBTType.SYNC && this.shouldSyncSlots())){
|
2016-11-27 11:37:55 +01:00
|
|
|
saveSlots(this, compound);
|
2016-05-13 21:57:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 22:41:33 +01:00
|
|
|
public boolean shouldSyncSlots(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-28 12:51:45 +01:00
|
|
|
@Override
|
|
|
|
public int getComparatorStrength(){
|
|
|
|
return Container.calcRedstoneFromInventory(this);
|
|
|
|
}
|
|
|
|
|
2015-10-18 15:31:01 +02:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
if(type == NBTType.SAVE_TILE || (type == NBTType.SYNC && this.shouldSyncSlots())){
|
2016-11-27 11:37:55 +01:00
|
|
|
loadSlots(this, 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-11-26 21:32:27 +01:00
|
|
|
public boolean isUsableByPlayer(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
|
|
|
|
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(){
|
2016-11-20 20:13:26 +01:00
|
|
|
this.slots.clear();
|
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){
|
2016-11-20 20:13:26 +01:00
|
|
|
this.slots.set(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-11-20 20:13:26 +01:00
|
|
|
return this.slots.size();
|
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-11-20 20:13:26 +01:00
|
|
|
return this.slots.get(i);
|
2015-05-07 16:36:29 +02:00
|
|
|
}
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
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-11-20 20:13:26 +01:00
|
|
|
if(StackUtil.isValid(this.slots.get(i))){
|
2014-12-12 15:40:01 +01:00
|
|
|
ItemStack stackAt;
|
2016-11-20 20:13:26 +01:00
|
|
|
if(StackUtil.getStackSize(this.slots.get(i)) <= j){
|
|
|
|
stackAt = this.slots.get(i);
|
|
|
|
this.slots.set(i, StackUtil.getNull());
|
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-11-20 20:13:26 +01:00
|
|
|
stackAt = this.slots.get(i).splitStack(j);
|
|
|
|
if(StackUtil.getStackSize(this.slots.get(i)) <= 0){
|
|
|
|
this.slots.set(i, StackUtil.getNull());
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 16:59:00 +01:00
|
|
|
return StackUtil.getNull();
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|
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-11-20 20:13:26 +01:00
|
|
|
ItemStack stack = this.slots.get(index);
|
|
|
|
this.slots.set(index, StackUtil.getNull());
|
2016-01-07 23:42:42 +01:00
|
|
|
return stack;
|
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 String getName(){
|
2016-11-20 13:46:35 +01:00
|
|
|
return this.getDisplayedName();
|
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-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-06-18 17:30:12 +02:00
|
|
|
return (T)this.invWrappers[facing == null ? 0 : facing.ordinal()];
|
2016-05-05 13:43:57 +02:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
return super.getCapability(capability, facing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasInvWrapperCapabilities(){
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-19 21:11:17 +01:00
|
|
|
|
|
|
|
@Override
|
2016-11-26 21:32:27 +01:00
|
|
|
public boolean isEmpty(){
|
2016-11-19 21:11:17 +01:00
|
|
|
return StackUtil.isIInvEmpty(this.slots);
|
|
|
|
}
|
2014-12-12 15:40:01 +01:00
|
|
|
}
|