| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.datafixers.DSL;
|
| 5 | import com.mojang.datafixers.DataFix;
|
| 6 | import com.mojang.datafixers.DataFixUtils;
|
| 7 | import com.mojang.datafixers.TypeRewriteRule;
|
| 8 | import com.mojang.datafixers.Typed;
|
| 9 | import com.mojang.datafixers.schemas.Schema;
|
| 10 | import com.mojang.datafixers.types.Type;
|
| 11 | import com.mojang.datafixers.util.Either;
|
| 12 | import com.mojang.datafixers.util.Pair;
|
| 13 | import com.mojang.datafixers.util.Unit;
|
| 14 | import com.mojang.serialization.Dynamic;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.function.Function;
|
| 18 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 19 |
|
| 20 | public class EntityBlockStateFix extends DataFix {
|
| 21 | private static final Map<String, Integer> MAP = DataFixUtils.make(Maps.newHashMap(), map -> {
|
| 22 | map.put("minecraft:air", 0);
|
| 23 | map.put("minecraft:stone", 1);
|
| 24 | map.put("minecraft:grass", 2);
|
| 25 | map.put("minecraft:dirt", 3);
|
| 26 | map.put("minecraft:cobblestone", 4);
|
| 27 | map.put("minecraft:planks", 5);
|
| 28 | map.put("minecraft:sapling", 6);
|
| 29 | map.put("minecraft:bedrock", 7);
|
| 30 | map.put("minecraft:flowing_water", 8);
|
| 31 | map.put("minecraft:water", 9);
|
| 32 | map.put("minecraft:flowing_lava", 10);
|
| 33 | map.put("minecraft:lava", 11);
|
| 34 | map.put("minecraft:sand", 12);
|
| 35 | map.put("minecraft:gravel", 13);
|
| 36 | map.put("minecraft:gold_ore", 14);
|
| 37 | map.put("minecraft:iron_ore", 15);
|
| 38 | map.put("minecraft:coal_ore", 16);
|
| 39 | map.put("minecraft:log", 17);
|
| 40 | map.put("minecraft:leaves", 18);
|
| 41 | map.put("minecraft:sponge", 19);
|
| 42 | map.put("minecraft:glass", 20);
|
| 43 | map.put("minecraft:lapis_ore", 21);
|
| 44 | map.put("minecraft:lapis_block", 22);
|
| 45 | map.put("minecraft:dispenser", 23);
|
| 46 | map.put("minecraft:sandstone", 24);
|
| 47 | map.put("minecraft:noteblock", 25);
|
| 48 | map.put("minecraft:bed", 26);
|
| 49 | map.put("minecraft:golden_rail", 27);
|
| 50 | map.put("minecraft:detector_rail", 28);
|
| 51 | map.put("minecraft:sticky_piston", 29);
|
| 52 | map.put("minecraft:web", 30);
|
| 53 | map.put("minecraft:tallgrass", 31);
|
| 54 | map.put("minecraft:deadbush", 32);
|
| 55 | map.put("minecraft:piston", 33);
|
| 56 | map.put("minecraft:piston_head", 34);
|
| 57 | map.put("minecraft:wool", 35);
|
| 58 | map.put("minecraft:piston_extension", 36);
|
| 59 | map.put("minecraft:yellow_flower", 37);
|
| 60 | map.put("minecraft:red_flower", 38);
|
| 61 | map.put("minecraft:brown_mushroom", 39);
|
| 62 | map.put("minecraft:red_mushroom", 40);
|
| 63 | map.put("minecraft:gold_block", 41);
|
| 64 | map.put("minecraft:iron_block", 42);
|
| 65 | map.put("minecraft:double_stone_slab", 43);
|
| 66 | map.put("minecraft:stone_slab", 44);
|
| 67 | map.put("minecraft:brick_block", 45);
|
| 68 | map.put("minecraft:tnt", 46);
|
| 69 | map.put("minecraft:bookshelf", 47);
|
| 70 | map.put("minecraft:mossy_cobblestone", 48);
|
| 71 | map.put("minecraft:obsidian", 49);
|
| 72 | map.put("minecraft:torch", 50);
|
| 73 | map.put("minecraft:fire", 51);
|
| 74 | map.put("minecraft:mob_spawner", 52);
|
| 75 | map.put("minecraft:oak_stairs", 53);
|
| 76 | map.put("minecraft:chest", 54);
|
| 77 | map.put("minecraft:redstone_wire", 55);
|
| 78 | map.put("minecraft:diamond_ore", 56);
|
| 79 | map.put("minecraft:diamond_block", 57);
|
| 80 | map.put("minecraft:crafting_table", 58);
|
| 81 | map.put("minecraft:wheat", 59);
|
| 82 | map.put("minecraft:farmland", 60);
|
| 83 | map.put("minecraft:furnace", 61);
|
| 84 | map.put("minecraft:lit_furnace", 62);
|
| 85 | map.put("minecraft:standing_sign", 63);
|
| 86 | map.put("minecraft:wooden_door", 64);
|
| 87 | map.put("minecraft:ladder", 65);
|
| 88 | map.put("minecraft:rail", 66);
|
| 89 | map.put("minecraft:stone_stairs", 67);
|
| 90 | map.put("minecraft:wall_sign", 68);
|
| 91 | map.put("minecraft:lever", 69);
|
| 92 | map.put("minecraft:stone_pressure_plate", 70);
|
| 93 | map.put("minecraft:iron_door", 71);
|
| 94 | map.put("minecraft:wooden_pressure_plate", 72);
|
| 95 | map.put("minecraft:redstone_ore", 73);
|
| 96 | map.put("minecraft:lit_redstone_ore", 74);
|
| 97 | map.put("minecraft:unlit_redstone_torch", 75);
|
| 98 | map.put("minecraft:redstone_torch", 76);
|
| 99 | map.put("minecraft:stone_button", 77);
|
| 100 | map.put("minecraft:snow_layer", 78);
|
| 101 | map.put("minecraft:ice", 79);
|
| 102 | map.put("minecraft:snow", 80);
|
| 103 | map.put("minecraft:cactus", 81);
|
| 104 | map.put("minecraft:clay", 82);
|
| 105 | map.put("minecraft:reeds", 83);
|
| 106 | map.put("minecraft:jukebox", 84);
|
| 107 | map.put("minecraft:fence", 85);
|
| 108 | map.put("minecraft:pumpkin", 86);
|
| 109 | map.put("minecraft:netherrack", 87);
|
| 110 | map.put("minecraft:soul_sand", 88);
|
| 111 | map.put("minecraft:glowstone", 89);
|
| 112 | map.put("minecraft:portal", 90);
|
| 113 | map.put("minecraft:lit_pumpkin", 91);
|
| 114 | map.put("minecraft:cake", 92);
|
| 115 | map.put("minecraft:unpowered_repeater", 93);
|
| 116 | map.put("minecraft:powered_repeater", 94);
|
| 117 | map.put("minecraft:stained_glass", 95);
|
| 118 | map.put("minecraft:trapdoor", 96);
|
| 119 | map.put("minecraft:monster_egg", 97);
|
| 120 | map.put("minecraft:stonebrick", 98);
|
| 121 | map.put("minecraft:brown_mushroom_block", 99);
|
| 122 | map.put("minecraft:red_mushroom_block", 100);
|
| 123 | map.put("minecraft:iron_bars", 101);
|
| 124 | map.put("minecraft:glass_pane", 102);
|
| 125 | map.put("minecraft:melon_block", 103);
|
| 126 | map.put("minecraft:pumpkin_stem", 104);
|
| 127 | map.put("minecraft:melon_stem", 105);
|
| 128 | map.put("minecraft:vine", 106);
|
| 129 | map.put("minecraft:fence_gate", 107);
|
| 130 | map.put("minecraft:brick_stairs", 108);
|
| 131 | map.put("minecraft:stone_brick_stairs", 109);
|
| 132 | map.put("minecraft:mycelium", 110);
|
| 133 | map.put("minecraft:waterlily", 111);
|
| 134 | map.put("minecraft:nether_brick", 112);
|
| 135 | map.put("minecraft:nether_brick_fence", 113);
|
| 136 | map.put("minecraft:nether_brick_stairs", 114);
|
| 137 | map.put("minecraft:nether_wart", 115);
|
| 138 | map.put("minecraft:enchanting_table", 116);
|
| 139 | map.put("minecraft:brewing_stand", 117);
|
| 140 | map.put("minecraft:cauldron", 118);
|
| 141 | map.put("minecraft:end_portal", 119);
|
| 142 | map.put("minecraft:end_portal_frame", 120);
|
| 143 | map.put("minecraft:end_stone", 121);
|
| 144 | map.put("minecraft:dragon_egg", 122);
|
| 145 | map.put("minecraft:redstone_lamp", 123);
|
| 146 | map.put("minecraft:lit_redstone_lamp", 124);
|
| 147 | map.put("minecraft:double_wooden_slab", 125);
|
| 148 | map.put("minecraft:wooden_slab", 126);
|
| 149 | map.put("minecraft:cocoa", 127);
|
| 150 | map.put("minecraft:sandstone_stairs", 128);
|
| 151 | map.put("minecraft:emerald_ore", 129);
|
| 152 | map.put("minecraft:ender_chest", 130);
|
| 153 | map.put("minecraft:tripwire_hook", 131);
|
| 154 | map.put("minecraft:tripwire", 132);
|
| 155 | map.put("minecraft:emerald_block", 133);
|
| 156 | map.put("minecraft:spruce_stairs", 134);
|
| 157 | map.put("minecraft:birch_stairs", 135);
|
| 158 | map.put("minecraft:jungle_stairs", 136);
|
| 159 | map.put("minecraft:command_block", 137);
|
| 160 | map.put("minecraft:beacon", 138);
|
| 161 | map.put("minecraft:cobblestone_wall", 139);
|
| 162 | map.put("minecraft:flower_pot", 140);
|
| 163 | map.put("minecraft:carrots", 141);
|
| 164 | map.put("minecraft:potatoes", 142);
|
| 165 | map.put("minecraft:wooden_button", 143);
|
| 166 | map.put("minecraft:skull", 144);
|
| 167 | map.put("minecraft:anvil", 145);
|
| 168 | map.put("minecraft:trapped_chest", 146);
|
| 169 | map.put("minecraft:light_weighted_pressure_plate", 147);
|
| 170 | map.put("minecraft:heavy_weighted_pressure_plate", 148);
|
| 171 | map.put("minecraft:unpowered_comparator", 149);
|
| 172 | map.put("minecraft:powered_comparator", 150);
|
| 173 | map.put("minecraft:daylight_detector", 151);
|
| 174 | map.put("minecraft:redstone_block", 152);
|
| 175 | map.put("minecraft:quartz_ore", 153);
|
| 176 | map.put("minecraft:hopper", 154);
|
| 177 | map.put("minecraft:quartz_block", 155);
|
| 178 | map.put("minecraft:quartz_stairs", 156);
|
| 179 | map.put("minecraft:activator_rail", 157);
|
| 180 | map.put("minecraft:dropper", 158);
|
| 181 | map.put("minecraft:stained_hardened_clay", 159);
|
| 182 | map.put("minecraft:stained_glass_pane", 160);
|
| 183 | map.put("minecraft:leaves2", 161);
|
| 184 | map.put("minecraft:log2", 162);
|
| 185 | map.put("minecraft:acacia_stairs", 163);
|
| 186 | map.put("minecraft:dark_oak_stairs", 164);
|
| 187 | map.put("minecraft:slime", 165);
|
| 188 | map.put("minecraft:barrier", 166);
|
| 189 | map.put("minecraft:iron_trapdoor", 167);
|
| 190 | map.put("minecraft:prismarine", 168);
|
| 191 | map.put("minecraft:sea_lantern", 169);
|
| 192 | map.put("minecraft:hay_block", 170);
|
| 193 | map.put("minecraft:carpet", 171);
|
| 194 | map.put("minecraft:hardened_clay", 172);
|
| 195 | map.put("minecraft:coal_block", 173);
|
| 196 | map.put("minecraft:packed_ice", 174);
|
| 197 | map.put("minecraft:double_plant", 175);
|
| 198 | map.put("minecraft:standing_banner", 176);
|
| 199 | map.put("minecraft:wall_banner", 177);
|
| 200 | map.put("minecraft:daylight_detector_inverted", 178);
|
| 201 | map.put("minecraft:red_sandstone", 179);
|
| 202 | map.put("minecraft:red_sandstone_stairs", 180);
|
| 203 | map.put("minecraft:double_stone_slab2", 181);
|
| 204 | map.put("minecraft:stone_slab2", 182);
|
| 205 | map.put("minecraft:spruce_fence_gate", 183);
|
| 206 | map.put("minecraft:birch_fence_gate", 184);
|
| 207 | map.put("minecraft:jungle_fence_gate", 185);
|
| 208 | map.put("minecraft:dark_oak_fence_gate", 186);
|
| 209 | map.put("minecraft:acacia_fence_gate", 187);
|
| 210 | map.put("minecraft:spruce_fence", 188);
|
| 211 | map.put("minecraft:birch_fence", 189);
|
| 212 | map.put("minecraft:jungle_fence", 190);
|
| 213 | map.put("minecraft:dark_oak_fence", 191);
|
| 214 | map.put("minecraft:acacia_fence", 192);
|
| 215 | map.put("minecraft:spruce_door", 193);
|
| 216 | map.put("minecraft:birch_door", 194);
|
| 217 | map.put("minecraft:jungle_door", 195);
|
| 218 | map.put("minecraft:acacia_door", 196);
|
| 219 | map.put("minecraft:dark_oak_door", 197);
|
| 220 | map.put("minecraft:end_rod", 198);
|
| 221 | map.put("minecraft:chorus_plant", 199);
|
| 222 | map.put("minecraft:chorus_flower", 200);
|
| 223 | map.put("minecraft:purpur_block", 201);
|
| 224 | map.put("minecraft:purpur_pillar", 202);
|
| 225 | map.put("minecraft:purpur_stairs", 203);
|
| 226 | map.put("minecraft:purpur_double_slab", 204);
|
| 227 | map.put("minecraft:purpur_slab", 205);
|
| 228 | map.put("minecraft:end_bricks", 206);
|
| 229 | map.put("minecraft:beetroots", 207);
|
| 230 | map.put("minecraft:grass_path", 208);
|
| 231 | map.put("minecraft:end_gateway", 209);
|
| 232 | map.put("minecraft:repeating_command_block", 210);
|
| 233 | map.put("minecraft:chain_command_block", 211);
|
| 234 | map.put("minecraft:frosted_ice", 212);
|
| 235 | map.put("minecraft:magma", 213);
|
| 236 | map.put("minecraft:nether_wart_block", 214);
|
| 237 | map.put("minecraft:red_nether_brick", 215);
|
| 238 | map.put("minecraft:bone_block", 216);
|
| 239 | map.put("minecraft:structure_void", 217);
|
| 240 | map.put("minecraft:observer", 218);
|
| 241 | map.put("minecraft:white_shulker_box", 219);
|
| 242 | map.put("minecraft:orange_shulker_box", 220);
|
| 243 | map.put("minecraft:magenta_shulker_box", 221);
|
| 244 | map.put("minecraft:light_blue_shulker_box", 222);
|
| 245 | map.put("minecraft:yellow_shulker_box", 223);
|
| 246 | map.put("minecraft:lime_shulker_box", 224);
|
| 247 | map.put("minecraft:pink_shulker_box", 225);
|
| 248 | map.put("minecraft:gray_shulker_box", 226);
|
| 249 | map.put("minecraft:silver_shulker_box", 227);
|
| 250 | map.put("minecraft:cyan_shulker_box", 228);
|
| 251 | map.put("minecraft:purple_shulker_box", 229);
|
| 252 | map.put("minecraft:blue_shulker_box", 230);
|
| 253 | map.put("minecraft:brown_shulker_box", 231);
|
| 254 | map.put("minecraft:green_shulker_box", 232);
|
| 255 | map.put("minecraft:red_shulker_box", 233);
|
| 256 | map.put("minecraft:black_shulker_box", 234);
|
| 257 | map.put("minecraft:white_glazed_terracotta", 235);
|
| 258 | map.put("minecraft:orange_glazed_terracotta", 236);
|
| 259 | map.put("minecraft:magenta_glazed_terracotta", 237);
|
| 260 | map.put("minecraft:light_blue_glazed_terracotta", 238);
|
| 261 | map.put("minecraft:yellow_glazed_terracotta", 239);
|
| 262 | map.put("minecraft:lime_glazed_terracotta", 240);
|
| 263 | map.put("minecraft:pink_glazed_terracotta", 241);
|
| 264 | map.put("minecraft:gray_glazed_terracotta", 242);
|
| 265 | map.put("minecraft:silver_glazed_terracotta", 243);
|
| 266 | map.put("minecraft:cyan_glazed_terracotta", 244);
|
| 267 | map.put("minecraft:purple_glazed_terracotta", 245);
|
| 268 | map.put("minecraft:blue_glazed_terracotta", 246);
|
| 269 | map.put("minecraft:brown_glazed_terracotta", 247);
|
| 270 | map.put("minecraft:green_glazed_terracotta", 248);
|
| 271 | map.put("minecraft:red_glazed_terracotta", 249);
|
| 272 | map.put("minecraft:black_glazed_terracotta", 250);
|
| 273 | map.put("minecraft:concrete", 251);
|
| 274 | map.put("minecraft:concrete_powder", 252);
|
| 275 | map.put("minecraft:structure_block", 255);
|
| 276 | });
|
| 277 |
|
| 278 | public EntityBlockStateFix(final Schema outputSchema, final boolean changesType) {
|
| 279 | super(outputSchema, changesType);
|
| 280 | }
|
| 281 |
|
| 282 | public static int getBlockId(final String name) {
|
| 283 | Integer result = MAP.get(name);
|
| 284 | return result == null ? 0 : result;
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | public TypeRewriteRule makeRule() {
|
| 289 | Schema inputSchema = this.getInputSchema();
|
| 290 | Schema outputSchema = this.getOutputSchema();
|
| 291 | Function<Typed<?>, Typed<?>> minecartUpdater = input -> this.updateBlockToBlockState(input, "DisplayTile", "DisplayData", "DisplayState");
|
| 292 | Function<Typed<?>, Typed<?>> arrowUpdater = input -> this.updateBlockToBlockState(input, "inTile", "inData", "inBlockState");
|
| 293 | Type<Pair<Either<Pair<String, Either<Integer, String>>, Unit>, Dynamic<?>>> oldProjectileType = DSL.and(
|
| 294 | DSL.optional(DSL.field("inTile", DSL.named(References.BLOCK_NAME.typeName(), DSL.or(DSL.intType(), NamespacedSchema.namespacedString())))),
|
| 295 | DSL.remainderType()
|
| 296 | );
|
| 297 | Function<Typed<?>, Typed<?>> removeInTile = input -> input.update(oldProjectileType.finder(), DSL.remainderType(), Pair::getSecond);
|
| 298 | return this.fixTypeEverywhereTyped("EntityBlockStateFix", inputSchema.getType(References.ENTITY), outputSchema.getType(References.ENTITY), input -> {
|
| 299 | input = this.updateEntity(input, "minecraft:falling_block", this::updateFallingBlock);
|
| 300 | input = this.updateEntity(input, "minecraft:enderman", input2 -> this.updateBlockToBlockState(input2, "carried", "carriedData", "carriedBlockState"));
|
| 301 | input = this.updateEntity(input, "minecraft:arrow", arrowUpdater);
|
| 302 | input = this.updateEntity(input, "minecraft:spectral_arrow", arrowUpdater);
|
| 303 | input = this.updateEntity(input, "minecraft:egg", removeInTile);
|
| 304 | input = this.updateEntity(input, "minecraft:ender_pearl", removeInTile);
|
| 305 | input = this.updateEntity(input, "minecraft:fireball", removeInTile);
|
| 306 | input = this.updateEntity(input, "minecraft:potion", removeInTile);
|
| 307 | input = this.updateEntity(input, "minecraft:small_fireball", removeInTile);
|
| 308 | input = this.updateEntity(input, "minecraft:snowball", removeInTile);
|
| 309 | input = this.updateEntity(input, "minecraft:wither_skull", removeInTile);
|
| 310 | input = this.updateEntity(input, "minecraft:xp_bottle", removeInTile);
|
| 311 | input = this.updateEntity(input, "minecraft:commandblock_minecart", minecartUpdater);
|
| 312 | input = this.updateEntity(input, "minecraft:minecart", minecartUpdater);
|
| 313 | input = this.updateEntity(input, "minecraft:chest_minecart", minecartUpdater);
|
| 314 | input = this.updateEntity(input, "minecraft:furnace_minecart", minecartUpdater);
|
| 315 | input = this.updateEntity(input, "minecraft:tnt_minecart", minecartUpdater);
|
| 316 | input = this.updateEntity(input, "minecraft:hopper_minecart", minecartUpdater);
|
| 317 | return this.updateEntity(input, "minecraft:spawner_minecart", minecartUpdater);
|
| 318 | });
|
| 319 | }
|
| 320 |
|
| 321 | private Typed<?> updateFallingBlock(final Typed<?> input) {
|
| 322 | Type<Either<Pair<String, Either<Integer, String>>, Unit>> oldType = DSL.optional(
|
| 323 | DSL.field("Block", DSL.named(References.BLOCK_NAME.typeName(), DSL.or(DSL.intType(), NamespacedSchema.namespacedString())))
|
| 324 | );
|
| 325 | Type<Either<Pair<String, Dynamic<?>>, Unit>> newType = DSL.optional(
|
| 326 | DSL.field("BlockState", DSL.named(References.BLOCK_STATE.typeName(), DSL.remainderType()))
|
| 327 | );
|
| 328 | Dynamic<?> tag = input.get(DSL.remainderFinder());
|
| 329 | return input.update(oldType.finder(), newType, tile -> {
|
| 330 | int block = tile.map(l -> l.getSecond().map(l2 -> (Integer)l2, EntityBlockStateFix::getBlockId), r -> {
|
| 331 | Optional<Number> tileID = tag.get("TileID").asNumber().result();
|
| 332 | return tileID.map(Number::intValue).orElseGet(() -> tag.get("Tile").asByte((byte)0) & 0xFF);
|
| 333 | });
|
| 334 | int data = tag.get("Data").asInt(0) & 15;
|
| 335 | return Either.left(Pair.of(References.BLOCK_STATE.typeName(), BlockStateData.getTag(block << 4 | data)));
|
| 336 | }).set(DSL.remainderFinder(), tag.remove("Data").remove("TileID").remove("Tile"));
|
| 337 | }
|
| 338 |
|
| 339 | private Typed<?> updateBlockToBlockState(final Typed<?> input, final String oldFieldName, final String dataName, final String newFieldName) {
|
| 340 | Type<Pair<String, Either<Integer, String>>> oldType = DSL.field(
|
| 341 | oldFieldName, DSL.named(References.BLOCK_NAME.typeName(), DSL.or(DSL.intType(), NamespacedSchema.namespacedString()))
|
| 342 | );
|
| 343 | Type<Pair<String, Dynamic<?>>> newType = DSL.field(newFieldName, DSL.named(References.BLOCK_STATE.typeName(), DSL.remainderType()));
|
| 344 | Dynamic<?> tag = input.getOrCreate(DSL.remainderFinder());
|
| 345 | return input.update(oldType.finder(), newType, tile -> {
|
| 346 | int block = tile.getSecond().map(l -> (Integer)l, EntityBlockStateFix::getBlockId);
|
| 347 | int data = tag.get(dataName).asInt(0) & 15;
|
| 348 | return Pair.of(References.BLOCK_STATE.typeName(), BlockStateData.getTag(block << 4 | data));
|
| 349 | }).set(DSL.remainderFinder(), tag.remove(dataName));
|
| 350 | }
|
| 351 |
|
| 352 | private Typed<?> updateEntity(final Typed<?> input, final String name, final Function<Typed<?>, Typed<?>> function) {
|
| 353 | Type<?> oldType = this.getInputSchema().getChoiceType(References.ENTITY, name);
|
| 354 | Type<?> newType = this.getOutputSchema().getChoiceType(References.ENTITY, name);
|
| 355 | return input.updateTyped(DSL.namedChoice(name, oldType), newType, function);
|
| 356 | }
|
| 357 | }
|
| 358 |
|