| 1 | package net.minecraft.client.renderer.block.dispatch;
|
| 2 |
|
| 3 | import com.mojang.math.Quadrant;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.resources.model.ModelBaker;
|
| 10 | import net.minecraft.client.resources.model.ResolvableModel;
|
| 11 | import net.minecraft.client.resources.model.SimpleModelWrapper;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public record Variant(Identifier modelLocation, Variant.SimpleModelState modelState) implements BlockStateModelPart.Unbaked {
|
| 16 | public static final MapCodec<Variant> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(Identifier.CODEC.fieldOf("model").forGetter(Variant::modelLocation), Variant.SimpleModelState.MAP_CODEC.forGetter(Variant::modelState))
|
| 18 | .apply(i, Variant::new)
|
| 19 | );
|
| 20 | public static final Codec<Variant> CODEC = MAP_CODEC.codec();
|
| 21 |
|
| 22 | public Variant(final Identifier modelLocation) {
|
| 23 | this(modelLocation, Variant.SimpleModelState.DEFAULT);
|
| 24 | }
|
| 25 |
|
| 26 | public Variant withXRot(final Quadrant x) {
|
| 27 | return this.withState(this.modelState.withX(x));
|
| 28 | }
|
| 29 |
|
| 30 | public Variant withYRot(final Quadrant y) {
|
| 31 | return this.withState(this.modelState.withY(y));
|
| 32 | }
|
| 33 |
|
| 34 | public Variant withZRot(final Quadrant z) {
|
| 35 | return this.withState(this.modelState.withZ(z));
|
| 36 | }
|
| 37 |
|
| 38 | public Variant withUvLock(final boolean uvLock) {
|
| 39 | return this.withState(this.modelState.withUvLock(uvLock));
|
| 40 | }
|
| 41 |
|
| 42 | public Variant withModel(final Identifier modelLocation) {
|
| 43 | return new Variant(modelLocation, this.modelState);
|
| 44 | }
|
| 45 |
|
| 46 | public Variant withState(final Variant.SimpleModelState modelState) {
|
| 47 | return new Variant(this.modelLocation, modelState);
|
| 48 | }
|
| 49 |
|
| 50 | public Variant with(final VariantMutator mutator) {
|
| 51 | return mutator.apply(this);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public BlockStateModelPart bake(final ModelBaker modelBakery) {
|
| 56 | return SimpleModelWrapper.bake(modelBakery, this.modelLocation, this.modelState.asModelState());
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void resolveDependencies(final ResolvableModel.Resolver resolver) {
|
| 61 | resolver.markDependency(this.modelLocation);
|
| 62 | }
|
| 63 |
|
| 64 | @Environment(EnvType.CLIENT)
|
| 65 | public record SimpleModelState(Quadrant x, Quadrant y, Quadrant z, boolean uvLock) {
|
| 66 | public static final MapCodec<Variant.SimpleModelState> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 67 | i -> i.group(
|
| 68 | Quadrant.CODEC.optionalFieldOf("x", Quadrant.R0).forGetter(Variant.SimpleModelState::x),
|
| 69 | Quadrant.CODEC.optionalFieldOf("y", Quadrant.R0).forGetter(Variant.SimpleModelState::y),
|
| 70 | Quadrant.CODEC.optionalFieldOf("z", Quadrant.R0).forGetter(Variant.SimpleModelState::z),
|
| 71 | Codec.BOOL.optionalFieldOf("uvlock", false).forGetter(Variant.SimpleModelState::uvLock)
|
| 72 | )
|
| 73 | .apply(i, Variant.SimpleModelState::new)
|
| 74 | );
|
| 75 | public static final Variant.SimpleModelState DEFAULT = new Variant.SimpleModelState(Quadrant.R0, Quadrant.R0, Quadrant.R0, false);
|
| 76 |
|
| 77 | public ModelState asModelState() {
|
| 78 | BlockModelRotation rotation = BlockModelRotation.get(Quadrant.fromXYZAngles(this.x, this.y, this.z));
|
| 79 | return this.uvLock ? rotation.withUvLock() : rotation;
|
| 80 | }
|
| 81 |
|
| 82 | public Variant.SimpleModelState withX(final Quadrant x) {
|
| 83 | return new Variant.SimpleModelState(x, this.y, this.z, this.uvLock);
|
| 84 | }
|
| 85 |
|
| 86 | public Variant.SimpleModelState withY(final Quadrant y) {
|
| 87 | return new Variant.SimpleModelState(this.x, y, this.z, this.uvLock);
|
| 88 | }
|
| 89 |
|
| 90 | public Variant.SimpleModelState withZ(final Quadrant z) {
|
| 91 | return new Variant.SimpleModelState(this.x, this.y, z, this.uvLock);
|
| 92 | }
|
| 93 |
|
| 94 | public Variant.SimpleModelState withUvLock(final boolean uvLock) {
|
| 95 | return new Variant.SimpleModelState(this.x, this.y, this.z, uvLock);
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|