Skip to content

Commit

Permalink
Centralize drawer rendering code.
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed May 2, 2022
1 parent fdce6e1 commit faca6de
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.github.mattidragon.extendeddrawers.client.renderer;

import io.github.mattidragon.extendeddrawers.config.ClientConfig;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3f;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;

@SuppressWarnings("UnstableApiUsage")
public abstract class AbstractDrawerBlockEntityRenderer<T extends BlockEntity> implements BlockEntityRenderer<T> {
public AbstractDrawerBlockEntityRenderer(BlockEntityRendererFactory.Context context) {}

public void renderSlot(ItemVariant item, @Nullable Long amount, List<Sprite> icons, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, int seed, BlockPos pos) {
//noinspection ConstantConditions
var playerPos = MinecraftClient.getInstance().player.getPos();
var config = ClientConfig.HANDLE.get();

if (pos.isWithinDistance(playerPos, config.textRenderDistance()) && amount != null)
renderText(amount, light, matrices, vertexConsumers);
if (pos.isWithinDistance(playerPos, config.iconRenderDistance()))
renderIcons(icons, light, matrices, vertexConsumers, overlay);
if (pos.isWithinDistance(playerPos, config.itemRenderDistance()))
renderItem(item, light, matrices, vertexConsumers, seed);
}

private void renderIcons(List<Sprite> icons, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int overlay) {
var increment = 1.0 / (icons.size() + 1.0);
matrices.push();
matrices.translate(-0.5, 0, 0);
for (var icon : icons) {
matrices.translate(increment, 0, 0);
renderIcon(icon, light, matrices, vertexConsumers, overlay);
}
matrices.pop();
}

private void renderIcon(Sprite sprite, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int overlay) {
matrices.push();
matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(90));
matrices.translate(-0.125, -0.24, -0.5);
matrices.scale(0.25f, 0.25f, 0.25f);
var emitter = Objects.requireNonNull(RendererAccess.INSTANCE.getRenderer()).meshBuilder().getEmitter();
emitter.square(Direction.UP, 0, 0, 1, 1, 0);
emitter.spriteBake(0, sprite, MutableQuadView.BAKE_LOCK_UV);
vertexConsumers.getBuffer(RenderLayer.getCutout()).quad(matrices.peek(), emitter.toBakedQuad(0, sprite, false), 1, 1, 1, light, overlay);
matrices.pop();
}

private void renderItem(ItemVariant item, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int seed) {
matrices.push();
matrices.scale(0.75f, 0.75f, 1);
matrices.multiplyPositionMatrix(Matrix4f.scale(1, 1, 0.01f));
MinecraftClient.getInstance().getItemRenderer().renderItem(item.toStack(), ModelTransformation.Mode.GUI, light, OverlayTexture.DEFAULT_UV, matrices, vertexConsumers, seed);
matrices.pop();
}

private void renderText(long amount, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers) {
matrices.push();
matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(180));
matrices.translate(0, 0.3, -0.01);
matrices.scale(0.02f, 0.02f, 0.02f);
var textRenderer = MinecraftClient.getInstance().textRenderer;
var text = Long.toString(amount);
textRenderer.draw(text, -textRenderer.getWidth(text) / 2f, 0, 0xffffff, false, matrices.peek().getPositionMatrix(), vertexConsumers, false, 0x000000, light);
matrices.pop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@
import io.github.mattidragon.extendeddrawers.block.entity.DrawerBlockEntity;
import io.github.mattidragon.extendeddrawers.config.ClientConfig;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactory;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.util.math.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3f;

import java.util.ArrayList;
import java.util.Objects;

import static io.github.mattidragon.extendeddrawers.ExtendedDrawers.id;

