| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DSL;
|
| 4 | import com.mojang.datafixers.DataFix;
|
| 5 | import com.mojang.datafixers.OpticFinder;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.types.templates.List.ListType;
|
| 11 | import java.util.ArrayList;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.Set;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 17 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 18 |
|
| 19 | public class RemoveBlockEntityTagFix extends DataFix {
|
| 20 | private final Set<String> blockEntityIdsToDrop;
|
| 21 | private final boolean useLegacyDataStructure;
|
| 22 |
|
| 23 | public RemoveBlockEntityTagFix(final Schema outputSchema, final Set<String> blockEntityIdsToDrop) {
|
| 24 | this(outputSchema, false, blockEntityIdsToDrop);
|
| 25 | }
|
| 26 |
|
| 27 | public RemoveBlockEntityTagFix(final Schema outputSchema, final boolean useLegacyDataStructure, final Set<String> blockEntityIdsToDrop) {
|
| 28 | super(outputSchema, true);
|
| 29 | this.blockEntityIdsToDrop = blockEntityIdsToDrop;
|
| 30 | this.useLegacyDataStructure = useLegacyDataStructure;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public TypeRewriteRule makeRule() {
|
| 35 | OpticFinder<String> blockEntityIdF = DSL.fieldFinder("id", NamespacedSchema.namespacedString());
|
| 36 | return this.useLegacyDataStructure
|
| 37 | ? TypeRewriteRule.seq(
|
| 38 | this.createItemBlockEntityRemover(blockEntityIdF, "tag", "BlockEntityTag"),
|
| 39 | this.createFallingBlockBlockEntityRemover(blockEntityIdF),
|
| 40 | this.createStructureBlockEntityRemover(blockEntityIdF),
|
| 41 | this.createUncheckedConverterHack()
|
| 42 | )
|
| 43 | : TypeRewriteRule.seq(
|
| 44 | this.createItemBlockEntityRemover(blockEntityIdF, "components", "minecraft:block_entity_data"),
|
| 45 | this.createFallingBlockBlockEntityRemover(blockEntityIdF),
|
| 46 | this.createStructureBlockEntityRemover(blockEntityIdF),
|
| 47 | this.createChunkBlockEntityRemover(blockEntityIdF),
|
| 48 | this.createUncheckedConverterHack()
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | private TypeRewriteRule createItemBlockEntityRemover(
|
| 53 | final OpticFinder<String> blockEntityIdF, final String itemTagOrComponentKey, final String itemBlockEntityDataKey
|
| 54 | ) {
|
| 55 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 56 | OpticFinder<?> itemTagF = itemStackType.findField(itemTagOrComponentKey);
|
| 57 | OpticFinder<?> itemBlockEntityF = itemTagF.type().findField(itemBlockEntityDataKey);
|
| 58 | return this.fixTypeEverywhereTyped(
|
| 59 | "ItemRemoveBlockEntityTagFix" + this.getOutputSchema().getVersionKey(),
|
| 60 | itemStackType,
|
| 61 | input -> input.updateTyped(itemTagF, tag -> this.removeBlockEntity(tag, itemBlockEntityF, blockEntityIdF, itemBlockEntityDataKey))
|
| 62 | );
|
| 63 | }
|
| 64 |
|
| 65 | private TypeRewriteRule createFallingBlockBlockEntityRemover(final OpticFinder<String> blockEntityIdF) {
|
| 66 | Type<?> entityType = this.getInputSchema().getType(References.ENTITY);
|
| 67 | OpticFinder<?> fallingBlockF = DSL.namedChoice("minecraft:falling_block", this.getInputSchema().getChoiceType(References.ENTITY, "minecraft:falling_block"));
|
| 68 | OpticFinder<?> fallingBlockEntityTagF = fallingBlockF.type().findField("TileEntityData");
|
| 69 | return this.fixTypeEverywhereTyped(
|
| 70 | "FallingBlockEntityRemoveBlockEntityTagFix" + this.getOutputSchema().getVersionKey(),
|
| 71 | entityType,
|
| 72 | input -> input.updateTyped(fallingBlockF, tag -> this.removeBlockEntity(tag, fallingBlockEntityTagF, blockEntityIdF, "TileEntityData"))
|
| 73 | );
|
| 74 | }
|
| 75 |
|
| 76 | private TypeRewriteRule createStructureBlockEntityRemover(final OpticFinder<String> blockEntityIdF) {
|
| 77 | Type<?> structureType = this.getInputSchema().getType(References.STRUCTURE);
|
| 78 | OpticFinder<?> blocksF = structureType.findField("blocks");
|
| 79 | OpticFinder<?> blockTypeF = DSL.typeFinder(((ListType)blocksF.type()).getElement());
|
| 80 | OpticFinder<?> blockNbtF = blockTypeF.type().findField("nbt");
|
| 81 | return this.fixTypeEverywhereTyped(
|
| 82 | "StructureRemoveBlockEntityTagFix" + this.getOutputSchema().getVersionKey(),
|
| 83 | structureType,
|
| 84 | input -> input.updateTyped(blocksF, tag -> tag.updateTyped(blockTypeF, blockTag -> this.removeBlockEntity(blockTag, blockNbtF, blockEntityIdF, "nbt")))
|
| 85 | );
|
| 86 | }
|
| 87 |
|
| 88 | private TypeRewriteRule createChunkBlockEntityRemover(final OpticFinder<String> blockEntityIdF) {
|
| 89 | Type<?> chunkType = this.getInputSchema().getType(References.CHUNK);
|
| 90 | OpticFinder<? extends List<?>> blockEntitiesF = (OpticFinder<? extends List<?>>)chunkType.findField("block_entities");
|
| 91 | Type<?> blockEntityElementsType = ((ListType)blockEntitiesF.type()).getElement();
|
| 92 | OpticFinder<?> blockEntityTypeFinder = this.getInputSchema().getType(References.BLOCK_ENTITY).finder();
|
| 93 | Type<?> chunkTypeOut = this.getOutputSchema().getType(References.CHUNK);
|
| 94 | Type<List<?>> blockEntitiesTypeOut = (Type<List<?>>)chunkType.findField("block_entities").type();
|
| 95 | return this.fixTypeEverywhereTyped(
|
| 96 | "BlockEntityChunkRemover" + this.getOutputSchema().getVersionKey(),
|
| 97 | chunkType,
|
| 98 | chunkTypeOut,
|
| 99 | input -> input.update(blockEntitiesF, blockEntitiesTypeOut, listTag -> {
|
| 100 | ArrayList<Object> keptBlockEntities = new ArrayList<>();
|
| 101 |
|
| 102 | for (Object untypedBlockEntity : listTag) {
|
| 103 | Typed<?> typedBlockEntity = ExtraDataFixUtils.cast(blockEntityElementsType, untypedBlockEntity, input.getOps());
|
| 104 | Typed<?> typedBlockEntityUnwrapped = typedBlockEntity.getOrCreateTyped(blockEntityTypeFinder);
|
| 105 | String blockEntityId = typedBlockEntityUnwrapped.getOptional(blockEntityIdF).orElse("");
|
| 106 | if (!this.blockEntityIdsToDrop.contains(blockEntityId)) {
|
| 107 | keptBlockEntities.add(untypedBlockEntity);
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | return List.copyOf(keptBlockEntities);
|
| 112 | })
|
| 113 | );
|
| 114 | }
|
| 115 |
|
| 116 | private TypeRewriteRule createUncheckedConverterHack() {
|
| 117 | return this.convertUnchecked(
|
| 118 | "ItemRemoveBlockEntityTagFix - update block entity type" + this.getOutputSchema().getVersionKey(),
|
| 119 | this.getInputSchema().getType(References.BLOCK_ENTITY),
|
| 120 | this.getOutputSchema().getType(References.BLOCK_ENTITY)
|
| 121 | );
|
| 122 | }
|
| 123 |
|
| 124 | private Typed<?> removeBlockEntity(
|
| 125 | final Typed<?> tag, final OpticFinder<?> blockEntityF, final OpticFinder<String> blockEntityIdF, final String blockEntityFieldName
|
| 126 | ) {
|
| 127 | Optional<? extends Typed<?>> maybeBlockEntity = tag.getOptionalTyped(blockEntityF);
|
| 128 | if (maybeBlockEntity.isEmpty()) {
|
| 129 | return tag;
|
| 130 | }
|
| 131 |
|
| 132 | String blockEntityId = maybeBlockEntity.get().getOptional(blockEntityIdF).orElse("");
|
| 133 | return !this.blockEntityIdsToDrop.contains(blockEntityId)
|
| 134 | ? tag
|
| 135 | : Util.writeAndReadTypedOrThrow(tag, tag.getType(), tagData -> tagData.remove(blockEntityFieldName));
|
| 136 | }
|
| 137 | }
|
| 138 |
|