mirror of
https://github.com/GTNewHorizons/AE2FluidCraft-Rework.git
synced 2026-11-16 10:34:40 +00:00
always sync TE with server and optimize packet sending
use markBlockForUpdate to send the packet only to players who can see the tank also grouped packet-related methods in one place
This commit is contained in:
1 parent
b0f0c22eae
commit
a54ee0f6fb
3 files changed
+25
-62
No files matched your search
@@ -2,7 +2,6 @@ package com.glodblock.github.common.block;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
@@ -20,7 +19,6 @@ import net.minecraftforge.fluids.FluidStack;
|
||||
import com.glodblock.github.common.item.ItemCertusQuartzTank;
|
||||
import com.glodblock.github.common.tabs.FluidCraftingTabs;
|
||||
import com.glodblock.github.common.tile.TileCertusQuartzTank;
|
||||
import com.glodblock.github.loader.ChannelLoader;
|
||||
import com.glodblock.github.loader.IRegister;
|
||||
import com.glodblock.github.loader.ItemAndBlockHolder;
|
||||
import com.glodblock.github.util.NameConst;
|
||||
@@ -167,13 +165,6 @@ public class BlockCertusQuartzTank extends BaseBlockContainer implements IRegist
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNeighborBlockChange(World world, int x, int y, int z, Block neighborBlock) {
|
||||
if (!world.isRemote) {
|
||||
ChannelLoader.sendPacketToAllPlayers(world.getTileEntity(x, y, z).getDescriptionPacket(), world);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@SideOnly(Side.CLIENT)
|
||||
|
||||
@@ -14,12 +14,10 @@ import net.minecraftforge.fluids.FluidTank;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.IFluidHandler;
|
||||
|
||||
import com.glodblock.github.loader.ChannelLoader;
|
||||
import com.glodblock.github.util.BlockPos;
|
||||
|
||||
public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
|
||||
private FluidStack lastBeforeUpdate = null;
|
||||
public FluidTank tank = new FluidTank(32000);
|
||||
private boolean hasUpdate;
|
||||
|
||||
@@ -38,32 +36,6 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
return tankFluid == null || tankFluid.amount == 0 ? 0 : tankFluid.getFluid().getLuminosity(tankFluid);
|
||||
}
|
||||
|
||||
public void compareAndUpdate() {
|
||||
if (this.worldObj.isRemote) {
|
||||
return;
|
||||
}
|
||||
|
||||
FluidStack current = this.tank.getFluid();
|
||||
|
||||
if (current == null || this.lastBeforeUpdate == null) {
|
||||
if (current != this.lastBeforeUpdate) {
|
||||
ChannelLoader.sendPacketToAllPlayers(getDescriptionPacket(), this.worldObj);
|
||||
this.lastBeforeUpdate = current == null ? null : current.copy();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
boolean amountSufficientlyChanged = Math.abs(current.amount - this.lastBeforeUpdate.amount) >= 500;
|
||||
boolean isFull = current.amount == this.tank.getCapacity();
|
||||
boolean wasFull = this.lastBeforeUpdate.amount == this.tank.getCapacity();
|
||||
boolean fullStateChanged = isFull != wasFull;
|
||||
|
||||
if (amountSufficientlyChanged || fullStateChanged) {
|
||||
ChannelLoader.sendPacketToAllPlayers(getDescriptionPacket(), this.worldObj);
|
||||
this.lastBeforeUpdate = current.copy();
|
||||
}
|
||||
}
|
||||
|
||||
public FluidStack drain(FluidStack fluid, boolean doDrain, boolean findMainTank) {
|
||||
if (fluid == null || this.getFluid() == null || this.getFluid() != fluid.getFluid()) {
|
||||
return null;
|
||||
@@ -88,7 +60,9 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
FluidStack drainedFluid = this.tank.drain(fluid.amount, doDrain);
|
||||
int drained = drainedFluid != null ? drainedFluid.amount : 0;
|
||||
|
||||
compareAndUpdate();
|
||||
if (drained > 0) {
|
||||
update();
|
||||
}
|
||||
|
||||
if (drained < fluid.amount) {
|
||||
TileCertusQuartzTank tankBelow = this.getTankBelow();
|
||||
@@ -144,7 +118,10 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
}
|
||||
|
||||
int filled = this.tank.fill(fluid, doFill);
|
||||
compareAndUpdate();
|
||||
|
||||
if (filled > 0) {
|
||||
update();
|
||||
}
|
||||
|
||||
if (filled < fluid.amount) {
|
||||
TileCertusQuartzTank tankAbove = this.getTankAbove();
|
||||
@@ -165,13 +142,6 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
return fill(resource, doFill, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet getDescriptionPacket() {
|
||||
NBTTagCompound nbtTag = new NBTTagCompound();
|
||||
writeToNBT(nbtTag);
|
||||
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
|
||||
}
|
||||
|
||||
public Fluid getFluid() {
|
||||
FluidStack tankFluid = this.tank.getFluid();
|
||||
return tankFluid != null && tankFluid.amount > 0 ? tankFluid.getFluid() : null;
|
||||
@@ -268,6 +238,24 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
return getTankInfo(true);
|
||||
}
|
||||
|
||||
public FluidTankInfo[] getInternalFluid() {
|
||||
return this.getTankInfo(true);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if (!this.worldObj.isRemote) {
|
||||
markDirty();
|
||||
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet getDescriptionPacket() {
|
||||
NBTTagCompound nbtTag = new NBTTagCompound();
|
||||
writeToNBT(nbtTag);
|
||||
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
|
||||
this.worldObj.markBlockRangeForRenderUpdate(
|
||||
@@ -299,8 +287,4 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
|
||||
public void writeToNBTWithoutCoords(NBTTagCompound tag) {
|
||||
this.tank.writeToNBT(tag);
|
||||
}
|
||||
|
||||
public FluidTankInfo[] getInternalFluid() {
|
||||
return this.getTankInfo(true);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
package com.glodblock.github.loader;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.network.Packet;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import com.glodblock.github.FluidCraft;
|
||||
import com.glodblock.github.network.CPacketCraftRequest;
|
||||
import com.glodblock.github.network.CPacketDumpTank;
|
||||
@@ -100,12 +96,4 @@ public class ChannelLoader implements Runnable {
|
||||
id++,
|
||||
Side.CLIENT);
|
||||
}
|
||||
|
||||
public static void sendPacketToAllPlayers(Packet packet, World world) {
|
||||
for (Object player : world.playerEntities) {
|
||||
if (player instanceof EntityPlayerMP) {
|
||||
((EntityPlayerMP) player).playerNetServerHandler.sendPacket(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user