Changed the way fluids and RF are handled by always having them both be pulled and pushed.

Closes #125
This commit is contained in:
Ellpeck 2016-06-17 13:25:15 +02:00
parent bfd5311512
commit eaed253629
13 changed files with 73 additions and 121 deletions

View file

@ -10,10 +10,13 @@
package de.ellpeck.actuallyadditions.mod.tile; 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.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler; import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient; import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.util.ModUtil; import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
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.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -172,6 +175,16 @@ public abstract class TileEntityBase extends TileEntity implements ITickable{
public void updateEntity(){ public void updateEntity(){
this.ticksElapsed++; 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){ public void setRedstonePowered(boolean powered){

View file

@ -103,16 +103,6 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IE
this.currentProcessTime = 0; 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()){ if((this.storage.getEnergyStored() != this.lastEnergyStored || this.tank.getFluidAmount() != this.lastTankAmount | this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()){
this.lastEnergyStored = this.storage.getEnergyStored(); this.lastEnergyStored = this.storage.getEnergyStored();
this.lastProcessTime = this.currentProcessTime; this.lastProcessTime = this.currentProcessTime;

View file

@ -83,10 +83,6 @@ public class TileEntityCoalGenerator extends TileEntityInventoryBase implements
} }
} }
if(this.storage.getEnergyStored() > 0){
WorldUtil.pushEnergyToAllSides(this);
}
if(flag != this.currentBurnTime > 0){ if(flag != this.currentBurnTime > 0){
this.markDirty(); this.markDirty();
} }

View file

@ -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()){ if(this.lastEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.lastEnergy = this.storage.getEnergyStored(); this.lastEnergy = this.storage.getEnergyStored();
} }

View file

@ -88,16 +88,6 @@ public class TileEntityFermentingBarrel extends TileEntityBase implements IFluid
this.currentProcessTime = 0; 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()){ if((this.canolaTank.getFluidAmount() != this.lastCanola || this.oilTank.getFluidAmount() != this.lastOil || this.currentProcessTime != this.lastProcessTime) && this.sendUpdateWithInterval()){
this.lastProcessTime = this.currentProcessTime; this.lastProcessTime = this.currentProcessTime;
this.lastCanola = this.canolaTank.getFluidAmount(); this.lastCanola = this.canolaTank.getFluidAmount();

View file

@ -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()){ if(this.lastTankAmount != this.tank.getFluidAmount() && this.sendUpdateWithInterval()){
this.lastTankAmount = this.tank.getFluidAmount(); this.lastTankAmount = this.tank.getFluidAmount();
} }

View file

@ -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()){ if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored(); this.oldEnergy = this.storage.getEnergyStored();
} }

View file

@ -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()){ if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored(); this.oldEnergy = this.storage.getEnergyStored();
} }

View file

@ -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()){ if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored(); this.oldEnergy = this.storage.getEnergyStored();
} }

View file

@ -99,10 +99,6 @@ public class TileEntityOilGenerator extends TileEntityBase implements IEnergyPro
} }
} }
if(this.storage.getEnergyStored() > 0){
WorldUtil.pushEnergyToAllSides(this);
}
if(flag != this.currentBurnTime > 0){ if(flag != this.currentBurnTime > 0){
this.markDirty(); this.markDirty();
} }

View file

