| 1 | package net.minecraft.client.renderer.special;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Transformation;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import java.util.Objects;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.model.geom.ModelLayers;
|
| 11 | import net.minecraft.client.model.object.equipment.ShieldModel;
|
| 12 | import net.minecraft.client.renderer.Sheets;
|
| 13 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 14 | import net.minecraft.client.renderer.blockentity.BannerRenderer;
|
| 15 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 16 | import net.minecraft.client.resources.model.sprite.SpriteGetter;
|
| 17 | import net.minecraft.client.resources.model.sprite.SpriteId;
|
| 18 | import net.minecraft.core.component.DataComponentMap;
|
| 19 | import net.minecraft.core.component.DataComponents;
|
| 20 | import net.minecraft.util.Unit;
|
| 21 | import net.minecraft.world.item.DyeColor;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 24 | import org.joml.Vector3f;
|
| 25 | import org.joml.Vector3fc;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class ShieldSpecialRenderer implements SpecialModelRenderer<DataComponentMap> {
|
| 30 | public static final Transformation DEFAULT_TRANSFORMATION = new Transformation(null, null, new Vector3f(1.0F, -1.0F, -1.0F), null);
|
| 31 | private final SpriteGetter sprites;
|
| 32 | private final ShieldModel model;
|
| 33 |
|
| 34 | public ShieldSpecialRenderer(final SpriteGetter sprites, final ShieldModel model) {
|
| 35 | this.sprites = sprites;
|
| 36 | this.model = model;
|
| 37 | }
|
| 38 |
|
| 39 | public @Nullable DataComponentMap extractArgument(final ItemStack stack) {
|
| 40 | return stack.immutableComponents();
|
| 41 | }
|
| 42 |
|
| 43 | public void submit(
|
| 44 | final @Nullable DataComponentMap components,
|
| 45 | final PoseStack poseStack,
|
| 46 | final SubmitNodeCollector submitNodeCollector,
|
| 47 | final int lightCoords,
|
| 48 | final int overlayCoords,
|
| 49 | final boolean hasFoil,
|
| 50 | final int outlineColor
|
| 51 | ) {
|
| 52 | BannerPatternLayers patterns = components != null
|
| 53 | ? components.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY)
|
| 54 | : BannerPatternLayers.EMPTY;
|
| 55 | DyeColor baseColor = components != null ? components.get(DataComponents.BASE_COLOR) : null;
|
| 56 | boolean hasPatterns = !patterns.layers().isEmpty() || baseColor != null;
|
| 57 | SpriteId base = hasPatterns ? Sheets.SHIELD_BASE : Sheets.SHIELD_BASE_NO_PATTERN;
|
| 58 | if (hasFoil && !hasPatterns) {
|
| 59 | submitNodeCollector.submitModel(
|
| 60 | this.model,
|
| 61 | Unit.INSTANCE,
|
| 62 | poseStack,
|
| 63 | RenderTypes.entitySolidGlint(base.atlasLocation()),
|
| 64 | lightCoords,
|
| 65 | overlayCoords,
|
| 66 | -1,
|
| 67 | this.sprites.get(base),
|
| 68 | outlineColor
|
| 69 | );
|
| 70 | } else {
|
| 71 | submitNodeCollector.submitModel(this.model, Unit.INSTANCE, poseStack, lightCoords, overlayCoords, -1, base, this.sprites, outlineColor);
|
| 72 | }
|
| 73 |
|
| 74 | if (hasPatterns) {
|
| 75 | BannerRenderer.submitPatterns(
|
| 76 | this.sprites,
|
| 77 | poseStack,
|
| 78 | submitNodeCollector,
|
| 79 | lightCoords,
|
| 80 | overlayCoords,
|
| 81 | this.model,
|
| 82 | Unit.INSTANCE,
|
| 83 | false,
|
| 84 | Objects.requireNonNullElse(baseColor, DyeColor.WHITE),
|
| 85 | patterns
|
| 86 | );
|
| 87 | if (hasFoil) {
|
| 88 | submitNodeCollector.order(patterns.layers().size() + 2)
|
| 89 | .submitModel(this.model, Unit.INSTANCE, poseStack, RenderTypes.patternedShieldGlint(), lightCoords, overlayCoords, -1, this.sprites.get(base), 0);
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void getExtents(final Consumer<Vector3fc> output) {
|
| 96 | PoseStack poseStack = new PoseStack();
|
| 97 | this.model.root().getExtentsForGui(poseStack, output);
|
| 98 | }
|
| 99 |
|
| 100 | @Environment(EnvType.CLIENT)
|
| 101 | public record Unbaked() implements SpecialModelRenderer.Unbaked<DataComponentMap> {
|
| 102 | public static final ShieldSpecialRenderer.Unbaked INSTANCE = new ShieldSpecialRenderer.Unbaked();
|
| 103 | public static final MapCodec<ShieldSpecialRenderer.Unbaked> MAP_CODEC = MapCodec.unit(INSTANCE);
|
| 104 |
|
| 105 | @Override
|
| 106 | public MapCodec<ShieldSpecialRenderer.Unbaked> type() {
|
| 107 | return MAP_CODEC;
|
| 108 | }
|
| 109 |
|
| 110 | public ShieldSpecialRenderer bake(final SpecialModelRenderer.BakingContext context) {
|
| 111 | return new ShieldSpecialRenderer(context.sprites(), new ShieldModel(context.entityModelSet().bakeLayer(ModelLayers.SHIELD)));
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|