From e9955348d81b39e72ceea3c90de6e55bf4570d6b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 20 Jul 2016 22:14:30 +0200 Subject: [PATCH] Finally made ESD use new capability system. Closes #91 --- .../mod/blocks/BlockInputter.java | 13 ++-- .../mod/blocks/BlockItemViewer.java | 13 ++-- .../mod/blocks/BlockLaserRelay.java | 12 ++-- .../mod/blocks/base/BlockContainerBase.java | 14 ++++- .../mod/tile/TileEntityInputter.java | 61 ++++++++++++++++--- .../actuallyadditions/mod/util/WorldUtil.java | 25 ++++++++ 6 files changed, 108 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockInputter.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockInputter.java index 76094ed0f..1ff9a76fc 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockInputter.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockInputter.java @@ -32,6 +32,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; public class BlockInputter extends BlockContainerBase{ @@ -91,14 +92,12 @@ public class BlockInputter extends BlockContainerBase{ } @Override - public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ - super.neighborChanged(state, world, pos, block); + public void neighborsChangedCustom(World world, BlockPos pos){ + super.neighborsChangedCustom(world, pos); - if(!world.isRemote){ - TileEntity tile = world.getTileEntity(pos); - if(tile instanceof TileEntityInputter){ - ((TileEntityInputter)tile).initVars(); - } + TileEntity tile = world.getTileEntity(pos); + if(tile instanceof TileEntityInputter){ + ((TileEntityInputter)tile).initVars(); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockItemViewer.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockItemViewer.java index 72cce7383..fc4ee32d0 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockItemViewer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockItemViewer.java @@ -20,6 +20,7 @@ import net.minecraft.item.EnumRarity; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; public class BlockItemViewer extends BlockContainerBase{ @@ -34,14 +35,12 @@ public class BlockItemViewer extends BlockContainerBase{ @Override - public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ - super.neighborChanged(state, world, pos, block); + public void neighborsChangedCustom(World world, BlockPos pos){ + super.neighborsChangedCustom(world, pos); - if(!world.isRemote){ - TileEntity tile = world.getTileEntity(pos); - if(tile instanceof TileEntityItemViewer){ - ((TileEntityItemViewer)tile).saveConnectedRelay(); - } + TileEntity tile = world.getTileEntity(pos); + if(tile instanceof TileEntityItemViewer){ + ((TileEntityItemViewer)tile).saveConnectedRelay(); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java index 248f39a8c..4df1c687c 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockLaserRelay.java @@ -114,14 +114,12 @@ public class BlockLaserRelay extends BlockContainerBase{ } @Override - public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ - super.neighborChanged(state, world, pos, block); + public void neighborsChangedCustom(World world, BlockPos pos){ + super.neighborsChangedCustom(world, pos); - if(!world.isRemote){ - TileEntity tile = world.getTileEntity(pos); - if(tile instanceof TileEntityLaserRelay){ - ((TileEntityLaserRelay)tile).saveAllHandlersAround(); - } + TileEntity tile = world.getTileEntity(pos); + if(tile instanceof TileEntityLaserRelay){ + ((TileEntityLaserRelay)tile).saveAllHandlersAround(); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockContainerBase.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockContainerBase.java index 15373376f..178a7d463 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockContainerBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/base/BlockContainerBase.java @@ -154,10 +154,22 @@ public abstract class BlockContainerBase extends BlockContainer implements ItemB } } + public void neighborsChangedCustom(World world, BlockPos pos){ + this.updateRedstoneState(world, pos); + } + @Override public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn){ super.neighborChanged(state, worldIn, pos, blockIn); - this.updateRedstoneState(worldIn, pos); + this.neighborsChangedCustom(worldIn, pos); + } + + @Override + public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor){ + super.onNeighborChange(world, pos, neighbor); + if(world instanceof World){ + this.neighborsChangedCustom((World)world, pos); + } } public void updateRedstoneState(World world, BlockPos pos){ diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java index 9b8e2cdd9..cf7524d76 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityInputter.java @@ -21,6 +21,9 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; public class TileEntityInputter extends TileEntityInventoryBase implements IButtonReactor, INumberReactor{ @@ -80,10 +83,46 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt this.markDirty(); } + private boolean newPulling(){ + for(EnumFacing side : EnumFacing.values()){ + if(this.placeToPull.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)){ + IItemHandler cap = this.placeToPull.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side); + if(cap != null){ + for(int i = Math.max(this.slotToPullStart, 0); i < Math.min(this.slotToPullEnd, cap.getSlots()); i++){ + if(WorldUtil.doItemInteraction(i, 0, this.placeToPull, this, side, null)){ + return true; + } + } + } + } + } + return false; + } + + private boolean newPutting(){ + for(EnumFacing side : EnumFacing.values()){ + if(this.placeToPut.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)){ + IItemHandler cap = this.placeToPut.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side); + if(cap != null){ + for(int i = Math.max(this.slotToPutStart, 0); i < Math.min(this.slotToPutEnd, cap.getSlots()); i++){ + if(WorldUtil.doItemInteraction(0, i, this, this.placeToPut, null, side)){ + return true; + } + } + } + } + } + return false; + } + /** * Pulls Items from the specified Slots on the specified Side */ private void pull(){ + if(this.newPulling()){ + return; + } + //The Inventory to pull from IInventory theInventory = (IInventory)this.placeToPull; //Does the Inventory even have Slots!? @@ -183,6 +222,10 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt * (Check pull() for Description, similar to this) */ private void put(){ + if(this.newPutting()){ + return; + } + IInventory theInventory = (IInventory)this.placeToPut; if(theInventory.getSizeInventory() > 0){ int theSlotToPut = this.slotToPutStart; @@ -281,20 +324,22 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt */ public void initVars(){ if(this.sideToPull != -1){ - this.placeToPull = this.worldObj.getTileEntity(this.pos.offset(WorldUtil.getDirectionBySidesInOrder(this.sideToPull))); + EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPull); + this.placeToPull = this.worldObj.getTileEntity(this.pos.offset(side)); - if(this.placeToPull instanceof IInventory){ - if(this.slotToPullEnd <= 0){ + if(this.slotToPullEnd <= 0 && this.placeToPull != null){ + if(this.placeToPull instanceof IInventory){ this.slotToPullEnd = ((IInventory)this.placeToPull).getSizeInventory(); } } } if(this.sideToPut != -1){ - this.placeToPut = this.worldObj.getTileEntity(this.pos.offset(WorldUtil.getDirectionBySidesInOrder(this.sideToPut))); + EnumFacing side = WorldUtil.getDirectionBySidesInOrder(this.sideToPut); + this.placeToPut = this.worldObj.getTileEntity(this.pos.offset(side)); - if(this.placeToPut instanceof IInventory){ - if(this.slotToPutEnd <= 0){ + if(this.slotToPutEnd <= 0 && this.placeToPut != null){ + if(this.placeToPut instanceof IInventory){ this.slotToPutEnd = ((IInventory)this.placeToPut).getSizeInventory(); } } @@ -395,10 +440,10 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt //Is Block not powered by Redstone? if(!this.isRedstonePowered){ if(!(this.sideToPull == this.sideToPut && this.slotToPullStart == this.slotToPutStart && this.slotToPullEnd == this.slotToPutEnd)){ - if(this.sideToPull != -1 && this.placeToPull instanceof IInventory){ + if(this.sideToPull != -1 && this.placeToPull != null){ this.pull(); } - if(this.sideToPut != -1 && this.placeToPut instanceof IInventory){ + if(this.slots[0] != null && this.sideToPut != -1 && this.placeToPut != null){ this.put(); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java index 90e29b4b4..fafa6799c 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java @@ -49,12 +49,37 @@ import net.minecraftforge.fluids.IFluidBlock; import net.minecraftforge.fluids.IFluidContainerItem; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; import java.util.ArrayList; import java.util.List; public final class WorldUtil{ + //This is probably hideous. Just saying. + public static boolean doItemInteraction(int slotExtract, int slotInsert, TileEntity extract, TileEntity insert, EnumFacing extractSide, EnumFacing insertSide){ + if(extract.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, extractSide) && insert.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, insertSide)){ + IItemHandler extractCap = extract.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, extractSide); + IItemHandler insertCap = insert.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, insertSide); + + if(extractCap != null && insertCap != null){ + ItemStack theoreticalExtract = extractCap.extractItem(slotExtract, Integer.MAX_VALUE, true); + if(theoreticalExtract != null){ + ItemStack remaining = insertCap.insertItem(slotInsert, theoreticalExtract, false); + + if(!ItemStack.areItemStacksEqual(remaining, theoreticalExtract)){ + int toExtract = remaining == null ? theoreticalExtract.stackSize : theoreticalExtract.stackSize-remaining.stackSize; + extractCap.extractItem(slotExtract, toExtract, false); + + return true; + } + } + } + } + return false; + } + public static void doEnergyInteraction(TileEntity tile){ for(EnumFacing side : EnumFacing.values()){ TileEntity otherTile = tile.getWorld().getTileEntity(tile.getPos().offset(side));