Finally made ESD use new capability system.

Closes #91
This commit is contained in:
Ellpeck 2016-07-20 22:14:30 +02:00
parent c31424944d
commit e9955348d8
6 changed files with 108 additions and 30 deletions

View file

@ -32,6 +32,7 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World; import net.minecraft.world.World;
public class BlockInputter extends BlockContainerBase{ public class BlockInputter extends BlockContainerBase{
@ -91,14 +92,12 @@ public class BlockInputter extends BlockContainerBase{
} }
@Override @Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ public void neighborsChangedCustom(World world, BlockPos pos){
super.neighborChanged(state, world, pos, block); super.neighborsChangedCustom(world, pos);
if(!world.isRemote){ TileEntity tile = world.getTileEntity(pos);
TileEntity tile = world.getTileEntity(pos); if(tile instanceof TileEntityInputter){
if(tile instanceof TileEntityInputter){ ((TileEntityInputter)tile).initVars();
((TileEntityInputter)tile).initVars();
}
} }
} }

View file

@ -20,6 +20,7 @@ import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World; import net.minecraft.world.World;
public class BlockItemViewer extends BlockContainerBase{ public class BlockItemViewer extends BlockContainerBase{
@ -34,14 +35,12 @@ public class BlockItemViewer extends BlockContainerBase{
@Override @Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ public void neighborsChangedCustom(World world, BlockPos pos){
super.neighborChanged(state, world, pos, block); super.neighborsChangedCustom(world, pos);
if(!world.isRemote){ TileEntity tile = world.getTileEntity(pos);
TileEntity tile = world.getTileEntity(pos); if(tile instanceof TileEntityItemViewer){
if(tile instanceof TileEntityItemViewer){ ((TileEntityItemViewer)tile).saveConnectedRelay();
((TileEntityItemViewer)tile).saveConnectedRelay();
}
} }
} }

View file

@ -114,14 +114,12 @@ public class BlockLaserRelay extends BlockContainerBase{
} }
@Override @Override
public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block){ public void neighborsChangedCustom(World world, BlockPos pos){
super.neighborChanged(state, world, pos, block); super.neighborsChangedCustom(world, pos);
if(!world.isRemote){ TileEntity tile = world.getTileEntity(pos);
TileEntity tile = world.getTileEntity(pos); if(tile instanceof TileEntityLaserRelay){
if(tile instanceof TileEntityLaserRelay){ ((TileEntityLaserRelay)tile).saveAllHandlersAround();
((TileEntityLaserRelay)tile).saveAllHandlersAround();
}
} }
} }

View file

@ -154,10 +154,22 @@ public abstract class BlockContainerBase extends BlockContainer implements ItemB
} }
} }
public void neighborsChangedCustom(World world, BlockPos pos){
this.updateRedstoneState(world, pos);
}
@Override @Override
public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn){ public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn){
super.neighborChanged(state, worldIn, pos, 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){ public void updateRedstoneState(World world, BlockPos pos){

View file

@ -21,6 +21,9 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
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;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
public class TileEntityInputter extends TileEntityInventoryBase implements IButtonReactor, INumberReactor{ public class TileEntityInputter extends TileEntityInventoryBase implements IButtonReactor, INumberReactor{
@ -80,10 +83,46 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
this.markDirty(); 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 * Pulls Items from the specified Slots on the specified Side
*/ */
private void pull(){ private void pull(){
if(this.newPulling()){
return;
}
//The Inventory to pull from //The Inventory to pull from
IInventory theInventory = (IInventory)this.placeToPull; IInventory theInventory = (IInventory)this.placeToPull;
//Does the Inventory even have Slots!? //Does the Inventory even have Slots!?
@ -183,6 +222,10 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
* (Check pull() for Description, similar to this) * (Check pull() for Description, similar to this)
*/ */
private void put(){ private void put(){
if(this.newPutting()){
return;
}
IInventory theInventory = (IInventory)this.placeToPut; IInventory theInventory = (IInventory)this.placeToPut;
if(theInventory.getSizeInventory() > 0){ if(theInventory.getSizeInventory() > 0){
int theSlotToPut = this.slotToPutStart; int theSlotToPut = this.slotToPutStart;
@ -281,20 +324,22 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
*/ */
public void initVars(){ public void initVars(){
if(this.sideToPull != -1){ 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 && this.placeToPull != null){
if(this.slotToPullEnd <= 0){ if(this.placeToPull instanceof IInventory){
this.slotToPullEnd = ((IInventory)this.placeToPull).getSizeInventory(); this.slotToPullEnd = ((IInventory)this.placeToPull).getSizeInventory();
} }
} }
} }
if(this.sideToPut != -1){ 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 && this.placeToPut != null){
if(this.slotToPutEnd <= 0){ if(this.placeToPut instanceof IInventory){
this.slotToPutEnd = ((IInventory)this.placeToPut).getSizeInventory(); this.slotToPutEnd = ((IInventory)this.placeToPut).getSizeInventory();
} }
} }
@ -395,10 +440,10 @@ public class TileEntityInputter extends TileEntityInventoryBase implements IButt
//Is Block not powered by Redstone? //Is Block not powered by Redstone?
if(!this.isRedstonePowered){ if(!this.isRedstonePowered){
if(!(this.sideToPull == this.sideToPut && this.slotToPullStart == this.slotToPutStart && this.slotToPullEnd == this.slotToPutEnd)){ 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(); this.pull();
} }
if(this.sideToPut != -1 && this.placeToPut instanceof IInventory){ if(this.slots[0] != null && this.sideToPut != -1 && this.placeToPut != null){
this.put(); this.put();
} }
} }

View file

@ -49,12 +49,37 @@ import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fluids.IFluidContainerItem; import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class WorldUtil{ 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){ public static void doEnergyInteraction(TileEntity tile){
for(EnumFacing side : EnumFacing.values()){ for(EnumFacing side : EnumFacing.values()){
TileEntity otherTile = tile.getWorld().getTileEntity(tile.getPos().offset(side)); TileEntity otherTile = tile.getWorld().getTileEntity(tile.getPos().offset(side));