mirror of
https://github.com/GeyserMC/Rainbow.git
synced 2025-12-19 14:59:16 +00:00
Add codecs for match predicates
This commit is contained in:
@@ -8,27 +8,27 @@ import net.minecraft.util.ExtraCodecs;
|
||||
import net.minecraft.util.StringRepresentable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record GeyserConditionPredicate(ConditionProperty property, boolean expected) implements GeyserPredicate {
|
||||
public record GeyserConditionPredicate(Property property, boolean expected) implements GeyserPredicate {
|
||||
|
||||
public static final MapCodec<GeyserConditionPredicate> CODEC = RecordCodecBuilder.mapCodec(instance ->
|
||||
instance.group(
|
||||
ConditionProperty.CODEC.forGetter(GeyserConditionPredicate::property),
|
||||
Property.CODEC.forGetter(GeyserConditionPredicate::property),
|
||||
Codec.BOOL.optionalFieldOf("expected", true).forGetter(GeyserConditionPredicate::expected)
|
||||
).apply(instance, GeyserConditionPredicate::new)
|
||||
);
|
||||
|
||||
public static final ConditionProperty BROKEN = unit(ConditionProperty.Type.BROKEN);
|
||||
public static final ConditionProperty DAMAGED = unit(ConditionProperty.Type.DAMAGED);
|
||||
public static final ConditionProperty FISHING_ROD_CAST = unit(ConditionProperty.Type.FISHING_ROD_CAST);
|
||||
public static final Property BROKEN = unit(Property.Type.BROKEN);
|
||||
public static final Property DAMAGED = unit(Property.Type.DAMAGED);
|
||||
public static final Property FISHING_ROD_CAST = unit(Property.Type.FISHING_ROD_CAST);
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
return Type.CONDITION;
|
||||
}
|
||||
|
||||
public interface ConditionProperty {
|
||||
public interface Property {
|
||||
|
||||
MapCodec<ConditionProperty> CODEC = Type.CODEC.dispatchMap("property", ConditionProperty::type, Type::codec);
|
||||
MapCodec<Property> CODEC = Type.CODEC.dispatchMap("property", Property::type, Type::codec);
|
||||
|
||||
Type type();
|
||||
|
||||
@@ -42,14 +42,14 @@ public record GeyserConditionPredicate(ConditionProperty property, boolean expec
|
||||
public static final Codec<Type> CODEC = StringRepresentable.fromEnum(Type::values);
|
||||
|
||||
private final String name;
|
||||
private final MapCodec<? extends ConditionProperty> codec;
|
||||
private final MapCodec<? extends Property> codec;
|
||||
|
||||
Type(String name, MapCodec<? extends ConditionProperty> codec) {
|
||||
Type(String name, MapCodec<? extends Property> codec) {
|
||||
this.name = name;
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
public MapCodec<? extends ConditionProperty> codec() {
|
||||
public MapCodec<? extends Property> codec() {
|
||||
return codec;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public record GeyserConditionPredicate(ConditionProperty property, boolean expec
|
||||
}
|
||||
}
|
||||
|
||||
public record CustomModelData(int index) implements ConditionProperty {
|
||||
public record CustomModelData(int index) implements Property {
|
||||
public static final MapCodec<CustomModelData> CODEC = RecordCodecBuilder.mapCodec(instance ->
|
||||
instance.group(
|
||||
ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("index", 0).forGetter(CustomModelData::index)
|
||||
@@ -73,7 +73,7 @@ public record GeyserConditionPredicate(ConditionProperty property, boolean expec
|
||||
}
|
||||
}
|
||||
|
||||
public record HasComponent(DataComponentType<?> component) implements ConditionProperty {
|
||||
public record HasComponent(DataComponentType<?> component) implements Property {
|
||||
public static final MapCodec<HasComponent> CODEC = RecordCodecBuilder.mapCodec(instance ->
|
||||
instance.group(
|
||||
DataComponentType.CODEC.fieldOf("component").forGetter(HasComponent::component)
|
||||
@@ -86,7 +86,7 @@ public record GeyserConditionPredicate(ConditionProperty property, boolean expec
|
||||
}
|
||||
}
|
||||
|
||||
private static ConditionProperty unit(ConditionProperty.Type type) {
|
||||
private static Property unit(Property.Type type) {
|
||||
return () -> type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
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.util.ExtraCodecs;
|
||||
import net.minecraft.util.StringRepresentable;
|
||||
import net.minecraft.world.item.CrossbowItem;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public record GeyserMatchPredicate(MatchPredicateData data) implements GeyserPredicate {
|
||||
|
||||
public static final MapCodec<GeyserMatchPredicate> CODEC = MatchPredicateData.CODEC.xmap(GeyserMatchPredicate::new, GeyserMatchPredicate::data);
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
return Type.MATCH;
|
||||
}
|
||||
|
||||
public interface MatchPredicateData {
|
||||
|
||||
MapCodec<MatchPredicateData> CODEC = Type.CODEC.dispatchMap("property", MatchPredicateData::type, Type::codec);
|
||||
|
||||
Type type();
|
||||
|
||||
enum Type implements StringRepresentable {
|
||||
CHARGE_TYPE("charge_type", ChargeType.CODEC),
|
||||
TRIM_MATERIAL("trim_material", TrimMaterial.CODEC),
|
||||
CONTEXT_DIMENSION("context_dimension", ContextDimension.CODEC),
|
||||
CUSTOM_MODEL_DATA("custom_model_data", CustomModelData.CODEC);
|
||||
|
||||
public static final Codec<Type> CODEC = StringRepresentable.fromEnum(Type::values);
|
||||
|
||||
private final String name;
|
||||
private final MapCodec<? extends MatchPredicateData> codec;
|
||||
|
||||
Type(String name, MapCodec<? extends MatchPredicateData> codec) {
|
||||
this.name = name;
|
||||
this.codec = codec;
|
||||
}
|
||||
|
||||
public MapCodec<? extends MatchPredicateData> codec() {
|
||||
return codec;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getSerializedName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public record ChargeType(CrossbowItem.ChargeType chargeType) implements MatchPredicateData {
|
||||
public static final MapCodec<ChargeType> CODEC = simpleCodec(CrossbowItem.ChargeType.CODEC, ChargeType::chargeType, ChargeType::new);
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
return Type.CHARGE_TYPE;
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
return Type.TRIM_MATERIAL;
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
return Type.CONTEXT_DIMENSION;
|
||||
}
|
||||
}
|
||||
|
||||
public record CustomModelData(String string, int index) implements MatchPredicateData {
|
||||
public static final MapCodec<CustomModelData> CODEC = RecordCodecBuilder.mapCodec(instance ->
|
||||
instance.group(
|
||||
Codec.STRING.fieldOf("value").forGetter(CustomModelData::string),
|
||||
ExtraCodecs.NON_NEGATIVE_INT.optionalFieldOf("index", 0).forGetter(CustomModelData::index)
|
||||
).apply(instance, CustomModelData::new)
|
||||
);
|
||||
|
||||
@Override
|
||||
public Type type() {
|
||||
return Type.CUSTOM_MODEL_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
private static <P, T> MapCodec<P> simpleCodec(Codec<T> codec, Function<P, T> getter, Function<T, P> constructor) {
|
||||
return codec.fieldOf("value").xmap(constructor, getter);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,8 @@ public interface GeyserPredicate {
|
||||
Type type();
|
||||
|
||||
enum Type implements StringRepresentable {
|
||||
CONDITION("condition", GeyserConditionPredicate.CODEC);
|
||||
CONDITION("condition", GeyserConditionPredicate.CODEC),
|
||||
MATCH("match", GeyserMatchPredicate.CODEC);
|
||||
|
||||
public static final Codec<Type> CODEC = StringRepresentable.fromEnum(Type::values);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user