mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-20 20:12:28 +00:00
Add itemhandler capabilities to TileInventory
This commit is contained in:
1 parent
f7e7792094
commit
21c02b66d6
3 files changed
+115
-5
No files matched your search
@@ -8,32 +8,43 @@ import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IWorldNameable;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.wrapper.EmptyHandler;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import slimeknights.mantle.util.SlimeknightException;
|
||||
|
||||
/** Same as Container but provides some extra functionality to simplify things */
|
||||
public abstract class BaseContainer<T extends TileEntity & IInventory> extends Container {
|
||||
public abstract class BaseContainer<T extends TileEntity> extends Container {
|
||||
|
||||
protected double maxDist = 8 * 8; // 8 blocks
|
||||
protected T tile;
|
||||
protected final Block originalBlock; // used to check if the block we interacted with got broken
|
||||
protected final BlockPos pos;
|
||||
protected final World world;
|
||||
protected final IItemHandler itemHandler;
|
||||
|
||||
public List<Container> subContainers = Lists.newArrayList();
|
||||
|
||||
public BaseContainer(T tile) {
|
||||
this.tile = tile;
|
||||
|
||||
if(tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
|
||||
itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
|
||||
}
|
||||
else {
|
||||
itemHandler = new EmptyHandler();
|
||||
}
|
||||
|
||||
this.world = tile.getWorld();
|
||||
this.pos = tile.getPos();
|
||||
this.originalBlock = world.getBlockState(pos).getBlock();
|
||||
@@ -62,6 +73,10 @@ public abstract class BaseContainer<T extends TileEntity & IInventory> extends C
|
||||
return tile;
|
||||
}
|
||||
|
||||
public IItemHandler getItemHandler() {
|
||||
return itemHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the container is opened and another player already has a container for this tile open
|
||||
* Sync to the same state here.
|
||||
@@ -100,8 +115,8 @@ public abstract class BaseContainer<T extends TileEntity & IInventory> extends C
|
||||
}
|
||||
|
||||
public String getInventoryDisplayName() {
|
||||
if(tile instanceof IInventory) {
|
||||
return tile.getDisplayName().getFormattedText();
|
||||
if(tile instanceof IWorldNameable) {
|
||||
return ((IWorldNameable) tile).getDisplayName().getFormattedText();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package slimeknights.mantle.inventory;
|
||||
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.renderer.texture.TextureMap;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import net.minecraftforge.fml.relauncher.SideOnly;
|
||||
|
||||
@@ -38,6 +42,72 @@ public class SlotWrapper extends Slot {
|
||||
return parent.canTakeStack(playerIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putStack(ItemStack stack) {
|
||||
parent.putStack(stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack) {
|
||||
parent.onPickupFromSlot(playerIn, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStack() {
|
||||
return parent.getStack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getHasStack() {
|
||||
return parent.getHasStack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotStackLimit() {
|
||||
return parent.getSlotStackLimit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemStackLimit(ItemStack stack) {
|
||||
return parent.getItemStackLimit(stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SideOnly(Side.CLIENT)
|
||||
public String getSlotTexture() {
|
||||
return parent.getSlotTexture();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack decrStackSize(int amount) {
|
||||
return parent.decrStackSize(amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isHere(IInventory inv, int slotIn) {
|
||||
return parent.isHere(inv, slotIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getBackgroundLocation() {
|
||||
return parent.getBackgroundLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBackgroundName(String name) {
|
||||
parent.setBackgroundName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextureAtlasSprite getBackgroundSprite() {
|
||||
return parent.getBackgroundSprite();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBackgroundLocation(ResourceLocation texture) {
|
||||
parent.setBackgroundLocation(texture);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@Override
|
||||
public boolean canBeHovered() {
|
||||
|
||||
@@ -7,9 +7,15 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.util.text.TextComponentTranslation;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.IItemHandlerModifiable;
|
||||
import net.minecraftforge.items.wrapper.InvWrapper;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -20,6 +26,7 @@ public class TileInventory extends TileEntity implements IInventory {
|
||||
protected String inventoryTitle;
|
||||
protected boolean hasCustomName;
|
||||
protected int stackSizeLimit;
|
||||
protected IItemHandlerModifiable itemHandler;
|
||||
|
||||
/**
|
||||
* @param name Localization String for the inventory title. Can be overridden through setCustomName
|
||||
@@ -35,9 +42,27 @@ public class TileInventory extends TileEntity implements IInventory {
|
||||
this.inventory = new ItemStack[inventorySize];
|
||||
this.stackSizeLimit = maxStackSize;
|
||||
this.inventoryTitle = name;
|
||||
this.itemHandler = new InvWrapper(this);
|
||||
}
|
||||
|
||||
/* Inventory management */
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
|
||||
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
|
||||
if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
|
||||
return (T) itemHandler;
|
||||
}
|
||||
return super.getCapability(capability, facing);
|
||||
}
|
||||
|
||||
public IItemHandlerModifiable getItemHandler() {
|
||||
return itemHandler;
|
||||
}
|
||||
|
||||
/* Inventory management */
|
||||
|
||||
@Override
|
||||
public ItemStack getStackInSlot(int slot) {
|
||||
|
||||
Reference in new issue
Block a user