Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors tank fluid renderer to show fluid level #2487

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
*/
public class QuantumTankRenderer extends TieredHullMachineRenderer {

private static final float MIN = 0.16f;
private static final float MAX = 0.84f;

private static Item CREATIVE_FLUID_ITEM = null;

public QuantumTankRenderer(int tier) {
Expand Down Expand Up @@ -70,8 +73,9 @@ public void renderItem(ItemStack stack, ItemDisplayContext transformType, boolea
FluidStack stored = FluidStack.loadFluidStackFromNBT(stack.getOrCreateTagElement("stored"));
long storedAmount = stack.getOrCreateTag().getLong("storedAmount");
if (storedAmount == 0 && !stored.isEmpty()) storedAmount = stored.getAmount();
long maxAmount = stack.getOrCreateTag().getLong("maxAmount");
// Don't need to handle locked fluids here since they don't get saved to the item
renderTank(poseStack, buffer, Direction.NORTH, stored, storedAmount, FluidStack.EMPTY,
renderTank(poseStack, buffer, Direction.NORTH, stored, storedAmount, maxAmount, FluidStack.EMPTY,
stack.is(CREATIVE_FLUID_ITEM));

poseStack.popPose();
Expand All @@ -86,13 +90,13 @@ public void render(BlockEntity blockEntity, float partialTicks, PoseStack poseSt
if (blockEntity instanceof IMachineBlockEntity machineBlockEntity &&
machineBlockEntity.getMetaMachine() instanceof QuantumTankMachine machine) {
renderTank(poseStack, buffer, machine.getFrontFacing(), machine.getStored(), machine.getStoredAmount(),
machine.getLockedFluid(), machine instanceof CreativeTankMachine);
machine.getMaxAmount(), machine.getLockedFluid(), machine instanceof CreativeTankMachine);
}
}

@OnlyIn(Dist.CLIENT)
public void renderTank(PoseStack poseStack, MultiBufferSource buffer, Direction frontFacing, FluidStack stored,
long storedAmount, FluidStack locked, boolean isCreative) {
long storedAmount, long maxAmount, FluidStack locked, boolean isCreative) {
FluidStack fluid = !stored.isEmpty() ? stored : locked;
if (fluid.isEmpty()) return;

Expand All @@ -102,16 +106,39 @@ public void renderTank(PoseStack poseStack, MultiBufferSource buffer, Direction

poseStack.pushPose();
VertexConsumer builder = buffer.getBuffer(Sheets.translucentCullBlockSheet());
RenderBufferUtils.renderCubeFace(poseStack, builder, 2.5f / 16, 2.5f / 16, 2.5f / 16, 13.5f / 16, 13.5f / 16,
13.5f / 16, ext.getTintColor() | 0xff000000, 0xf000f0, fluidTexture);
var gas = fluid.getFluid().getFluidType().isLighterThanAir();
var percentFull = isCreative || maxAmount <= storedAmount ? 1f : (float) storedAmount / maxAmount;
var facingYAxis = frontFacing.getAxis() == Direction.Axis.Y;
var maxTop = gas ? MAX : MIN + percentFull * (MAX - MIN);
var minBot = gas ? MIN + (1 - percentFull) * (MAX - MIN) : MIN;
float minY, maxY, minZ, maxZ;
if (facingYAxis) {
minY = MIN;
maxY = MAX;
if (frontFacing == Direction.UP) {
minZ = minBot;
maxZ = maxTop;
} else {
// -z is top
minZ = 1 - maxTop;
maxZ = 1 - minBot;
}
} else {
minY = minBot;
maxY = maxTop;
minZ = MIN;
maxZ = MAX;
}
RenderBufferUtils.renderCubeFace(poseStack, builder, MIN, minY, minZ, MAX, maxY, maxZ,
ext.getTintColor() | 0xff000000, 0xf000f0, fluidTexture);
poseStack.popPose();

poseStack.pushPose();
RenderSystem.disableDepthTest();
poseStack.translate(frontFacing.getStepX() * -1 / 16f, frontFacing.getStepY() * -1 / 16f,
frontFacing.getStepZ() * -1 / 16f);
RenderUtils.moveToFace(poseStack, 0, 0, 0, frontFacing);
if (frontFacing.getAxis() == Direction.Axis.Y) {
if (facingYAxis) {
RenderUtils.rotateToFace(poseStack, frontFacing,
frontFacing == Direction.UP ? Direction.SOUTH : Direction.NORTH);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public void saveCustomPersistedData(@NotNull CompoundTag tag, boolean forDrop) {
if (!forDrop) tag.put("lockedFluid", lockedFluid.writeToNBT(new CompoundTag()));
tag.put("stored", stored.writeToNBT(new CompoundTag()));
tag.putLong("storedAmount", storedAmount);
tag.putLong("maxAmount", maxAmount);
}

@Override
Expand Down
Loading