| 1 | package net.minecraft.util.datafix.schemas;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.datafixers.DSL;
|
| 6 | import com.mojang.datafixers.DataFixUtils;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.templates.TypeTemplate;
|
| 9 | import com.mojang.datafixers.types.templates.Hook.HookFunction;
|
| 10 | import com.mojang.datafixers.util.Pair;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import com.mojang.serialization.DynamicOps;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.function.Supplier;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import net.minecraft.util.datafix.fixes.References;
|
| 18 |
|
| 19 | public class V1451_6 extends NamespacedSchema {
|
| 20 | public static final String SPECIAL_OBJECTIVE_MARKER = "_special";
|
| 21 | protected static final HookFunction UNPACK_OBJECTIVE_ID = new HookFunction() {
|
| 22 | @Override
|
| 23 | public <T> T apply(final DynamicOps<T> ops, final T value) {
|
| 24 | Dynamic<T> input = new Dynamic<>(ops, value);
|
| 25 | return DataFixUtils.orElse(
|
| 26 | input.get("CriteriaName")
|
| 27 | .asString()
|
| 28 | .result()
|
| 29 | .map(name -> {
|
| 30 | int colonPos = name.indexOf(58);
|
| 31 | if (colonPos < 0) {
|
| 32 | return Pair.of("_special", name);
|
| 33 | }
|
| 34 |
|
| 35 | try {
|
| 36 | Identifier statType = Identifier.bySeparator(name.substring(0, colonPos), '.');
|
| 37 | Identifier statId = Identifier.bySeparator(name.substring(colonPos + 1), '.');
|
| 38 | return Pair.of(statType.toString(), statId.toString());
|
| 39 | } catch (Exception e) {
|
| 40 | return Pair.of("_special", name);
|
| 41 | }
|
| 42 | })
|
| 43 | .map(
|
| 44 | explodedId -> input.set(
|
| 45 | "CriteriaType",
|
| 46 | input.createMap(
|
| 47 | ImmutableMap.of(
|
| 48 | input.createString("type"), input.createString(explodedId.getFirst()), input.createString("id"), input.createString(explodedId.getSecond())
|
| 49 | )
|
| 50 | )
|
| 51 | )
|
| 52 | ),
|
| 53 | input
|
| 54 | )
|
| 55 | .getValue();
|
| 56 | }
|
| 57 | };
|
| 58 | protected static final HookFunction REPACK_OBJECTIVE_ID = new HookFunction() {
|
| 59 | @Override
|
| 60 | public <T> T apply(final DynamicOps<T> ops, final T value) {
|
| 61 | Dynamic<T> input = new Dynamic<>(ops, value);
|
| 62 | Optional<Dynamic<T>> repackedId = input.get("CriteriaType")
|
| 63 | .get()
|
| 64 | .result()
|
| 65 | .flatMap(
|
| 66 | type -> {
|
| 67 | Optional<String> statType = type.get("type").asString().result();
|
| 68 | Optional<String> statId = type.get("id").asString().result();
|
| 69 | if (statType.isPresent() && statId.isPresent()) {
|
| 70 | String unpackedType = statType.get();
|
| 71 | return unpackedType.equals("_special")
|
| 72 | ? Optional.of(input.createString(statId.get()))
|
| 73 | : Optional.of(type.createString(V1451_6.packNamespacedWithDot(unpackedType) + ":" + V1451_6.packNamespacedWithDot(statId.get())));
|
| 74 | } else {
|
| 75 | return Optional.empty();
|
| 76 | }
|
| 77 | }
|
| 78 | );
|
| 79 | return DataFixUtils.orElse(repackedId.map(id -> input.set("CriteriaName", (Dynamic<?>)id).remove("CriteriaType")), input).getValue();
|
| 80 | }
|
| 81 | };
|
| 82 |
|
| 83 | public V1451_6(final int versionKey, final Schema parent) {
|
| 84 | super(versionKey, parent);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void registerTypes(
|
| 89 | final Schema schema, final Map<String, Supplier<TypeTemplate>> entityTypes, final Map<String, Supplier<TypeTemplate>> blockEntityTypes
|
| 90 | ) {
|
| 91 | super.registerTypes(schema, entityTypes, blockEntityTypes);
|
| 92 | Supplier<TypeTemplate> ITEM_STATS = () -> DSL.compoundList(References.ITEM_NAME.in(schema), DSL.constType(DSL.intType()));
|
| 93 | schema.registerType(
|
| 94 | false,
|
| 95 | References.STATS,
|
| 96 | () -> DSL.optionalFields(
|
| 97 | "stats",
|
| 98 | DSL.optionalFields(
|
| 99 | Pair.of("minecraft:mined", DSL.compoundList(References.BLOCK_NAME.in(schema), DSL.constType(DSL.intType()))),
|
| 100 | Pair.of("minecraft:crafted", ITEM_STATS.get()),
|
| 101 | Pair.of("minecraft:used", ITEM_STATS.get()),
|
| 102 | Pair.of("minecraft:broken", ITEM_STATS.get()),
|
| 103 | Pair.of("minecraft:picked_up", ITEM_STATS.get()),
|
| 104 | Pair.of("minecraft:dropped", ITEM_STATS.get()),
|
| 105 | Pair.of("minecraft:killed", DSL.compoundList(References.ENTITY_NAME.in(schema), DSL.constType(DSL.intType()))),
|
| 106 | Pair.of("minecraft:killed_by", DSL.compoundList(References.ENTITY_NAME.in(schema), DSL.constType(DSL.intType()))),
|
| 107 | Pair.of("minecraft:custom", DSL.compoundList(DSL.constType(namespacedString()), DSL.constType(DSL.intType())))
|
| 108 | )
|
| 109 | )
|
| 110 | );
|
| 111 | Map<String, Supplier<TypeTemplate>> criterionTypes = createCriterionTypes(schema);
|
| 112 | schema.registerType(
|
| 113 | false,
|
| 114 | References.OBJECTIVE,
|
| 115 | () -> DSL.hook(
|
| 116 | DSL.optionalFields("CriteriaType", DSL.taggedChoiceLazy("type", DSL.string(), criterionTypes), "DisplayName", References.TEXT_COMPONENT.in(schema)),
|
| 117 | UNPACK_OBJECTIVE_ID,
|
| 118 | REPACK_OBJECTIVE_ID
|
| 119 | )
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | protected static Map<String, Supplier<TypeTemplate>> createCriterionTypes(final Schema schema) {
|
| 124 | Supplier<TypeTemplate> itemCriterion = () -> DSL.optionalFields("id", References.ITEM_NAME.in(schema));
|
| 125 | Supplier<TypeTemplate> blockCriterion = () -> DSL.optionalFields("id", References.BLOCK_NAME.in(schema));
|
| 126 | Supplier<TypeTemplate> entityCriterion = () -> DSL.optionalFields("id", References.ENTITY_NAME.in(schema));
|
| 127 | Map<String, Supplier<TypeTemplate>> criterionTypes = Maps.newHashMap();
|
| 128 | criterionTypes.put("minecraft:mined", blockCriterion);
|
| 129 | criterionTypes.put("minecraft:crafted", itemCriterion);
|
| 130 | criterionTypes.put("minecraft:used", itemCriterion);
|
| 131 | criterionTypes.put("minecraft:broken", itemCriterion);
|
| 132 | criterionTypes.put("minecraft:picked_up", itemCriterion);
|
| 133 | criterionTypes.put("minecraft:dropped", itemCriterion);
|
| 134 | criterionTypes.put("minecraft:killed", entityCriterion);
|
| 135 | criterionTypes.put("minecraft:killed_by", entityCriterion);
|
| 136 | criterionTypes.put("minecraft:custom", () -> DSL.optionalFields("id", DSL.constType(namespacedString())));
|
| 137 | criterionTypes.put("_special", () -> DSL.optionalFields("id", DSL.constType(DSL.string())));
|
| 138 | return criterionTypes;
|
| 139 | }
|
| 140 |
|
| 141 | public static String packNamespacedWithDot(final String location) {
|
| 142 | Identifier parsedLoc = Identifier.tryParse(location);
|
| 143 | return parsedLoc != null ? parsedLoc.getNamespace() + "." + parsedLoc.getPath() : location;
|
| 144 | }
|
| 145 | }
|
| 146 |
|