Fix fluid generator client/server desync, fixes #1474

This commit is contained in:
modmuss50 2018-04-05 16:09:17 +01:00
parent f9d99aad1d
commit 9a566e0583
No known key found for this signature in database
GPG key ID: 773D17BE8BF49C82
6 changed files with 45 additions and 24 deletions

View file

@ -25,8 +25,6 @@
package techreborn.tiles.generator;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fluids.FluidStack;
@ -74,17 +72,20 @@ public abstract class TileBaseFluidGenerator extends TilePowerAcceptor implement
super.update();
this.ticksSinceLastChange++;
if(world.isRemote){
return;
}
// Check cells input slot 2 time per second
// Please, keep ticks counting on client also to report progress to GUI
if (this.ticksSinceLastChange >= 10) {
if (!world.isRemote) {
if (!this.inventory.getStackInSlot(0).isEmpty()) {
FluidUtils.drainContainers(this.tank, this.inventory, 0, 1);
FluidUtils.fillContainers(this.tank, this.inventory, 0, 1, this.tank.getFluidType());
}
tank.setTileEntity(this);
tank.compareAndUpdate();
if (!this.inventory.getStackInSlot(0).isEmpty()) {
FluidUtils.drainContainers(this.tank, this.inventory, 0, 1);
FluidUtils.fillContainers(this.tank, this.inventory, 0, 1, this.tank.getFluidType());
}
tank.setTileEntity(this);
tank.compareAndUpdate();
this.ticksSinceLastChange = 0;
}
@ -105,13 +106,12 @@ public abstract class TileBaseFluidGenerator extends TilePowerAcceptor implement
}
}
}
if (!this.world.isRemote) {
if (this.world.getTotalWorldTime() - this.lastOutput < 30 && !this.isActive())
this.world.setBlockState(this.getPos(), this.world.getBlockState(this.getPos()).withProperty(BlockMachineBase.ACTIVE, true));
else if (this.world.getTotalWorldTime() - this.lastOutput > 30 && this.isActive())
this.world.setBlockState(this.getPos(), this.world.getBlockState(this.getPos()).withProperty(BlockMachineBase.ACTIVE, false));
}
if (this.world.getTotalWorldTime() - this.lastOutput < 30 && !this.isActive())
this.world.setBlockState(this.getPos(), this.world.getBlockState(this.getPos()).withProperty(BlockMachineBase.ACTIVE, true));
else if (this.world.getTotalWorldTime() - this.lastOutput > 30 && this.isActive())
this.world.setBlockState(this.getPos(), this.world.getBlockState(this.getPos()).withProperty(BlockMachineBase.ACTIVE, false));
}
@ -130,6 +130,7 @@ public abstract class TileBaseFluidGenerator extends TilePowerAcceptor implement
addEnergy(this.getMaxPower() - this.getEnergy());
return true;
}
return false;
}
@ -201,15 +202,24 @@ public abstract class TileBaseFluidGenerator extends TilePowerAcceptor implement
return tagCompound;
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
world.markBlockRangeForRenderUpdate(getPos().getX(), getPos().getY(), getPos().getZ(), getPos().getX(),
getPos().getY(), getPos().getZ());
readFromNBT(packet.getNbtCompound());
}
@Override
public boolean canBeUpgraded() {
return false;
}
public int getTicksSinceLastChange() {
return ticksSinceLastChange;
}
public void setTicksSinceLastChange(int ticksSinceLastChange) {
this.ticksSinceLastChange = ticksSinceLastChange;
}
public int getTankAmount(){
return tank.getFluidAmount();
}
public void setTankAmount(int amount){
tank.setFluidAmount(amount);
}
}

View file

@ -70,6 +70,8 @@ public class TileDieselGenerator extends TileBaseFluidGenerator implements ICont
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("dieselgenerator").player(player.inventory).inventory().hotbar()
.addInventory().tile(this).slot(0, 25, 35).outputSlot(1, 25, 55).syncEnergyValue()
.syncIntegerValue(this::getTicksSinceLastChange, this::setTicksSinceLastChange)
.syncIntegerValue(this::getTankAmount, this::setTankAmount)
.addInventory().create(this);
}
}

View file

@ -70,6 +70,8 @@ public class TileGasTurbine extends TileBaseFluidGenerator implements IContainer
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("gasturbine").player(player.inventory).inventory().hotbar()
.addInventory().tile(this).slot(0, 25, 35).outputSlot(1, 25, 55).syncEnergyValue()
.syncIntegerValue(this::getTicksSinceLastChange, this::setTicksSinceLastChange)
.syncIntegerValue(this::getTankAmount, this::setTankAmount)
.addInventory().create(this);
}
}

View file

@ -66,7 +66,10 @@ public class TilePlasmaGenerator extends TileBaseFluidGenerator implements ICont
@Override
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("plasmagenerator").player(player.inventory).inventory().hotbar().addInventory()
.tile(this).slot(0, 25, 35).outputSlot(1, 25, 55).syncEnergyValue().addInventory().create(this);
.tile(this).slot(0, 25, 35).outputSlot(1, 25, 55).syncEnergyValue()
.syncIntegerValue(this::getTicksSinceLastChange, this::setTicksSinceLastChange)
.syncIntegerValue(this::getTankAmount, this::setTankAmount)
.addInventory().create(this);
}
}

View file

@ -70,6 +70,8 @@ public class TileSemiFluidGenerator extends TileBaseFluidGenerator implements IC
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("semifluidgenerator").player(player.inventory).inventory().hotbar()
.addInventory().tile(this).slot(0, 25, 35).outputSlot(1, 25, 55).syncEnergyValue()
.syncIntegerValue(this::getTicksSinceLastChange, this::setTicksSinceLastChange)
.syncIntegerValue(this::getTankAmount, this::setTankAmount)
.addInventory().create(this);
}
}

View file

@ -70,6 +70,8 @@ public class TileThermalGenerator extends TileBaseFluidGenerator implements ICon
public BuiltContainer createContainer(final EntityPlayer player) {
return new ContainerBuilder("thermalgenerator").player(player.inventory).inventory().hotbar()
.addInventory().tile(this).slot(0, 25, 35).outputSlot(1, 25, 55).syncEnergyValue()
.syncIntegerValue(this::getTicksSinceLastChange, this::setTicksSinceLastChange)
.syncIntegerValue(this::getTankAmount, this::setTankAmount)
.addInventory().create(this);
}
}