Skip to content

Commit

Permalink
Change multiblocks rendered fluid update (#2477)
Browse files Browse the repository at this point in the history
Co-authored-by: Spicierspace153 <21964509+Spicierspace153@users.noreply.github.com>
Co-authored-by: Spicierspace153 <cameronjmiller@outlook.com>
  • Loading branch information
3 people authored Dec 3, 2024
1 parent 24bc137 commit 1b14ab8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.gregtechceu.gtceu.client.renderer.machine.gcym;

import com.gregtechceu.gtceu.api.blockentity.MetaMachineBlockEntity;
import com.gregtechceu.gtceu.api.capability.recipe.FluidRecipeCapability;
import com.gregtechceu.gtceu.api.pattern.util.RelativeDirection;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.content.Content;
import com.gregtechceu.gtceu.api.recipe.ingredient.FluidIngredient;
import com.gregtechceu.gtceu.client.renderer.block.FluidBlockRenderer;
import com.gregtechceu.gtceu.client.renderer.machine.WorkableCasingMachineRenderer;
import com.gregtechceu.gtceu.client.util.RenderUtil;
Expand All @@ -18,18 +14,16 @@
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.client.RenderTypeHelper;
import net.minecraftforge.fluids.FluidStack;

import com.mojang.blaze3d.vertex.PoseStack;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class LargeChemicalBathRenderer extends WorkableCasingMachineRenderer {

private final FluidBlockRenderer fluidBlockRenderer;
private Fluid cachedFluid;
private ResourceLocation cachedRecipe;

public LargeChemicalBathRenderer(ResourceLocation baseCasing, ResourceLocation workableModel) {
super(baseCasing, workableModel);
Expand Down Expand Up @@ -62,35 +56,31 @@ public void render(BlockEntity blockEntity, float partialTicks, PoseStack stack,

if (!ConfigHolder.INSTANCE.client.renderer.renderFluids) return;
if (blockEntity instanceof MetaMachineBlockEntity mm) {
if (mm.metaMachine instanceof LargeChemicalBathMachine lcb && lcb.isActive()) {
GTRecipe last = lcb.recipeLogic.getLastRecipe();
if (last == null) return;

List<Content> contents = last.inputs.get(FluidRecipeCapability.CAP);
if (contents == null || contents.isEmpty()) return;

Optional<Content> fluidContent = contents.stream().filter(
content -> content.content instanceof FluidIngredient ingredient && !ingredient.isEmpty())
.findAny();
if (fluidContent.isEmpty()) return;
FluidIngredient ingredient = (FluidIngredient) fluidContent.get().content;

FluidStack[] stacks = ingredient.getStacks();
if (stacks.length == 0) return;
Optional<FluidStack> first = Arrays.stream(stacks).filter(s -> !s.isEmpty()).findFirst();
if (first.isEmpty()) return;
if (mm.metaMachine instanceof LargeChemicalBathMachine lcb) {
var lastRecipe = lcb.recipeLogic.getLastRecipe();
if (lastRecipe != null && (lcb.getOffsetTimer() % 20 == 0 || lastRecipe.id != cachedRecipe)) {
cachedRecipe = lastRecipe.id;
if (lcb.isActive()) {
cachedFluid = RenderUtil.getRecipeFluidToRender(lastRecipe);
} else {
cachedFluid = null;
}
}

if (cachedFluid == null) {
return;
}

stack.pushPose();
var pose = stack.last().pose();

var fluid = first.get().getFluid();
var fluidRenderType = ItemBlockRenderTypes.getRenderLayer(fluid.defaultFluidState());
var fluidRenderType = ItemBlockRenderTypes.getRenderLayer(cachedFluid.defaultFluidState());
var consumer = buffer.getBuffer(RenderTypeHelper.getEntityRenderType(fluidRenderType, false));

var up = RelativeDirection.UP.getRelativeFacing(lcb.getFrontFacing(), lcb.getUpwardsFacing(),
lcb.isFlipped());
if (up != Direction.UP && up != Direction.DOWN) up = up.getOpposite();
fluidBlockRenderer.drawPlane(up, lcb.getFluidBlockOffsets(), pose, consumer, fluid,
fluidBlockRenderer.drawPlane(up, lcb.getFluidBlockOffsets(), pose, consumer, cachedFluid,
RenderUtil.FluidTextureType.STILL, combinedOverlay, lcb.getPos());

stack.popPose();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.gregtechceu.gtceu.client.renderer.machine.gcym;

import com.gregtechceu.gtceu.api.blockentity.MetaMachineBlockEntity;
import com.gregtechceu.gtceu.api.capability.recipe.FluidRecipeCapability;
import com.gregtechceu.gtceu.api.pattern.util.RelativeDirection;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.content.Content;
import com.gregtechceu.gtceu.api.recipe.ingredient.FluidIngredient;
import com.gregtechceu.gtceu.client.renderer.block.FluidBlockRenderer;
import com.gregtechceu.gtceu.client.renderer.machine.WorkableCasingMachineRenderer;
import com.gregtechceu.gtceu.client.util.RenderUtil;
Expand All @@ -18,18 +14,16 @@
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.client.RenderTypeHelper;
import net.minecraftforge.fluids.FluidStack;

import com.mojang.blaze3d.vertex.PoseStack;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class LargeMixerRenderer extends WorkableCasingMachineRenderer {

private final FluidBlockRenderer fluidBlockRenderer;
private Fluid cachedFluid;
private ResourceLocation cachedRecipe;

public LargeMixerRenderer(ResourceLocation baseCasing, ResourceLocation workableModel) {
super(baseCasing, workableModel);
Expand Down Expand Up @@ -62,35 +56,31 @@ public void render(BlockEntity blockEntity, float partialTicks, PoseStack stack,

if (!ConfigHolder.INSTANCE.client.renderer.renderFluids) return;
if (blockEntity instanceof MetaMachineBlockEntity mm) {
if (mm.metaMachine instanceof LargeMixerMachine lm && lm.isActive()) {
GTRecipe last = lm.recipeLogic.getLastRecipe();
if (last == null) return;

List<Content> contents = last.inputs.get(FluidRecipeCapability.CAP);
if (contents == null || contents.isEmpty()) return;

Optional<Content> fluidContent = contents.stream().filter(
content -> content.content instanceof FluidIngredient ingredient && !ingredient.isEmpty())
.findAny();
if (fluidContent.isEmpty()) return;
FluidIngredient ingredient = (FluidIngredient) fluidContent.get().content;

FluidStack[] stacks = ingredient.getStacks();
if (stacks.length == 0) return;
Optional<FluidStack> first = Arrays.stream(stacks).filter(s -> !s.isEmpty()).findFirst();
if (first.isEmpty()) return;
if (mm.metaMachine instanceof LargeMixerMachine lm) {
var lastRecipe = lm.recipeLogic.getLastRecipe();
if (lastRecipe != null && (lm.getOffsetTimer() % 20 == 0 || lastRecipe.id != cachedRecipe)) {
cachedRecipe = lastRecipe.id;
if (lm.isActive()) {
cachedFluid = RenderUtil.getRecipeFluidToRender(lastRecipe);
} else {
cachedFluid = null;
}
}

if (cachedFluid == null) {
return;
}

stack.pushPose();
var pose = stack.last().pose();

var fluid = first.get().getFluid();
var fluidRenderType = ItemBlockRenderTypes.getRenderLayer(fluid.defaultFluidState());
var fluidRenderType = ItemBlockRenderTypes.getRenderLayer(cachedFluid.defaultFluidState());
var consumer = buffer.getBuffer(RenderTypeHelper.getEntityRenderType(fluidRenderType, false));

var up = RelativeDirection.UP.getRelativeFacing(lm.getFrontFacing(), lm.getUpwardsFacing(),
lm.isFlipped());
if (up != Direction.UP && up != Direction.DOWN) up = up.getOpposite();
fluidBlockRenderer.drawPlane(up, lm.getFluidBlockOffsets(), pose, consumer, fluid,
fluidBlockRenderer.drawPlane(up, lm.getFluidBlockOffsets(), pose, consumer, cachedFluid,
RenderUtil.FluidTextureType.STILL, combinedOverlay, lm.getPos());

stack.popPose();
Expand Down
44 changes: 42 additions & 2 deletions src/main/java/com/gregtechceu/gtceu/client/util/RenderUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.gregtechceu.gtceu.client.util;

import com.gregtechceu.gtceu.api.capability.recipe.FluidRecipeCapability;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.content.Content;
import com.gregtechceu.gtceu.api.recipe.ingredient.FluidIngredient;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand All @@ -13,11 +18,11 @@
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions;

import com.mojang.blaze3d.vertex.VertexConsumer;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.joml.Matrix4f;
import org.joml.Vector3f;

import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.function.Function;

@OnlyIn(Dist.CLIENT)
Expand Down Expand Up @@ -118,4 +123,39 @@ public static Vector3f transformVertex(Vector3f vertex, Direction direction, flo

return vec3f(vertex.x + addX, vertex.y + addY, vertex.z + addZ);
}

public static Fluid getRecipeFluidToRender(GTRecipe recipe) {
if (recipe == null) {
return null;
}
var contents = new ObjectArrayList<Content>();
var empty = new ArrayList<Content>();
contents.addAll(recipe.outputs.getOrDefault(FluidRecipeCapability.CAP, empty));
contents.addAll(recipe.inputs.getOrDefault(FluidRecipeCapability.CAP, empty));
if (contents.isEmpty()) {
return null;
}

var fluidContent = contents.stream()
.filter(content -> content.content instanceof FluidIngredient ingredient && !ingredient.isEmpty())
.findAny();
if (fluidContent.isEmpty()) {
return null;
}
var ingredient = (FluidIngredient) fluidContent.get().content;

var stacks = ingredient.getStacks();
if (stacks.length == 0) {
return null;
}

Fluid fluid = null;
for (int i = 0; i < stacks.length && fluid == null; i++) {
if (!stacks[i].isEmpty()) {
fluid = stacks[i].getFluid();
}
}

return fluid;
}
}

0 comments on commit 1b14ab8

Please sign in to comment.