From eaed2536299b810bbd714b8d4d0e158b31b44c2e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 17 Jun 2016 13:25:15 +0200 Subject: [PATCH] Changed the way fluids and RF are handled by always having them both be pulled and pushed. Closes #125 --- .../mod/tile/TileEntityBase.java | 13 +++ .../mod/tile/TileEntityCanolaPress.java | 10 -- .../mod/tile/TileEntityCoalGenerator.java | 4 - .../mod/tile/TileEntityEnervator.java | 4 - .../mod/tile/TileEntityFermentingBarrel.java | 10 -- .../mod/tile/TileEntityFluidCollector.java | 10 -- .../mod/tile/TileEntityFurnaceSolar.java | 4 - .../mod/tile/TileEntityHeatCollector.java | 4 - .../mod/tile/TileEntityLeafGenerator.java | 4 - .../mod/tile/TileEntityOilGenerator.java | 4 - .../mod/tile/TileEntityPhantomEnergyface.java | 11 -- .../mod/tile/TileEntityPhantomLiquiface.java | 8 -- .../actuallyadditions/mod/util/WorldUtil.java | 108 ++++++++++-------- 13 files changed, 73 insertions(+), 121 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java index 5696cfc7e..bb8c241be 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityBase.java @@ -10,10 +10,13 @@ package de.ellpeck.actuallyadditions.mod.tile; +import cofh.api.energy.IEnergyProvider; +import cofh.api.energy.IEnergyReceiver; import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.mod.network.PacketHandler; import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient; import de.ellpeck.actuallyadditions.mod.util.ModUtil; +import de.ellpeck.actuallyadditions.mod.util.WorldUtil; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; @@ -172,6 +175,16 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{ public void updateEntity(){ this.ticksElapsed++; + + if(!this.worldObj.isRemote){ + if(this instanceof IEnergyReceiver || this instanceof IEnergyProvider){ + WorldUtil.doEnergyInteraction(this); + } + + if(this instanceof net.minecraftforge.fluids.IFluidHandler || this.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null)){ + WorldUtil.doFluidInteraction(this); + } + } } public void setRedstonePowered(boolean powered){ diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java index 89f456803..6570a6750 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java @@ -103,16 +103,6 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IE this.currentProcessTime = 0; } - if(this.tank.getFluidAmount() > 0){ - WorldUtil.pushFluid(this, EnumFacing.DOWN); - if(!this.isRedstonePowered){ - WorldUtil.pushFluid(this, EnumFacing.NORTH); - WorldUtil.pushFluid(this, EnumFacing.EAST); - WorldUtil.pushFluid(this, EnumFacing.SOUTH); - WorldUtil.pushFluid(this, EnumFacing.WEST); - } - } - if((this.storage.getEnergyStored() != this.lastEnergyStored || this.tank.getFluidAmount() != this.lastTankAmount | this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()){ this.lastEnergyStored = this.storage.getEnergyStored(); this.lastProcessTime = this.currentProcessTime; diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java index 80a82ee82..2a652e4bd 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java @@ -83,10 +83,6 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements } } - if(this.storage.getEnergyStored() > 0){ - WorldUtil.pushEnergyToAllSides(this); - } - if(flag != this.currentBurnTime > 0){ this.markDirty(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java index 6c41caf0b..36363aeee 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityEnervator.java @@ -60,10 +60,6 @@ public class TileEntityEnervator extends TileEntityInventoryBase implements IEne } } - if(this.storage.getEnergyStored() > 0){ - WorldUtil.pushEnergyToAllSides(this); - } - if(this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ this.lastEnergy = this.storage.getEnergyStored(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java index e602c0288..b76b7dda1 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java @@ -88,16 +88,6 @@ public class TileEntityFermentingBarrel extends TileEntityBase implements IFluid this.currentProcessTime = 0; } - if(this.oilTank.getFluidAmount() > 0){ - WorldUtil.pushFluid(this, EnumFacing.DOWN); - if(!this.isRedstonePowered){ - WorldUtil.pushFluid(this, EnumFacing.NORTH); - WorldUtil.pushFluid(this, EnumFacing.EAST); - WorldUtil.pushFluid(this, EnumFacing.SOUTH); - WorldUtil.pushFluid(this, EnumFacing.WEST); - } - } - if((this.canolaTank.getFluidAmount() != this.lastCanola || this.oilTank.getFluidAmount() != this.lastOil || this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()){ this.lastProcessTime = this.currentProcessTime; this.lastCanola = this.canolaTank.getFluidAmount(); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java index d8550c945..42eb5e093 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java @@ -134,16 +134,6 @@ public class TileEntityFluidCollector extends TileEntityBase implements IFluidSa } } - if(!this.isPlacer && this.tank.getFluidAmount() > 0){ - WorldUtil.pushFluid(this, EnumFacing.DOWN); - if(!this.isRedstonePowered){ - WorldUtil.pushFluid(this, EnumFacing.NORTH); - WorldUtil.pushFluid(this, EnumFacing.EAST); - WorldUtil.pushFluid(this, EnumFacing.SOUTH); - WorldUtil.pushFluid(this, EnumFacing.WEST); - } - } - if(this.lastTankAmount != this.tank.getFluidAmount() && this.sendUpdateWithInterval()){ this.lastTankAmount = this.tank.getFluidAmount(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceSolar.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceSolar.java index 955e0af18..1ec9e0536 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceSolar.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFurnaceSolar.java @@ -73,10 +73,6 @@ public class TileEntityFurnaceSolar extends TileEntityBase implements IEnergyPro } } - if(this.storage.getEnergyStored() > 0){ - WorldUtil.pushEnergyToAllSides(this); - } - if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ this.oldEnergy = this.storage.getEnergyStored(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityHeatCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityHeatCollector.java index 10ce2edb9..ce2b3ae11 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityHeatCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityHeatCollector.java @@ -73,10 +73,6 @@ public class TileEntityHeatCollector extends TileEntityBase implements IEnergyPr } } - if(this.storage.getEnergyStored() > 0){ - WorldUtil.pushEnergyToAllSides(this); - } - if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ this.oldEnergy = this.storage.getEnergyStored(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLeafGenerator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLeafGenerator.java index e8abf01dc..c6f1d273e 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLeafGenerator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityLeafGenerator.java @@ -100,10 +100,6 @@ public class TileEntityLeafGenerator extends TileEntityBase implements IEnergyPr } } - if(this.storage.getEnergyStored() > 0){ - WorldUtil.pushEnergyToAllSides(this); - } - if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){ this.oldEnergy = this.storage.getEnergyStored(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java index 9f59130ba..9f5163931 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java @@ -99,10 +99,6 @@ public class TileEntityOilGenerator extends TileEntityBase implements IEnergyPro } } - if(this.storage.getEnergyStored() > 0){ - WorldUtil.pushEnergyToAllSides(this); - } - if(flag != this.currentBurnTime > 0){ this.markDirty(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java index 46adf947f..4b7fb4036 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomEnergyface.java @@ -80,17 +80,6 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement return null; } - @Override - public void updateEntity(){ - super.updateEntity(); - - if(!this.worldObj.isRemote){ - if(this.isBoundThingInRange() && this.getProvider() != null){ - WorldUtil.pushEnergyToAllSides(this); - } - } - } - @Override public boolean isBoundThingInRange(){ return super.isBoundThingInRange() && (this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyReceiver || this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyProvider); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomLiquiface.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomLiquiface.java index 258c93de6..de7f76ed0 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomLiquiface.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityPhantomLiquiface.java @@ -30,14 +30,6 @@ public class TileEntityPhantomLiquiface extends TileEntityPhantomface implements @Override public void updateEntity(){ super.updateEntity(); - - if(!this.worldObj.isRemote){ - if(this.isRedstonePowered && this.isBoundThingInRange()){ - for(EnumFacing side : EnumFacing.values()){ - WorldUtil.pushFluid(this, side); - } - } - } } @Override 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 0053ffd40..700a66bec 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/util/WorldUtil.java @@ -10,7 +10,6 @@ package de.ellpeck.actuallyadditions.mod.util; -import cofh.api.energy.EnergyStorage; import cofh.api.energy.IEnergyProvider; import cofh.api.energy.IEnergyReceiver; import net.minecraft.block.Block; @@ -50,7 +49,7 @@ import net.minecraftforge.fluids.capability.IFluidHandler; import java.util.ArrayList; import java.util.List; -public class WorldUtil{ +public final class WorldUtil{ public static void breakBlockAtSide(EnumFacing side, World world, BlockPos pos){ breakBlockAtSide(side, world, pos, 0); @@ -64,26 +63,67 @@ public class WorldUtil{ return new BlockPos(pos.getX()+side.getFrontOffsetX()*(offset+1), pos.getY()+side.getFrontOffsetY()*(offset+1), pos.getZ()+side.getFrontOffsetZ()*(offset+1)); } - public static void pushEnergyToAllSides(TileEntity tileFrom){ - pushEnergy(tileFrom, EnumFacing.UP); - pushEnergy(tileFrom, EnumFacing.DOWN); - pushEnergy(tileFrom, EnumFacing.NORTH); - pushEnergy(tileFrom, EnumFacing.EAST); - pushEnergy(tileFrom, EnumFacing.SOUTH); - pushEnergy(tileFrom, EnumFacing.WEST); + public static void doEnergyInteraction(TileEntity tile){ + for(EnumFacing side : EnumFacing.values()){ + TileEntity otherTile = getTileEntityFromSide(side, tile.getWorld(), tile.getPos()); + if(otherTile != null){ + IEnergyReceiver handlerTo = null; + IEnergyProvider handlerFrom = null; + + //Push RF + if(tile instanceof IEnergyProvider && otherTile instanceof IEnergyReceiver){ + handlerTo = (IEnergyReceiver)otherTile; + handlerFrom = (IEnergyProvider)tile; + } + //Pull RF + else if(tile instanceof IEnergyReceiver && otherTile instanceof IEnergyProvider){ + handlerTo = (IEnergyReceiver)tile; + handlerFrom = (IEnergyProvider)otherTile; + } + + if(handlerFrom != null && handlerTo != null){ + int drain = handlerFrom.extractEnergy(side, Integer.MAX_VALUE, true); + if(drain > 0){ + if(handlerTo.canConnectEnergy(side.getOpposite())){ + int filled = handlerTo.receiveEnergy(side.getOpposite(), drain, false); + handlerFrom.extractEnergy(side, filled, false); + } + } + } + } + } } - public static void pushEnergy(TileEntity tileFrom, EnumFacing side){ - TileEntity tileTo = getTileEntityFromSide(side, tileFrom.getWorld(), tileFrom.getPos()); - if(tileTo != null){ - if(tileFrom instanceof IEnergyProvider && tileTo instanceof IEnergyReceiver){ - IEnergyReceiver handlerTo = (IEnergyReceiver)tileTo; - IEnergyProvider handlerFrom = (IEnergyProvider)tileFrom; - int drain = handlerFrom.extractEnergy(side, Integer.MAX_VALUE, true); - if(drain > 0){ - if(handlerTo.canConnectEnergy(side.getOpposite())){ - int filled = handlerTo.receiveEnergy(side.getOpposite(), drain, false); - handlerFrom.extractEnergy(side, filled, false); + public static void doFluidInteraction(TileEntity tile){ + for(EnumFacing side : EnumFacing.values()){ + TileEntity otherTile = getTileEntityFromSide(side, tile.getWorld(), tile.getPos()); + if(otherTile != null){ + for(int i = 0; i < 2; i++){ + //Push and pull with old fluid system + if(tile instanceof net.minecraftforge.fluids.IFluidHandler && otherTile instanceof net.minecraftforge.fluids.IFluidHandler){ + net.minecraftforge.fluids.IFluidHandler handlerTo = (net.minecraftforge.fluids.IFluidHandler)(i == 0 ? tile : otherTile); + net.minecraftforge.fluids.IFluidHandler handlerFrom = (net.minecraftforge.fluids.IFluidHandler)(i == 0 ? otherTile : tile); + FluidStack drain = handlerFrom.drain(side, Integer.MAX_VALUE, false); + if(drain != null){ + if(handlerTo.canFill(side.getOpposite(), drain.getFluid())){ + int filled = handlerTo.fill(side.getOpposite(), drain.copy(), true); + handlerFrom.drain(side, filled, true); + break; + } + } + } + //Push and pull with new fluid system + else{ + IFluidHandler handlerFrom = tile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, i == 0 ? side.getOpposite() : side); + IFluidHandler handlerTo = otherTile.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, i == 0 ? side : side.getOpposite()); + if(handlerFrom != null && handlerTo != null){ + FluidStack drain = handlerFrom.drain(Integer.MAX_VALUE, false); + if(drain != null){ + int filled = handlerTo.fill(drain.copy(), true); + handlerFrom.drain(filled, true); + break; + } + } } } } @@ -113,34 +153,6 @@ public class WorldUtil{ return true; } - public static void pushFluid(TileEntity tileFrom, EnumFacing side){ - TileEntity tileTo = getTileEntityFromSide(side, tileFrom.getWorld(), tileFrom.getPos()); - if(tileTo != null){ - if(tileFrom instanceof net.minecraftforge.fluids.IFluidHandler && tileTo instanceof net.minecraftforge.fluids.IFluidHandler){ - net.minecraftforge.fluids.IFluidHandler handlerTo = (net.minecraftforge.fluids.IFluidHandler)tileTo; - net.minecraftforge.fluids.IFluidHandler handlerFrom = (net.minecraftforge.fluids.IFluidHandler)tileFrom; - FluidStack drain = handlerFrom.drain(side, Integer.MAX_VALUE, false); - if(drain != null){ - if(handlerTo.canFill(side.getOpposite(), drain.getFluid())){ - int filled = handlerTo.fill(side.getOpposite(), drain.copy(), true); - handlerFrom.drain(side, filled, true); - } - } - } - else{ - IFluidHandler handlerFrom = tileFrom.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side); - IFluidHandler handlerTo = tileTo.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side.getOpposite()); - if(handlerFrom != null && handlerTo != null){ - FluidStack drain = handlerFrom.drain(Integer.MAX_VALUE, false); - if(drain != null){ - int filled = handlerTo.fill(drain.copy(), true); - handlerFrom.drain(filled, true); - } - } - } - } - } - public static ItemStack useItemAtSide(EnumFacing side, World world, BlockPos pos, ItemStack stack){ if(world instanceof WorldServer && stack != null && stack.getItem() != null){ BlockPos offsetPos = pos.offset(side);