| 1 | package net.minecraft.client.data.models.blockstates;
|
| 2 |
|
| 3 | import java.util.HashMap;
|
| 4 | import java.util.HashSet;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.stream.Stream;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.data.models.MultiVariant;
|
| 13 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 14 | import net.minecraft.client.renderer.block.dispatch.BlockStateModelDispatcher;
|
| 15 | import net.minecraft.client.renderer.block.dispatch.VariantMutator;
|
| 16 | import net.minecraft.world.level.block.Block;
|
| 17 | import net.minecraft.world.level.block.state.properties.Property;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class MultiVariantGenerator implements BlockModelDefinitionGenerator {
|
| 21 | private final Block block;
|
| 22 | private final List<MultiVariantGenerator.Entry> entries;
|
| 23 | private final Set<Property<?>> seenProperties;
|
| 24 |
|
| 25 | private MultiVariantGenerator(final Block block, final List<MultiVariantGenerator.Entry> entries, final Set<Property<?>> seenProperties) {
|
| 26 | this.block = block;
|
| 27 | this.entries = entries;
|
| 28 | this.seenProperties = seenProperties;
|
| 29 | }
|
| 30 |
|
| 31 | private static Set<Property<?>> validateAndExpandProperties(final Set<Property<?>> seenProperties, final Block block, final PropertyDispatch<?> generator) {
|
| 32 | List<Property<?>> addedProperties = generator.getDefinedProperties();
|
| 33 | addedProperties.forEach(property -> {
|
| 34 | if (block.getStateDefinition().getProperty(property.getName()) != property) {
|
| 35 | throw new IllegalStateException("Property " + property + " is not defined for block " + block);
|
| 36 | }
|
| 37 |
|
| 38 | if (seenProperties.contains(property)) {
|
| 39 | throw new IllegalStateException("Values of property " + property + " already defined for block " + block);
|
| 40 | }
|
| 41 | });
|
| 42 | Set<Property<?>> newSeenProperties = new HashSet<>(seenProperties);
|
| 43 | newSeenProperties.addAll(addedProperties);
|
| 44 | return newSeenProperties;
|
| 45 | }
|
| 46 |
|
| 47 | public MultiVariantGenerator with(final PropertyDispatch<VariantMutator> newStage) {
|
| 48 | Set<Property<?>> newSeenProperties = validateAndExpandProperties(this.seenProperties, this.block, newStage);
|
| 49 | List<MultiVariantGenerator.Entry> newEntries = this.entries.stream().flatMap(entry -> entry.apply(newStage)).toList();
|
| 50 | return new MultiVariantGenerator(this.block, newEntries, newSeenProperties);
|
| 51 | }
|
| 52 |
|
| 53 | public MultiVariantGenerator with(final VariantMutator singleMutator) {
|
| 54 | List<MultiVariantGenerator.Entry> newEntries = this.entries.stream().flatMap(entry -> entry.apply(singleMutator)).toList();
|
| 55 | return new MultiVariantGenerator(this.block, newEntries, this.seenProperties);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public BlockStateModelDispatcher create() {
|
| 60 | Map<String, BlockStateModel.Unbaked> variants = new HashMap<>();
|
| 61 |
|
| 62 | for (MultiVariantGenerator.Entry entry : this.entries) {
|
| 63 | variants.put(entry.properties.getKey(), entry.variant.toUnbaked());
|
| 64 | }
|
| 65 |
|
| 66 | return new BlockStateModelDispatcher(Optional.of(new BlockStateModelDispatcher.SimpleModelSelectors(variants)), Optional.empty());
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public Block block() {
|
| 71 | return this.block;
|
| 72 | }
|
| 73 |
|
| 74 | public static MultiVariantGenerator.Empty dispatch(final Block block) {
|
| 75 | return new MultiVariantGenerator.Empty(block);
|
| 76 | }
|
| 77 |
|
| 78 | public static MultiVariantGenerator dispatch(final Block block, final MultiVariant initialModel) {
|
| 79 | return new MultiVariantGenerator(block, List.of(new MultiVariantGenerator.Entry(PropertyValueList.EMPTY, initialModel)), Set.of());
|
| 80 | }
|
| 81 |
|
| 82 | @Environment(EnvType.CLIENT)
|
| 83 | public static class Empty {
|
| 84 | private final Block block;
|
| 85 |
|
| 86 | public Empty(final Block block) {
|
| 87 | this.block = block;
|
| 88 | }
|
| 89 |
|
| 90 | public MultiVariantGenerator with(final PropertyDispatch<MultiVariant> newStage) {
|
| 91 | Set<Property<?>> newSeenProperties = MultiVariantGenerator.validateAndExpandProperties(Set.of(), this.block, newStage);
|
| 92 | List<MultiVariantGenerator.Entry> newEntries = newStage.getEntries()
|
| 93 | .entrySet()
|
| 94 | .stream()
|
| 95 | .map(e -> new MultiVariantGenerator.Entry(e.getKey(), e.getValue()))
|
| 96 | .toList();
|
| 97 | return new MultiVariantGenerator(this.block, newEntries, newSeenProperties);
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | @Environment(EnvType.CLIENT)
|
| 102 | private record Entry(PropertyValueList properties, MultiVariant variant) {
|
| 103 | public Stream<MultiVariantGenerator.Entry> apply(final PropertyDispatch<VariantMutator> stage) {
|
| 104 | return stage.getEntries().entrySet().stream().map(property -> {
|
| 105 | PropertyValueList newSelector = this.properties.extend(property.getKey());
|
| 106 | MultiVariant newVariants = this.variant.with(property.getValue());
|
| 107 | return new MultiVariantGenerator.Entry(newSelector, newVariants);
|
| 108 | });
|
| 109 | }
|
| 110 |
|
| 111 | public Stream<MultiVariantGenerator.Entry> apply(final VariantMutator mutator) {
|
| 112 | return Stream.of(new MultiVariantGenerator.Entry(this.properties, this.variant.with(mutator)));
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|