| 1 | package net.minecraft.client.resources.model;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonParseException;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.JsonOps;
|
| 7 | import java.io.Reader;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.IdentityHashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Map.Entry;
|
| 13 | import java.util.concurrent.CompletableFuture;
|
| 14 | import java.util.concurrent.Executor;
|
| 15 | import java.util.function.Function;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 19 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelDispatcher;
|
| 20 | import net.minecraft.resources.FileToIdConverter;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.server.packs.resources.Resource;
|
| 23 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 24 | import net.minecraft.util.StrictJsonParser;
|
| 25 | import net.minecraft.util.Util;
|
| 26 | import net.minecraft.world.level.block.Block;
|
| 27 | import net.minecraft.world.level.block.state.BlockState;
|
| 28 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 29 | import org.slf4j.Logger;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class BlockStateModelLoader {
|
| 33 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 34 | private static final FileToIdConverter BLOCKSTATE_LISTER = FileToIdConverter.json("blockstates");
|
| 35 |
|
| 36 | public static CompletableFuture<BlockStateModelLoader.LoadedModels> loadBlockStates(final ResourceManager manager, final Executor executor) {
|
| 37 | Function<Identifier, StateDefinition<Block, BlockState>> definitionToBlockState = BlockStateDefinitions.definitionLocationToBlockStateMapper();
|
| 38 | return CompletableFuture.<Map<Identifier, List<Resource>>>supplyAsync(() -> BLOCKSTATE_LISTER.listMatchingResourceStacks(manager), executor)
|
| 39 | .thenCompose(resources -> {
|
| 40 | List<CompletableFuture<BlockStateModelLoader.LoadedModels>> result = new ArrayList<>(resources.size());
|
| 41 |
|
| 42 | for (Entry<Identifier, List<Resource>> resourceStack : resources.entrySet()) {
|
| 43 | result.add(CompletableFuture.supplyAsync(() -> {
|
| 44 | Identifier stateDefinitionId = BLOCKSTATE_LISTER.fileToId(resourceStack.getKey());
|
| 45 | StateDefinition<Block, BlockState> stateDefinition = definitionToBlockState.apply(stateDefinitionId);
|
| 46 | if (stateDefinition == null) {
|
| 47 | LOGGER.debug("Discovered unknown block state definition {}, ignoring", stateDefinitionId);
|
| 48 | return null;
|
| 49 | }
|
| 50 |
|
| 51 | List<Resource> stack = resourceStack.getValue();
|
| 52 | List<BlockStateModelLoader.LoadedBlockStateModelDispatcher> loadedStack = new ArrayList<>(stack.size());
|
| 53 |
|
| 54 | for (Resource resource : stack) {
|
| 55 | try (Reader reader = resource.openAsReader()) {
|
| 56 | JsonElement element = StrictJsonParser.parse(reader);
|
| 57 | BlockStateModelDispatcher definition = BlockStateModelDispatcher.CODEC.parse(JsonOps.INSTANCE, element).getOrThrow(JsonParseException::new);
|
| 58 | loadedStack.add(new BlockStateModelLoader.LoadedBlockStateModelDispatcher(resource.sourcePackId(), definition));
|
| 59 | } catch (Exception e) {
|
| 60 | LOGGER.error("Failed to load blockstate definition {} from pack {}", stateDefinitionId, resource.sourcePackId(), e);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | try {
|
| 65 | return loadBlockStateDefinitionStack(stateDefinitionId, stateDefinition, loadedStack);
|
| 66 | } catch (Exception e) {
|
| 67 | LOGGER.error("Failed to load blockstate definition {}", stateDefinitionId, e);
|
| 68 | return null;
|
| 69 | }
|
| 70 | }, executor));
|
| 71 | }
|
| 72 |
|
| 73 | return Util.sequence(result).thenApply(partialMaps -> {
|
| 74 | Map<BlockState, BlockStateModel.UnbakedRoot> fullMap = new IdentityHashMap<>();
|
| 75 |
|
| 76 | for (BlockStateModelLoader.LoadedModels partialMap : partialMaps) {
|
| 77 | if (partialMap != null) {
|
| 78 | fullMap.putAll(partialMap.models());
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | return new BlockStateModelLoader.LoadedModels(fullMap);
|
| 83 | });
|
| 84 | });
|
| 85 | }
|
| 86 |
|
| 87 | private static BlockStateModelLoader.LoadedModels loadBlockStateDefinitionStack(
|
| 88 | final Identifier stateDefinitionId,
|
| 89 | final StateDefinition<Block, BlockState> stateDefinition,
|
| 90 | final List<BlockStateModelLoader.LoadedBlockStateModelDispatcher> definitionStack
|
| 91 | ) {
|
| 92 | Map<BlockState, BlockStateModel.UnbakedRoot> result = new IdentityHashMap<>();
|
| 93 |
|
| 94 | for (BlockStateModelLoader.LoadedBlockStateModelDispatcher definition : definitionStack) {
|
| 95 | result.putAll(definition.contents.instantiate(stateDefinition, () -> stateDefinitionId + "/" + definition.source));
|
| 96 | }
|
| 97 |
|
| 98 | return new BlockStateModelLoader.LoadedModels(result);
|
| 99 | }
|
| 100 |
|
| 101 | @Environment(EnvType.CLIENT)
|
| 102 | private record LoadedBlockStateModelDispatcher(String source, BlockStateModelDispatcher contents) {
|
| 103 | }
|
| 104 |
|
| 105 | @Environment(EnvType.CLIENT)
|
| 106 | public record LoadedModels(Map<BlockState, BlockStateModel.UnbakedRoot> models) {
|
| 107 | }
|
| 108 | }
|
| 109 |
|