1
0
mirror of https://github.com/GeyserMC/Rainbow.git synced 2025-12-19 14:59:16 +00:00

Finish implementing select/match predicates

This commit is contained in:
Eclipse
2025-06-28 20:43:18 +00:00
parent 676782bc97
commit 3ddbe567f0
4 changed files with 35 additions and 18 deletions

View File

@@ -4,7 +4,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.minecraft.client.renderer.item.ItemModel;
// Implemented on BlockModelWrapper, since this class doesn't store the cases it has in a nice format, we have to store it manually
public interface SelectItemModelCaseAccessor<T> {
public interface SelectItemModelCasesAccessor<T> {
Object2ObjectMap<T, ItemModel> geyser_mappings_generator$getCases();

View File

@@ -1,5 +1,6 @@
package org.geysermc.packgenerator.mappings;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.item.BlockModelWrapper;
import net.minecraft.client.renderer.item.ConditionalItemModel;
@@ -16,8 +17,13 @@ import net.minecraft.client.renderer.item.properties.select.ContextDimension;
import net.minecraft.client.renderer.item.properties.select.SelectItemModelProperty;
import net.minecraft.client.renderer.item.properties.select.TrimMaterialProperty;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.item.equipment.trim.TrimMaterial;
import net.minecraft.world.level.Level;
import org.geysermc.packgenerator.accessor.BlockModelWrapperLocationAccessor;
import org.geysermc.packgenerator.accessor.SelectItemModelCasesAccessor;
import org.geysermc.packgenerator.mappings.predicate.GeyserConditionPredicate;
import org.geysermc.packgenerator.mappings.predicate.GeyserMatchPredicate;
import org.geysermc.packgenerator.mappings.predicate.GeyserPredicate;
@@ -47,7 +53,7 @@ public class GeyserItemMapper {
return mapConditionalModel(conditional, context);
}
case SelectItemModel<?> select -> {
return Stream.of();
return mapSelectModel(select, context);
}
default -> {}
}
@@ -78,11 +84,21 @@ public class GeyserItemMapper {
//noinspection unchecked
SelectItemModelProperty<T> property = ((SelectItemModelAccessor<T>) model).getProperty();
Function<T, GeyserMatchPredicate.MatchPredicateData> dataConstructor = switch (property) {
case Charge ignored -> t -> new GeyserMatchPredicate.ChargeType(t);
case TrimMaterialProperty trimMaterial -> ;
case ContextDimension contextDimension -> ;
case net.minecraft.client.renderer.item.properties.select.CustomModelDataProperty customModelData -> ; // Why, Mojang?
}
case Charge ignored -> chargeType -> new GeyserMatchPredicate.ChargeType((CrossbowItem.ChargeType) chargeType);
case TrimMaterialProperty ignored -> material -> new GeyserMatchPredicate.TrimMaterialData((ResourceKey<TrimMaterial>) material);
case ContextDimension ignored -> dimension -> new GeyserMatchPredicate.ContextDimension((ResourceKey<Level>) dimension);
// Why, Mojang?
case net.minecraft.client.renderer.item.properties.select.CustomModelDataProperty customModelData -> string -> new GeyserMatchPredicate.CustomModelData((String) string, customModelData.index());
default -> throw new UnsupportedOperationException("Unsupported select model property " + property.getClass());
};
//noinspection unchecked
Object2ObjectMap<T, ItemModel> cases = ((SelectItemModelCasesAccessor<T>) model).geyser_mappings_generator$getCases();
return Stream.concat(
cases.entrySet().stream()
.flatMap(caze -> mapItem(caze.getValue(), context.with(new GeyserMatchPredicate(dataConstructor.apply(caze.getKey()))))),
mapItem(cases.defaultReturnValue(), context)
);
}
private record MappingContext(List<GeyserPredicate> predicateStack, ResourceLocation model, String displayName, int protectionValue, DataComponentPatch componentPatch) {

View File

@@ -3,10 +3,13 @@ package org.geysermc.packgenerator.mappings.predicate;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.CrossbowItem;
import net.minecraft.world.item.equipment.trim.TrimMaterial;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import java.util.function.Function;
@@ -28,7 +31,7 @@ public record GeyserMatchPredicate(MatchPredicateData data) implements GeyserPre
enum Type implements StringRepresentable {
CHARGE_TYPE("charge_type", ChargeType.CODEC),
TRIM_MATERIAL("trim_material", TrimMaterial.CODEC),
TRIM_MATERIAL("trim_material", TrimMaterialData.CODEC),
CONTEXT_DIMENSION("context_dimension", ContextDimension.CODEC),
CUSTOM_MODEL_DATA("custom_model_data", CustomModelData.CODEC);
@@ -62,9 +65,8 @@ public record GeyserMatchPredicate(MatchPredicateData data) implements GeyserPre
}
}
// TODO might change into ResourceKey?
public record TrimMaterial(ResourceLocation material) implements MatchPredicateData {
public static final MapCodec<TrimMaterial> CODEC = simpleCodec(ResourceLocation.CODEC, TrimMaterial::material, TrimMaterial::new);
public record TrimMaterialData(ResourceKey<TrimMaterial> material) implements MatchPredicateData {
public static final MapCodec<TrimMaterialData> CODEC = simpleCodec(ResourceKey.codec(Registries.TRIM_MATERIAL), TrimMaterialData::material, TrimMaterialData::new);
@Override
public Type type() {
@@ -72,9 +74,8 @@ public record GeyserMatchPredicate(MatchPredicateData data) implements GeyserPre
}
}
// TODO might change into ResourceKey?
public record ContextDimension(ResourceLocation dimension) implements MatchPredicateData {
public static final MapCodec<ContextDimension> CODEC = simpleCodec(ResourceLocation.CODEC, ContextDimension::dimension, ContextDimension::new);
public record ContextDimension(ResourceKey<Level> level) implements MatchPredicateData {
public static final MapCodec<ContextDimension> CODEC = simpleCodec(ResourceKey.codec(Registries.DIMENSION), ContextDimension::level, ContextDimension::new);
@Override
public Type type() {

View File

@@ -5,7 +5,7 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import net.minecraft.client.renderer.item.ItemModel;
import net.minecraft.client.renderer.item.SelectItemModel;
import net.minecraft.client.renderer.item.properties.select.SelectItemModelProperty;
import org.geysermc.packgenerator.accessor.SelectItemModelCaseAccessor;
import org.geysermc.packgenerator.accessor.SelectItemModelCasesAccessor;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
@@ -13,7 +13,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(SelectItemModel.class)
public abstract class SelectItemModelMixin<T> implements ItemModel, SelectItemModelCaseAccessor<T> {
public abstract class SelectItemModelMixin<T> implements ItemModel, SelectItemModelCasesAccessor<T> {
@Unique
private Object2ObjectMap<T, ItemModel> cases;
@@ -35,7 +35,7 @@ public abstract class SelectItemModelMixin<T> implements ItemModel, SelectItemMo
public void setCases(BakingContext bakingContext, ItemModel model, CallbackInfoReturnable<ItemModel> callbackInfoReturnable,
@Local Object2ObjectMap<T, ItemModel> cases) {
//noinspection unchecked
((SelectItemModelCaseAccessor<T>) callbackInfoReturnable.getReturnValue()).geyser_mappings_generator$setCases(cases);
((SelectItemModelCasesAccessor<T>) callbackInfoReturnable.getReturnValue()).geyser_mappings_generator$setCases(cases);
}
}
}