Fixed tank item rendering

This commit is contained in:
Calclavia 2014-06-20 22:48:44 -07:00
parent 91f402b18f
commit ffa9be03d5
3 changed files with 232 additions and 185 deletions

View file

@ -1,9 +1,13 @@
package resonantinduction.archaic; package resonantinduction.archaic;
import resonant.lib.render.item.GlobalItemRenderer;
import resonantinduction.archaic.fluid.tank.TileTank;
public class ClientProxy extends CommonProxy public class ClientProxy extends CommonProxy
{ {
@Override @Override
public void preInit() public void preInit()
{ {
GlobalItemRenderer.register(Archaic.blockTank.blockID, TileTank.ItemRenderer.instance);
} }
} }

View file

@ -9,212 +9,211 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem; import net.minecraftforge.fluids.IFluidContainerItem;
import resonant.lib.utility.LanguageUtility; import resonant.lib.utility.LanguageUtility;
import resonantinduction.archaic.Archaic;
import resonantinduction.core.grid.fluid.TileFluidDistribution; import resonantinduction.core.grid.fluid.TileFluidDistribution;
import universalelectricity.api.energy.UnitDisplay; import universalelectricity.api.energy.UnitDisplay;
import universalelectricity.api.energy.UnitDisplay.Unit; import universalelectricity.api.energy.UnitDisplay.Unit;
import universalelectricity.api.energy.UnitDisplay.UnitPrefix; import universalelectricity.api.energy.UnitDisplay.UnitPrefix;
import universalelectricity.api.vector.Vector3;
/** @author Darkguardsman */ /**
* @author Darkguardsman
*/
public class ItemBlockTank extends ItemBlock implements IFluidContainerItem public class ItemBlockTank extends ItemBlock implements IFluidContainerItem
{ {
public ItemBlockTank(int id) public ItemBlockTank(int id)
{ {
super(id); super(id);
this.setMaxDamage(0); this.setMaxDamage(0);
this.setHasSubtypes(true); this.setHasSubtypes(true);
} }
@Override @Override
public int getMetadata(int damage) public int getMetadata(int damage)
{ {
return damage; return damage;
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
@Override @Override
public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean par4) public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4)
{ {
if (stack.getTagCompound() != null && stack.getTagCompound().hasKey("fluid")) if (itemStack.getTagCompound() != null && itemStack.getTagCompound().hasKey("fluid"))
{ {
FluidStack fluid = getFluid(stack); FluidStack fluid = getFluid(itemStack);
if (fluid != null) if (fluid != null)
{ {
list.add("Fluid: " + fluid.getFluid().getLocalizedName()); list.add("Fluid: " + fluid.getFluid().getLocalizedName());
list.add("Volume: " + UnitDisplay.getDisplay(fluid.amount, Unit.LITER, UnitPrefix.MILLI)); list.add("Volume: " + UnitDisplay.getDisplay(fluid.amount, Unit.LITER, UnitPrefix.MILLI));
} }
} }
} }
@Override @Override
public int getItemStackLimit(ItemStack stack) public int getItemStackLimit(ItemStack stack)
{ {
if (stack.getTagCompound() != null && stack.getTagCompound().hasKey("fluid")) if (stack.getTagCompound() != null && stack.getTagCompound().hasKey("fluid"))
{ {
return 1; return 1;
} }
return this.maxStackSize; return this.maxStackSize;
} }
@Override @Override
public String getUnlocalizedName(ItemStack itemStack) public String getUnlocalizedName(ItemStack itemStack)
{ {
String translation = LanguageUtility.getLocal(Block.blocksList[this.getBlockID()].getUnlocalizedName() + "." + itemStack.getItemDamage()); String translation = LanguageUtility.getLocal(Block.blocksList[this.getBlockID()].getUnlocalizedName() + "." + itemStack.getItemDamage());
if (translation == null || translation.isEmpty()) if (translation == null || translation.isEmpty())
{ {
return Block.blocksList[this.getBlockID()].getUnlocalizedName(); return Block.blocksList[this.getBlockID()].getUnlocalizedName();
} }
return Block.blocksList[this.getBlockID()].getUnlocalizedName() + "." + itemStack.getItemDamage(); return Block.blocksList[this.getBlockID()].getUnlocalizedName() + "." + itemStack.getItemDamage();
} }
@Override @Override
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
{ {
if (super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata)) if (super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata))
{ {
TileEntity tile = world.getBlockTileEntity(x, y, z); TileEntity tile = world.getBlockTileEntity(x, y, z);
if (tile instanceof TileFluidDistribution) if (tile instanceof TileFluidDistribution)
{ {
((TileFluidDistribution) tile).setSubID(stack.getItemDamage()); ((TileFluidDistribution) tile).setSubID(stack.getItemDamage());
((TileFluidDistribution) tile).getInternalTank().fill(getFluid(stack), true); ((TileFluidDistribution) tile).getInternalTank().fill(getFluid(stack), true);
} }
return true; return true;
} }
return false; return false;
} }
@Override @Override
public FluidStack getFluid(ItemStack container) public FluidStack getFluid(ItemStack container)
{ {
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid")) if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid"))
{ {
return null; return null;
} }
return FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid")); return FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid"));
} }
@Override @Override
public int getCapacity(ItemStack container) public int getCapacity(ItemStack container)
{ {
return TileTank.VOLUME; return TileTank.VOLUME;
} }
@Override @Override
public int fill(ItemStack container, FluidStack resource, boolean doFill) public int fill(ItemStack container, FluidStack resource, boolean doFill)
{ {
if (resource == null) if (resource == null)
{ {
return 0; return 0;
} }
if (!doFill) if (!doFill)
{ {
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid")) if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid"))
{ {
return Math.min(getCapacity(container), resource.amount); return Math.min(getCapacity(container), resource.amount);
} }
FluidStack stack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid")); FluidStack stack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid"));
if (stack == null) if (stack == null)
{ {
return Math.min(getCapacity(container), resource.amount); return Math.min(getCapacity(container), resource.amount);
} }
if (!stack.isFluidEqual(resource)) if (!stack.isFluidEqual(resource))
{ {
return 0; return 0;
} }
return Math.min(getCapacity(container) - stack.amount, resource.amount); return Math.min(getCapacity(container) - stack.amount, resource.amount);
} }
if (container.stackTagCompound == null) if (container.stackTagCompound == null)
{ {
container.stackTagCompound = new NBTTagCompound(); container.stackTagCompound = new NBTTagCompound();
} }
if (!container.stackTagCompound.hasKey("fluid")) if (!container.stackTagCompound.hasKey("fluid"))
{ {
NBTTagCompound fluidTag = resource.writeToNBT(new NBTTagCompound()); NBTTagCompound fluidTag = resource.writeToNBT(new NBTTagCompound());
if (getCapacity(container) < resource.amount) if (getCapacity(container) < resource.amount)
{ {
fluidTag.setInteger("Amount", getCapacity(container)); fluidTag.setInteger("Amount", getCapacity(container));
container.stackTagCompound.setTag("fluid", fluidTag); container.stackTagCompound.setTag("fluid", fluidTag);
return getCapacity(container); return getCapacity(container);
} }
container.stackTagCompound.setTag("fluid", fluidTag); container.stackTagCompound.setTag("fluid", fluidTag);
return resource.amount; return resource.amount;
} }
NBTTagCompound fluidTag = container.stackTagCompound.getCompoundTag("fluid"); NBTTagCompound fluidTag = container.stackTagCompound.getCompoundTag("fluid");
FluidStack stack = FluidStack.loadFluidStackFromNBT(fluidTag); FluidStack stack = FluidStack.loadFluidStackFromNBT(fluidTag);
if (!stack.isFluidEqual(resource)) if (!stack.isFluidEqual(resource))
{ {
return 0; return 0;
} }
int filled = getCapacity(container) - stack.amount; int filled = getCapacity(container) - stack.amount;
if (resource.amount < filled) if (resource.amount < filled)
{ {
stack.amount += resource.amount; stack.amount += resource.amount;
filled = resource.amount; filled = resource.amount;
} }
else else
{ {
stack.amount = getCapacity(container); stack.amount = getCapacity(container);
} }
container.stackTagCompound.setTag("fluid", stack.writeToNBT(fluidTag)); container.stackTagCompound.setTag("fluid", stack.writeToNBT(fluidTag));
return filled; return filled;
} }
@Override @Override
public FluidStack drain(ItemStack container, int maxDrain, boolean doDrain) public FluidStack drain(ItemStack container, int maxDrain, boolean doDrain)
{ {
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid") || maxDrain == 0) if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("fluid") || maxDrain == 0)
{ {
return null; return null;
} }
FluidStack stack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid")); FluidStack stack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("fluid"));
if (stack == null) if (stack == null)
{ {
return null; return null;
} }
int drained = Math.min(stack.amount, maxDrain); int drained = Math.min(stack.amount, maxDrain);
if (doDrain) if (doDrain)
{ {
if (maxDrain >= stack.amount) if (maxDrain >= stack.amount)
{ {
container.stackTagCompound.removeTag("fluid"); container.stackTagCompound.removeTag("fluid");
if (container.stackTagCompound.hasNoTags()) if (container.stackTagCompound.hasNoTags())
{ {
container.stackTagCompound = null; container.stackTagCompound = null;
} }
return stack; return stack;
} }
NBTTagCompound fluidTag = container.stackTagCompound.getCompoundTag("fluid"); NBTTagCompound fluidTag = container.stackTagCompound.getCompoundTag("fluid");
fluidTag.setInteger("Amount", fluidTag.getInteger("Amount") - maxDrain); fluidTag.setInteger("Amount", fluidTag.getInteger("Amount") - maxDrain);
container.stackTagCompound.setTag("fluid", fluidTag); container.stackTagCompound.setTag("fluid", fluidTag);
} }
stack.amount = drained; stack.amount = drained;
return stack; return stack;
} }
} }

