| 1 | package net.minecraft.client.data.models;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import java.nio.file.Path;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.concurrent.CompletableFuture;
|
| 10 | import java.util.function.BiConsumer;
|
| 11 | import java.util.function.Consumer;
|
| 12 | import java.util.function.Function;
|
| 13 | import java.util.function.Supplier;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.data.models.blockstates.BlockModelDefinitionGenerator;
|
| 17 | import net.minecraft.client.data.models.model.ItemModelUtils;
|
| 18 | import net.minecraft.client.data.models.model.ModelInstance;
|
| 19 | import net.minecraft.client.data.models.model.ModelLocationUtils;
|
| 20 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelDispatcher;
|
| 21 | import net.minecraft.client.renderer.item.ClientItem;
|
| 22 | import net.minecraft.client.renderer.item.ItemModel;
|
| 23 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 24 | import net.minecraft.data.CachedOutput;
|
| 25 | import net.minecraft.data.DataProvider;
|
| 26 | import net.minecraft.data.PackOutput;
|
| 27 | import net.minecraft.data.PackOutput.PathProvider;
|
| 28 | import net.minecraft.data.PackOutput.Target;
|
| 29 | import net.minecraft.resources.Identifier;
|
| 30 | import net.minecraft.world.item.BlockItem;
|
| 31 | import net.minecraft.world.item.Item;
|
| 32 | import net.minecraft.world.level.block.Block;
|
| 33 |
|
| 34 | /**
|
| 35 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 36 | */
|
| 37 | @Environment(EnvType.CLIENT)
|
| 38 | public class ModelProvider implements DataProvider {
|
| 39 | private final PathProvider blockStatePathProvider;
|
| 40 | private final PathProvider itemInfoPathProvider;
|
| 41 | private final PathProvider modelPathProvider;
|
| 42 |
|
| 43 | public ModelProvider(final PackOutput output) {
|
| 44 | this.blockStatePathProvider = output.createPathProvider(Target.RESOURCE_PACK, "blockstates");
|
| 45 | this.itemInfoPathProvider = output.createPathProvider(Target.RESOURCE_PACK, "items");
|
| 46 | this.modelPathProvider = output.createPathProvider(Target.RESOURCE_PACK, "models");
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 51 | ModelProvider.ItemInfoCollector itemModels = new ModelProvider.ItemInfoCollector();
|
| 52 | ModelProvider.BlockStateGeneratorCollector blockStateGenerators = new ModelProvider.BlockStateGeneratorCollector();
|
| 53 | ModelProvider.SimpleModelCollector simpleModels = new ModelProvider.SimpleModelCollector();
|
| 54 | new BlockModelGenerators(blockStateGenerators, itemModels, simpleModels).run();
|
| 55 | new ItemModelGenerators(itemModels, simpleModels).run();
|
| 56 | blockStateGenerators.validate();
|
| 57 | itemModels.finalizeAndValidate();
|
| 58 | return CompletableFuture.allOf(
|
| 59 | blockStateGenerators.save(cache, this.blockStatePathProvider),
|
| 60 | simpleModels.save(cache, this.modelPathProvider),
|
| 61 | itemModels.save(cache, this.itemInfoPathProvider)
|
| 62 | );
|
| 63 | }
|
| 64 |
|
| 65 | /**
|
| 66 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 67 | */
|
| 68 | @Override
|
| 69 | public String getName() {
|
| 70 | return "Model Definitions";
|
| 71 | }
|
| 72 |
|
| 73 | @Environment(EnvType.CLIENT)
|
| 74 | private static class BlockStateGeneratorCollector implements Consumer<BlockModelDefinitionGenerator> {
|
| 75 | private final Map<Block, BlockModelDefinitionGenerator> generators = new HashMap<>();
|
| 76 |
|
| 77 | public void accept(final BlockModelDefinitionGenerator generator) {
|
| 78 | Block block = generator.block();
|
| 79 | BlockModelDefinitionGenerator prev = this.generators.put(block, generator);
|
| 80 | if (prev != null) {
|
| 81 | throw new IllegalStateException("Duplicate blockstate definition for " + block);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public void validate() {
|
| 86 | List<Identifier> missingDefinitions = BuiltInRegistries.BLOCK
|
| 87 | .listElements()
|
| 88 | .filter(e -> !this.generators.containsKey(e.value()))
|
| 89 | .map(e -> e.key().identifier())
|
| 90 | .toList();
|
| 91 | if (!missingDefinitions.isEmpty()) {
|
| 92 | throw new IllegalStateException("Missing blockstate definitions for: " + missingDefinitions);
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public CompletableFuture<?> save(final CachedOutput cache, final PathProvider pathProvider) {
|
| 97 | Map<Block, BlockStateModelDispatcher> definitions = Maps.transformValues(this.generators, BlockModelDefinitionGenerator::create);
|
| 98 | Function<Block, Path> pathGetter = block -> pathProvider.json(block.builtInRegistryHolder().key().identifier());
|
| 99 | return DataProvider.saveAll(cache, BlockStateModelDispatcher.CODEC, pathGetter, definitions);
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | @Environment(EnvType.CLIENT)
|
| 104 | private static class ItemInfoCollector implements ItemModelOutput {
|
| 105 | private final Map<Item, ClientItem> itemInfos = new HashMap<>();
|
| 106 | private final Map<Item, Item> copies = new HashMap<>();
|
| 107 |
|
| 108 | @Override
|
| 109 | public void accept(final Item item, final ItemModel.Unbaked model, final ClientItem.Properties properties) {
|
| 110 | this.register(item, new ClientItem(model, properties));
|
| 111 | }
|
| 112 |
|
| 113 | private void register(final Item item, final ClientItem itemInfo) {
|
| 114 | ClientItem prev = this.itemInfos.put(item, itemInfo);
|
| 115 | if (prev != null) {
|
| 116 | throw new IllegalStateException("Duplicate item model definition for " + item);
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public void copy(final Item donor, final Item acceptor) {
|
| 122 | this.copies.put(acceptor, donor);
|
| 123 | }
|
| 124 |
|
| 125 | public void finalizeAndValidate() {
|
| 126 | BuiltInRegistries.ITEM.forEach(item -> {
|
| 127 | if (!this.copies.containsKey(item)) {
|
| 128 | if (item instanceof BlockItem blockItem && !this.itemInfos.containsKey(blockItem)) {
|
| 129 | Identifier targetModel = ModelLocationUtils.getModelLocation(blockItem.getBlock());
|
| 130 | this.accept(blockItem, ItemModelUtils.plainModel(targetModel));
|
| 131 | }
|
| 132 | }
|
| 133 | });
|
| 134 | this.copies.forEach((acceptor, donor) -> {
|
| 135 | ClientItem donorInfo = this.itemInfos.get(donor);
|
| 136 | if (donorInfo == null) {
|
| 137 | throw new IllegalStateException("Missing donor: " + donor + " -> " + acceptor);
|
| 138 | }
|
| 139 |
|
| 140 | this.register(acceptor, donorInfo);
|
| 141 | });
|
| 142 | List<Identifier> missingDefinitions = BuiltInRegistries.ITEM
|
| 143 | .listElements()
|
| 144 | .filter(e -> !this.itemInfos.containsKey(e.value()))
|
| 145 | .map(e -> e.key().identifier())
|
| 146 | .toList();
|
| 147 | if (!missingDefinitions.isEmpty()) {
|
| 148 | throw new IllegalStateException("Missing item model definitions for: " + missingDefinitions);
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | public CompletableFuture<?> save(final CachedOutput cache, final PathProvider pathProvider) {
|
| 153 | return DataProvider.saveAll(cache, ClientItem.CODEC, item -> pathProvider.json(item.builtInRegistryHolder().key().identifier()), this.itemInfos);
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | @Environment(EnvType.CLIENT)
|
| 158 | private static class SimpleModelCollector implements BiConsumer<Identifier, ModelInstance> {
|
| 159 | private final Map<Identifier, ModelInstance> models = new HashMap<>();
|
| 160 |
|
| 161 | public void accept(final Identifier id, final ModelInstance contents) {
|
| 162 | Supplier<JsonElement> prev = this.models.put(id, contents);
|
| 163 | if (prev != null) {
|
| 164 | throw new IllegalStateException("Duplicate model definition for " + id);
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | public CompletableFuture<?> save(final CachedOutput cache, final PathProvider pathProvider) {
|
| 169 | return DataProvider.saveAll(cache, Supplier::get, pathProvider::json, this.models);
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 |
|