fix fluid pipe crash on Forge 1355+

This commit is contained in:
asiekierka 2015-04-10 16:03:08 +02:00
parent 180a020fc7
commit 5c767249a6
4 changed files with 62 additions and 39 deletions

View file

@ -13,6 +13,7 @@ import java.util.BitSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import buildcraft.transport.utils.FluidRenderData;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -160,8 +161,7 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
public byte initClient = 0; public byte initClient = 0;
public int travelDelay = 12; public int travelDelay = 12;
public int flowRate; public int flowRate;
public FluidStack[] renderCache = new FluidStack[orientations.length]; public FluidRenderData[] renderCache = new FluidRenderData[orientations.length];
public int[] colorRenderCache = new int[orientations.length];
public final PipeSection[] internalTanks = new PipeSection[orientations.length]; public final PipeSection[] internalTanks = new PipeSection[orientations.length];
private final TransferState[] transferState = new TransferState[directions.length]; private final TransferState[] transferState = new TransferState[directions.length];
private final int[] inputPerTick = new int[directions.length]; private final int[] inputPerTick = new int[directions.length];
@ -258,12 +258,11 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
} }
} }
FluidStack[] renderCacheCopy = this.renderCache.clone(); FluidRenderData[] renderCacheCopy = this.renderCache.clone();
int[] colorRenderCacheCopy = this.colorRenderCache.clone();
for (ForgeDirection dir : orientations) { for (ForgeDirection dir : orientations) {
FluidStack current = internalTanks[dir.ordinal()].getFluid(); FluidStack current = internalTanks[dir.ordinal()].getFluid();
FluidStack prev = renderCacheCopy[dir.ordinal()]; FluidStack prev = renderCacheCopy[dir.ordinal()] != null ? renderCacheCopy[dir.ordinal()].getFluidStack() : null;
if (current != null && current.getFluid() == null) { if (current != null && current.getFluid() == null) {
continue; continue;
@ -276,11 +275,9 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
if (prev == null ^ current == null) { if (prev == null ^ current == null) {
changed = true; changed = true;
if (current != null) { if (current != null) {
renderCacheCopy[dir.ordinal()] = current.copy(); renderCacheCopy[dir.ordinal()] = new FluidRenderData(current);
colorRenderCacheCopy[dir.ordinal()] = current.getFluid().getColor(current);
} else { } else {
renderCacheCopy[dir.ordinal()] = null; renderCacheCopy[dir.ordinal()] = null;
colorRenderCacheCopy[dir.ordinal()] = 0xFFFFFF;
} }
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_ID_BIT); delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_ID_BIT);
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_AMOUNT_BIT); delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_AMOUNT_BIT);
@ -293,8 +290,7 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
if (!prev.equals(current) || initPacket) { if (!prev.equals(current) || initPacket) {
changed = true; changed = true;
renderCacheCopy[dir.ordinal()] = current; renderCacheCopy[dir.ordinal()] = new FluidRenderData(current);
colorRenderCacheCopy[dir.ordinal()] = current.getFluid().getColor(current);
delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_ID_BIT); delta.set(dir.ordinal() * PacketFluidUpdate.FLUID_DATA_NUM + PacketFluidUpdate.FLUID_ID_BIT);
} }
@ -313,13 +309,11 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
if (persistChange) { if (persistChange) {
this.renderCache = renderCacheCopy; this.renderCache = renderCacheCopy;
this.colorRenderCache = colorRenderCacheCopy;
} }
if (changed || initPacket) { if (changed || initPacket) {
PacketFluidUpdate packet = new PacketFluidUpdate(container.xCoord, container.yCoord, container.zCoord, initPacket); PacketFluidUpdate packet = new PacketFluidUpdate(container.xCoord, container.yCoord, container.zCoord, initPacket);
packet.renderCache = renderCacheCopy; packet.renderCache = renderCacheCopy;
packet.colorRenderCache = colorRenderCacheCopy;
packet.delta = delta; packet.delta = delta;
return packet; return packet;
} }
@ -528,7 +522,6 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler
internalTanks[direction.ordinal()].reset(); internalTanks[direction.ordinal()].reset();
transferState[direction.ordinal()] = TransferState.None; transferState[direction.ordinal()] = TransferState.None;
renderCache[direction.ordinal()] = null; renderCache[direction.ordinal()] = null;
colorRenderCache[direction.ordinal()] = 0xFFFFFF;
} }
} }
} }

