From b9f80c0365d4bf3bb10b3d63ce5535b515b5de34 Mon Sep 17 00:00:00 2001 From: aidancbrady Date: Wed, 17 Jun 2015 13:26:47 -0400 Subject: [PATCH] Dynamic Tanks are now server friendly, closes #2690 --- .../render/tileentity/RenderDynamicTank.java | 19 ++--- .../common/content/tank/DynamicFluidTank.java | 20 ++--- .../content/tank/SynchronizedTankData.java | 32 +++++++- .../common/tile/TileEntityDynamicTank.java | 76 +++++++++++-------- 4 files changed, 89 insertions(+), 58 deletions(-) diff --git a/src/main/java/mekanism/client/render/tileentity/RenderDynamicTank.java b/src/main/java/mekanism/client/render/tileentity/RenderDynamicTank.java index 48a39bef2..54444d7a5 100644 --- a/src/main/java/mekanism/client/render/tileentity/RenderDynamicTank.java +++ b/src/main/java/mekanism/client/render/tileentity/RenderDynamicTank.java @@ -73,23 +73,20 @@ public class RenderDynamicTank extends TileEntitySpecialRenderer pop(); - for(ValveData valveData : tileEntity.valveViewing.keySet()) + for(ValveData valveData : tileEntity.valveViewing) { - if(tileEntity.valveViewing.get(valveData) > 0) - { - push(); + push(); - GL11.glTranslated(getX(valveData.location.xCoord), getY(valveData.location.yCoord), getZ(valveData.location.zCoord)); + GL11.glTranslated(getX(valveData.location.xCoord), getY(valveData.location.yCoord), getZ(valveData.location.zCoord)); - MekanismRenderer.glowOn(tileEntity.structure.fluidStored.getFluid().getLuminosity()); + MekanismRenderer.glowOn(tileEntity.structure.fluidStored.getFluid().getLuminosity()); - getValveDisplay(ValveRenderData.get(data, valveData), tileEntity.structure.fluidStored.getFluid(), tileEntity.getWorldObj()).render(); + getValveDisplay(ValveRenderData.get(data, valveData), tileEntity.structure.fluidStored.getFluid(), tileEntity.getWorldObj()).render(); - MekanismRenderer.glowOff(); - MekanismRenderer.resetColor(); + MekanismRenderer.glowOff(); + MekanismRenderer.resetColor(); - pop(); - } + pop(); } } } diff --git a/src/main/java/mekanism/common/content/tank/DynamicFluidTank.java b/src/main/java/mekanism/common/content/tank/DynamicFluidTank.java index 59afdaac0..1b0d3b10b 100644 --- a/src/main/java/mekanism/common/content/tank/DynamicFluidTank.java +++ b/src/main/java/mekanism/common/content/tank/DynamicFluidTank.java @@ -55,9 +55,7 @@ public class DynamicFluidTank implements IFluidTank if(resource.amount > 0) { MekanismUtils.saveChunk(dynamicTank); - updateValveData(true); - dynamicTank.sendPacketToRenderer(); - updateValveData(false); + updateValveData(); } } @@ -72,9 +70,7 @@ public class DynamicFluidTank implements IFluidTank if(getCapacity() > 0) { MekanismUtils.saveChunk(dynamicTank); - updateValveData(true); - dynamicTank.sendPacketToRenderer(); - updateValveData(false); + updateValveData(); } } @@ -90,9 +86,7 @@ public class DynamicFluidTank implements IFluidTank if(resource.amount > 0) { MekanismUtils.saveChunk(dynamicTank); - updateValveData(true); - dynamicTank.sendPacketToRenderer(); - updateValveData(false); + updateValveData(); } } @@ -108,9 +102,7 @@ public class DynamicFluidTank implements IFluidTank if(prevNeeded > 0) { MekanismUtils.saveChunk(dynamicTank); - updateValveData(true); - dynamicTank.sendPacketToRenderer(); - updateValveData(false); + updateValveData(); } } @@ -121,7 +113,7 @@ public class DynamicFluidTank implements IFluidTank return 0; } - public void updateValveData(boolean value) + public void updateValveData() { if(dynamicTank.structure != null) { @@ -129,7 +121,7 @@ public class DynamicFluidTank implements IFluidTank { if(data.location.equals(Coord4D.get(dynamicTank))) { - data.serverFluid = value; + data.onTransfer(); } } } diff --git a/src/main/java/mekanism/common/content/tank/SynchronizedTankData.java b/src/main/java/mekanism/common/content/tank/SynchronizedTankData.java index 564021541..7e231adc6 100644 --- a/src/main/java/mekanism/common/content/tank/SynchronizedTankData.java +++ b/src/main/java/mekanism/common/content/tank/SynchronizedTankData.java @@ -6,21 +6,42 @@ import java.util.Set; import mekanism.api.Coord4D; import mekanism.common.multiblock.SynchronizedData; import mekanism.common.util.FluidContainerUtils.ContainerEditMode; - import net.minecraft.item.ItemStack; import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; public class SynchronizedTankData extends SynchronizedData { public FluidStack fluidStored; + /** For use by rendering segment */ + public FluidStack prevFluid; + public ContainerEditMode editMode = ContainerEditMode.BOTH; public ItemStack[] inventory = new ItemStack[2]; public Set valves = new HashSet(); + public boolean needsRenderUpdate() + { + if((fluidStored == null && prevFluid != null) || (fluidStored != null && prevFluid == null)) + { + return true; + } + + if(fluidStored != null && prevFluid != null) + { + if((fluidStored.getFluid() != prevFluid.getFluid()) || (fluidStored.amount != prevFluid.amount)) + { + return true; + } + } + + return false; + } + @Override public ItemStack[] getInventory() { @@ -31,7 +52,14 @@ public class SynchronizedTankData extends SynchronizedData { public ForgeDirection side; public Coord4D location; - public boolean serverFluid; + + public boolean prevActive; + public int activeTicks; + + public void onTransfer() + { + activeTicks = 30; + } @Override public int hashCode() diff --git a/src/main/java/mekanism/common/tile/TileEntityDynamicTank.java b/src/main/java/mekanism/common/tile/TileEntityDynamicTank.java index c75df87b2..499022fe2 100644 --- a/src/main/java/mekanism/common/tile/TileEntityDynamicTank.java +++ b/src/main/java/mekanism/common/tile/TileEntityDynamicTank.java @@ -3,8 +3,8 @@ package mekanism.common.tile; import io.netty.buffer.ByteBuf; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; +import java.util.HashSet; +import java.util.Set; import mekanism.api.Coord4D; import mekanism.api.Range4D; @@ -26,8 +26,8 @@ import net.minecraftforge.fluids.IFluidContainerItem; public class TileEntityDynamicTank extends TileEntityMultiblock implements IFluidContainerManager { - /** A client-sided and server-sided map of valves on this tank's structure, used on the client for rendering fluids. */ - public Map valveViewing = new HashMap(); + /** A client-sided set of valves on this tank's structure that are currently active, used on the client for rendering fluids. */ + public Set valveViewing = new HashSet(); /** The capacity this tank has on the client-side. */ public int clientCapacity; @@ -54,14 +54,6 @@ public class TileEntityDynamicTank extends TileEntityMultiblock 0) - { - valveViewing.put(data, valveViewing.get(data)-1); - } - } - float targetScale = (float)(structure.fluidStored != null ? structure.fluidStored.amount : 0)/clientCapacity; if(Math.abs(prevScale - targetScale) > 0.01) @@ -72,7 +64,7 @@ public class TileEntityDynamicTank extends TileEntityMultiblock 0) + { + data.activeTicks--; + } + + if(data.activeTicks > 0 != data.prevActive) + { + needsValveUpdate = true; + } + + data.prevActive = data.activeTicks > 0; + } + + if(needsValveUpdate || structure.needsRenderUpdate()) + { + sendPacketToRenderer(); + } + } + manageInventory(); } } @@ -307,13 +324,22 @@ public class TileEntityDynamicTank extends TileEntityMultiblock toSend = new HashSet(); for(ValveData valveData : structure.valves) + { + if(valveData.activeTicks > 0) + { + toSend.add(valveData); + } + } + + data.add(toSend.size()); + + for(ValveData valveData : toSend) { valveData.location.write(data); data.add(valveData.side.ordinal()); - data.add(valveData.serverFluid); } } } @@ -342,28 +368,16 @@ public class TileEntityDynamicTank extends TileEntityMultiblock 0) - { - continue; - } - } - - valveViewing.put(data, viewingTicks); + + valveViewing.add(data); TileEntityDynamicTank tileEntity = (TileEntityDynamicTank)data.location.getTileEntity(worldObj);