Added InvWrapper Capabilities to inventories

This commit is contained in:
Ellpeck 2016-05-05 13:43:57 +02:00
parent b3729703b7
commit 11bc13aefd
3 changed files with 38 additions and 1 deletions

View file

@ -20,15 +20,23 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.wrapper.SidedInvWrapper;
public abstract class TileEntityInventoryBase extends TileEntityBase implements ISidedInventory{ public abstract class TileEntityInventoryBase extends TileEntityBase implements ISidedInventory{
public ItemStack slots[]; public ItemStack slots[];
public String name; public String name;
private SidedInvWrapper[] invWrappers = new SidedInvWrapper[6];
public TileEntityInventoryBase(int slots, String name){ public TileEntityInventoryBase(int slots, String name){
this.initializeSlots(slots); this.initializeSlots(slots);
this.name = "container."+ModUtil.MOD_ID+"."+name; this.name = "container."+ModUtil.MOD_ID+"."+name;
for(int i = 0; i < this.invWrappers.length; i++){
this.invWrappers[i] = new SidedInvWrapper(this, EnumFacing.values()[i]);
}
} }
public void initializeSlots(int itemAmount){ public void initializeSlots(int itemAmount){
@ -203,4 +211,22 @@ public abstract class TileEntityInventoryBase extends TileEntityBase implements
return new TextComponentString(StringUtil.localize(this.getName())); return new TextComponentString(StringUtil.localize(this.getName()));
} }
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing){
return this.getCapability(capability, facing) != null;
}
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
if(this.hasInvWrapperCapabilities() && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY){
return (T)this.invWrappers[facing.ordinal()];
}
else{
return super.getCapability(capability, facing);
}
}
public boolean hasInvWrapperCapabilities(){
return true;
}
} }

View file

@ -16,6 +16,7 @@ import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
public class TileEntityPhantomItemface extends TileEntityPhantomface{ public class TileEntityPhantomItemface extends TileEntityPhantomface{
@ -110,7 +111,7 @@ public class TileEntityPhantomItemface extends TileEntityPhantomface{
@Override @Override
public boolean isBoundThingInRange(){ public boolean isBoundThingInRange(){
return super.isBoundThingInRange() && this.worldObj.getTileEntity(this.boundPosition) instanceof IInventory; return super.isBoundThingInRange() && this.getInventory() != null;
} }
@Override @Override
@ -122,4 +123,9 @@ public class TileEntityPhantomItemface extends TileEntityPhantomface{
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){ public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
return this.isBoundThingInRange() && (this.getSided() == null || this.getSided().canExtractItem(slot, stack, side)); return this.isBoundThingInRange() && (this.getSided() == null || this.getSided().canExtractItem(slot, stack, side));
} }
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing){
return this.isBoundThingInRange() ? this.worldObj.getTileEntity(this.boundPosition).getCapability(capability, facing) : super.getCapability(capability, facing);
}
} }

View file

@ -179,4 +179,9 @@ public class TileEntityPhantomface extends TileEntityInventoryBase implements IP
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){ public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
return false; return false;
} }
@Override
public boolean hasInvWrapperCapabilities(){
return false;
}
} }