| 1 | package net.minecraft.client.renderer.item;
|
| 2 |
|
| 3 | import com.mojang.math.Transformation;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.multiplayer.CacheSlot;
|
| 10 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 11 | import net.minecraft.client.renderer.item.properties.conditional.ConditionalItemModelProperties;
|
| 12 | import net.minecraft.client.renderer.item.properties.conditional.ConditionalItemModelProperty;
|
| 13 | import net.minecraft.client.renderer.item.properties.conditional.ItemModelPropertyTest;
|
| 14 | import net.minecraft.client.resources.model.ResolvableModel;
|
| 15 | import net.minecraft.util.RegistryContextSwapper;
|
| 16 | import net.minecraft.world.entity.ItemOwner;
|
| 17 | import net.minecraft.world.item.ItemDisplayContext;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import org.joml.Matrix4fc;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class ConditionalItemModel implements ItemModel {
|
| 24 | private final ItemModelPropertyTest property;
|
| 25 | private final ItemModel onTrue;
|
| 26 | private final ItemModel onFalse;
|
| 27 |
|
| 28 | public ConditionalItemModel(final ItemModelPropertyTest property, final ItemModel onTrue, final ItemModel onFalse) {
|
| 29 | this.property = property;
|
| 30 | this.onTrue = onTrue;
|
| 31 | this.onFalse = onFalse;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public void update(
|
| 36 | final ItemStackRenderState output,
|
| 37 | final ItemStack item,
|
| 38 | final ItemModelResolver resolver,
|
| 39 | final ItemDisplayContext displayContext,
|
| 40 | final @Nullable ClientLevel level,
|
| 41 | final @Nullable ItemOwner owner,
|
| 42 | final int seed
|
| 43 | ) {
|
| 44 | output.appendModelIdentityElement(this);
|
| 45 | (this.property.get(item, level, owner == null ? null : owner.asLivingEntity(), seed, displayContext) ? this.onTrue : this.onFalse)
|
| 46 | .update(output, item, resolver, displayContext, level, owner, seed);
|
| 47 | }
|
| 48 |
|
| 49 | @Environment(EnvType.CLIENT)
|
| 50 | public record Unbaked(Optional<Transformation> transformation, ConditionalItemModelProperty property, ItemModel.Unbaked onTrue, ItemModel.Unbaked onFalse)
|
| 51 | implements ItemModel.Unbaked {
|
| 52 | public static final MapCodec<ConditionalItemModel.Unbaked> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 53 | i -> i.group(
|
| 54 | Transformation.EXTENDED_CODEC.optionalFieldOf("transformation").forGetter(ConditionalItemModel.Unbaked::transformation),
|
| 55 | ConditionalItemModelProperties.MAP_CODEC.forGetter(ConditionalItemModel.Unbaked::property),
|
| 56 | ItemModels.CODEC.fieldOf("on_true").forGetter(ConditionalItemModel.Unbaked::onTrue),
|
| 57 | ItemModels.CODEC.fieldOf("on_false").forGetter(ConditionalItemModel.Unbaked::onFalse)
|
| 58 | )
|
| 59 | .apply(i, ConditionalItemModel.Unbaked::new)
|
| 60 | );
|
| 61 |
|
| 62 | @Override
|
| 63 | public MapCodec<ConditionalItemModel.Unbaked> type() {
|
| 64 | return MAP_CODEC;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public ItemModel bake(final ItemModel.BakingContext context, final Matrix4fc transformation) {
|
| 69 | Matrix4fc childTransform = Transformation.compose(transformation, this.transformation);
|
| 70 | return new ConditionalItemModel(
|
| 71 | this.adaptProperty(this.property, context.contextSwapper()), this.onTrue.bake(context, childTransform), this.onFalse.bake(context, childTransform)
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | private ItemModelPropertyTest adaptProperty(final ConditionalItemModelProperty originalProperty, final @Nullable RegistryContextSwapper contextSwapper) {
|
| 76 | if (contextSwapper == null) {
|
| 77 | return originalProperty;
|
| 78 | }
|
| 79 |
|
| 80 | CacheSlot<ClientLevel, ItemModelPropertyTest> remappedModelCache = new CacheSlot<>(context -> swapContext(originalProperty, contextSwapper, context));
|
| 81 | return (itemStack, level, owner, seed, displayContext) -> {
|
| 82 | ItemModelPropertyTest property = level == null ? originalProperty : remappedModelCache.compute(level);
|
| 83 | return property.get(itemStack, level, owner, seed, displayContext);
|
| 84 | };
|
| 85 | }
|
| 86 |
|
| 87 | private static <T extends ConditionalItemModelProperty> T swapContext(
|
| 88 | final T originalProperty, final RegistryContextSwapper contextSwapper, final ClientLevel context
|
| 89 | ) {
|
| 90 | return (T)contextSwapper.swapTo(originalProperty.type().codec(), originalProperty, context.registryAccess()).result().orElse(originalProperty);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void resolveDependencies(final ResolvableModel.Resolver resolver) {
|
| 95 | this.onTrue.resolveDependencies(resolver);
|
| 96 | this.onFalse.resolveDependencies(resolver);
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|