| 1 | package net.minecraft.client.renderer.block.dispatch;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.List;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.fabricmc.fabric.api.client.renderer.v1.model.FabricBlockStateModel;
|
| 13 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel.SimpleCachedUnbakedRoot.1;
|
| 14 | import net.minecraft.client.resources.model.ModelBaker;
|
| 15 | import net.minecraft.client.resources.model.ResolvableModel;
|
| 16 | import net.minecraft.client.resources.model.geometry.BakedQuad;
|
| 17 | import net.minecraft.client.resources.model.sprite.Material;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 | import net.minecraft.util.RandomSource;
|
| 20 | import net.minecraft.util.random.Weighted;
|
| 21 | import net.minecraft.util.random.WeightedList;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public interface BlockStateModel extends FabricBlockStateModel {
|
| 26 | void collectParts(RandomSource random, List<BlockStateModelPart> output);
|
| 27 |
|
| 28 | Material.Baked particleMaterial();
|
| 29 |
|
| 30 | @BakedQuad.MaterialFlags int materialFlags();
|
| 31 |
|
| 32 | default boolean hasMaterialFlag(final @BakedQuad.MaterialFlags int flag) {
|
| 33 | return (this.materialFlags() & flag) != 0;
|
| 34 | }
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | class SimpleCachedUnbakedRoot implements BlockStateModel.UnbakedRoot {
|
| 38 | private final BlockStateModel.Unbaked contents;
|
| 39 | private final ModelBaker.SharedOperationKey<BlockStateModel> bakingKey = new 1(this);
|
| 40 |
|
| 41 | public SimpleCachedUnbakedRoot(final BlockStateModel.Unbaked contents) {
|
| 42 | this.contents = contents;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void resolveDependencies(final ResolvableModel.Resolver resolver) {
|
| 47 | this.contents.resolveDependencies(resolver);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public BlockStateModel bake(final BlockState blockState, final ModelBaker modelBakery) {
|
| 52 | return modelBakery.compute(this.bakingKey);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public Object visualEqualityGroup(final BlockState blockState) {
|
| 57 | return this;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | @Environment(EnvType.CLIENT)
|
| 62 | interface Unbaked extends ResolvableModel {
|
| 63 | Codec<Weighted<Variant>> ELEMENT_CODEC = RecordCodecBuilder.create(
|
| 64 | i -> i.group(Variant.MAP_CODEC.forGetter(Weighted::value), ExtraCodecs.POSITIVE_INT.optionalFieldOf("weight", 1).forGetter(Weighted::weight))
|
| 65 | .apply(i, Weighted::new)
|
| 66 | );
|
| 67 | Codec<WeightedVariants.Unbaked> HARDCODED_WEIGHTED_CODEC = ExtraCodecs.nonEmptyList(ELEMENT_CODEC.listOf())
|
| 68 | .flatComapMap(
|
| 69 | w -> new WeightedVariants.Unbaked(WeightedList.of(Lists.transform((List<Weighted<Variant>>)w, e -> e.map(SingleVariant.Unbaked::new)))), unbaked -> {
|
| 70 | List<Weighted<BlockStateModel.Unbaked>> entries = unbaked.entries().unwrap();
|
| 71 | List<Weighted<Variant>> result = new ArrayList<>(entries.size());
|
| 72 |
|
| 73 | for (Weighted<BlockStateModel.Unbaked> entry : entries) {
|
| 74 | if (!(entry.value() instanceof SingleVariant.Unbaked singleVariant)) {
|
| 75 | return DataResult.error(() -> "Only single variants are supported");
|
| 76 | }
|
| 77 |
|
| 78 | result.add(new Weighted<>(singleVariant.variant(), entry.weight()));
|
| 79 | }
|
| 80 |
|
| 81 | return DataResult.success(result);
|
| 82 | }
|
| 83 | );
|
| 84 | Codec<BlockStateModel.Unbaked> CODEC = Codec.either(HARDCODED_WEIGHTED_CODEC, SingleVariant.Unbaked.CODEC).flatComapMap(v -> v.map(l -> l, r -> r), o -> {
|
| 85 | return switch (o) {
|
| 86 | case SingleVariant.Unbaked single -> DataResult.success(Either.right(single));
|
| 87 | case WeightedVariants.Unbaked multiple -> DataResult.success(Either.left(multiple));
|
| 88 | default -> DataResult.error(() -> "Only a single variant or a list of variants are supported");
|
| 89 | };
|
| 90 | });
|
| 91 |
|
| 92 | BlockStateModel bake(ModelBaker modelBakery);
|
| 93 |
|
| 94 | default BlockStateModel.UnbakedRoot asRoot() {
|
| 95 | return new BlockStateModel.SimpleCachedUnbakedRoot(this);
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | @Environment(EnvType.CLIENT)
|
| 100 | interface UnbakedRoot extends ResolvableModel {
|
| 101 | BlockStateModel bake(BlockState blockState, ModelBaker modelBakery);
|
| 102 |
|
| 103 | Object visualEqualityGroup(BlockState blockState);
|
| 104 | }
|
| 105 | }
|
| 106 |
|