| 1 | package net.minecraft.client.renderer.block.dispatch;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 9 | import java.util.IdentityHashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.BiConsumer;
|
| 14 | import java.util.function.Predicate;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.client.renderer.block.dispatch.multipart.MultiPartModel;
|
| 19 | import net.minecraft.client.renderer.block.dispatch.multipart.Selector;
|
| 20 | import net.minecraft.util.ExtraCodecs;
|
| 21 | import net.minecraft.world.level.block.Block;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 24 | import net.minecraft.world.level.block.state.StateHolder;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public record BlockStateModelDispatcher(
|
| 29 | Optional<BlockStateModelDispatcher.SimpleModelSelectors> simpleModels, Optional<BlockStateModelDispatcher.MultiPartDefinition> multiPart
|
| 30 | ) {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | public static final Codec<BlockStateModelDispatcher> CODEC = RecordCodecBuilder.<BlockStateModelDispatcher>create(
|
| 33 | i -> i.group(
|
| 34 | BlockStateModelDispatcher.SimpleModelSelectors.CODEC.optionalFieldOf("variants").forGetter(BlockStateModelDispatcher::simpleModels),
|
| 35 | BlockStateModelDispatcher.MultiPartDefinition.CODEC.optionalFieldOf("multipart").forGetter(BlockStateModelDispatcher::multiPart)
|
| 36 | )
|
| 37 | .apply(i, BlockStateModelDispatcher::new)
|
| 38 | )
|
| 39 | .validate(
|
| 40 | o -> o.simpleModels().isEmpty() && o.multiPart().isEmpty() ? DataResult.error(() -> "Neither 'variants' nor 'multipart' found") : DataResult.success(o)
|
| 41 | );
|
| 42 |
|
| 43 | public Map<BlockState, BlockStateModel.UnbakedRoot> instantiate(final StateDefinition<Block, BlockState> stateDefinition, final Supplier<String> source) {
|
| 44 | Map<BlockState, BlockStateModel.UnbakedRoot> matchedStates = new IdentityHashMap<>();
|
| 45 | this.simpleModels.ifPresent(s -> s.instantiate(stateDefinition, source, (state, model) -> {
|
| 46 | BlockStateModel.UnbakedRoot previousValue = matchedStates.put(state, model);
|
| 47 | if (previousValue != null) {
|
| 48 | throw new IllegalArgumentException("Overlapping definition on state: " + state);
|
| 49 | }
|
| 50 | }));
|
| 51 | this.multiPart.ifPresent(m -> {
|
| 52 | List<BlockState> possibleStates = stateDefinition.getPossibleStates();
|
| 53 | BlockStateModel.UnbakedRoot model = m.instantiate(stateDefinition);
|
| 54 |
|
| 55 | for (BlockState state : possibleStates) {
|
| 56 | matchedStates.putIfAbsent(state, model);
|
| 57 | }
|
| 58 | });
|
| 59 | return matchedStates;
|
| 60 | }
|
| 61 |
|
| 62 | @Environment(EnvType.CLIENT)
|
| 63 | public record MultiPartDefinition(List<Selector> selectors) {
|
| 64 | public static final Codec<BlockStateModelDispatcher.MultiPartDefinition> CODEC = ExtraCodecs.nonEmptyList(Selector.CODEC.listOf())
|
| 65 | .xmap(BlockStateModelDispatcher.MultiPartDefinition::new, BlockStateModelDispatcher.MultiPartDefinition::selectors);
|
| 66 |
|
| 67 | public MultiPartModel.Unbaked instantiate(final StateDefinition<Block, BlockState> stateDefinition) {
|
| 68 | Builder<MultiPartModel.Selector<BlockStateModel.Unbaked>> instantiatedSelectors = ImmutableList.builderWithExpectedSize(this.selectors.size());
|
| 69 |
|
| 70 | for (Selector selector : this.selectors) {
|
| 71 | instantiatedSelectors.add(new MultiPartModel.Selector<>(selector.instantiate(stateDefinition), selector.variant()));
|
| 72 | }
|
| 73 |
|
| 74 | return new MultiPartModel.Unbaked(instantiatedSelectors.build());
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Environment(EnvType.CLIENT)
|
| 79 | public record SimpleModelSelectors(Map<String, BlockStateModel.Unbaked> models) {
|
| 80 | public static final Codec<BlockStateModelDispatcher.SimpleModelSelectors> CODEC = ExtraCodecs.nonEmptyMap(
|
| 81 | Codec.unboundedMap(Codec.STRING, BlockStateModel.Unbaked.CODEC)
|
| 82 | )
|
| 83 | .xmap(BlockStateModelDispatcher.SimpleModelSelectors::new, BlockStateModelDispatcher.SimpleModelSelectors::models);
|
| 84 |
|
| 85 | public void instantiate(
|
| 86 | final StateDefinition<Block, BlockState> stateDefinition, final Supplier<String> source, final BiConsumer<BlockState, BlockStateModel.UnbakedRoot> output
|
| 87 | ) {
|
| 88 | this.models.forEach((selectorString, model) -> {
|
| 89 | try {
|
| 90 | Predicate<StateHolder<Block, BlockState>> selector = VariantSelector.predicate(stateDefinition, selectorString);
|
| 91 | BlockStateModel.UnbakedRoot wrapper = model.asRoot();
|
| 92 |
|
| 93 | for (BlockState state : stateDefinition.getPossibleStates()) {
|
| 94 | if (selector.test(state)) {
|
| 95 | output.accept(state, wrapper);
|
| 96 | }
|
| 97 | }
|
| 98 | } catch (Exception e) {
|
| 99 | BlockStateModelDispatcher.LOGGER.warn("Exception loading blockstate definition: '{}' for variant: '{}'", source.get(), selectorString);
|
| 100 | }
|
| 101 | });
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|