ModernLifePatch/src-source/main/java/com/dairymoose/modernlife/renderer/tileentity/ModernBookshelfBlockEntityRenderer.java
2024-10-26 09:40:21 +08:00

176 lines
10 KiB
Java

package com.dairymoose.modernlife.renderer.tileentity;
import com.dairymoose.modernlife.blocks.ModernBookshelfBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.tileentities.ModernBookshelfBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Vector3f;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@OnlyIn(Dist.CLIENT)
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ModernBookshelfBlockEntityRenderer.class */
public class ModernBookshelfBlockEntityRenderer implements BlockEntityRenderer<ModernBookshelfBlockEntity> {
private static final Logger LOGGER = LogManager.getLogger();
public static ItemStack renderBook = null;
public static int renderRgb0 = 0;
public static int renderRgb1 = 0;
public static Map<Item, Integer> rgb0ForItem = new HashMap();
public static Map<Item, Integer> rgb1ForItem = new HashMap();
public ModernBookshelfBlockEntityRenderer(BlockEntityRendererProvider.Context ctx) {
}
private int reverseEndianness(int input) {
return ((input & 255) << 24) | ((input & 65280) << 8) | ((input & 16711680) >>> 8) | ((input & (-16777216)) >>> 24);
}
public void render(ModernBookshelfBlockEntity tileEntity, float v, PoseStack matrixStack, MultiBufferSource iRenderTypeBuffer, int lightmap, int i1) {
int mostCommonRgb;
int secondMostCommonRgb;
List<BakedQuad> quads;
if (!tileEntity.isEmpty()) {
for (int i = 0; i < tileEntity.getContainerSize(); i++) {
ItemStack itemStack = tileEntity.getItem(i);
if (!itemStack.isEmpty()) {
matrixStack.pushPose();
if (tileEntity.getBlockState().getValue(ModernBookshelfBlock.FACING) == Direction.EAST) {
matrixStack.mulPose(Vector3f.YP.rotationDegrees(90.0f));
matrixStack.translate(-0.9375d, 0.5d, 0.0d);
} else if (tileEntity.getBlockState().getValue(ModernBookshelfBlock.FACING) == Direction.NORTH) {
matrixStack.mulPose(Vector3f.YP.rotationDegrees(180.0f));
matrixStack.translate(-0.9375d, 0.5d, -1.0d);
} else if (tileEntity.getBlockState().getValue(ModernBookshelfBlock.FACING) == Direction.WEST) {
matrixStack.mulPose(Vector3f.YP.rotationDegrees(270.0f));
matrixStack.translate(0.0625d, 0.5d, -1.0d);
} else if (tileEntity.getBlockState().getValue(ModernBookshelfBlock.FACING) == Direction.SOUTH) {
matrixStack.translate(0.0625d, 0.5d, 0.0d);
}
List<Double> yList = null;
if (tileEntity.getType() == ModernBookshelfBlockEntity.MODERN_BOOKSHELF) {
yList = ModernBookshelfBlockEntity.itemPosY;
}
matrixStack.translate(ModernBookshelfBlockEntity.itemPosX.get(i % ModernBookshelfBlockEntity.itemPosX.size()).doubleValue(), yList.get(i % yList.size()).doubleValue(), ModernBookshelfBlockEntity.itemPosZ.get(i % ModernBookshelfBlockEntity.itemPosZ.size()).doubleValue());
matrixStack.mulPose(Vector3f.YP.rotationDegrees(180.0f));
renderBook = itemStack;
HashMap<Integer, Integer> colorCount = new HashMap<>();
TextureAtlasSprite sprite = null;
BakedModel bm = Minecraft.getInstance().getItemRenderer().getModel(itemStack, (Level) null, (LivingEntity) null, i);
if (bm != null && (quads = bm.getQuads((BlockState) null, (Direction) null, new Random())) != null && !quads.isEmpty()) {
sprite = quads.get(0).getSprite();
}
Integer lookupRgb0 = rgb0ForItem.get(itemStack.getItem());
Integer lookupRgb1 = rgb1ForItem.get(itemStack.getItem());
if (lookupRgb0 == null) {
if (sprite != null) {
for (int x = 0; x < sprite.getWidth(); x++) {
for (int y = 0; y < sprite.getHeight(); y++) {
int rgba = sprite.getPixelRGBA(0, x, y);
int rgb = rgba & 16777215;
int a = (rgba & (-16777216)) >>> 24;
if (a == 255) {
Integer count = colorCount.get(Integer.valueOf(rgb));
if (count == null) {
count = 0;
}
colorCount.put(Integer.valueOf(rgb), Integer.valueOf(count.intValue() + 1));
}
}
}
int highestIndex = 0;
for (Integer rgb2 : colorCount.keySet()) {
Integer highestCount = colorCount.get(Integer.valueOf(highestIndex));
Integer count2 = colorCount.get(rgb2);
if (highestCount == null || count2.intValue() > highestCount.intValue()) {
highestIndex = rgb2.intValue();
}
}
Integer highestCount2 = colorCount.get(Integer.valueOf(highestIndex));
int secondHighestIndex = 0;
for (Integer rgb3 : colorCount.keySet()) {
Integer secondHighestCount = colorCount.get(Integer.valueOf(secondHighestIndex));
Integer count3 = colorCount.get(rgb3);
if (secondHighestCount == null || (count3.intValue() > secondHighestCount.intValue() && count3.intValue() < highestCount2.intValue())) {
secondHighestIndex = rgb3.intValue();
}
}
mostCommonRgb = reverseEndianness(highestIndex) >> 8;
secondMostCommonRgb = reverseEndianness(secondHighestIndex) >> 8;
} else {
mostCommonRgb = 8355711;
secondMostCommonRgb = 0;
}
rgb0ForItem.put(itemStack.getItem(), Integer.valueOf(mostCommonRgb));
rgb1ForItem.put(itemStack.getItem(), Integer.valueOf(secondMostCommonRgb));
} else {
mostCommonRgb = lookupRgb0.intValue();
secondMostCommonRgb = lookupRgb1.intValue();
}
renderRgb0 = mostCommonRgb;
renderRgb1 = secondMostCommonRgb;
tileEntity.bookHeight.get(i).floatValue();
matrixStack.scale(1.0f, 1.0f, 1.0f);
ItemStack toRender = new ItemStack(CustomBlocks.BLOCK_MODERN_BOOK.get());
Minecraft.getInstance().getItemRenderer().renderStatic(toRender, ItemTransforms.TransformType.NONE, lightmap, i1, matrixStack, iRenderTypeBuffer, i);
matrixStack.translate(-0.013000000268220901d, -0.24500000476837158d, -0.17550000548362732d);
float scaleValue = -0.003f;
String displayText = itemStack.getHoverName().getString();
String storedName = (String) tileEntity.bookNames.get(i);
if (!storedName.isBlank()) {
displayText = storedName;
}
int textLength = displayText.length();
if (textLength > 14) {
int overMaxDiff = textLength - 14;
scaleValue = (-0.003f) + (overMaxDiff * 8.0E-5f);
}
if (scaleValue > 0.0d) {
scaleValue = 0.1f;
}
matrixStack.scale(scaleValue, scaleValue, scaleValue);
matrixStack.mulPose(Vector3f.ZP.rotationDegrees(90.0f));
float lightmapPct = lightmap / 200.0f;
if (lightmapPct < 0.2f) {
lightmapPct = 0.2f;
} else if (lightmapPct > 0.9f) {
lightmapPct = 0.9f;
}
int textR = (int) (255.0f * lightmapPct);
int textG = (int) (255.0f * lightmapPct);
int textB = (int) (255.0f * lightmapPct);
int textColor = (textR << 16) | (textG << 8) | textB;
TextComponent displayComponent = new TextComponent(displayText);
Style displayStyle = displayComponent.getStyle();
Font font = Minecraft.getInstance().font;
Minecraft.getInstance();
font.draw(matrixStack, displayComponent.m_130948_(displayStyle.withFont(Minecraft.UNIFORM_FONT).withBold(true)), 0.0f, 0.0f, textColor);
matrixStack.popPose();
}
}
}
}
}