| 1 | package net.minecraft.client.resources.model;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableMap;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.Collections;
|
| 10 | import java.util.EnumMap;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Map.Entry;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.core.registries.Registries;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import net.minecraft.resources.ResourceKey;
|
| 20 | import net.minecraft.util.ExtraCodecs;
|
| 21 | import net.minecraft.util.StringRepresentable;
|
| 22 | import net.minecraft.world.item.equipment.trim.ArmorTrim;
|
| 23 | import net.minecraft.world.item.equipment.trim.TrimMaterial;
|
| 24 | import net.minecraft.world.item.equipment.trim.TrimPattern;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public record EquipmentClientInfo(
|
| 28 | Map<EquipmentClientInfo.LayerType, List<EquipmentClientInfo.Layer>> layers, List<EquipmentClientInfo.TrimOverride> trimOverrides
|
| 29 | ) {
|
| 30 | private static final Codec<List<EquipmentClientInfo.Layer>> LAYER_LIST_CODEC = ExtraCodecs.nonEmptyList(EquipmentClientInfo.Layer.CODEC.listOf());
|
| 31 | public static final Codec<EquipmentClientInfo> CODEC = RecordCodecBuilder.create(
|
| 32 | i -> i.group(
|
| 33 | ExtraCodecs.nonEmptyMap(Codec.unboundedMap(EquipmentClientInfo.LayerType.CODEC, LAYER_LIST_CODEC)).fieldOf("layers").forGetter(EquipmentClientInfo::layers),
|
| 34 | EquipmentClientInfo.TrimOverride.CODEC.listOf().optionalFieldOf("trim_overrides", List.of()).forGetter(EquipmentClientInfo::trimOverrides)
|
| 35 | )
|
| 36 | .apply(i, EquipmentClientInfo::new)
|
| 37 | );
|
| 38 |
|
| 39 | public static EquipmentClientInfo.Builder builder() {
|
| 40 | return new EquipmentClientInfo.Builder();
|
| 41 | }
|
| 42 |
|
| 43 | public List<EquipmentClientInfo.Layer> getLayers(final EquipmentClientInfo.LayerType type) {
|
| 44 | return this.layers.getOrDefault(type, List.of());
|
| 45 | }
|
| 46 |
|
| 47 | @Environment(EnvType.CLIENT)
|
| 48 | public static class Builder {
|
| 49 | private final Map<EquipmentClientInfo.LayerType, List<EquipmentClientInfo.Layer>> layersByType = new EnumMap<>(EquipmentClientInfo.LayerType.class);
|
| 50 | private final ImmutableList.Builder<EquipmentClientInfo.TrimOverride> trimOverrides = ImmutableList.builder();
|
| 51 |
|
| 52 | private Builder() {
|
| 53 | }
|
| 54 |
|
| 55 | public EquipmentClientInfo.Builder addHumanoidLayers(final Identifier textureId) {
|
| 56 | return this.addHumanoidLayers(textureId, false);
|
| 57 | }
|
| 58 |
|
| 59 | public EquipmentClientInfo.Builder addHumanoidLayers(final Identifier textureId, final boolean dyeable) {
|
| 60 | this.addLayers(EquipmentClientInfo.LayerType.HUMANOID_LEGGINGS, EquipmentClientInfo.Layer.leatherDyeable(textureId, dyeable));
|
| 61 | this.addMainHumanoidLayer(textureId, dyeable);
|
| 62 | return this;
|
| 63 | }
|
| 64 |
|
| 65 | public EquipmentClientInfo.Builder addMainHumanoidLayer(final Identifier textureId, final boolean dyeable) {
|
| 66 | this.addLayers(EquipmentClientInfo.LayerType.HUMANOID, EquipmentClientInfo.Layer.leatherDyeable(textureId, dyeable));
|
| 67 | this.addLayers(EquipmentClientInfo.LayerType.HUMANOID_BABY, EquipmentClientInfo.Layer.leatherDyeable(textureId, dyeable));
|
| 68 | return this;
|
| 69 | }
|
| 70 |
|
| 71 | public EquipmentClientInfo.Builder addLayers(final EquipmentClientInfo.LayerType type, final EquipmentClientInfo.Layer... layers) {
|
| 72 | Collections.addAll(this.layersByType.computeIfAbsent(type, t -> new ArrayList<>()), layers);
|
| 73 | return this;
|
| 74 | }
|
| 75 |
|
| 76 | public EquipmentClientInfo.Builder replaceTrimPalette(final ResourceKey<TrimMaterial> fromMaterial, final Identifier toPaletteId) {
|
| 77 | EquipmentClientInfo.TrimPredicate predicate = new EquipmentClientInfo.TrimPredicate(Optional.of(fromMaterial), Optional.empty());
|
| 78 | this.trimOverrides.add(new EquipmentClientInfo.TrimOverride(predicate, Optional.empty(), Optional.of(toPaletteId)));
|
| 79 | return this;
|
| 80 | }
|
| 81 |
|
| 82 | public EquipmentClientInfo build() {
|
| 83 | return new EquipmentClientInfo(
|
| 84 | this.layersByType.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, entry -> List.copyOf(entry.getValue()))),
|
| 85 | this.trimOverrides.build()
|
| 86 | );
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Environment(EnvType.CLIENT)
|
| 91 | public record Dyeable(Optional<Integer> colorWhenUndyed) {
|
| 92 | public static final Codec<EquipmentClientInfo.Dyeable> CODEC = RecordCodecBuilder.create(
|
| 93 | i -> i.group(ExtraCodecs.RGB_COLOR_CODEC.optionalFieldOf("color_when_undyed").forGetter(EquipmentClientInfo.Dyeable::colorWhenUndyed))
|
| 94 | .apply(i, EquipmentClientInfo.Dyeable::new)
|
| 95 | );
|
| 96 | }
|
| 97 |
|
| 98 | @Environment(EnvType.CLIENT)
|
| 99 | public record Layer(Identifier textureId, Optional<EquipmentClientInfo.Dyeable> dyeable, boolean usePlayerTexture) {
|
| 100 | public static final Codec<EquipmentClientInfo.Layer> CODEC = RecordCodecBuilder.create(
|
| 101 | i -> i.group(
|
| 102 | Identifier.CODEC.fieldOf("texture").forGetter(EquipmentClientInfo.Layer::textureId),
|
| 103 | EquipmentClientInfo.Dyeable.CODEC.optionalFieldOf("dyeable").forGetter(EquipmentClientInfo.Layer::dyeable),
|
| 104 | Codec.BOOL.optionalFieldOf("use_player_texture", false).forGetter(EquipmentClientInfo.Layer::usePlayerTexture)
|
| 105 | )
|
| 106 | .apply(i, EquipmentClientInfo.Layer::new)
|
| 107 | );
|
| 108 |
|
| 109 | public Layer(final Identifier textureId) {
|
| 110 | this(textureId, Optional.empty(), false);
|
| 111 | }
|
| 112 |
|
| 113 | public static EquipmentClientInfo.Layer leatherDyeable(final Identifier textureId, final boolean dyeable) {
|
| 114 | return new EquipmentClientInfo.Layer(textureId, dyeable ? Optional.of(new EquipmentClientInfo.Dyeable(Optional.of(-6265536))) : Optional.empty(), false);
|
| 115 | }
|
| 116 |
|
| 117 | public static EquipmentClientInfo.Layer onlyIfDyed(final Identifier textureId, final boolean dyeable) {
|
| 118 | return new EquipmentClientInfo.Layer(textureId, dyeable ? Optional.of(new EquipmentClientInfo.Dyeable(Optional.empty())) : Optional.empty(), false);
|
| 119 | }
|
| 120 |
|
| 121 | public Identifier getTextureLocation(final EquipmentClientInfo.LayerType type) {
|
| 122 | return this.textureId.withPath(path -> "textures/entity/equipment/" + type.getSerializedName() + "/" + path + ".png");
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | @Environment(EnvType.CLIENT)
|
| 127 | public enum LayerType implements StringRepresentable {
|
| 128 | HUMANOID("humanoid"),
|
| 129 | HUMANOID_LEGGINGS("humanoid_leggings"),
|
| 130 | HUMANOID_BABY("humanoid_baby"),
|
| 131 | WINGS("wings"),
|
| 132 | WOLF_BODY("wolf_body"),
|
| 133 | HORSE_BODY("horse_body"),
|
| 134 | LLAMA_BODY("llama_body"),
|
| 135 | PIG_SADDLE("pig_saddle"),
|
| 136 | STRIDER_SADDLE("strider_saddle"),
|
| 137 | CAMEL_SADDLE("camel_saddle"),
|
| 138 | CAMEL_HUSK_SADDLE("camel_husk_saddle"),
|
| 139 | HORSE_SADDLE("horse_saddle"),
|
| 140 | DONKEY_SADDLE("donkey_saddle"),
|
| 141 | MULE_SADDLE("mule_saddle"),
|
| 142 | ZOMBIE_HORSE_SADDLE("zombie_horse_saddle"),
|
| 143 | SKELETON_HORSE_SADDLE("skeleton_horse_saddle"),
|
| 144 | HAPPY_GHAST_BODY("happy_ghast_body"),
|
| 145 | NAUTILUS_SADDLE("nautilus_saddle"),
|
| 146 | NAUTILUS_BODY("nautilus_body");
|
| 147 |
|
| 148 | public static final Codec<EquipmentClientInfo.LayerType> CODEC = StringRepresentable.fromEnum(EquipmentClientInfo.LayerType::values);
|
| 149 | private final String id;
|
| 150 |
|
| 151 | LayerType(final String id) {
|
| 152 | this.id = id;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public String getSerializedName() {
|
| 157 | return this.id;
|
| 158 | }
|
| 159 |
|
| 160 | public String trimAssetPrefix() {
|
| 161 | return "trims/entity/" + this.id;
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | @Environment(EnvType.CLIENT)
|
| 166 | public record TrimOverride(EquipmentClientInfo.TrimPredicate predicate, Optional<Identifier> textureId, Optional<Identifier> paletteId) {
|
| 167 | public static final Codec<EquipmentClientInfo.TrimOverride> CODEC = RecordCodecBuilder.<EquipmentClientInfo.TrimOverride>create(
|
| 168 | i -> i.group(
|
| 169 | EquipmentClientInfo.TrimPredicate.CODEC.fieldOf("when").forGetter(EquipmentClientInfo.TrimOverride::predicate),
|
| 170 | Identifier.CODEC.optionalFieldOf("texture").forGetter(EquipmentClientInfo.TrimOverride::textureId),
|
| 171 | Identifier.CODEC.optionalFieldOf("palette").forGetter(EquipmentClientInfo.TrimOverride::paletteId)
|
| 172 | )
|
| 173 | .apply(i, EquipmentClientInfo.TrimOverride::new)
|
| 174 | )
|
| 175 | .validate(EquipmentClientInfo.TrimOverride::validate);
|
| 176 |
|
| 177 | private static DataResult<EquipmentClientInfo.TrimOverride> validate(final EquipmentClientInfo.TrimOverride override) {
|
| 178 | return override.textureId.isEmpty() && override.paletteId.isEmpty()
|
| 179 | ? DataResult.error(() -> "One of texture or palette must be specified")
|
| 180 | : DataResult.success(override);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Environment(EnvType.CLIENT)
|
| 185 | public record TrimPredicate(Optional<ResourceKey<TrimMaterial>> material, Optional<ResourceKey<TrimPattern>> pattern) {
|
| 186 | public static final Codec<EquipmentClientInfo.TrimPredicate> CODEC = RecordCodecBuilder.<EquipmentClientInfo.TrimPredicate>create(
|
| 187 | i -> i.group(
|
| 188 | ResourceKey.codec(Registries.TRIM_MATERIAL).optionalFieldOf("material").forGetter(EquipmentClientInfo.TrimPredicate::material),
|
| 189 | ResourceKey.codec(Registries.TRIM_PATTERN).optionalFieldOf("pattern").forGetter(EquipmentClientInfo.TrimPredicate::pattern)
|
| 190 | )
|
| 191 | .apply(i, EquipmentClientInfo.TrimPredicate::new)
|
| 192 | )
|
| 193 | .validate(EquipmentClientInfo.TrimPredicate::validate);
|
| 194 |
|
| 195 | private static DataResult<EquipmentClientInfo.TrimPredicate> validate(final EquipmentClientInfo.TrimPredicate override) {
|
| 196 | return override.material.isEmpty() && override.pattern.isEmpty()
|
| 197 | ? DataResult.error(() -> "One of material or pattern must be specified")
|
| 198 | : DataResult.success(override);
|
| 199 | }
|
| 200 |
|
| 201 | public boolean matches(final ArmorTrim trim) {
|
| 202 | return (this.material.isEmpty() || trim.material().is(this.material.get())) && (this.pattern.isEmpty() || trim.pattern().is(this.pattern.get()));
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 |
|