View file

@ -10,12 +10,15 @@ package buildcraft.transport.network;
import java.util.BitSet; import java.util.BitSet;
import buildcraft.transport.utils.FluidRenderData;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import buildcraft.core.network.PacketCoordinates; import buildcraft.core.network.PacketCoordinates;
@ -31,8 +34,7 @@ public class PacketFluidUpdate extends PacketCoordinates {
public static int FLUID_AMOUNT_BIT = 1; public static int FLUID_AMOUNT_BIT = 1;
public static int FLUID_DATA_NUM = 2; public static int FLUID_DATA_NUM = 2;
public FluidStack[] renderCache = new FluidStack[ForgeDirection.values().length]; public FluidRenderData[] renderCache = new FluidRenderData[ForgeDirection.values().length];
public int[] colorRenderCache = new int[ForgeDirection.values().length];
public BitSet delta; public BitSet delta;
public PacketFluidUpdate(int xCoord, int yCoord, int zCoord) { public PacketFluidUpdate(int xCoord, int yCoord, int zCoord) {
@ -73,7 +75,6 @@ public class PacketFluidUpdate extends PacketCoordinates {
PipeTransportFluids transLiq = (PipeTransportFluids) pipe.pipe.transport; PipeTransportFluids transLiq = (PipeTransportFluids) pipe.pipe.transport;
renderCache = transLiq.renderCache; renderCache = transLiq.renderCache;
colorRenderCache = transLiq.colorRenderCache;
byte[] dBytes = new byte[2]; byte[] dBytes = new byte[2];
data.readBytes(dBytes); data.readBytes(dBytes);
@ -83,18 +84,20 @@ public class PacketFluidUpdate extends PacketCoordinates {
for (ForgeDirection dir : ForgeDirection.values()) { for (ForgeDirection dir : ForgeDirection.values()) {
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_ID_BIT)) { if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_ID_BIT)) {
int id = data.readShort();
int amt = renderCache[dir.ordinal()] != null ? renderCache[dir.ordinal()].amount : 0; int amt = renderCache[dir.ordinal()] != null ? renderCache[dir.ordinal()].amount : 0;
renderCache[dir.ordinal()] = new FluidStack(data.readShort(), amt); int color = data.readInt();
colorRenderCache[dir.ordinal()] = data.readInt();
renderCache[dir.ordinal()] = new FluidRenderData(id, amt, color);
} }
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_AMOUNT_BIT)) { if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_AMOUNT_BIT)) {
if (renderCache[dir.ordinal()] == null) { if (renderCache[dir.ordinal()] != null) {
renderCache[dir.ordinal()] = new FluidStack(0, 0);
}
renderCache[dir.ordinal()].amount = Math.min(transLiq.getCapacity(), data.readUnsignedShort()); renderCache[dir.ordinal()].amount = Math.min(transLiq.getCapacity(), data.readUnsignedShort());
} }
} }
} }
}
@Override @Override
public void writeData(ByteBuf data) { public void writeData(ByteBuf data) {
@ -105,12 +108,12 @@ public class PacketFluidUpdate extends PacketCoordinates {
data.writeBytes(dBytes); data.writeBytes(dBytes);
for (ForgeDirection dir : ForgeDirection.values()) { for (ForgeDirection dir : ForgeDirection.values()) {
FluidStack liquid = renderCache[dir.ordinal()]; FluidRenderData liquid = renderCache[dir.ordinal()];
if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_ID_BIT)) { if (delta.get(dir.ordinal() * FLUID_DATA_NUM + FLUID_ID_BIT)) {
if (liquid != null) { if (liquid != null) {
data.writeShort(liquid.fluidID); data.writeShort(liquid.fluidID);
data.writeInt(colorRenderCache[dir.ordinal()]); data.writeInt(liquid.color);
} else { } else {
data.writeShort(0); data.writeShort(0);
data.writeInt(0xFFFFFF); data.writeInt(0xFFFFFF);

View file

@ -10,6 +10,7 @@ package buildcraft.transport.render;
import java.util.HashMap; import java.util.HashMap;
import buildcraft.transport.utils.FluidRenderData;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@ -32,7 +33,6 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.BuildCraftCore; import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftCore.RenderMode; import buildcraft.BuildCraftCore.RenderMode;
@ -669,8 +669,8 @@ public class PipeRendererTESR extends TileEntitySpecialRenderer {
boolean needsRender = false; boolean needsRender = false;
for (int i = 0; i < 7; ++i) { for (int i = 0; i < 7; ++i) {
FluidStack fluidStack = trans.renderCache[i]; FluidRenderData renderData = trans.renderCache[i];
if (fluidStack != null && fluidStack.amount > 0) { if (renderData != null && renderData.amount > 0) {
needsRender = true; needsRender = true;
break; break;
} }
@ -696,9 +696,9 @@ public class PipeRendererTESR extends TileEntitySpecialRenderer {
for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) { for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) {
int i = side.ordinal(); int i = side.ordinal();
FluidStack fluidStack = trans.renderCache[i]; FluidRenderData fluidRenderData = trans.renderCache[i];
if (fluidStack == null || fluidStack.amount <= 0) { if (fluidRenderData == null || fluidRenderData.amount <= 0) {
continue; continue;
} }
@ -706,13 +706,13 @@ public class PipeRendererTESR extends TileEntitySpecialRenderer {
continue; continue;
} }
DisplayFluidList d = getListFromBuffer(fluidStack, pipe.container.getWorldObj()); DisplayFluidList d = getListFromBuffer(fluidRenderData, pipe.container.getWorldObj());
if (d == null) { if (d == null) {
continue; continue;
} }
int stage = (int) ((float) fluidStack.amount / (float) (trans.getCapacity()) * (LIQUID_STAGES - 1)); int stage = (int) ((float) fluidRenderData.amount / (float) (trans.getCapacity()) * (LIQUID_STAGES - 1));
GL11.glPushMatrix(); GL11.glPushMatrix();
int list = 0; int list = 0;
@ -741,21 +741,21 @@ public class PipeRendererTESR extends TileEntitySpecialRenderer {
default: default:
} }
bindTexture(TextureMap.locationBlocksTexture); bindTexture(TextureMap.locationBlocksTexture);
RenderUtils.setGLColorFromInt(trans.colorRenderCache[i]); RenderUtils.setGLColorFromInt(fluidRenderData.color);
GL11.glCallList(list); GL11.glCallList(list);
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
// CENTER // CENTER
FluidStack fluidStack = trans.renderCache[ForgeDirection.UNKNOWN.ordinal()]; FluidRenderData fluidRenderData = trans.renderCache[ForgeDirection.UNKNOWN.ordinal()];
if (fluidStack != null && fluidStack.amount > 0) { if (fluidRenderData != null && fluidRenderData.amount > 0) {
DisplayFluidList d = getListFromBuffer(fluidStack, pipe.container.getWorldObj()); DisplayFluidList d = getListFromBuffer(fluidRenderData, pipe.container.getWorldObj());
if (d != null) { if (d != null) {
int stage = (int) ((float) fluidStack.amount / (float) (trans.getCapacity()) * (LIQUID_STAGES - 1)); int stage = (int) ((float) fluidRenderData.amount / (float) (trans.getCapacity()) * (LIQUID_STAGES - 1));
bindTexture(TextureMap.locationBlocksTexture); bindTexture(TextureMap.locationBlocksTexture);
RenderUtils.setGLColorFromInt(trans.colorRenderCache[ForgeDirection.UNKNOWN.ordinal()]); RenderUtils.setGLColorFromInt(fluidRenderData.color);
if (above) { if (above) {
GL11.glCallList(d.centerVertical[stage]); GL11.glCallList(d.centerVertical[stage]);
@ -772,8 +772,7 @@ public class PipeRendererTESR extends TileEntitySpecialRenderer {
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
private DisplayFluidList getListFromBuffer(FluidStack stack, World world) { private DisplayFluidList getListFromBuffer(FluidRenderData stack, World world) {
int liquidId = stack.fluidID; int liquidId = stack.fluidID;
if (liquidId == 0) { if (liquidId == 0) {

View file

@ -0,0 +1,28 @@
package buildcraft.transport.utils;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
public class FluidRenderData {
public int fluidID, amount, color;
public FluidRenderData(int fluidID, int amount, int color) {
this.fluidID = fluidID;
this.amount = amount;
this.color = color;
}
public FluidRenderData(FluidStack stack) {
this(stack.getFluid().getID(), stack.amount, stack.getFluid().getColor(stack));
}
public FluidStack getFluidStack() {
Fluid fluid = FluidRegistry.getFluid(fluidID);
if (fluid != null) {
return new FluidStack(fluid, amount);
} else {
return null;
}
}
}