| 1 | package net.minecraft.client.renderer.special;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.model.object.chest.ChestModel;
|
| 11 | import net.minecraft.client.renderer.MultiblockChestResources;
|
| 12 | import net.minecraft.client.renderer.Sheets;
|
| 13 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 14 | import net.minecraft.client.renderer.blockentity.ChestRenderer;
|
| 15 | import net.minecraft.client.resources.model.sprite.SpriteGetter;
|
| 16 | import net.minecraft.client.resources.model.sprite.SpriteId;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.world.level.block.WeatheringCopperCollection.ByState;
|
| 19 | import net.minecraft.world.level.block.state.properties.ChestType;
|
| 20 | import org.joml.Vector3fc;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class ChestSpecialRenderer implements NoDataSpecialModelRenderer {
|
| 24 | public static final Identifier ENDER_CHEST = Identifier.withDefaultNamespace("ender");
|
| 25 | public static final MultiblockChestResources<Identifier> REGULAR = createDefaultTextures("normal");
|
| 26 | public static final MultiblockChestResources<Identifier> TRAPPED = createDefaultTextures("trapped");
|
| 27 | public static final MultiblockChestResources<Identifier> CHRISTMAS = createDefaultTextures("christmas");
|
| 28 | public static final ByState<MultiblockChestResources<Identifier>> COPPER = new ByState<>(
|
| 29 | createDefaultTextures("copper"), createDefaultTextures("copper_exposed"), createDefaultTextures("copper_weathered"), createDefaultTextures("copper_oxidized")
|
| 30 | );
|
| 31 | private final SpriteGetter sprites;
|
| 32 | private final ChestModel model;
|
| 33 | private final SpriteId sprite;
|
| 34 | private final float openness;
|
| 35 |
|
| 36 | public ChestSpecialRenderer(final SpriteGetter sprites, final ChestModel model, final SpriteId sprite, final float openness) {
|
| 37 | this.sprites = sprites;
|
| 38 | this.model = model;
|
| 39 | this.sprite = sprite;
|
| 40 | this.openness = openness;
|
| 41 | }
|
| 42 |
|
| 43 | private static MultiblockChestResources<Identifier> createDefaultTextures(final String prefix) {
|
| 44 | return new MultiblockChestResources<>(
|
| 45 | Identifier.withDefaultNamespace(prefix), Identifier.withDefaultNamespace(prefix + "_left"), Identifier.withDefaultNamespace(prefix + "_right")
|
| 46 | );
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void submit(
|
| 51 | final PoseStack poseStack,
|
| 52 | final SubmitNodeCollector submitNodeCollector,
|
| 53 | final int lightCoords,
|
| 54 | final int overlayCoords,
|
| 55 | final boolean hasFoil,
|
| 56 | final int outlineColor
|
| 57 | ) {
|
| 58 | submitNodeCollector.submitModel(this.model, this.openness, poseStack, lightCoords, overlayCoords, -1, this.sprite, this.sprites, outlineColor);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void getExtents(final Consumer<Vector3fc> output) {
|
| 63 | PoseStack poseStack = new PoseStack();
|
| 64 | this.model.setupAnim(this.openness);
|
| 65 | this.model.root().getExtentsForGui(poseStack, output);
|
| 66 | }
|
| 67 |
|
| 68 | @Environment(EnvType.CLIENT)
|
| 69 | public record Unbaked(Identifier texture, float openness, ChestType chestType) implements NoDataSpecialModelRenderer.Unbaked {
|
| 70 | public static final MapCodec<ChestSpecialRenderer.Unbaked> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 71 | i -> i.group(
|
| 72 | Identifier.CODEC.fieldOf("texture").forGetter(ChestSpecialRenderer.Unbaked::texture),
|
| 73 | Codec.FLOAT.optionalFieldOf("openness", 0.0F).forGetter(ChestSpecialRenderer.Unbaked::openness),
|
| 74 | ChestType.CODEC.optionalFieldOf("chest_type", ChestType.SINGLE).forGetter(ChestSpecialRenderer.Unbaked::chestType)
|
| 75 | )
|
| 76 | .apply(i, ChestSpecialRenderer.Unbaked::new)
|
| 77 | );
|
| 78 |
|
| 79 | public Unbaked(final Identifier texture, final ChestType chestType) {
|
| 80 | this(texture, 0.0F, chestType);
|
| 81 | }
|
| 82 |
|
| 83 | public Unbaked(final Identifier texture) {
|
| 84 | this(texture, 0.0F, ChestType.SINGLE);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public MapCodec<ChestSpecialRenderer.Unbaked> type() {
|
| 89 | return MAP_CODEC;
|
| 90 | }
|
| 91 |
|
| 92 | public ChestSpecialRenderer bake(final SpecialModelRenderer.BakingContext context) {
|
| 93 | ChestModel model = new ChestModel(context.entityModelSet().bakeLayer(ChestRenderer.LAYERS.select(this.chestType)));
|
| 94 | SpriteId fullTexture = Sheets.CHEST_MAPPER.apply(this.texture);
|
| 95 | return new ChestSpecialRenderer(context.sprites(), model, fullTexture, this.openness);
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|