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;
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){

View file

@ -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;

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){
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()){
this.lastEnergy = this.storage.getEnergyStored();
}

View file

@ -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();

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()){
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()){
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()){
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()){
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){
this.markDirty();
}

View file

@ -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);

View file

@ -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

View file

@ -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);