@ -80,17 +80,6 @@ public class TileEntityPhantomEnergyface extends TileEntityPhantomface implement
return null; return null;
} }
@Override
public void updateEntity(){
super.updateEntity();
if(!this.worldObj.isRemote){
if(this.isBoundThingInRange() && this.getProvider() != null){
WorldUtil.pushEnergyToAllSides(this);
}
}
}
@Override @Override
public boolean isBoundThingInRange(){ public boolean isBoundThingInRange(){
return super.isBoundThingInRange() && (this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyReceiver || this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyProvider); return super.isBoundThingInRange() && (this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyReceiver || this.worldObj.getTileEntity(this.boundPosition) instanceof IEnergyProvider);

View file

@ -30,14 +30,6 @@ public class TileEntityPhantomLiquiface extends TileEntityPhantomface implements
@Override @Override
public void updateEntity(){ public void updateEntity(){
super.updateEntity(); super.updateEntity();
if(!this.worldObj.isRemote){
if(this.isRedstonePowered && this.isBoundThingInRange()){
for(EnumFacing side : EnumFacing.values()){
WorldUtil.pushFluid(this, side);
}
}
}
} }
@Override @Override

View file

@ -10,7 +10,6 @@
package de.ellpeck.actuallyadditions.mod.util; package de.ellpeck.actuallyadditions.mod.util;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider; import cofh.api.energy.IEnergyProvider;
import cofh.api.energy.IEnergyReceiver; import cofh.api.energy.IEnergyReceiver;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -50,7 +49,7 @@ import net.minecraftforge.fluids.capability.IFluidHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class WorldUtil{ public final class WorldUtil{
public static void breakBlockAtSide(EnumFacing side, World world, BlockPos pos){ public static void breakBlockAtSide(EnumFacing side, World world, BlockPos pos){
breakBlockAtSide(side, world, pos, 0); 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)); 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){ public static void doEnergyInteraction(TileEntity tile){
pushEnergy(tileFrom, EnumFacing.UP); for(EnumFacing side : EnumFacing.values()){
pushEnergy(tileFrom, EnumFacing.DOWN); TileEntity otherTile = getTileEntityFromSide(side, tile.getWorld(), tile.getPos());
pushEnergy(tileFrom, EnumFacing.NORTH); if(otherTile != null){
pushEnergy(tileFrom, EnumFacing.EAST); IEnergyReceiver handlerTo = null;
pushEnergy(tileFrom, EnumFacing.SOUTH); IEnergyProvider handlerFrom = null;
pushEnergy(tileFrom, EnumFacing.WEST);
//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){ public static void doFluidInteraction(TileEntity tile){
TileEntity tileTo = getTileEntityFromSide(side, tileFrom.getWorld(), tileFrom.getPos()); for(EnumFacing side : EnumFacing.values()){
if(tileTo != null){ TileEntity otherTile = getTileEntityFromSide(side, tile.getWorld(), tile.getPos());
if(tileFrom instanceof IEnergyProvider && tileTo instanceof IEnergyReceiver){ if(otherTile != null){
IEnergyReceiver handlerTo = (IEnergyReceiver)tileTo; for(int i = 0; i < 2; i++){
IEnergyProvider handlerFrom = (IEnergyProvider)tileFrom; //Push and pull with old fluid system
int drain = handlerFrom.extractEnergy(side, Integer.MAX_VALUE, true); if(tile instanceof net.minecraftforge.fluids.IFluidHandler && otherTile instanceof net.minecraftforge.fluids.IFluidHandler){
if(drain > 0){ net.minecraftforge.fluids.IFluidHandler handlerTo = (net.minecraftforge.fluids.IFluidHandler)(i == 0 ? tile : otherTile);
if(handlerTo.canConnectEnergy(side.getOpposite())){ net.minecraftforge.fluids.IFluidHandler handlerFrom = (net.minecraftforge.fluids.IFluidHandler)(i == 0 ? otherTile : tile);
int filled = handlerTo.receiveEnergy(side.getOpposite(), drain, false); FluidStack drain = handlerFrom.drain(side, Integer.MAX_VALUE, false);
handlerFrom.extractEnergy(side, filled, 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; 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){ public static ItemStack useItemAtSide(EnumFacing side, World world, BlockPos pos, ItemStack stack){
if(world instanceof WorldServer && stack != null && stack.getItem() != null){ if(world instanceof WorldServer && stack != null && stack.getItem() != null){
BlockPos offsetPos = pos.offset(side); BlockPos offsetPos = pos.offset(side);