View file

@ -18,6 +18,7 @@ import net.minecraftforge.fluids.FluidTank;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import resonant.api.IRemovable.ISneakPickup; import resonant.api.IRemovable.ISneakPickup;
import resonant.api.items.ISimpleItemRenderer;
import resonant.lib.content.module.TileBlock.IComparatorInputOverride; import resonant.lib.content.module.TileBlock.IComparatorInputOverride;
import resonant.lib.content.module.TileRender; import resonant.lib.content.module.TileRender;
import resonant.lib.render.FluidRenderUtility; import resonant.lib.render.FluidRenderUtility;
@ -154,9 +155,9 @@ public class TileTank extends TileFluidDistribution implements IComparatorInputO
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 = percentageFilled;//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 = percentageFilled;//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 = percentageFilled;//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
@ -198,30 +199,73 @@ public class TileTank extends TileFluidDistribution implements IComparatorInputO
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 public static class ItemRenderer implements ISimpleItemRenderer
public boolean renderItem(ItemStack itemStack) {
public static ItemRenderer instance = new ItemRenderer();
public void renderTank(double x, double y, double z, FluidStack fluid, int capacity)
{
FluidTank tank = new FluidTank(fluid, capacity);
GL11.glPushMatrix();
GL11.glTranslated(0.02, 0.02, 0.02);
GL11.glScaled(0.92, 0.92, 0.92);
if (fluid != null)
{ {
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glTranslated(0.5, 0.5, 0.5);
RenderBlockUtility.tessellateBlockWithConnectedTextures(itemStack.getItemDamage(), Archaic.blockTank, null, RenderUtility.getIcon(Reference.PREFIX + "tankEdge"));
GL11.glPopMatrix();
GL11.glPushMatrix(); if (!fluid.getFluid().isGaseous())
GL11.glTranslated(0, -0.1, 0);
FluidStack fluid = null;
if (itemStack.getTagCompound() != null && itemStack.getTagCompound().hasKey("fluid"))
{ {
fluid = FluidStack.loadFluidStackFromNBT(itemStack.getTagCompound().getCompoundTag("fluid")); double percentageFilled = (double) tank.getFluidAmount() / (double) tank.getCapacity();
FluidRenderUtility.renderFluidTesselation(tank, percentageFilled, percentageFilled, percentageFilled, percentageFilled);
}
else
{
double filledPercentage = (double) fluid.amount / (double) capacity;
GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
Color color = new Color(fluid.getFluid().getColor());
RenderUtility.enableBlending();
GL11.glColor4d(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, fluid.getFluid().isGaseous() ? filledPercentage : 1);
RenderUtility.bind(FluidRenderUtility.getFluidSheet(fluid));
FluidRenderUtility.renderFluidTesselation(tank, 1, 1, 1, 1);
RenderUtility.disableBlending();
GL11.glPopAttrib();
} }
renderTank(TileTank.this, 0, 0, 0, fluid);
GL11.glPopMatrix(); GL11.glPopMatrix();
return true;
} }
};
GL11.glPopMatrix();
}
@Override
public void renderInventoryItem(ItemStack itemStack)
{
GL11.glPushMatrix();
RenderBlockUtility.tessellateBlockWithConnectedTextures(itemStack.getItemDamage(), Archaic.blockTank, null, RenderUtility.getIcon(Reference.PREFIX + "tankEdge"));
GL11.glPopMatrix();
GL11.glPushMatrix();
if (itemStack.getTagCompound() != null && itemStack.getTagCompound().hasKey("fluid"))
{
renderTank(0, 0, 0, FluidStack.loadFluidStackFromNBT(itemStack.getTagCompound().getCompoundTag("fluid")), VOLUME * FluidContainerRegistry.BUCKET_VOLUME);
}
GL11.glPopMatrix();
}
} }
@Override @Override