| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import com.mojang.datafixers.DataFix;
|
| 5 | import com.mojang.datafixers.DataFixUtils;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.util.Pair;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 | import com.mojang.serialization.DynamicOps;
|
| 11 | import com.mojang.serialization.MapLike;
|
| 12 | import com.mojang.serialization.OptionalDynamic;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.Set;
|
| 18 | import java.util.function.Function;
|
| 19 | import java.util.function.UnaryOperator;
|
| 20 | import java.util.stream.Collectors;
|
| 21 | import java.util.stream.Stream;
|
| 22 | import net.minecraft.util.Mth;
|
| 23 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 24 | import net.minecraft.util.datafix.LegacyComponentDataFixUtils;
|
| 25 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class ItemStackComponentizationFix extends DataFix {
|
| 29 | private static final int HIDE_ENCHANTMENTS = 1;
|
| 30 | private static final int HIDE_MODIFIERS = 2;
|
| 31 | private static final int HIDE_UNBREAKABLE = 4;
|
| 32 | private static final int HIDE_CAN_DESTROY = 8;
|
| 33 | private static final int HIDE_CAN_PLACE = 16;
|
| 34 | private static final int HIDE_ADDITIONAL = 32;
|
| 35 | private static final int HIDE_DYE = 64;
|
| 36 | private static final int HIDE_UPGRADES = 128;
|
| 37 | private static final Set<String> POTION_HOLDER_IDS = Set.of(
|
| 38 | "minecraft:potion", "minecraft:splash_potion", "minecraft:lingering_potion", "minecraft:tipped_arrow"
|
| 39 | );
|
| 40 | private static final Set<String> BUCKETED_MOB_IDS = Set.of(
|
| 41 | "minecraft:pufferfish_bucket",
|
| 42 | "minecraft:salmon_bucket",
|
| 43 | "minecraft:cod_bucket",
|
| 44 | "minecraft:tropical_fish_bucket",
|
| 45 | "minecraft:axolotl_bucket",
|
| 46 | "minecraft:tadpole_bucket"
|
| 47 | );
|
| 48 | private static final List<String> BUCKETED_MOB_TAGS = List.of(
|
| 49 | "NoAI", "Silent", "NoGravity", "Glowing", "Invulnerable", "Health", "Age", "Variant", "HuntingCooldown", "BucketVariantTag"
|
| 50 | );
|
| 51 | private static final Set<String> BOOLEAN_BLOCK_STATE_PROPERTIES = Set.of(
|
| 52 | "attached",
|
| 53 | "bottom",
|
| 54 | "conditional",
|
| 55 | "disarmed",
|
| 56 | "drag",
|
| 57 | "enabled",
|
| 58 | "extended",
|
| 59 | "eye",
|
| 60 | "falling",
|
| 61 | "hanging",
|
| 62 | "has_bottle_0",
|
| 63 | "has_bottle_1",
|
| 64 | "has_bottle_2",
|
| 65 | "has_record",
|
| 66 | "has_book",
|
| 67 | "inverted",
|
| 68 | "in_wall",
|
| 69 | "lit",
|
| 70 | "locked",
|
| 71 | "occupied",
|
| 72 | "open",
|
| 73 | "persistent",
|
| 74 | "powered",
|
| 75 | "short",
|
| 76 | "signal_fire",
|
| 77 | "snowy",
|
| 78 | "triggered",
|
| 79 | "unstable",
|
| 80 | "waterlogged",
|
| 81 | "berries",
|
| 82 | "bloom",
|
| 83 | "shrieking",
|
| 84 | "can_summon",
|
| 85 | "up",
|
| 86 | "down",
|
| 87 | "north",
|
| 88 | "east",
|
| 89 | "south",
|
| 90 | "west",
|
| 91 | "slot_0_occupied",
|
| 92 | "slot_1_occupied",
|
| 93 | "slot_2_occupied",
|
| 94 | "slot_3_occupied",
|
| 95 | "slot_4_occupied",
|
| 96 | "slot_5_occupied",
|
| 97 | "cracked",
|
| 98 | "crafting"
|
| 99 | );
|
| 100 | private static final Splitter PROPERTY_SPLITTER = Splitter.on(',');
|
| 101 |
|
| 102 | public ItemStackComponentizationFix(final Schema outputSchema) {
|
| 103 | super(outputSchema, true);
|
| 104 | }
|
| 105 |
|
| 106 | private static void fixItemStack(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 107 | int hideFlags = itemStack.removeTag("HideFlags").asInt(0);
|
| 108 | itemStack.moveTagToComponent("Damage", "minecraft:damage", dynamic.createInt(0));
|
| 109 | itemStack.moveTagToComponent("RepairCost", "minecraft:repair_cost", dynamic.createInt(0));
|
| 110 | itemStack.moveTagToComponent("CustomModelData", "minecraft:custom_model_data");
|
| 111 | itemStack.removeTag("BlockStateTag")
|
| 112 | .result()
|
| 113 | .ifPresent(blockStateTag -> itemStack.setComponent("minecraft:block_state", fixBlockStateTag((Dynamic<?>)blockStateTag)));
|
| 114 | itemStack.moveTagToComponent("EntityTag", "minecraft:entity_data");
|
| 115 | itemStack.fixSubTag("BlockEntityTag", false, blockEntityTag -> {
|
| 116 | String id = NamespacedSchema.ensureNamespaced(blockEntityTag.get("id").asString(""));
|
| 117 | blockEntityTag = fixBlockEntityTag(itemStack, blockEntityTag, id);
|
| 118 | Dynamic<?> withoutId = blockEntityTag.remove("id");
|
| 119 | return withoutId.equals(blockEntityTag.emptyMap()) ? withoutId : blockEntityTag;
|
| 120 | });
|
| 121 | itemStack.moveTagToComponent("BlockEntityTag", "minecraft:block_entity_data");
|
| 122 | if (itemStack.removeTag("Unbreakable").asBoolean(false)) {
|
| 123 | Dynamic<?> component = dynamic.emptyMap();
|
| 124 | if ((hideFlags & 4) != 0) {
|
| 125 | component = component.set("show_in_tooltip", dynamic.createBoolean(false));
|
| 126 | }
|
| 127 |
|
| 128 | itemStack.setComponent("minecraft:unbreakable", component);
|
| 129 | }
|
| 130 |
|
| 131 | fixEnchantments(itemStack, dynamic, "Enchantments", "minecraft:enchantments", (hideFlags & 1) != 0);
|
| 132 | if (itemStack.is("minecraft:enchanted_book")) {
|
| 133 | fixEnchantments(itemStack, dynamic, "StoredEnchantments", "minecraft:stored_enchantments", (hideFlags & 32) != 0);
|
| 134 | }
|
| 135 |
|
| 136 | itemStack.fixSubTag("display", false, display -> fixDisplay(itemStack, display, hideFlags));
|
| 137 | fixAdventureModeChecks(itemStack, dynamic, hideFlags);
|
| 138 | fixAttributeModifiers(itemStack, dynamic, hideFlags);
|
| 139 | Optional<? extends Dynamic<?>> trim = itemStack.removeTag("Trim").result();
|
| 140 | if (trim.isPresent()) {
|
| 141 | Dynamic<?> fixedTrim = (Dynamic<?>)trim.get();
|
| 142 | if ((hideFlags & 128) != 0) {
|
| 143 | fixedTrim = fixedTrim.set("show_in_tooltip", fixedTrim.createBoolean(false));
|
| 144 | }
|
| 145 |
|
| 146 | itemStack.setComponent("minecraft:trim", fixedTrim);
|
| 147 | }
|
| 148 |
|
| 149 | if ((hideFlags & 32) != 0) {
|
| 150 | itemStack.setComponent("minecraft:hide_additional_tooltip", dynamic.emptyMap());
|
| 151 | }
|
| 152 |
|
| 153 | if (itemStack.is("minecraft:crossbow")) {
|
| 154 | itemStack.removeTag("Charged");
|
| 155 | itemStack.moveTagToComponent("ChargedProjectiles", "minecraft:charged_projectiles", dynamic.createList(Stream.empty()));
|
| 156 | }
|
| 157 |
|
| 158 | if (itemStack.is("minecraft:bundle")) {
|
| 159 | itemStack.moveTagToComponent("Items", "minecraft:bundle_contents", dynamic.createList(Stream.empty()));
|
| 160 | }
|
| 161 |
|
| 162 | if (itemStack.is("minecraft:filled_map")) {
|
| 163 | itemStack.moveTagToComponent("map", "minecraft:map_id");
|
| 164 | Map<? extends Dynamic<?>, ? extends Dynamic<?>> decorations = itemStack.removeTag("Decorations")
|
| 165 | .asStream()
|
| 166 | .map(ItemStackComponentizationFix::fixMapDecoration)
|
| 167 | .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond, (first, second) -> first));
|
| 168 | if (!decorations.isEmpty()) {
|
| 169 | itemStack.setComponent("minecraft:map_decorations", dynamic.createMap(decorations));
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | if (itemStack.is(POTION_HOLDER_IDS)) {
|
| 174 | fixPotionContents(itemStack, dynamic);
|
| 175 | }
|
| 176 |
|
| 177 | if (itemStack.is("minecraft:writable_book")) {
|
| 178 | fixWritableBook(itemStack, dynamic);
|
| 179 | }
|
| 180 |
|
| 181 | if (itemStack.is("minecraft:written_book")) {
|
| 182 | fixWrittenBook(itemStack, dynamic);
|
| 183 | }
|
| 184 |
|
| 185 | if (itemStack.is("minecraft:suspicious_stew")) {
|
| 186 | itemStack.moveTagToComponent("effects", "minecraft:suspicious_stew_effects");
|
| 187 | }
|
| 188 |
|
| 189 | if (itemStack.is("minecraft:debug_stick")) {
|
| 190 | itemStack.moveTagToComponent("DebugProperty", "minecraft:debug_stick_state");
|
| 191 | }
|
| 192 |
|
| 193 | if (itemStack.is(BUCKETED_MOB_IDS)) {
|
| 194 | fixBucketedMobData(itemStack, dynamic);
|
| 195 | }
|
| 196 |
|
| 197 | if (itemStack.is("minecraft:goat_horn")) {
|
| 198 | itemStack.moveTagToComponent("instrument", "minecraft:instrument");
|
| 199 | }
|
| 200 |
|
| 201 | if (itemStack.is("minecraft:knowledge_book")) {
|
| 202 | itemStack.moveTagToComponent("Recipes", "minecraft:recipes");
|
| 203 | }
|
| 204 |
|
| 205 | if (itemStack.is("minecraft:compass")) {
|
| 206 | fixLodestoneTracker(itemStack, dynamic);
|
| 207 | }
|
| 208 |
|
| 209 | if (itemStack.is("minecraft:firework_rocket")) {
|
| 210 | fixFireworkRocket(itemStack);
|
| 211 | }
|
| 212 |
|
| 213 | if (itemStack.is("minecraft:firework_star")) {
|
| 214 | fixFireworkStar(itemStack);
|
| 215 | }
|
| 216 |
|
| 217 | if (itemStack.is("minecraft:player_head")) {
|
| 218 | itemStack.removeTag("SkullOwner").result().ifPresent(skullOwner -> itemStack.setComponent("minecraft:profile", fixProfile((Dynamic<?>)skullOwner)));
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | private static Dynamic<?> fixBlockStateTag(final Dynamic<?> blockStateTag) {
|
| 223 | return DataFixUtils.orElse(blockStateTag.asMapOpt().result().map(entries -> entries.collect(Collectors.toMap(Pair::getFirst, entry -> {
|
| 224 | String key = ((Dynamic)entry.getFirst()).asString("");
|
| 225 | Dynamic<?> value = (Dynamic<?>)entry.getSecond();
|
| 226 | if (BOOLEAN_BLOCK_STATE_PROPERTIES.contains(key)) {
|
| 227 | Optional<Boolean> bool = value.asBoolean().result();
|
| 228 | if (bool.isPresent()) {
|
| 229 | return value.createString(String.valueOf(bool.get()));
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | Optional<Number> number = value.asNumber().result();
|
| 234 | return number.isPresent() ? value.createString(number.get().toString()) : value;
|
| 235 | }))).map(blockStateTag::createMap), blockStateTag);
|
| 236 | }
|
| 237 |
|
| 238 | private static Dynamic<?> fixDisplay(final ItemStackComponentizationFix.ItemStackData itemStack, Dynamic<?> display, final int hideFlags) {
|
| 239 | display.get("Name")
|
| 240 | .result()
|
| 241 | .filter(LegacyComponentDataFixUtils::isStrictlyValidJson)
|
| 242 | .ifPresent(name -> itemStack.setComponent("minecraft:custom_name", (Dynamic<?>)name));
|
| 243 | OptionalDynamic<?> lore = display.get("Lore");
|
| 244 | if (lore.result().isPresent()) {
|
| 245 | itemStack.setComponent("minecraft:lore", display.createList(display.get("Lore").asStream().filter(LegacyComponentDataFixUtils::isStrictlyValidJson)));
|
| 246 | }
|
| 247 |
|
| 248 | Optional<Integer> color = display.get("color").asNumber().result().map(Number::intValue);
|
| 249 | boolean hideDye = (hideFlags & 64) != 0;
|
| 250 | if (color.isPresent() || hideDye) {
|
| 251 | Dynamic<?> dyedColor = display.emptyMap().set("rgb", display.createInt(color.orElse(10511680)));
|
| 252 | if (hideDye) {
|
| 253 | dyedColor = dyedColor.set("show_in_tooltip", display.createBoolean(false));
|
| 254 | }
|
| 255 |
|
| 256 | itemStack.setComponent("minecraft:dyed_color", dyedColor);
|
| 257 | }
|
| 258 |
|
| 259 | Optional<String> locName = display.get("LocName").asString().result();
|
| 260 | if (locName.isPresent()) {
|
| 261 | itemStack.setComponent("minecraft:item_name", LegacyComponentDataFixUtils.createTranslatableComponent(display.getOps(), locName.get()));
|
| 262 | }
|
| 263 |
|
| 264 | if (itemStack.is("minecraft:filled_map")) {
|
| 265 | itemStack.setComponent("minecraft:map_color", display.get("MapColor"));
|
| 266 | display = display.remove("MapColor");
|
| 267 | }
|
| 268 |
|
| 269 | return display.remove("Name").remove("Lore").remove("color").remove("LocName");
|
| 270 | }
|
| 271 |
|
| 272 | private static <T> Dynamic<T> fixBlockEntityTag(final ItemStackComponentizationFix.ItemStackData itemStack, Dynamic<T> blockEntity, final String id) {
|
| 273 | itemStack.setComponent("minecraft:lock", blockEntity.get("Lock"));
|
| 274 | blockEntity = blockEntity.remove("Lock");
|
| 275 | Optional<Dynamic<T>> lootTable = blockEntity.get("LootTable").result();
|
| 276 | if (lootTable.isPresent()) {
|
| 277 | Dynamic<T> containerLoot = blockEntity.emptyMap().set("loot_table", lootTable.get());
|
| 278 | long seed = blockEntity.get("LootTableSeed").asLong(0L);
|
| 279 | if (seed != 0L) {
|
| 280 | containerLoot = containerLoot.set("seed", blockEntity.createLong(seed));
|
| 281 | }
|
| 282 |
|
| 283 | itemStack.setComponent("minecraft:container_loot", containerLoot);
|
| 284 | blockEntity = blockEntity.remove("LootTable").remove("LootTableSeed");
|
| 285 | }
|
| 286 | return switch (id) {
|
| 287 | case "minecraft:skull" -> {
|
| 288 | itemStack.setComponent("minecraft:note_block_sound", blockEntity.get("note_block_sound"));
|
| 289 | yield blockEntity.remove("note_block_sound");
|
| 290 | }
|
| 291 | case "minecraft:decorated_pot" -> {
|
| 292 | itemStack.setComponent("minecraft:pot_decorations", blockEntity.get("sherds"));
|
| 293 | Optional<Dynamic<T>> item = blockEntity.get("item").result();
|
| 294 | if (item.isPresent()) {
|
| 295 | itemStack.setComponent(
|
| 296 | "minecraft:container", blockEntity.createList(Stream.of(blockEntity.emptyMap().set("slot", blockEntity.createInt(0)).set("item", item.get())))
|
| 297 | );
|
| 298 | }
|
| 299 |
|
| 300 | yield blockEntity.remove("sherds").remove("item");
|
| 301 | }
|
| 302 | case "minecraft:banner" -> {
|
| 303 | itemStack.setComponent("minecraft:banner_patterns", blockEntity.get("patterns"));
|
| 304 | Optional<Number> base = blockEntity.get("Base").asNumber().result();
|
| 305 | if (base.isPresent()) {
|
| 306 | itemStack.setComponent("minecraft:base_color", blockEntity.createString(ExtraDataFixUtils.dyeColorIdToName(base.get().intValue())));
|
| 307 | }
|
| 308 |
|
| 309 | yield blockEntity.remove("patterns").remove("Base");
|
| 310 | }
|
| 311 | case "minecraft:shulker_box", "minecraft:chest", "minecraft:trapped_chest", "minecraft:furnace", "minecraft:ender_chest", "minecraft:dispenser", "minecraft:dropper", "minecraft:brewing_stand", "minecraft:hopper", "minecraft:barrel", "minecraft:smoker", "minecraft:blast_furnace", "minecraft:campfire", "minecraft:chiseled_bookshelf", "minecraft:crafter" -> {
|
| 312 | List<Dynamic<T>> items = blockEntity.get("Items")
|
| 313 | .asList(dynamic -> dynamic.emptyMap().set("slot", dynamic.createInt(dynamic.get("Slot").asByte((byte)0) & 255)).set("item", dynamic.remove("Slot")));
|
| 314 | if (!items.isEmpty()) {
|
| 315 | itemStack.setComponent("minecraft:container", blockEntity.createList(items.stream()));
|
| 316 | }
|
| 317 |
|
| 318 | yield blockEntity.remove("Items");
|
| 319 | }
|
| 320 | case "minecraft:beehive" -> {
|
| 321 | itemStack.setComponent("minecraft:bees", blockEntity.get("bees"));
|
| 322 | yield blockEntity.remove("bees");
|
| 323 | }
|
| 324 | default -> blockEntity;
|
| 325 | };
|
| 326 | }
|
| 327 |
|
| 328 | private static void fixEnchantments(
|
| 329 | final ItemStackComponentizationFix.ItemStackData itemStack,
|
| 330 | final Dynamic<?> dynamic,
|
| 331 | final String key,
|
| 332 | final String componentType,
|
| 333 | final boolean hideInTooltip
|
| 334 | ) {
|
| 335 | OptionalDynamic<?> rawEnchantments = itemStack.removeTag(key);
|
| 336 | List<Pair<String, Integer>> enchantments = rawEnchantments.asList(Function.identity())
|
| 337 | .stream()
|
| 338 | .flatMap(enchantmentx -> parseEnchantment(enchantmentx).stream())
|
| 339 | .filter(enchantmentx -> (Integer)enchantmentx.getSecond() > 0)
|
| 340 | .toList();
|
| 341 | if (!enchantments.isEmpty() || hideInTooltip) {
|
| 342 | Dynamic<?> component = dynamic.emptyMap();
|
| 343 | Dynamic<?> levels = dynamic.emptyMap();
|
| 344 |
|
| 345 | for (Pair<String, Integer> enchantment : enchantments) {
|
| 346 | levels = levels.set(enchantment.getFirst(), dynamic.createInt(enchantment.getSecond()));
|
| 347 | }
|
| 348 |
|
| 349 | component = component.set("levels", levels);
|
| 350 | if (hideInTooltip) {
|
| 351 | component = component.set("show_in_tooltip", dynamic.createBoolean(false));
|
| 352 | }
|
| 353 |
|
| 354 | itemStack.setComponent(componentType, component);
|
| 355 | }
|
| 356 |
|
| 357 | if (rawEnchantments.result().isPresent() && enchantments.isEmpty()) {
|
| 358 | itemStack.setComponent("minecraft:enchantment_glint_override", dynamic.createBoolean(true));
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | private static Optional<Pair<String, Integer>> parseEnchantment(final Dynamic<?> entry) {
|
| 363 | return entry.get("id").asString().apply2stable((id, level) -> Pair.of(id, Mth.clamp(level.intValue(), 0, 255)), entry.get("lvl").asNumber()).result();
|
| 364 | }
|
| 365 |
|
| 366 | private static void fixAdventureModeChecks(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic, final int hideFlags) {
|
| 367 | fixBlockStatePredicates(itemStack, dynamic, "CanDestroy", "minecraft:can_break", (hideFlags & 8) != 0);
|
| 368 | fixBlockStatePredicates(itemStack, dynamic, "CanPlaceOn", "minecraft:can_place_on", (hideFlags & 16) != 0);
|
| 369 | }
|
| 370 |
|
| 371 | private static void fixBlockStatePredicates(
|
| 372 | final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic, final String tag, final String componentId, final boolean hideInTooltip
|
| 373 | ) {
|
| 374 | Optional<? extends Dynamic<?>> oldPredicate = itemStack.removeTag(tag).result();
|
| 375 | if (!oldPredicate.isEmpty()) {
|
| 376 | Dynamic<?> component = dynamic.emptyMap()
|
| 377 | .set(
|
| 378 | "predicates",
|
| 379 | dynamic.createList(
|
| 380 | oldPredicate.get()
|
| 381 | .asStream()
|
| 382 | .map(value -> DataFixUtils.orElse(value.asString().map(string -> fixBlockStatePredicate((Dynamic<?>)value, string)).result(), value))
|
| 383 | )
|
| 384 | );
|
| 385 | if (hideInTooltip) {
|
| 386 | component = component.set("show_in_tooltip", dynamic.createBoolean(false));
|
| 387 | }
|
| 388 |
|
| 389 | itemStack.setComponent(componentId, component);
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | private static Dynamic<?> fixBlockStatePredicate(final Dynamic<?> dynamic, final String string) {
|
| 394 | int startProperties = string.indexOf(91);
|
| 395 | int startNbt = string.indexOf(123);
|
| 396 | int blockNameEnd = string.length();
|
| 397 | if (startProperties != -1) {
|
| 398 | blockNameEnd = startProperties;
|
| 399 | }
|
| 400 |
|
| 401 | if (startNbt != -1) {
|
| 402 | blockNameEnd = Math.min(blockNameEnd, startNbt);
|
| 403 | }
|
| 404 |
|
| 405 | String blockOrTagName = string.substring(0, blockNameEnd);
|
| 406 | Dynamic<?> predicate = dynamic.emptyMap().set("blocks", dynamic.createString(blockOrTagName.trim()));
|
| 407 | int endProperties = string.indexOf(93);
|
| 408 | if (startProperties != -1 && endProperties != -1) {
|
| 409 | Dynamic<?> properties = dynamic.emptyMap();
|
| 410 |
|
| 411 | for (String property : PROPERTY_SPLITTER.split(string.substring(startProperties + 1, endProperties))) {
|
| 412 | int assignment = property.indexOf(61);
|
| 413 | if (assignment != -1) {
|
| 414 | String key = property.substring(0, assignment).trim();
|
| 415 | String value = property.substring(assignment + 1).trim();
|
| 416 | properties = properties.set(key, dynamic.createString(value));
|
| 417 | }
|
| 418 | }
|
| 419 |
|
| 420 | predicate = predicate.set("state", properties);
|
| 421 | }
|
| 422 |
|
| 423 | int endNbt = string.indexOf(125);
|
| 424 | if (startNbt != -1 && endNbt != -1) {
|
| 425 | predicate = predicate.set("nbt", dynamic.createString(string.substring(startNbt, endNbt + 1)));
|
| 426 | }
|
| 427 |
|
| 428 | return predicate;
|
| 429 | }
|
| 430 |
|
| 431 | private static void fixAttributeModifiers(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic, final int hideFlags) {
|
| 432 | OptionalDynamic<?> attributeModifiersField = itemStack.removeTag("AttributeModifiers");
|
| 433 | if (!attributeModifiersField.result().isEmpty()) {
|
| 434 | boolean hideInTooltip = (hideFlags & 2) != 0;
|
| 435 | List<? extends Dynamic<?>> attributeModifiers = attributeModifiersField.asList(ItemStackComponentizationFix::fixAttributeModifier);
|
| 436 | Dynamic<?> component = dynamic.emptyMap().set("modifiers", dynamic.createList(attributeModifiers.stream()));
|
| 437 | if (hideInTooltip) {
|
| 438 | component = component.set("show_in_tooltip", dynamic.createBoolean(false));
|
| 439 | }
|
| 440 |
|
| 441 | itemStack.setComponent("minecraft:attribute_modifiers", component);
|
| 442 | }
|
| 443 | }
|
| 444 |
|
| 445 | private static Dynamic<?> fixAttributeModifier(final Dynamic<?> input) {
|
| 446 | Dynamic<?> result = input.emptyMap()
|
| 447 | .set("name", input.createString(""))
|
| 448 | .set("amount", input.createDouble(0.0))
|
| 449 | .set("operation", input.createString("add_value"));
|
| 450 | result = Dynamic.copyField(input, "AttributeName", result, "type");
|
| 451 | result = Dynamic.copyField(input, "Slot", result, "slot");
|
| 452 | result = Dynamic.copyField(input, "UUID", result, "uuid");
|
| 453 | result = Dynamic.copyField(input, "Name", result, "name");
|
| 454 | result = Dynamic.copyField(input, "Amount", result, "amount");
|
| 455 | return Dynamic.copyAndFixField(input, "Operation", result, "operation", operation -> {
|
| 456 | return operation.createString(switch (operation.asInt(0)) {
|
| 457 | case 1 -> "add_multiplied_base";
|
| 458 | case 2 -> "add_multiplied_total";
|
| 459 | default -> "add_value";
|
| 460 | });
|
| 461 | });
|
| 462 | }
|
| 463 |
|
| 464 | private static Pair<Dynamic<?>, Dynamic<?>> fixMapDecoration(final Dynamic<?> decoration) {
|
| 465 | Dynamic<?> id = DataFixUtils.orElseGet(decoration.get("id").result(), () -> decoration.createString(""));
|
| 466 | Dynamic<?> value = decoration.emptyMap()
|
| 467 | .set("type", decoration.createString(fixMapDecorationType(decoration.get("type").asInt(0))))
|
| 468 | .set("x", decoration.createDouble(decoration.get("x").asDouble(0.0)))
|
| 469 | .set("z", decoration.createDouble(decoration.get("z").asDouble(0.0)))
|
| 470 | .set("rotation", decoration.createFloat((float)decoration.get("rot").asDouble(0.0)));
|
| 471 | return Pair.of(id, value);
|
| 472 | }
|
| 473 |
|
| 474 | private static String fixMapDecorationType(final int id) {
|
| 475 | return switch (id) {
|
| 476 | case 1 -> "frame";
|
| 477 | case 2 -> "red_marker";
|
| 478 | case 3 -> "blue_marker";
|
| 479 | case 4 -> "target_x";
|
| 480 | case 5 -> "target_point";
|
| 481 | case 6 -> "player_off_map";
|
| 482 | case 7 -> "player_off_limits";
|
| 483 | case 8 -> "mansion";
|
| 484 | case 9 -> "monument";
|
| 485 | case 10 -> "banner_white";
|
| 486 | case 11 -> "banner_orange";
|
| 487 | case 12 -> "banner_magenta";
|
| 488 | case 13 -> "banner_light_blue";
|
| 489 | case 14 -> "banner_yellow";
|
| 490 | case 15 -> "banner_lime";
|
| 491 | case 16 -> "banner_pink";
|
| 492 | case 17 -> "banner_gray";
|
| 493 | case 18 -> "banner_light_gray";
|
| 494 | case 19 -> "banner_cyan";
|
| 495 | case 20 -> "banner_purple";
|
| 496 | case 21 -> "banner_blue";
|
| 497 | case 22 -> "banner_brown";
|
| 498 | case 23 -> "banner_green";
|
| 499 | case 24 -> "banner_red";
|
| 500 | case 25 -> "banner_black";
|
| 501 | case 26 -> "red_x";
|
| 502 | case 27 -> "village_desert";
|
| 503 | case 28 -> "village_plains";
|
| 504 | case 29 -> "village_savanna";
|
| 505 | case 30 -> "village_snowy";
|
| 506 | case 31 -> "village_taiga";
|
| 507 | case 32 -> "jungle_temple";
|
| 508 | case 33 -> "swamp_hut";
|
| 509 | default -> "player";
|
| 510 | };
|
| 511 | }
|
| 512 |
|
| 513 | private static void fixPotionContents(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 514 | Dynamic<?> component = dynamic.emptyMap();
|
| 515 | Optional<String> potion = itemStack.removeTag("Potion").asString().result().filter(id -> !id.equals("minecraft:empty"));
|
| 516 | if (potion.isPresent()) {
|
| 517 | component = component.set("potion", dynamic.createString(potion.get()));
|
| 518 | }
|
| 519 |
|
| 520 | component = itemStack.moveTagInto("CustomPotionColor", component, "custom_color");
|
| 521 | component = itemStack.moveTagInto("custom_potion_effects", component, "custom_effects");
|
| 522 | if (!component.equals(dynamic.emptyMap())) {
|
| 523 | itemStack.setComponent("minecraft:potion_contents", component);
|
| 524 | }
|
| 525 | }
|
| 526 |
|
| 527 | private static void fixWritableBook(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 528 | Dynamic<?> pages = fixBookPages(itemStack, dynamic);
|
| 529 | if (pages != null) {
|
| 530 | itemStack.setComponent("minecraft:writable_book_content", dynamic.emptyMap().set("pages", pages));
|
| 531 | }
|
| 532 | }
|
| 533 |
|
| 534 | private static void fixWrittenBook(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 535 | Dynamic<?> pages = fixBookPages(itemStack, dynamic);
|
| 536 | String title = itemStack.removeTag("title").asString("");
|
| 537 | Optional<String> filteredTitle = itemStack.removeTag("filtered_title").asString().result();
|
| 538 | Dynamic<?> component = dynamic.emptyMap();
|
| 539 | component = component.set("title", createFilteredText(dynamic, title, filteredTitle));
|
| 540 | component = itemStack.moveTagInto("author", component, "author");
|
| 541 | component = itemStack.moveTagInto("resolved", component, "resolved");
|
| 542 | component = itemStack.moveTagInto("generation", component, "generation");
|
| 543 | if (pages != null) {
|
| 544 | component = component.set("pages", pages);
|
| 545 | }
|
| 546 |
|
| 547 | itemStack.setComponent("minecraft:written_book_content", component);
|
| 548 | }
|
| 549 |
|
| 550 | private static @Nullable Dynamic<?> fixBookPages(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 551 | List<String> pages = itemStack.removeTag("pages").asList(pagex -> pagex.asString(""));
|
| 552 | Map<String, String> filteredPages = itemStack.removeTag("filtered_pages").asMap(key -> key.asString("0"), pagex -> pagex.asString(""));
|
| 553 | if (pages.isEmpty()) {
|
| 554 | return null;
|
| 555 | }
|
| 556 |
|
| 557 | List<Dynamic<?>> fixedPages = new ArrayList<>(pages.size());
|
| 558 |
|
| 559 | for (int i = 0; i < pages.size(); i++) {
|
| 560 | String page = pages.get(i);
|
| 561 | String filteredPage = filteredPages.get(String.valueOf(i));
|
| 562 | fixedPages.add(createFilteredText(dynamic, page, Optional.ofNullable(filteredPage)));
|
| 563 | }
|
| 564 |
|
| 565 | return dynamic.createList(fixedPages.stream());
|
| 566 | }
|
| 567 |
|
| 568 | private static Dynamic<?> createFilteredText(final Dynamic<?> dynamic, final String text, final Optional<String> filtered) {
|
| 569 | Dynamic<?> fixedPage = dynamic.emptyMap().set("raw", dynamic.createString(text));
|
| 570 | if (filtered.isPresent()) {
|
| 571 | fixedPage = fixedPage.set("filtered", dynamic.createString(filtered.get()));
|
| 572 | }
|
| 573 |
|
| 574 | return fixedPage;
|
| 575 | }
|
| 576 |
|
| 577 | private static void fixBucketedMobData(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 578 | Dynamic<?> data = dynamic.emptyMap();
|
| 579 |
|
| 580 | for (String key : BUCKETED_MOB_TAGS) {
|
| 581 | data = itemStack.moveTagInto(key, data, key);
|
| 582 | }
|
| 583 |
|
| 584 | if (!data.equals(dynamic.emptyMap())) {
|
| 585 | itemStack.setComponent("minecraft:bucket_entity_data", data);
|
| 586 | }
|
| 587 | }
|
| 588 |
|
| 589 | private static void fixLodestoneTracker(final ItemStackComponentizationFix.ItemStackData itemStack, final Dynamic<?> dynamic) {
|
| 590 | Optional<? extends Dynamic<?>> lodestonePos = itemStack.removeTag("LodestonePos").result();
|
| 591 | Optional<? extends Dynamic<?>> lodestoneDimension = itemStack.removeTag("LodestoneDimension").result();
|
| 592 | if (!lodestonePos.isEmpty() || !lodestoneDimension.isEmpty()) {
|
| 593 | boolean lodestoneTracked = itemStack.removeTag("LodestoneTracked").asBoolean(true);
|
| 594 | Dynamic<?> component = dynamic.emptyMap();
|
| 595 | if (lodestonePos.isPresent() && lodestoneDimension.isPresent()) {
|
| 596 | component = component.set("target", dynamic.emptyMap().set("pos", (Dynamic<?>)lodestonePos.get()).set("dimension", (Dynamic<?>)lodestoneDimension.get()));
|
| 597 | }
|
| 598 |
|
| 599 | if (!lodestoneTracked) {
|
| 600 | component = component.set("tracked", dynamic.createBoolean(false));
|
| 601 | }
|
| 602 |
|
| 603 | itemStack.setComponent("minecraft:lodestone_tracker", component);
|
| 604 | }
|
| 605 | }
|
| 606 |
|
| 607 | private static void fixFireworkStar(final ItemStackComponentizationFix.ItemStackData itemStack) {
|
| 608 | itemStack.fixSubTag("Explosion", true, explosion -> {
|
| 609 | itemStack.setComponent("minecraft:firework_explosion", fixFireworkExplosion(explosion));
|
| 610 | return explosion.remove("Type").remove("Colors").remove("FadeColors").remove("Trail").remove("Flicker");
|
| 611 | });
|
| 612 | }
|
| 613 |
|
| 614 | private static void fixFireworkRocket(final ItemStackComponentizationFix.ItemStackData itemStack) {
|
| 615 | itemStack.fixSubTag(
|
| 616 | "Fireworks",
|
| 617 | true,
|
| 618 | fireworks -> {
|
| 619 | Stream<? extends Dynamic<?>> explosions = fireworks.get("Explosions").asStream().map(ItemStackComponentizationFix::fixFireworkExplosion);
|
| 620 | int flight = fireworks.get("Flight").asInt(0);
|
| 621 | itemStack.setComponent(
|
| 622 | "minecraft:fireworks", fireworks.emptyMap().set("explosions", fireworks.createList(explosions)).set("flight_duration", fireworks.createByte((byte)flight))
|
| 623 | );
|
| 624 | return fireworks.remove("Explosions").remove("Flight");
|
| 625 | }
|
| 626 | );
|
| 627 | }
|
| 628 |
|
| 629 | private static Dynamic<?> fixFireworkExplosion(Dynamic<?> explosion) {
|
| 630 | explosion = explosion.set("shape", explosion.createString(switch (explosion.get("Type").asInt(0)) {
|
| 631 | case 1 -> "large_ball";
|
| 632 | case 2 -> "star";
|
| 633 | case 3 -> "creeper";
|
| 634 | case 4 -> "burst";
|
| 635 | default -> "small_ball";
|
| 636 | })).remove("Type");
|
| 637 | explosion = explosion.renameField("Colors", "colors");
|
| 638 | explosion = explosion.renameField("FadeColors", "fade_colors");
|
| 639 | explosion = explosion.renameField("Trail", "has_trail");
|
| 640 | return explosion.renameField("Flicker", "has_twinkle");
|
| 641 | }
|
| 642 |
|
| 643 | public static Dynamic<?> fixProfile(final Dynamic<?> dynamic) {
|
| 644 | Optional<String> simpleName = dynamic.asString().result();
|
| 645 | if (simpleName.isPresent()) {
|
| 646 | return isValidPlayerName(simpleName.get()) ? dynamic.emptyMap().set("name", dynamic.createString(simpleName.get())) : dynamic.emptyMap();
|
| 647 | }
|
| 648 |
|
| 649 | String name = dynamic.get("Name").asString("");
|
| 650 | Optional<? extends Dynamic<?>> id = dynamic.get("Id").result();
|
| 651 | Dynamic<?> properties = fixProfileProperties(dynamic.get("Properties"));
|
| 652 | Dynamic<?> profile = dynamic.emptyMap();
|
| 653 | if (isValidPlayerName(name)) {
|
| 654 | profile = profile.set("name", dynamic.createString(name));
|
| 655 | }
|
| 656 |
|
| 657 | if (id.isPresent()) {
|
| 658 | profile = profile.set("id", (Dynamic<?>)id.get());
|
| 659 | }
|
| 660 |
|
| 661 | if (properties != null) {
|
| 662 | profile = profile.set("properties", properties);
|
| 663 | }
|
| 664 |
|
| 665 | return profile;
|
| 666 | }
|
| 667 |
|
| 668 | private static boolean isValidPlayerName(final String name) {
|
| 669 | return name.length() > 16 ? false : name.chars().filter(c -> c <= 32 || c >= 127).findAny().isEmpty();
|
| 670 | }
|
| 671 |
|
| 672 | private static @Nullable Dynamic<?> fixProfileProperties(final OptionalDynamic<?> dynamic) {
|
| 673 | Map<String, List<Pair<String, Optional<String>>>> properties = dynamic.asMap(key -> key.asString(""), list -> list.asList(property -> {
|
| 674 | String value = property.get("Value").asString("");
|
| 675 | Optional<String> signature = property.get("Signature").asString().result();
|
| 676 | return Pair.of(value, signature);
|
| 677 | }));
|
| 678 | return properties.isEmpty() ? null : dynamic.createList(properties.entrySet().stream().flatMap(entry -> entry.getValue().stream().map(pair -> {
|
| 679 | Dynamic<?> property = dynamic.emptyMap().set("name", dynamic.createString(entry.getKey())).set("value", dynamic.createString(pair.getFirst()));
|
| 680 | Optional<String> signature = pair.getSecond();
|
| 681 | return signature.isPresent() ? property.set("signature", dynamic.createString(signature.get())) : property;
|
| 682 | })));
|
| 683 | }
|
| 684 |
|
| 685 | @Override
|
| 686 | protected TypeRewriteRule makeRule() {
|
| 687 | return this.writeFixAndRead(
|
| 688 | "ItemStack componentization", this.getInputSchema().getType(References.ITEM_STACK), this.getOutputSchema().getType(References.ITEM_STACK), dynamic -> {
|
| 689 | Optional<? extends Dynamic<?>> fixedItemStack = ItemStackComponentizationFix.ItemStackData.read(dynamic).map(itemStack -> {
|
| 690 | fixItemStack(itemStack, itemStack.tag);
|
| 691 | return itemStack.write();
|
| 692 | });
|
| 693 | return DataFixUtils.orElse(fixedItemStack, dynamic);
|
| 694 | }
|
| 695 | );
|
| 696 | }
|
| 697 |
|
| 698 | private static class ItemStackData {
|
| 699 | private final String item;
|
| 700 | private final int count;
|
| 701 | private Dynamic<?> components;
|
| 702 | private final Dynamic<?> remainder;
|
| 703 | private Dynamic<?> tag;
|
| 704 |
|
| 705 | private ItemStackData(final String item, final int count, final Dynamic<?> remainder) {
|
| 706 | this.item = NamespacedSchema.ensureNamespaced(item);
|
| 707 | this.count = count;
|
| 708 | this.components = remainder.emptyMap();
|
| 709 | this.tag = remainder.get("tag").orElseEmptyMap();
|
| 710 | this.remainder = remainder.remove("tag");
|
| 711 | }
|
| 712 |
|
| 713 | public static Optional<ItemStackComponentizationFix.ItemStackData> read(final Dynamic<?> dynamic) {
|
| 714 | return dynamic.get("id")
|
| 715 | .asString()
|
| 716 | .apply2stable(
|
| 717 | (item, count) -> new ItemStackComponentizationFix.ItemStackData(item, count.intValue(), dynamic.remove("id").remove("Count")),
|
| 718 | dynamic.get("Count").asNumber()
|
| 719 | )
|
| 720 | .result();
|
| 721 | }
|
| 722 |
|
| 723 | public OptionalDynamic<?> removeTag(final String key) {
|
| 724 | OptionalDynamic<?> value = this.tag.get(key);
|
| 725 | this.tag = this.tag.remove(key);
|
| 726 | return value;
|
| 727 | }
|
| 728 |
|
| 729 | public void setComponent(final String type, final Dynamic<?> value) {
|
| 730 | this.components = this.components.set(type, value);
|
| 731 | }
|
| 732 |
|
| 733 | public void setComponent(final String type, final OptionalDynamic<?> optionalValue) {
|
| 734 | optionalValue.result().ifPresent(value -> this.components = this.components.set(type, (Dynamic<?>)value));
|
| 735 | }
|
| 736 |
|
| 737 | public Dynamic<?> moveTagInto(final String fromKey, final Dynamic<?> target, final String toKey) {
|
| 738 | Optional<? extends Dynamic<?>> value = this.removeTag(fromKey).result();
|
| 739 | return value.isPresent() ? target.set(toKey, (Dynamic<?>)value.get()) : target;
|
| 740 | }
|
| 741 |
|
| 742 | public void moveTagToComponent(final String key, final String type, final Dynamic<?> defaultValue) {
|
| 743 | Optional<? extends Dynamic<?>> value = this.removeTag(key).result();
|
| 744 | if (value.isPresent() && !value.get().equals(defaultValue)) {
|
| 745 | this.setComponent(type, (Dynamic<?>)value.get());
|
| 746 | }
|
| 747 | }
|
| 748 |
|
| 749 | public void moveTagToComponent(final String key, final String type) {
|
| 750 | this.removeTag(key).result().ifPresent(value -> this.setComponent(type, (Dynamic<?>)value));
|
| 751 | }
|
| 752 |
|
| 753 | public void fixSubTag(final String key, final boolean dontFixWhenFieldIsMissing, final UnaryOperator<Dynamic<?>> function) {
|
| 754 | OptionalDynamic<?> value = this.tag.get(key);
|
| 755 | if (!dontFixWhenFieldIsMissing || !value.result().isEmpty()) {
|
| 756 | Dynamic<?> map = value.orElseEmptyMap();
|
| 757 | map = function.apply(map);
|
| 758 | if (map.equals(map.emptyMap())) {
|
| 759 | this.tag = this.tag.remove(key);
|
| 760 | } else {
|
| 761 | this.tag = this.tag.set(key, map);
|
| 762 | }
|
| 763 | }
|
| 764 | }
|
| 765 |
|
| 766 | public Dynamic<?> write() {
|
| 767 | Dynamic<?> result = this.tag.emptyMap().set("id", this.tag.createString(this.item)).set("count", this.tag.createInt(this.count));
|
| 768 | if (!this.tag.equals(this.tag.emptyMap())) {
|
| 769 | this.components = this.components.set("minecraft:custom_data", this.tag);
|
| 770 | }
|
| 771 |
|
| 772 | if (!this.components.equals(this.tag.emptyMap())) {
|
| 773 | result = result.set("components", this.components);
|
| 774 | }
|
| 775 |
|
| 776 | return mergeRemainder(result, this.remainder);
|
| 777 | }
|
| 778 |
|
| 779 | private static <T> Dynamic<T> mergeRemainder(final Dynamic<T> itemStack, final Dynamic<?> remainder) {
|
| 780 | DynamicOps<T> ops = itemStack.getOps();
|
| 781 | return ops.getMap(itemStack.getValue())
|
| 782 | .flatMap(itemStackMap -> ops.mergeToMap(remainder.convert(ops).getValue(), (MapLike<T>)itemStackMap))
|
| 783 | .map(merged -> new Dynamic<>(ops, (T)merged))
|
| 784 | .result()
|
| 785 | .orElse(itemStack);
|
| 786 | }
|
| 787 |
|
| 788 | public boolean is(final String id) {
|
| 789 | return this.item.equals(id);
|
| 790 | }
|
| 791 |
|
| 792 | public boolean is(final Set<String> ids) {
|
| 793 | return ids.contains(this.item);
|
| 794 | }
|
| 795 |
|
| 796 | public boolean hasComponent(final String id) {
|
| 797 | return this.components.get(id).result().isPresent();
|
| 798 | }
|
| 799 | }
|
| 800 | }
|
| 801 |
|