diff --git a/src/main/java/ellpeck/actuallyadditions/blocks/BlockPhantom.java b/src/main/java/ellpeck/actuallyadditions/blocks/BlockPhantom.java index e099f18c2..6ab9a7df1 100644 --- a/src/main/java/ellpeck/actuallyadditions/blocks/BlockPhantom.java +++ b/src/main/java/ellpeck/actuallyadditions/blocks/BlockPhantom.java @@ -14,9 +14,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import ellpeck.actuallyadditions.ActuallyAdditions; import ellpeck.actuallyadditions.config.values.ConfigIntValues; -import ellpeck.actuallyadditions.tile.IPhantomTile; -import ellpeck.actuallyadditions.tile.TileEntityPhantomPlacer; -import ellpeck.actuallyadditions.tile.TileEntityPhantomface; +import ellpeck.actuallyadditions.tile.*; import ellpeck.actuallyadditions.util.IActAddItemOrBlock; import ellpeck.actuallyadditions.util.ModUtil; import ellpeck.actuallyadditions.util.StringUtil; @@ -71,11 +69,11 @@ public class BlockPhantom extends BlockContainerBase implements IActAddItemOrBlo case BREAKER: return new TileEntityPhantomPlacer.TileEntityPhantomBreaker(); case LIQUIFACE: - return new TileEntityPhantomface.TileEntityPhantomLiquiface(); + return new TileEntityPhantomLiquiface(); case ENERGYFACE: - return new TileEntityPhantomface.TileEntityPhantomEnergyface(); + return new TileEntityPhantomEnergyface(); default: - return new TileEntityPhantomface.TileEntityPhantomItemface(); + return new TileEntityPhantomItemface(); } } diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java index 1d4034283..e666b69be 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java @@ -44,9 +44,9 @@ public class TileEntityBase extends TileEntity{ GameRegistry.registerTileEntity(TileEntityFermentingBarrel.class, ModUtil.MOD_ID_LOWER+":tileEntityFermentingBarrel"); GameRegistry.registerTileEntity(TileEntityOilGenerator.class, ModUtil.MOD_ID_LOWER+":tileEntityOilGenerator"); GameRegistry.registerTileEntity(TileEntityCoalGenerator.class, ModUtil.MOD_ID_LOWER+":tileEntityCoalGenerator"); - GameRegistry.registerTileEntity(TileEntityPhantomface.TileEntityPhantomItemface.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomItemface"); - GameRegistry.registerTileEntity(TileEntityPhantomface.TileEntityPhantomLiquiface.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomLiquiface"); - GameRegistry.registerTileEntity(TileEntityPhantomface.TileEntityPhantomEnergyface.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomEnergyface"); + GameRegistry.registerTileEntity(TileEntityPhantomItemface.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomItemface"); + GameRegistry.registerTileEntity(TileEntityPhantomLiquiface.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomLiquiface"); + GameRegistry.registerTileEntity(TileEntityPhantomEnergyface.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomEnergyface"); GameRegistry.registerTileEntity(TileEntityPhantomPlacer.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomPlacer"); GameRegistry.registerTileEntity(TileEntityPhantomPlacer.TileEntityPhantomBreaker.class, ModUtil.MOD_ID_LOWER+":tileEntityPhantomBreaker"); GameRegistry.registerTileEntity(TileEntityFluidCollector.class, ModUtil.MOD_ID_LOWER+":tileEntityFluidCollector"); diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomEnergyface.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomEnergyface.java new file mode 100644 index 000000000..d64e71c05 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomEnergyface.java @@ -0,0 +1,128 @@ +/* + * This file ("TileEntityPhantomEnergyface.java") is part of the Actually Additions Mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015 Ellpeck + */ + +package ellpeck.actuallyadditions.tile; + +import cofh.api.energy.IEnergyHandler; +import cofh.api.energy.IEnergyProvider; +import cofh.api.energy.IEnergyReceiver; +import ellpeck.actuallyadditions.blocks.BlockPhantom; +import ellpeck.actuallyadditions.util.WorldUtil; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.util.ForgeDirection; + +public class TileEntityPhantomEnergyface extends TileEntityPhantomface implements IEnergyHandler{ + + public TileEntityPhantomEnergyface(){ + super("energyface"); + this.type = BlockPhantom.Type.ENERGYFACE; + } + + @Override + public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){ + return this.isBoundThingInRange() && this.getReceiver() != null ? this.getReceiver().receiveEnergy(from, maxReceive, simulate) : 0; + } + + @Override + public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate){ + return this.isBoundThingInRange() && this.getProvider() != null ? this.getProvider().extractEnergy(from, maxExtract, simulate) : 0; + } + + @Override + public int getEnergyStored(ForgeDirection from){ + if(this.isBoundThingInRange()){ + if(this.getProvider() != null){ + return this.getProvider().getEnergyStored(from); + } + if(this.getReceiver() != null){ + return this.getReceiver().getEnergyStored(from); + } + } + return 0; + } + + @Override + public int getMaxEnergyStored(ForgeDirection from){ + if(this.isBoundThingInRange()){ + if(this.getProvider() != null){ + return this.getProvider().getMaxEnergyStored(from); + } + if(this.getReceiver() != null){ + return this.getReceiver().getMaxEnergyStored(from); + } + } + return 0; + } + + public IEnergyProvider getProvider(){ + if(this.boundPosition != null && this.boundPosition.getWorld() != null){ + TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); + if(tile instanceof IEnergyProvider){ + return (IEnergyProvider)tile; + } + } + return null; + } + + public IEnergyReceiver getReceiver(){ + if(this.boundPosition != null && this.boundPosition.getWorld() != null){ + TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); + if(tile instanceof IEnergyReceiver){ + return (IEnergyReceiver)tile; + } + } + return null; + } + + @Override + public void updateEntity(){ + super.updateEntity(); + + if(!worldObj.isRemote){ + if(this.isBoundThingInRange() && this.getProvider() != null){ + this.pushEnergy(ForgeDirection.UP); + this.pushEnergy(ForgeDirection.DOWN); + this.pushEnergy(ForgeDirection.NORTH); + this.pushEnergy(ForgeDirection.EAST); + this.pushEnergy(ForgeDirection.SOUTH); + this.pushEnergy(ForgeDirection.WEST); + } + } + } + + @Override + public boolean isBoundThingInRange(){ + return super.isBoundThingInRange() && (this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IEnergyReceiver || this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IEnergyProvider); + } + + private void pushEnergy(ForgeDirection side){ + TileEntity tile = WorldUtil.getTileEntityFromSide(side, worldObj, xCoord, yCoord, zCoord); + if(tile != null && tile instanceof IEnergyReceiver && this.getProvider().getEnergyStored(ForgeDirection.UNKNOWN) > 0){ + if(((IEnergyReceiver)tile).canConnectEnergy(side.getOpposite()) && this.canConnectEnergy(side)){ + int receive = this.extractEnergy(side, Math.min(((IEnergyReceiver)tile).getMaxEnergyStored(ForgeDirection.UNKNOWN)-((IEnergyReceiver)tile).getEnergyStored(ForgeDirection.UNKNOWN), this.getEnergyStored(ForgeDirection.UNKNOWN)), true); + int actualReceive = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), receive, false); + this.extractEnergy(side, actualReceive, false); + } + } + } + + @Override + public boolean canConnectEnergy(ForgeDirection from){ + if(this.isBoundThingInRange()){ + if(this.getProvider() != null){ + return this.getProvider().canConnectEnergy(from); + } + if(this.getReceiver() != null){ + return this.getReceiver().canConnectEnergy(from); + } + } + return false; + } +} diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomItemface.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomItemface.java new file mode 100644 index 000000000..c2376ff34 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomItemface.java @@ -0,0 +1,115 @@ +/* + * This file ("TileEntityPhantomItemface.java") is part of the Actually Additions Mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015 Ellpeck + */ + +package ellpeck.actuallyadditions.tile; + +import ellpeck.actuallyadditions.blocks.BlockPhantom; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; + +public class TileEntityPhantomItemface extends TileEntityPhantomface{ + + public TileEntityPhantomItemface(){ + super("phantomface"); + this.type = BlockPhantom.Type.FACE; + } + + @Override + public int[] getAccessibleSlotsFromSide(int side){ + if(this.isBoundThingInRange()){ + if(this.getSided() != null){ + return this.getSided().getAccessibleSlotsFromSide(side); + } + else{ + int[] theInt = new int[this.getSizeInventory()]; + for(int i = 0; i < theInt.length; i++){ + theInt[i] = i; + } + return theInt; + } + } + return new int[0]; + } + + public ISidedInventory getSided(){ + return this.getInventory() instanceof ISidedInventory ? (ISidedInventory)this.getInventory() : null; + } + + @Override + public int getInventoryStackLimit(){ + return this.isBoundThingInRange() ? this.getInventory().getInventoryStackLimit() : 0; + } + + @Override + public boolean isBoundThingInRange(){ + return super.isBoundThingInRange() && this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IInventory; + } + + public IInventory getInventory(){ + if(this.boundPosition != null && this.boundPosition.getWorld() != null){ + TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); + if(tile instanceof IInventory){ + return (IInventory)tile; + } + } + return null; + } + + @Override + public boolean canInsertItem(int slot, ItemStack stack, int side){ + return this.isBoundThingInRange() && (this.getSided() == null || this.getSided().canInsertItem(slot, stack, side)); + } + + @Override + public boolean canExtractItem(int slot, ItemStack stack, int side){ + return this.isBoundThingInRange() && (this.getSided() == null || this.getSided().canExtractItem(slot, stack, side)); + } + + @Override + public boolean isItemValidForSlot(int i, ItemStack stack){ + return this.isBoundThingInRange() && this.getInventory().isItemValidForSlot(i, stack); + } + + @Override + public ItemStack getStackInSlotOnClosing(int i){ + return this.isBoundThingInRange() ? this.getInventory().getStackInSlotOnClosing(i) : null; + } + + @Override + public void setInventorySlotContents(int i, ItemStack stack){ + if(this.isBoundThingInRange()){ + this.getInventory().setInventorySlotContents(i, stack); + } + this.markDirty(); + } + + @Override + public int getSizeInventory(){ + return this.isBoundThingInRange() ? this.getInventory().getSizeInventory() : 0; + } + + @Override + public ItemStack getStackInSlot(int i){ + return this.isBoundThingInRange() ? this.getInventory().getStackInSlot(i) : null; + } + + @Override + public ItemStack decrStackSize(int i, int j){ + return this.isBoundThingInRange() ? this.getInventory().decrStackSize(i, j) : null; + } + + @Override + public String getInventoryName(){ + return this.name; + } + +} diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomLiquiface.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomLiquiface.java new file mode 100644 index 000000000..9ad50d3c7 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomLiquiface.java @@ -0,0 +1,120 @@ +/* + * This file ("TileEntityPhantomLiquiface.java") is part of the Actually Additions Mod for Minecraft. + * It is created and owned by Ellpeck and distributed + * under the Actually Additions License to be found at + * http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015 Ellpeck + */ + +package ellpeck.actuallyadditions.tile; + +import ellpeck.actuallyadditions.blocks.BlockPhantom; +import ellpeck.actuallyadditions.util.WorldUtil; +import net.minecraft.tileentity.TileEntity; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidHandler; + +public class TileEntityPhantomLiquiface extends TileEntityPhantomface implements IFluidHandler{ + + public TileEntityPhantomLiquiface(){ + super("liquiface"); + this.type = BlockPhantom.Type.LIQUIFACE; + } + + @Override + public void updateEntity(){ + super.updateEntity(); + + if(!worldObj.isRemote){ + if(worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord) && this.isBoundThingInRange() && this.getHandler() != null){ + this.pushFluid(ForgeDirection.UP); + this.pushFluid(ForgeDirection.DOWN); + this.pushFluid(ForgeDirection.NORTH); + this.pushFluid(ForgeDirection.EAST); + this.pushFluid(ForgeDirection.SOUTH); + this.pushFluid(ForgeDirection.WEST); + } + } + } + + public IFluidHandler getHandler(){ + if(this.boundPosition != null && this.boundPosition.getWorld() != null){ + TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); + if(tile instanceof IFluidHandler){ + return (IFluidHandler)tile; + } + } + return null; + } + + private void pushFluid(ForgeDirection side){ + TileEntity tile = WorldUtil.getTileEntityFromSide(side, worldObj, xCoord, yCoord, zCoord); + if(tile != null && tile instanceof IFluidHandler && this.getTankInfo(side) != null && this.getTankInfo(side).length > 0 && ((IFluidHandler)tile).getTankInfo(side.getOpposite()) != null && ((IFluidHandler)tile).getTankInfo(side.getOpposite()).length > 0){ + for(FluidTankInfo myInfo : this.getTankInfo(side)){ + for(FluidTankInfo hisInfo : ((IFluidHandler)tile).getTankInfo(side.getOpposite())){ + if(myInfo != null && hisInfo != null && myInfo.fluid != null && myInfo.fluid.getFluid() != null){ + if(((IFluidHandler)tile).canFill(side.getOpposite(), myInfo.fluid.getFluid()) && this.canDrain(side, myInfo.fluid.getFluid())){ + FluidStack receive = this.drain(side, Math.min(hisInfo.capacity-(hisInfo.fluid == null ? 0 : hisInfo.fluid.amount), myInfo.fluid.amount), false); + if(receive != null){ + int actualReceive = ((IFluidHandler)tile).fill(side.getOpposite(), receive, true); + this.drain(side, new FluidStack(receive.getFluid(), actualReceive), true); + } + } + } + } + } + } + } + + @Override + public boolean isBoundThingInRange(){ + return super.isBoundThingInRange() && this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IFluidHandler; + } + + @Override + public int fill(ForgeDirection from, FluidStack resource, boolean doFill){ + if(this.isBoundThingInRange()){ + return this.getHandler().fill(from, resource, doFill); + } + return 0; + } + + @Override + public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain){ + if(this.isBoundThingInRange()){ + return this.getHandler().drain(from, resource, doDrain); + } + return null; + } + + @Override + public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain){ + if(this.isBoundThingInRange()){ + return this.getHandler().drain(from, maxDrain, doDrain); + } + return null; + } + + @Override + public boolean canFill(ForgeDirection from, Fluid fluid){ + return this.isBoundThingInRange() && this.getHandler().canFill(from, fluid); + } + + @Override + public boolean canDrain(ForgeDirection from, Fluid fluid){ + return this.isBoundThingInRange() && this.getHandler().canDrain(from, fluid); + } + + @Override + public FluidTankInfo[] getTankInfo(ForgeDirection from){ + if(this.isBoundThingInRange()){ + return this.getHandler().getTankInfo(from); + } + return new FluidTankInfo[0]; + } +} diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomface.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomface.java index c99698e58..40e1671ab 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomface.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityPhantomface.java @@ -10,27 +10,16 @@ package ellpeck.actuallyadditions.tile; -import cofh.api.energy.IEnergyHandler; -import cofh.api.energy.IEnergyProvider; -import cofh.api.energy.IEnergyReceiver; import ellpeck.actuallyadditions.blocks.BlockPhantom; import ellpeck.actuallyadditions.blocks.InitBlocks; import ellpeck.actuallyadditions.config.values.ConfigIntValues; import ellpeck.actuallyadditions.util.WorldPos; import ellpeck.actuallyadditions.util.WorldUtil; import net.minecraft.block.Block; -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; -import net.minecraftforge.common.util.ForgeDirection; -import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.FluidTankInfo; -import net.minecraftforge.fluids.IFluidHandler; public class TileEntityPhantomface extends TileEntityInventoryBase implements IPhantomTile{ @@ -163,315 +152,4 @@ public class TileEntityPhantomface extends TileEntityInventoryBase implements IP public boolean canExtractItem(int slot, ItemStack stack, int side){ return false; } - - public static class TileEntityPhantomLiquiface extends TileEntityPhantomface implements IFluidHandler{ - - public TileEntityPhantomLiquiface(){ - super("liquiface"); - this.type = BlockPhantom.Type.LIQUIFACE; - } - - @Override - public void updateEntity(){ - super.updateEntity(); - - if(!worldObj.isRemote){ - if(worldObj.isBlockIndirectlyGettingPowered(xCoord, yCoord, zCoord) && this.isBoundThingInRange() && this.getHandler() != null){ - this.pushFluid(ForgeDirection.UP); - this.pushFluid(ForgeDirection.DOWN); - this.pushFluid(ForgeDirection.NORTH); - this.pushFluid(ForgeDirection.EAST); - this.pushFluid(ForgeDirection.SOUTH); - this.pushFluid(ForgeDirection.WEST); - } - } - } - - public IFluidHandler getHandler(){ - if(this.boundPosition != null && this.boundPosition.getWorld() != null){ - TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); - if(tile instanceof IFluidHandler){ - return (IFluidHandler)tile; - } - } - return null; - } - - private void pushFluid(ForgeDirection side){ - TileEntity tile = WorldUtil.getTileEntityFromSide(side, worldObj, xCoord, yCoord, zCoord); - if(tile != null && tile instanceof IFluidHandler && this.getTankInfo(side) != null && this.getTankInfo(side).length > 0 && ((IFluidHandler)tile).getTankInfo(side.getOpposite()) != null && ((IFluidHandler)tile).getTankInfo(side.getOpposite()).length > 0){ - for(FluidTankInfo myInfo : this.getTankInfo(side)){ - for(FluidTankInfo hisInfo : ((IFluidHandler)tile).getTankInfo(side.getOpposite())){ - if(myInfo != null && hisInfo != null && myInfo.fluid != null && myInfo.fluid.getFluid() != null){ - if(((IFluidHandler)tile).canFill(side.getOpposite(), myInfo.fluid.getFluid()) && this.canDrain(side, myInfo.fluid.getFluid())){ - FluidStack receive = this.drain(side, Math.min(hisInfo.capacity-(hisInfo.fluid == null ? 0 : hisInfo.fluid.amount), myInfo.fluid.amount), false); - if(receive != null){ - int actualReceive = ((IFluidHandler)tile).fill(side.getOpposite(), receive, true); - this.drain(side, new FluidStack(receive.getFluid(), actualReceive), true); - } - } - } - } - } - } - } - - @Override - public int fill(ForgeDirection from, FluidStack resource, boolean doFill){ - if(this.isBoundThingInRange()){ - return this.getHandler().fill(from, resource, doFill); - } - return 0; - } - - @Override - public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain){ - if(this.isBoundThingInRange()){ - return this.getHandler().drain(from, resource, doDrain); - } - return null; - } - - @Override - public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain){ - if(this.isBoundThingInRange()){ - return this.getHandler().drain(from, maxDrain, doDrain); - } - return null; - } @Override - public boolean isBoundThingInRange(){ - return super.isBoundThingInRange() && this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IFluidHandler; - } - - @Override - public boolean canFill(ForgeDirection from, Fluid fluid){ - return this.isBoundThingInRange() && this.getHandler().canFill(from, fluid); - } - - @Override - public boolean canDrain(ForgeDirection from, Fluid fluid){ - return this.isBoundThingInRange() && this.getHandler().canDrain(from, fluid); - } - - @Override - public FluidTankInfo[] getTankInfo(ForgeDirection from){ - if(this.isBoundThingInRange()){ - return this.getHandler().getTankInfo(from); - } - return new FluidTankInfo[0]; - } - - - - - } - - public static class TileEntityPhantomEnergyface extends TileEntityPhantomface implements IEnergyHandler{ - - public TileEntityPhantomEnergyface(){ - super("energyface"); - this.type = BlockPhantom.Type.ENERGYFACE; - } - - @Override - public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){ - return this.isBoundThingInRange() && this.getReceiver() != null ? this.getReceiver().receiveEnergy(from, maxReceive, simulate) : 0; - } - - @Override - public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate){ - return this.isBoundThingInRange() && this.getProvider() != null ? this.getProvider().extractEnergy(from, maxExtract, simulate) : 0; - } - - @Override - public int getEnergyStored(ForgeDirection from){ - if(this.isBoundThingInRange()){ - if(this.getProvider() != null){ - return this.getProvider().getEnergyStored(from); - } - if(this.getReceiver() != null){ - return this.getReceiver().getEnergyStored(from); - } - } - return 0; - } - - @Override - public int getMaxEnergyStored(ForgeDirection from){ - if(this.isBoundThingInRange()){ - if(this.getProvider() != null){ - return this.getProvider().getMaxEnergyStored(from); - } - if(this.getReceiver() != null){ - return this.getReceiver().getMaxEnergyStored(from); - } - } - return 0; - } @Override - public boolean isBoundThingInRange(){ - return super.isBoundThingInRange() && (this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IEnergyReceiver || this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IEnergyProvider); - } - - public IEnergyProvider getProvider(){ - if(this.boundPosition != null && this.boundPosition.getWorld() != null){ - TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); - if(tile instanceof IEnergyProvider){ - return (IEnergyProvider)tile; - } - } - return null; - } - - public IEnergyReceiver getReceiver(){ - if(this.boundPosition != null && this.boundPosition.getWorld() != null){ - TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); - if(tile instanceof IEnergyReceiver){ - return (IEnergyReceiver)tile; - } - } - return null; - } - - - - @Override - public void updateEntity(){ - super.updateEntity(); - - if(!worldObj.isRemote){ - if(this.isBoundThingInRange() && this.getProvider() != null){ - this.pushEnergy(ForgeDirection.UP); - this.pushEnergy(ForgeDirection.DOWN); - this.pushEnergy(ForgeDirection.NORTH); - this.pushEnergy(ForgeDirection.EAST); - this.pushEnergy(ForgeDirection.SOUTH); - this.pushEnergy(ForgeDirection.WEST); - } - } - } - - - private void pushEnergy(ForgeDirection side){ - TileEntity tile = WorldUtil.getTileEntityFromSide(side, worldObj, xCoord, yCoord, zCoord); - if(tile != null && tile instanceof IEnergyReceiver && this.getProvider().getEnergyStored(ForgeDirection.UNKNOWN) > 0){ - if(((IEnergyReceiver)tile).canConnectEnergy(side.getOpposite()) && this.canConnectEnergy(side)){ - int receive = this.extractEnergy(side, Math.min(((IEnergyReceiver)tile).getMaxEnergyStored(ForgeDirection.UNKNOWN)-((IEnergyReceiver)tile).getEnergyStored(ForgeDirection.UNKNOWN), this.getEnergyStored(ForgeDirection.UNKNOWN)), true); - int actualReceive = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), receive, false); - this.extractEnergy(side, actualReceive, false); - } - } - } - - - @Override - public boolean canConnectEnergy(ForgeDirection from){ - if(this.isBoundThingInRange()){ - if(this.getProvider() != null){ - return this.getProvider().canConnectEnergy(from); - } - if(this.getReceiver() != null){ - return this.getReceiver().canConnectEnergy(from); - } - } - return false; - } - } - - public static class TileEntityPhantomItemface extends TileEntityPhantomface{ - - public TileEntityPhantomItemface(){ - super("phantomface"); - this.type = BlockPhantom.Type.FACE; - } - - public IInventory getInventory(){ - if(this.boundPosition != null && this.boundPosition.getWorld() != null){ - TileEntity tile = boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()); - if(tile instanceof IInventory){ - return (IInventory)tile; - } - } - return null; - } - - @Override - public boolean isBoundThingInRange(){ - return super.isBoundThingInRange() && this.boundPosition.getWorld().getTileEntity(boundPosition.getX(), boundPosition.getY(), boundPosition.getZ()) instanceof IInventory; - } - - public ISidedInventory getSided(){ - return this.getInventory() instanceof ISidedInventory ? (ISidedInventory)this.getInventory() : null; - } - - @Override - public int getInventoryStackLimit(){ - return this.isBoundThingInRange() ? this.getInventory().getInventoryStackLimit() : 0; - } - - @Override - public boolean isItemValidForSlot(int i, ItemStack stack){ - return this.isBoundThingInRange() && this.getInventory().isItemValidForSlot(i, stack); - } - - @Override - public ItemStack getStackInSlotOnClosing(int i){ - return this.isBoundThingInRange() ? this.getInventory().getStackInSlotOnClosing(i) : null; - } - - @Override - public void setInventorySlotContents(int i, ItemStack stack){ - if(this.isBoundThingInRange()){ - this.getInventory().setInventorySlotContents(i, stack); - } - this.markDirty(); - } - - @Override - public int getSizeInventory(){ - return this.isBoundThingInRange() ? this.getInventory().getSizeInventory() : 0; - } - - @Override - public ItemStack getStackInSlot(int i){ - return this.isBoundThingInRange() ? this.getInventory().getStackInSlot(i) : null; - } - - @Override - public ItemStack decrStackSize(int i, int j){ - return this.isBoundThingInRange() ? this.getInventory().decrStackSize(i, j) : null; - } - - @Override - public String getInventoryName(){ - return this.name; - } - - @Override - public int[] getAccessibleSlotsFromSide(int side){ - if(this.isBoundThingInRange()){ - if(this.getSided() != null){ - return this.getSided().getAccessibleSlotsFromSide(side); - } - else{ - int[] theInt = new int[this.getSizeInventory()]; - for(int i = 0; i < theInt.length; i++){ - theInt[i] = i; - } - return theInt; - } - } - return new int[0]; - } - - @Override - public boolean canInsertItem(int slot, ItemStack stack, int side){ - return this.isBoundThingInRange() && (this.getSided() == null || this.getSided().canInsertItem(slot, stack, side)); - } - - @Override - public boolean canExtractItem(int slot, ItemStack stack, int side){ - return this.isBoundThingInRange() && (this.getSided() == null || this.getSided().canExtractItem(slot, stack, side)); - } - - } }