@SuppressWarnings("UnstableApiUsage")
public class DrawerBlockEntityRenderer implements BlockEntityRenderer<DrawerBlockEntity> {
public DrawerBlockEntityRenderer(BlockEntityRendererFactory.Context context) {}
public class DrawerBlockEntityRenderer extends AbstractDrawerBlockEntityRenderer<DrawerBlockEntity> {
public DrawerBlockEntityRenderer(BlockEntityRendererFactory.Context context) {
super(context);
}

@Override
public int getRenderDistance() {
Expand Down Expand Up @@ -76,70 +72,12 @@ public void render(DrawerBlockEntity entity, float tickDelta, MatrixStack matric
}

private void renderSlot(DrawerSlot storage, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int seed, int overlay, BlockPos pos) {
//noinspection ConstantConditions
var playerPos = MinecraftClient.getInstance().player.getPos();
var config = ClientConfig.HANDLE.get();

if (pos.isWithinDistance(playerPos, config.textRenderDistance()))
renderText(storage, light, matrices, vertexConsumers);
if (pos.isWithinDistance(playerPos, config.iconRenderDistance()))
renderIcons(storage, light, matrices, vertexConsumers, overlay);
if (pos.isWithinDistance(playerPos, config.itemRenderDistance()))
renderItem(storage, light, matrices, vertexConsumers, seed);
}

private void renderIcons(DrawerSlot storage, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int overlay) {
var icons = new ArrayList<Sprite>();
var mc = MinecraftClient.getInstance();
var blockAtlas = mc.getSpriteAtlas(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE);

if (storage.locked)
icons.add(blockAtlas.apply(id("item/lock")));
var blockAtlas = MinecraftClient.getInstance().getSpriteAtlas(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE);

if (storage.upgrade != null)
icons.add(blockAtlas.apply(storage.upgrade.sprite));

if (storage.locked) icons.add(blockAtlas.apply(id("item/lock")));
if (storage.upgrade != null) icons.add(blockAtlas.apply(storage.upgrade.sprite));

var increment = 1.0 / (icons.size() + 1.0);
matrices.push();
matrices.translate(-0.5, 0, 0);
for (var icon : icons) {
matrices.translate(increment, 0, 0);
renderIcon(icon, light, matrices, vertexConsumers, overlay);
}
matrices.pop();
}

private void renderIcon(Sprite sprite, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int overlay) {
matrices.push();
matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(90));
matrices.translate(-0.125, -0.24, -0.5);
matrices.scale(0.25f, 0.25f, 0.25f);
var emitter = Objects.requireNonNull(RendererAccess.INSTANCE.getRenderer()).meshBuilder().getEmitter();
emitter.square(Direction.UP, 0, 0, 1, 1, 0);
emitter.spriteBake(0, sprite, MutableQuadView.BAKE_LOCK_UV);
vertexConsumers.getBuffer(RenderLayer.getCutout()).quad(matrices.peek(), emitter.toBakedQuad(0, sprite, false), 1, 1, 1, light, overlay);
matrices.pop();
}

private void renderItem(DrawerSlot storage, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int seed) {
matrices.push();
matrices.scale(0.75f, 0.75f, 1);
matrices.multiplyPositionMatrix(Matrix4f.scale(1, 1, 0.01f));
MinecraftClient.getInstance().getItemRenderer().renderItem(storage.item.toStack(), ModelTransformation.Mode.GUI, light, OverlayTexture.DEFAULT_UV, matrices, vertexConsumers, seed);
matrices.pop();
}

private void renderText(DrawerSlot storage, int light, MatrixStack matrices, VertexConsumerProvider vertexConsumers) {
if (storage.isResourceBlank()) return;

matrices.push();
matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(180));
matrices.translate(0, 0.3, -0.01);
matrices.scale(0.02f, 0.02f, 0.02f);
var textRenderer = MinecraftClient.getInstance().textRenderer;
var text = Long.toString(storage.amount);
textRenderer.draw(text, -textRenderer.getWidth(text) / 2f, 0, 0xffffff, false, matrices.peek().getPositionMatrix(), vertexConsumers, false, 0x000000, light);
matrices.pop();
renderSlot(storage.item, storage.amount == 0 ? null: storage.amount, icons, matrices, vertexConsumers, light, overlay, seed, pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3f;

import java.util.List;
import java.util.Objects;

public class ShadowDrawerBlockEntityRenderer implements BlockEntityRenderer<ShadowDrawerBlockEntity> {
public ShadowDrawerBlockEntityRenderer(BlockEntityRendererFactory.Context context) {}
public class ShadowDrawerBlockEntityRenderer extends AbstractDrawerBlockEntityRenderer<ShadowDrawerBlockEntity> {
public ShadowDrawerBlockEntityRenderer(BlockEntityRendererFactory.Context context) {
super(context);
}

@Override
public int getRenderDistance() {
Expand All @@ -31,8 +34,6 @@ public void render(ShadowDrawerBlockEntity entity, float tickDelta, MatrixStack
matrices.push();
var dir = entity.getCachedState().get(ShadowDrawerBlock.FACING);
var pos = dir.getUnitVector();
var config = ClientConfig.HANDLE.get();
var playerPos = MinecraftClient.getInstance().player.getPos();

matrices.translate(pos.getX() / 2 + 0.5, pos.getY() / 2 + 0.5, pos.getZ() / 2 + 0.5);
matrices.multiply(dir.getRotationQuaternion());
Expand All @@ -41,26 +42,7 @@ public void render(ShadowDrawerBlockEntity entity, float tickDelta, MatrixStack

light = WorldRenderer.getLightmapCoordinates(Objects.requireNonNull(entity.getWorld()), entity.getPos().offset(dir));

if (entity.getPos().isWithinDistance(playerPos, config.itemRenderDistance())) {
matrices.push();
matrices.scale(0.75f, 0.75f, 1);
matrices.multiplyPositionMatrix(Matrix4f.scale(1, 1, 0.01f));
MinecraftClient.getInstance().getItemRenderer().renderItem(entity.item.toStack(), ModelTransformation.Mode.GUI, light, OverlayTexture.DEFAULT_UV, matrices, vertexConsumers, (int) entity.getPos().asLong());
matrices.pop();
}

if (!entity.item.isBlank() && entity.getPos().isWithinDistance(playerPos, config.textRenderDistance())) {

matrices.push();
matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(180));
matrices.translate(0, 0.3, -0.01);
matrices.scale(0.02f, 0.02f, 0.02f);
var textRenderer = MinecraftClient.getInstance().textRenderer;

var text = Long.toString(entity.getCount());
textRenderer.draw(text, -textRenderer.getWidth(text) / 2f, 0, 0xffffff, false, matrices.peek().getPositionMatrix(), vertexConsumers, false, 0x000000, light);
matrices.pop();
}
renderSlot(entity.item, entity.item.isBlank() ? null : entity.getCount(), List.of(), matrices, vertexConsumers, light, overlay, (int) entity.getPos().asLong(), entity.getPos());
matrices.pop();
}
}

0 comments on commit faca6de

Please sign in to comment.