| 1 | package net.minecraft.client.data.models.model;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableSet;
|
| 5 | import com.google.common.collect.Streams;
|
| 6 | import com.google.gson.JsonElement;
|
| 7 | import com.google.gson.JsonObject;
|
| 8 | import com.mojang.serialization.JsonOps;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.function.BiConsumer;
|
| 13 | import java.util.function.Function;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.resources.model.sprite.Material;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.world.item.Item;
|
| 19 | import net.minecraft.world.level.block.Block;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class ModelTemplate {
|
| 23 | private final Optional<Identifier> model;
|
| 24 | private final Set<TextureSlot> requiredSlots;
|
| 25 | private final Optional<String> suffix;
|
| 26 |
|
| 27 | public ModelTemplate(final Optional<Identifier> model, final Optional<String> suffix, final TextureSlot... requiredSlots) {
|
| 28 | this.model = model;
|
| 29 | this.suffix = suffix;
|
| 30 | this.requiredSlots = ImmutableSet.copyOf(requiredSlots);
|
| 31 | }
|
| 32 |
|
| 33 | public Identifier getDefaultModelLocation(final Block block) {
|
| 34 | return ModelLocationUtils.getModelLocation(block, this.suffix.orElse(""));
|
| 35 | }
|
| 36 |
|
| 37 | public Identifier create(final Block block, final TextureMapping textures, final BiConsumer<Identifier, ModelInstance> output) {
|
| 38 | return this.create(ModelLocationUtils.getModelLocation(block, this.suffix.orElse("")), textures, output);
|
| 39 | }
|
| 40 |
|
| 41 | public Identifier createWithSuffix(
|
| 42 | final Block block, final String extraSuffix, final TextureMapping textures, final BiConsumer<Identifier, ModelInstance> output
|
| 43 | ) {
|
| 44 | return this.create(ModelLocationUtils.getModelLocation(block, extraSuffix + this.suffix.orElse("")), textures, output);
|
| 45 | }
|
| 46 |
|
| 47 | public Identifier createWithOverride(
|
| 48 | final Block block, final String suffixOverride, final TextureMapping textures, final BiConsumer<Identifier, ModelInstance> output
|
| 49 | ) {
|
| 50 | return this.create(ModelLocationUtils.getModelLocation(block, suffixOverride), textures, output);
|
| 51 | }
|
| 52 |
|
| 53 | public Identifier create(final Item item, final TextureMapping textures, final BiConsumer<Identifier, ModelInstance> output) {
|
| 54 | return this.create(ModelLocationUtils.getModelLocation(item, this.suffix.orElse("")), textures, output);
|
| 55 | }
|
| 56 |
|
| 57 | public Identifier create(final Identifier target, final TextureMapping textures, final BiConsumer<Identifier, ModelInstance> output) {
|
| 58 | Map<TextureSlot, Material> slots = this.createMap(textures);
|
| 59 | output.accept(target, () -> {
|
| 60 | JsonObject result = new JsonObject();
|
| 61 | this.model.ifPresent(m -> result.addProperty("parent", m.toString()));
|
| 62 | if (!slots.isEmpty()) {
|
| 63 | JsonObject textureObj = new JsonObject();
|
| 64 | slots.forEach((slot, value) -> {
|
| 65 | JsonElement valueJson = Material.CODEC.encodeStart(JsonOps.INSTANCE, value).getOrThrow();
|
| 66 | textureObj.add(slot.getId(), valueJson);
|
| 67 | });
|
| 68 | result.add("textures", textureObj);
|
| 69 | }
|
| 70 |
|
| 71 | return result;
|
| 72 | });
|
| 73 | return target;
|
| 74 | }
|
| 75 |
|
| 76 | private Map<TextureSlot, Material> createMap(final TextureMapping mapping) {
|
| 77 | return Streams.concat(this.requiredSlots.stream(), mapping.getForced()).collect(ImmutableMap.toImmutableMap(Function.identity(), mapping::get));
|
| 78 | }
|
| 79 | }
|
| 80 |
|