Initial work on Capabilities usage for inventories.

This commit is contained in:
WayofTime 2016-04-13 17:58:06 -04:00
parent 744feffc54
commit 7ca72320c5
4 changed files with 194 additions and 28 deletions

View file

@ -1,23 +1,26 @@
package WayofTime.bloodmagic.ritual; package WayofTime.bloodmagic.ritual;
import WayofTime.bloodmagic.api.Constants; import java.util.ArrayList;
import WayofTime.bloodmagic.api.network.SoulNetwork; import java.util.List;
import WayofTime.bloodmagic.api.ritual.*;
import WayofTime.bloodmagic.api.util.helper.NetworkHelper;
import WayofTime.bloodmagic.registry.ModBlocks;
import WayofTime.bloodmagic.util.Utils;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
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.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import WayofTime.bloodmagic.api.Constants;
import java.util.ArrayList; import WayofTime.bloodmagic.api.network.SoulNetwork;
import java.util.List; import WayofTime.bloodmagic.api.ritual.AreaDescriptor;
import WayofTime.bloodmagic.api.ritual.EnumRuneType;
import WayofTime.bloodmagic.api.ritual.IMasterRitualStone;
import WayofTime.bloodmagic.api.ritual.Ritual;
import WayofTime.bloodmagic.api.ritual.RitualComponent;
import WayofTime.bloodmagic.api.util.helper.NetworkHelper;
import WayofTime.bloodmagic.registry.ModBlocks;
import WayofTime.bloodmagic.util.Utils;
public class RitualCrushing extends Ritual public class RitualCrushing extends Ritual
{ {
@ -49,19 +52,6 @@ public class RitualCrushing extends Ritual
TileEntity tile = world.getTileEntity(masterRitualStone.getBlockPos().up()); TileEntity tile = world.getTileEntity(masterRitualStone.getBlockPos().up());
if (!(tile instanceof IInventory))
{
return;
}
IInventory tileEntity;
tileEntity = (IInventory) tile;
if (tileEntity.getSizeInventory() <= 0)
{
return;
}
boolean isSilkTouch = false; boolean isSilkTouch = false;
int fortune = 0; int fortune = 0;
@ -88,7 +78,7 @@ public class RitualCrushing extends Ritual
ItemStack item = new ItemStack(block, 1, meta); ItemStack item = new ItemStack(block, 1, meta);
ItemStack copyStack = ItemStack.copyItemStack(item); ItemStack copyStack = ItemStack.copyItemStack(item);
Utils.insertStackIntoInventory(copyStack, tileEntity, EnumFacing.DOWN); Utils.insertStackIntoTile(copyStack, tile, EnumFacing.DOWN);
if (copyStack.stackSize > 0) if (copyStack.stackSize > 0)
{ {
@ -104,8 +94,8 @@ public class RitualCrushing extends Ritual
{ {
ItemStack copyStack = ItemStack.copyItemStack(item); ItemStack copyStack = ItemStack.copyItemStack(item);
Utils.insertStackIntoInventory(copyStack, tileEntity, EnumFacing.DOWN); copyStack = Utils.insertStackIntoTile(copyStack, tile, EnumFacing.DOWN);
if (copyStack.stackSize > 0) if (copyStack != null && copyStack.stackSize > 0)
{ {
world.spawnEntityInWorld(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 2, pos.getZ() + 0.5, copyStack)); world.spawnEntityInWorld(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 2, pos.getZ() + 0.5, copyStack));
} }

View file

@ -1,9 +1,9 @@
package WayofTime.bloodmagic.tile; package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.util.helper.TextHelper;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.inventory.InventoryHelper; import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -12,10 +12,17 @@ import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet; import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity; import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
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.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import net.minecraftforge.items.wrapper.SidedInvWrapper;
import WayofTime.bloodmagic.util.helper.TextHelper;
public class TileInventory extends TileEntity implements IInventory public class TileInventory extends TileEntity implements IInventory
{ {
@ -29,6 +36,7 @@ public class TileInventory extends TileEntity implements IInventory
this.inventory = new ItemStack[size]; this.inventory = new ItemStack[size];
this.size = size; this.size = size;
this.name = name; this.name = name;
initializeItemHandlers();
} }
private boolean isSyncedSlot(int slot) private boolean isSyncedSlot(int slot)
@ -248,4 +256,64 @@ public class TileInventory extends TileEntity implements IInventory
{ {
return new TextComponentString(getName()); return new TextComponentString(getName());
} }
protected void initializeItemHandlers()
{
if (this instanceof ISidedInventory)
{
handlerDown = new SidedInvWrapper((ISidedInventory) this, EnumFacing.DOWN);
handlerUp = new SidedInvWrapper((ISidedInventory) this, EnumFacing.UP);
handlerNorth = new SidedInvWrapper((ISidedInventory) this, EnumFacing.NORTH);
handlerSouth = new SidedInvWrapper((ISidedInventory) this, EnumFacing.SOUTH);
handlerWest = new SidedInvWrapper((ISidedInventory) this, EnumFacing.WEST);
handlerEast = new SidedInvWrapper((ISidedInventory) this, EnumFacing.EAST);
} else
{
handlerDown = new InvWrapper(this);
handlerUp = handlerDown;
handlerNorth = handlerDown;
handlerSouth = handlerDown;
handlerWest = handlerDown;
handlerEast = handlerDown;
}
}
IItemHandler handlerDown;
IItemHandler handlerUp;
IItemHandler handlerNorth;
IItemHandler handlerSouth;
IItemHandler handlerWest;
IItemHandler handlerEast;
@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
{
if (facing != null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
{
switch (facing)
{
case DOWN:
return (T) handlerDown;
case EAST:
return (T) handlerEast;
case NORTH:
return (T) handlerNorth;
case SOUTH:
return (T) handlerSouth;
case UP:
return (T) handlerUp;
case WEST:
return (T) handlerWest;
}
}
return super.getCapability(capability, facing);
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
{
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
}
} }

View file

@ -33,6 +33,8 @@ import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidBlock; import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fluids.IFluidHandler; import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import WayofTime.bloodmagic.api.BlockStack; import WayofTime.bloodmagic.api.BlockStack;
import WayofTime.bloodmagic.api.altar.EnumAltarComponent; import WayofTime.bloodmagic.api.altar.EnumAltarComponent;
import WayofTime.bloodmagic.network.BloodMagicPacketHandler; import WayofTime.bloodmagic.network.BloodMagicPacketHandler;
@ -366,6 +368,102 @@ public class Utils
return returned; return returned;
} }
public static ItemStack insertStackIntoTile(ItemStack stack, TileEntity tile, EnumFacing dir)
{
if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir))
{
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir);
int numberOfSlots = handler.getSlots();
ItemStack copyStack = stack.copy();
for (int slot = 0; slot < numberOfSlots; slot++)
{
copyStack = handler.insertItem(slot, copyStack, false);
if (copyStack == null)
{
return null;
}
}
return copyStack;
} else if (tile instanceof IInventory)
{
return insertStackIntoInventory(stack, (IInventory) tile, dir);
}
return stack;
}
/**
* Inserts the desired stack into the tile up to a limit for the tile.
* Respects capabilities.
*
* @param stack
* @param tile
* @param dir
* @param limit
* @return
*/
public static ItemStack insertStackIntoTile(ItemStack stack, TileEntity tile, EnumFacing dir, int limit)
{
if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir))
{
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir);
int numberOfSlots = handler.getSlots();
ItemStack copyStack = stack.copy();
int numberMatching = 0;
for (int slot = 0; slot < numberOfSlots; slot++)
{
ItemStack invStack = handler.getStackInSlot(slot);
if (invStack != null && canCombine(stack, invStack))
{
numberMatching += invStack.stackSize;
}
}
if (numberMatching >= limit)
{
return stack;
}
int newLimit = limit - numberMatching;
for (int slot = 0; slot < numberOfSlots; slot++)
{
ItemStack newCopyStack = copyStack.copy();
newCopyStack.stackSize = Math.min(copyStack.stackSize, newLimit);
newCopyStack = handler.insertItem(slot, newCopyStack, false);
if (newCopyStack == null)
{
return null;
}
newLimit -= (copyStack.stackSize - newCopyStack.stackSize);
if (newLimit <= 0)
{
return null; //TODO
}
copyStack.stackSize -= (copyStack.stackSize - newCopyStack.stackSize);
}
return copyStack;
} else if (tile instanceof IInventory)
{
return insertStackIntoInventory(stack, (IInventory) tile, dir, limit);
}
return stack;
}
public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir) public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir)
{ {
if (stack == null) if (stack == null)
@ -495,6 +593,16 @@ public class Utils
return false; return false;
} }
/**
* Inserts the desired stack into the inventory up to a limit for the
* inventory.
*
* @param stack
* @param inventory
* @param dir
* @param limit
* @return
*/
public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir, int limit) public static ItemStack insertStackIntoInventory(ItemStack stack, IInventory inventory, EnumFacing dir, int limit)
{ {
if (stack == null) if (stack == null)
@ -554,7 +662,7 @@ public class Utils
ItemStack[] combinedStacks = combineStacks(stack, inventory.getStackInSlot(i), newLimit); ItemStack[] combinedStacks = combineStacks(stack, inventory.getStackInSlot(i), newLimit);
stack = combinedStacks[0]; stack = combinedStacks[0];
inventory.setInventorySlotContents(i, combinedStacks[1]); inventory.setInventorySlotContents(i, combinedStacks[1]); //TODO
newLimit -= (prevStackSize - stack.stackSize); newLimit -= (prevStackSize - stack.stackSize);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB