Fixed a NPE with tank renderer

This commit is contained in:
Robert S 2014-03-24 07:31:22 -04:00
parent 3791924480
commit 296dc1422b

View file

@ -32,204 +32,204 @@ import java.awt.*;
public class TileTank extends TileFluidDistribution implements IComparatorInputOverride public class TileTank extends TileFluidDistribution implements IComparatorInputOverride
{ {
public static final int VOLUME = 16; public static final int VOLUME = 16;
public TileTank() public TileTank()
{ {
super(UniversalElectricity.machine); super(UniversalElectricity.machine);
this.getInternalTank().setCapacity(VOLUME * FluidContainerRegistry.BUCKET_VOLUME); this.getInternalTank().setCapacity(VOLUME * FluidContainerRegistry.BUCKET_VOLUME);
isOpaqueCube = false; isOpaqueCube = false;
normalRender = false; normalRender = false;
itemBlock = ItemBlockFluidContainer.class; itemBlock = ItemBlockFluidContainer.class;
} }
@Override @Override
public boolean shouldSideBeRendered(IBlockAccess access, int x, int y, int z, int side) public boolean shouldSideBeRendered(IBlockAccess access, int x, int y, int z, int side)
{ {
return access.getBlockId(x, y, z) != block.blockID; return access != null && block != null && access.getBlockId(x, y, z) != block.blockID;
} }
@Override @Override
protected boolean use(EntityPlayer player, int side, Vector3 vector3) protected boolean use(EntityPlayer player, int side, Vector3 vector3)
{ {
if (!world().isRemote) if (!world().isRemote)
{ {
if (player.isSneaking()) if (player.isSneaking())
{ {
ItemStack dropStack = ItemBlockFluidContainer.getWrenchedItem(world(), position()); ItemStack dropStack = ItemBlockFluidContainer.getWrenchedItem(world(), position());
if (dropStack != null) if (dropStack != null)
{ {
if (player.getHeldItem() == null) if (player.getHeldItem() == null)
{ {
player.inventory.setInventorySlotContents(player.inventory.currentItem, dropStack); player.inventory.setInventorySlotContents(player.inventory.currentItem, dropStack);
} }
else else
{ {
InventoryUtility.dropItemStack(world(), position(), dropStack); InventoryUtility.dropItemStack(world(), position(), dropStack);
} }
position().setBlock(world(), 0); position().setBlock(world(), 0);
} }
} }
return FluidUtility.playerActivatedFluidItem(world(), x(), y(), z(), player, side); return FluidUtility.playerActivatedFluidItem(world(), x(), y(), z(), player, side);
} }
return true; return true;
} }
@Override @Override
public int getComparatorInputOverride(int side) public int getComparatorInputOverride(int side)
{ {
if (getNetwork().getTank().getFluid() != null) if (getNetwork().getTank().getFluid() != null)
{ {
return (int) (15 * ((double) getNetwork().getTank().getFluidAmount() / (double) getNetwork().getTank().getCapacity())); return (int) (15 * ((double) getNetwork().getTank().getFluidAmount() / (double) getNetwork().getTank().getCapacity()));
} }
return 0; return 0;
} }
@Override @Override
public int getLightValue(IBlockAccess access) public int getLightValue(IBlockAccess access)
{ {
if (getInternalTank().getFluid() != null) if (getInternalTank().getFluid() != null)
{ {
return getInternalTank().getFluid().getFluid().getLuminosity(); return getInternalTank().getFluid().getFluid().getLuminosity();
} }
return super.getLightValue(access); return super.getLightValue(access);
} }
@Override @Override
public FluidDistributionetwork getNetwork() public FluidDistributionetwork getNetwork()
{ {
if (this.network == null) if (this.network == null)
{ {
this.network = new TankNetwork(); this.network = new TankNetwork();
this.network.addConnector(this); this.network.addConnector(this);
} }
return this.network; return this.network;
} }
@Override @Override
public void setNetwork(FluidDistributionetwork network) public void setNetwork(FluidDistributionetwork network)
{ {
if (network instanceof TankNetwork) if (network instanceof TankNetwork)
{ {
this.network = network; this.network = network;
} }
} }
@Override @Override
public void validateConnectionSide(TileEntity tileEntity, ForgeDirection side) public void validateConnectionSide(TileEntity tileEntity, ForgeDirection side)
{ {
if (!this.worldObj.isRemote) if (!this.worldObj.isRemote)
{ {
if (tileEntity instanceof TileTank) if (tileEntity instanceof TileTank)
{ {
getNetwork().merge(((IFluidDistribution) tileEntity).getNetwork()); getNetwork().merge(((IFluidDistribution) tileEntity).getNetwork());
renderSides = WorldUtility.setEnableSide(renderSides, side, true); renderSides = WorldUtility.setEnableSide(renderSides, side, true);
connectedBlocks[side.ordinal()] = tileEntity; connectedBlocks[side.ordinal()] = tileEntity;
} }
} }
} }
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
@Override @Override
protected TileRender newRenderer() protected TileRender newRenderer()
{ {
return new TileRender() return new TileRender()
{ {
@Override @Override
public boolean renderStatic(RenderBlocks renderer, Vector3 position) public boolean renderStatic(RenderBlocks renderer, Vector3 position)
{ {
RenderBlockUtility.tessellateBlockWithConnectedTextures(renderSides, world(), x(), y(), z(), Archaic.blockTank, null, RenderUtility.getIcon(Reference.PREFIX + "tankEdge")); RenderBlockUtility.tessellateBlockWithConnectedTextures(renderSides, world(), x(), y(), z(), Archaic.blockTank, null, RenderUtility.getIcon(Reference.PREFIX + "tankEdge"));
return true; return true;
} }
public void renderTank(TileEntity tileEntity, double x, double y, double z, FluidStack fluid) public void renderTank(TileEntity tileEntity, double x, double y, double z, FluidStack fluid)
{ {
if (tileEntity instanceof TileTank) if (tileEntity instanceof TileTank)
{ {
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glTranslated(x + 0.5, y + 0.5, z + 0.5); GL11.glTranslated(x + 0.5, y + 0.5, z + 0.5);
if (fluid != null) if (fluid != null)
{ {
GL11.glPushMatrix(); GL11.glPushMatrix();
if (tileEntity.worldObj != null && !fluid.getFluid().isGaseous()) if (tileEntity.worldObj != null && !fluid.getFluid().isGaseous())
{ {
GL11.glScaled(0.99, 0.99, 0.99); GL11.glScaled(0.99, 0.99, 0.99);
FluidTank tank = ((TileTank) tileEntity).getInternalTank(); FluidTank tank = ((TileTank) tileEntity).getInternalTank();
double percentageFilled = (double) tank.getFluidAmount() / (double) tank.getCapacity(); double percentageFilled = (double) tank.getFluidAmount() / (double) tank.getCapacity();
double ySouthEast = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.SOUTH, ForgeDirection.EAST); double ySouthEast = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.SOUTH, ForgeDirection.EAST);
double yNorthEast = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.NORTH, ForgeDirection.EAST); double yNorthEast = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.NORTH, ForgeDirection.EAST);
double ySouthWest = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.SOUTH, ForgeDirection.WEST); double ySouthWest = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.SOUTH, ForgeDirection.WEST);
double yNorthWest = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.NORTH, ForgeDirection.WEST); double yNorthWest = FluidUtility.getAveragePercentageFilledForSides(TileTank.class, percentageFilled, tileEntity.worldObj, new Vector3(tileEntity), ForgeDirection.NORTH, ForgeDirection.WEST);
FluidRenderUtility.renderFluidTesselation(tank, ySouthEast, yNorthEast, ySouthWest, yNorthWest); FluidRenderUtility.renderFluidTesselation(tank, ySouthEast, yNorthEast, ySouthWest, yNorthWest);
} }
else else
{ {
GL11.glTranslated(-0.5, -0.5, -0.5); GL11.glTranslated(-0.5, -0.5, -0.5);
GL11.glScaled(0.99, 0.99, 0.99); GL11.glScaled(0.99, 0.99, 0.99);
int capacity = tileEntity instanceof TileTank ? ((TileTank) tileEntity).getInternalTank().getCapacity() : fluid.amount; int capacity = tileEntity instanceof TileTank ? ((TileTank) tileEntity).getInternalTank().getCapacity() : fluid.amount;
double filledPercentage = (double) fluid.amount / (double) capacity; double filledPercentage = (double) fluid.amount / (double) capacity;
double renderPercentage = fluid.getFluid().isGaseous() ? 1 : filledPercentage; double renderPercentage = fluid.getFluid().isGaseous() ? 1 : filledPercentage;
int[] displayList = FluidRenderUtility.getFluidDisplayLists(fluid, tileEntity.worldObj, false); int[] displayList = FluidRenderUtility.getFluidDisplayLists(fluid, tileEntity.worldObj, false);
GL11.glPushAttrib(GL11.GL_ENABLE_BIT); GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_CULL_FACE); GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_LIGHTING); GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND); GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
Color color = new Color(fluid.getFluid().getColor()); Color color = new Color(fluid.getFluid().getColor());
RenderUtility.enableBlending(); RenderUtility.enableBlending();
GL11.glColor4d(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, fluid.getFluid().isGaseous() ? filledPercentage : 1); GL11.glColor4d(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, fluid.getFluid().isGaseous() ? filledPercentage : 1);
RenderUtility.bind(FluidRenderUtility.getFluidSheet(fluid)); RenderUtility.bind(FluidRenderUtility.getFluidSheet(fluid));
GL11.glCallList(displayList[(int) (renderPercentage * (FluidRenderUtility.DISPLAY_STAGES - 1))]); GL11.glCallList(displayList[(int) (renderPercentage * (FluidRenderUtility.DISPLAY_STAGES - 1))]);
RenderUtility.disableBlending(); RenderUtility.disableBlending();
GL11.glPopAttrib(); GL11.glPopAttrib();
} }
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
} }
@Override @Override
public boolean renderDynamic(Vector3 position, boolean isItem, float frame) public boolean renderDynamic(Vector3 position, boolean isItem, float frame)
{ {
renderTank(TileTank.this, position.x, position.y, position.z, getInternalTank().getFluid()); renderTank(TileTank.this, position.x, position.y, position.z, getInternalTank().getFluid());
return false; return false;
} }
@Override @Override
public boolean renderItem(ItemStack itemStack) public boolean renderItem(ItemStack itemStack)
{ {
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glTranslated(0.5, 0.5, 0.5); GL11.glTranslated(0.5, 0.5, 0.5);
RenderBlockUtility.tessellateBlockWithConnectedTextures(itemStack.getItemDamage(), Archaic.blockTank, null, RenderUtility.getIcon(Reference.PREFIX + "tankEdge")); RenderBlockUtility.tessellateBlockWithConnectedTextures(itemStack.getItemDamage(), Archaic.blockTank, null, RenderUtility.getIcon(Reference.PREFIX + "tankEdge"));
GL11.glPopMatrix(); GL11.glPopMatrix();
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glTranslated(0, -0.1, 0); GL11.glTranslated(0, -0.1, 0);
FluidStack fluid = null; FluidStack fluid = null;
if (itemStack.getTagCompound() != null && itemStack.getTagCompound().hasKey("fluid")) if (itemStack.getTagCompound() != null && itemStack.getTagCompound().hasKey("fluid"))
{ {
fluid = FluidStack.loadFluidStackFromNBT(itemStack.getTagCompound().getCompoundTag("fluid")); fluid = FluidStack.loadFluidStackFromNBT(itemStack.getTagCompound().getCompoundTag("fluid"));
} }
renderTank(TileTank.this, 0, 0, 0, fluid); renderTank(TileTank.this, 0, 0, 0, fluid);
GL11.glPopMatrix(); GL11.glPopMatrix();
return true; return true;
} }
}; };
} }
} }