| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableSet;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.mojang.datafixers.DSL;
|
| 7 | import com.mojang.datafixers.DataFix;
|
| 8 | import com.mojang.datafixers.DataFixUtils;
|
| 9 | import com.mojang.datafixers.TypeRewriteRule;
|
| 10 | import com.mojang.datafixers.schemas.Schema;
|
| 11 | import com.mojang.datafixers.types.Type;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.Set;
|
| 16 | import java.util.Map.Entry;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import net.minecraft.util.datafix.schemas.V1451_6;
|
| 19 | import org.apache.commons.lang3.StringUtils;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class StatsCounterFix extends DataFix {
|
| 23 | private static final Set<String> SPECIAL_OBJECTIVE_CRITERIA = Set.of(
|
| 24 | "dummy",
|
| 25 | "trigger",
|
| 26 | "deathCount",
|
| 27 | "playerKillCount",
|
| 28 | "totalKillCount",
|
| 29 | "health",
|
| 30 | "food",
|
| 31 | "air",
|
| 32 | "armor",
|
| 33 | "xp",
|
| 34 | "level",
|
| 35 | "killedByTeam.aqua",
|
| 36 | "killedByTeam.black",
|
| 37 | "killedByTeam.blue",
|
| 38 | "killedByTeam.dark_aqua",
|
| 39 | "killedByTeam.dark_blue",
|
| 40 | "killedByTeam.dark_gray",
|
| 41 | "killedByTeam.dark_green",
|
| 42 | "killedByTeam.dark_purple",
|
| 43 | "killedByTeam.dark_red",
|
| 44 | "killedByTeam.gold",
|
| 45 | "killedByTeam.gray",
|
| 46 | "killedByTeam.green",
|
| 47 | "killedByTeam.light_purple",
|
| 48 | "killedByTeam.red",
|
| 49 | "killedByTeam.white",
|
| 50 | "killedByTeam.yellow",
|
| 51 | "teamkill.aqua",
|
| 52 | "teamkill.black",
|
| 53 | "teamkill.blue",
|
| 54 | "teamkill.dark_aqua",
|
| 55 | "teamkill.dark_blue",
|
| 56 | "teamkill.dark_gray",
|
| 57 | "teamkill.dark_green",
|
| 58 | "teamkill.dark_purple",
|
| 59 | "teamkill.dark_red",
|
| 60 | "teamkill.gold",
|
| 61 | "teamkill.gray",
|
| 62 | "teamkill.green",
|
| 63 | "teamkill.light_purple",
|
| 64 | "teamkill.red",
|
| 65 | "teamkill.white",
|
| 66 | "teamkill.yellow"
|
| 67 | );
|
| 68 | private static final Set<String> SKIP = ImmutableSet.<String>builder()
|
| 69 | .add("stat.craftItem.minecraft.spawn_egg")
|
| 70 | .add("stat.useItem.minecraft.spawn_egg")
|
| 71 | .add("stat.breakItem.minecraft.spawn_egg")
|
| 72 | .add("stat.pickup.minecraft.spawn_egg")
|
| 73 | .add("stat.drop.minecraft.spawn_egg")
|
| 74 | .build();
|
| 75 | private static final Map<String, String> CUSTOM_MAP = ImmutableMap.<String, String>builder()
|
| 76 | .put("stat.leaveGame", "minecraft:leave_game")
|
| 77 | .put("stat.playOneMinute", "minecraft:play_one_minute")
|
| 78 | .put("stat.timeSinceDeath", "minecraft:time_since_death")
|
| 79 | .put("stat.sneakTime", "minecraft:sneak_time")
|
| 80 | .put("stat.walkOneCm", "minecraft:walk_one_cm")
|
| 81 | .put("stat.crouchOneCm", "minecraft:crouch_one_cm")
|
| 82 | .put("stat.sprintOneCm", "minecraft:sprint_one_cm")
|
| 83 | .put("stat.swimOneCm", "minecraft:swim_one_cm")
|
| 84 | .put("stat.fallOneCm", "minecraft:fall_one_cm")
|
| 85 | .put("stat.climbOneCm", "minecraft:climb_one_cm")
|
| 86 | .put("stat.flyOneCm", "minecraft:fly_one_cm")
|
| 87 | .put("stat.diveOneCm", "minecraft:dive_one_cm")
|
| 88 | .put("stat.minecartOneCm", "minecraft:minecart_one_cm")
|
| 89 | .put("stat.boatOneCm", "minecraft:boat_one_cm")
|
| 90 | .put("stat.pigOneCm", "minecraft:pig_one_cm")
|
| 91 | .put("stat.horseOneCm", "minecraft:horse_one_cm")
|
| 92 | .put("stat.aviateOneCm", "minecraft:aviate_one_cm")
|
| 93 | .put("stat.jump", "minecraft:jump")
|
| 94 | .put("stat.drop", "minecraft:drop")
|
| 95 | .put("stat.damageDealt", "minecraft:damage_dealt")
|
| 96 | .put("stat.damageTaken", "minecraft:damage_taken")
|
| 97 | .put("stat.deaths", "minecraft:deaths")
|
| 98 | .put("stat.mobKills", "minecraft:mob_kills")
|
| 99 | .put("stat.animalsBred", "minecraft:animals_bred")
|
| 100 | .put("stat.playerKills", "minecraft:player_kills")
|
| 101 | .put("stat.fishCaught", "minecraft:fish_caught")
|
| 102 | .put("stat.talkedToVillager", "minecraft:talked_to_villager")
|
| 103 | .put("stat.tradedWithVillager", "minecraft:traded_with_villager")
|
| 104 | .put("stat.cakeSlicesEaten", "minecraft:eat_cake_slice")
|
| 105 | .put("stat.cauldronFilled", "minecraft:fill_cauldron")
|
| 106 | .put("stat.cauldronUsed", "minecraft:use_cauldron")
|
| 107 | .put("stat.armorCleaned", "minecraft:clean_armor")
|
| 108 | .put("stat.bannerCleaned", "minecraft:clean_banner")
|
| 109 | .put("stat.brewingstandInteraction", "minecraft:interact_with_brewingstand")
|
| 110 | .put("stat.beaconInteraction", "minecraft:interact_with_beacon")
|
| 111 | .put("stat.dropperInspected", "minecraft:inspect_dropper")
|
| 112 | .put("stat.hopperInspected", "minecraft:inspect_hopper")
|
| 113 | .put("stat.dispenserInspected", "minecraft:inspect_dispenser")
|
| 114 | .put("stat.noteblockPlayed", "minecraft:play_noteblock")
|
| 115 | .put("stat.noteblockTuned", "minecraft:tune_noteblock")
|
| 116 | .put("stat.flowerPotted", "minecraft:pot_flower")
|
| 117 | .put("stat.trappedChestTriggered", "minecraft:trigger_trapped_chest")
|
| 118 | .put("stat.enderchestOpened", "minecraft:open_enderchest")
|
| 119 | .put("stat.itemEnchanted", "minecraft:enchant_item")
|
| 120 | .put("stat.recordPlayed", "minecraft:play_record")
|
| 121 | .put("stat.furnaceInteraction", "minecraft:interact_with_furnace")
|
| 122 | .put("stat.craftingTableInteraction", "minecraft:interact_with_crafting_table")
|
| 123 | .put("stat.chestOpened", "minecraft:open_chest")
|
| 124 | .put("stat.sleepInBed", "minecraft:sleep_in_bed")
|
| 125 | .put("stat.shulkerBoxOpened", "minecraft:open_shulker_box")
|
| 126 | .build();
|
| 127 | private static final String BLOCK_KEY = "stat.mineBlock";
|
| 128 | private static final String NEW_BLOCK_KEY = "minecraft:mined";
|
| 129 | private static final Map<String, String> ITEM_KEYS = ImmutableMap.<String, String>builder()
|
| 130 | .put("stat.craftItem", "minecraft:crafted")
|
| 131 | .put("stat.useItem", "minecraft:used")
|
| 132 | .put("stat.breakItem", "minecraft:broken")
|
| 133 | .put("stat.pickup", "minecraft:picked_up")
|
| 134 | .put("stat.drop", "minecraft:dropped")
|
| 135 | .build();
|
| 136 | private static final Map<String, String> ENTITY_KEYS = ImmutableMap.<String, String>builder()
|
| 137 | .put("stat.entityKilledBy", "minecraft:killed_by")
|
| 138 | .put("stat.killEntity", "minecraft:killed")
|
| 139 | .build();
|
| 140 | private static final Map<String, String> ENTITIES = ImmutableMap.<String, String>builder()
|
| 141 | .put("Bat", "minecraft:bat")
|
| 142 | .put("Blaze", "minecraft:blaze")
|
| 143 | .put("CaveSpider", "minecraft:cave_spider")
|
| 144 | .put("Chicken", "minecraft:chicken")
|
| 145 | .put("Cow", "minecraft:cow")
|
| 146 | .put("Creeper", "minecraft:creeper")
|
| 147 | .put("Donkey", "minecraft:donkey")
|
| 148 | .put("ElderGuardian", "minecraft:elder_guardian")
|
| 149 | .put("Enderman", "minecraft:enderman")
|
| 150 | .put("Endermite", "minecraft:endermite")
|
| 151 | .put("EvocationIllager", "minecraft:evocation_illager")
|
| 152 | .put("Ghast", "minecraft:ghast")
|
| 153 | .put("Guardian", "minecraft:guardian")
|
| 154 | .put("Horse", "minecraft:horse")
|
| 155 | .put("Husk", "minecraft:husk")
|
| 156 | .put("Llama", "minecraft:llama")
|
| 157 | .put("LavaSlime", "minecraft:magma_cube")
|
| 158 | .put("MushroomCow", "minecraft:mooshroom")
|
| 159 | .put("Mule", "minecraft:mule")
|
| 160 | .put("Ozelot", "minecraft:ocelot")
|
| 161 | .put("Parrot", "minecraft:parrot")
|
| 162 | .put("Pig", "minecraft:pig")
|
| 163 | .put("PolarBear", "minecraft:polar_bear")
|
| 164 | .put("Rabbit", "minecraft:rabbit")
|
| 165 | .put("Sheep", "minecraft:sheep")
|
| 166 | .put("Shulker", "minecraft:shulker")
|
| 167 | .put("Silverfish", "minecraft:silverfish")
|
| 168 | .put("SkeletonHorse", "minecraft:skeleton_horse")
|
| 169 | .put("Skeleton", "minecraft:skeleton")
|
| 170 | .put("Slime", "minecraft:slime")
|
| 171 | .put("Spider", "minecraft:spider")
|
| 172 | .put("Squid", "minecraft:squid")
|
| 173 | .put("Stray", "minecraft:stray")
|
| 174 | .put("Vex", "minecraft:vex")
|
| 175 | .put("Villager", "minecraft:villager")
|
| 176 | .put("VindicationIllager", "minecraft:vindication_illager")
|
| 177 | .put("Witch", "minecraft:witch")
|
| 178 | .put("WitherSkeleton", "minecraft:wither_skeleton")
|
| 179 | .put("Wolf", "minecraft:wolf")
|
| 180 | .put("ZombieHorse", "minecraft:zombie_horse")
|
| 181 | .put("PigZombie", "minecraft:zombie_pigman")
|
| 182 | .put("ZombieVillager", "minecraft:zombie_villager")
|
| 183 | .put("Zombie", "minecraft:zombie")
|
| 184 | .build();
|
| 185 | private static final String NEW_CUSTOM_KEY = "minecraft:custom";
|
| 186 |
|
| 187 | public StatsCounterFix(final Schema outputSchema, final boolean changesType) {
|
| 188 | super(outputSchema, changesType);
|
| 189 | }
|
| 190 |
|
| 191 | private static StatsCounterFix.@Nullable StatType unpackLegacyKey(final String key) {
|
| 192 | if (SKIP.contains(key)) {
|
| 193 | return null;
|
| 194 | } else {
|
| 195 | String customKey = CUSTOM_MAP.get(key);
|
| 196 | if (customKey != null) {
|
| 197 | return new StatsCounterFix.StatType("minecraft:custom", customKey);
|
| 198 | } else {
|
| 199 | int splitIndex = StringUtils.ordinalIndexOf(key, ".", 2);
|
| 200 | if (splitIndex < 0) {
|
| 201 | return null;
|
| 202 | } else {
|
| 203 | String prefix = key.substring(0, splitIndex);
|
| 204 | if ("stat.mineBlock".equals(prefix)) {
|
| 205 | String newKey = upgradeBlock(key.substring(splitIndex + 1).replace('.', ':'));
|
| 206 | return new StatsCounterFix.StatType("minecraft:mined", newKey);
|
| 207 | } else {
|
| 208 | String itemKey = ITEM_KEYS.get(prefix);
|
| 209 | if (itemKey != null) {
|
| 210 | String oldItem = key.substring(splitIndex + 1).replace('.', ':');
|
| 211 | String newItem = upgradeItem(oldItem);
|
| 212 | String newKey = newItem == null ? oldItem : newItem;
|
| 213 | return new StatsCounterFix.StatType(itemKey, newKey);
|
| 214 | } else {
|
| 215 | String entityKey = ENTITY_KEYS.get(prefix);
|
| 216 | if (entityKey != null) {
|
| 217 | String oldEntity = key.substring(splitIndex + 1).replace('.', ':');
|
| 218 | String newKey = ENTITIES.getOrDefault(oldEntity, oldEntity);
|
| 219 | return new StatsCounterFix.StatType(entityKey, newKey);
|
| 220 | } else {
|
| 221 | return null;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public TypeRewriteRule makeRule() {
|
| 232 | return TypeRewriteRule.seq(this.makeStatFixer(), this.makeObjectiveFixer());
|
| 233 | }
|
| 234 |
|
| 235 | private TypeRewriteRule makeStatFixer() {
|
| 236 | Type<?> inputType = this.getInputSchema().getType(References.STATS);
|
| 237 | Type<?> outputType = this.getOutputSchema().getType(References.STATS);
|
| 238 | return this.fixTypeEverywhereTyped("StatsCounterFix", inputType, outputType, input -> {
|
| 239 | Dynamic<?> tag = input.get(DSL.remainderFinder());
|
| 240 | Map<Dynamic<?>, Dynamic<?>> stats = Maps.newHashMap();
|
| 241 | Optional<? extends Map<? extends Dynamic<?>, ? extends Dynamic<?>>> map = tag.getMapValues().result();
|
| 242 | if (map.isPresent()) {
|
| 243 | for (Entry<? extends Dynamic<?>, ? extends Dynamic<?>> entry : map.get().entrySet()) {
|
| 244 | if (entry.getValue().asNumber().result().isPresent()) {
|
| 245 | String key = entry.getKey().asString("");
|
| 246 | StatsCounterFix.StatType statType = unpackLegacyKey(key);
|
| 247 | if (statType != null) {
|
| 248 | Dynamic<?> newTypeKey = tag.createString(statType.type());
|
| 249 | Dynamic<?> element = stats.computeIfAbsent(newTypeKey, k -> tag.emptyMap());
|
| 250 | stats.put(newTypeKey, element.set(statType.typeKey(), (Dynamic<?>)entry.getValue()));
|
| 251 | }
|
| 252 | }
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | return Util.readTypedOrThrow(outputType, tag.emptyMap().set("stats", tag.createMap(stats)));
|
| 257 | });
|
| 258 | }
|
| 259 |
|
| 260 | private TypeRewriteRule makeObjectiveFixer() {
|
| 261 | Type<?> inputType = this.getInputSchema().getType(References.OBJECTIVE);
|
| 262 | Type<?> outputType = this.getOutputSchema().getType(References.OBJECTIVE);
|
| 263 | return this.fixTypeEverywhereTyped("ObjectiveStatFix", inputType, outputType, input -> {
|
| 264 | Dynamic<?> tag = input.get(DSL.remainderFinder());
|
| 265 | Dynamic<?> updatedTag = tag.update("CriteriaName", name -> DataFixUtils.orElse(name.asString().result().map(key -> {
|
| 266 | if (SPECIAL_OBJECTIVE_CRITERIA.contains(key)) {
|
| 267 | return (String)key;
|
| 268 | }
|
| 269 |
|
| 270 | StatsCounterFix.StatType statType = unpackLegacyKey(key);
|
| 271 | return statType == null ? "dummy" : V1451_6.packNamespacedWithDot(statType.type) + ":" + V1451_6.packNamespacedWithDot(statType.typeKey);
|
| 272 | }).map(name::createString), name));
|
| 273 | return Util.readTypedOrThrow(outputType, updatedTag);
|
| 274 | });
|
| 275 | }
|
| 276 |
|
| 277 | private static @Nullable String upgradeItem(final String name) {
|
| 278 | return ItemStackTheFlatteningFix.updateItem(name, 0);
|
| 279 | }
|
| 280 |
|
| 281 | private static String upgradeBlock(final String name) {
|
| 282 | return BlockStateData.upgradeBlock(name);
|
| 283 | }
|
| 284 |
|
| 285 | private record StatType(String type, String typeKey) {
|
| 286 | }
|
| 287 | }
|
| 288 |
|