package net.minecraft.util.datafix.schemas; import com.mojang.datafixers.DSL.TypeReference; import com.mojang.datafixers.schemas.Schema; import com.mojang.datafixers.types.Type; import com.mojang.datafixers.types.templates.Const.PrimitiveType; import com.mojang.serialization.DataResult; import com.mojang.serialization.DynamicOps; import com.mojang.serialization.codecs.PrimitiveCodec; import net.minecraft.resources.ResourceLocation; public class NamespacedSchema extends Schema { public static final PrimitiveCodec NAMESPACED_STRING_CODEC = new PrimitiveCodec() { @Override public DataResult read(DynamicOps dynamicOps, T object) { return dynamicOps.getStringValue(object).map(NamespacedSchema::ensureNamespaced); } public T write(DynamicOps ops, String value) { return ops.createString(value); } public String toString() { return "NamespacedString"; } }; private static final Type NAMESPACED_STRING = new PrimitiveType<>(NAMESPACED_STRING_CODEC); public NamespacedSchema(int versionKey, Schema parent) { super(versionKey, parent); } public static String ensureNamespaced(String string) { ResourceLocation resourceLocation = ResourceLocation.tryParse(string); return resourceLocation != null ? resourceLocation.toString() : string; } public static Type namespacedString() { return NAMESPACED_STRING; } @Override public Type getChoiceType(TypeReference typeReference, String string) { return super.getChoiceType(typeReference, ensureNamespaced(string)); } }