Skip to content

Commit

Permalink
Add itemhandler capabilities to TileInventory
Browse files Browse the repository at this point in the history
  • Loading branch information
bonii-xx committed Apr 14, 2016
1 parent f7e7792 commit 21c02b6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/main/java/slimeknights/mantle/inventory/BaseContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,43 @@
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();
Expand Down Expand Up @@ -62,6 +73,10 @@ public T getTile() {
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.
Expand Down Expand Up @@ -100,8 +115,8 @@ public List<ItemStack> getInventory() {
}

public String getInventoryDisplayName() {
if(tile instanceof IInventory) {
return tile.getDisplayName().getFormattedText();
if(tile instanceof IWorldNameable) {
return ((IWorldNameable) tile).getDisplayName().getFormattedText();
}
return null;
}
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/slimeknights/mantle/inventory/SlotWrapper.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -38,6 +42,72 @@ public boolean canTakeStack(EntityPlayer playerIn) {
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() {
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/slimeknights/mantle/tileentity/TileInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
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;

Expand All @@ -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
Expand All @@ -35,9 +42,27 @@ public TileInventory(String name, int inventorySize, int maxStackSize) {
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) {
Expand Down

0 comments on commit 21c02b6

Please sign in to comment.