| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.Streams;
|
| 6 | import com.mojang.datafixers.DSL;
|
| 7 | import com.mojang.datafixers.DataFix;
|
| 8 | import com.mojang.datafixers.OpticFinder;
|
| 9 | import com.mojang.datafixers.TypeRewriteRule;
|
| 10 | import com.mojang.datafixers.Typed;
|
| 11 | import com.mojang.datafixers.schemas.Schema;
|
| 12 | import com.mojang.datafixers.types.Type;
|
| 13 | import com.mojang.datafixers.types.templates.List.ListType;
|
| 14 | import com.mojang.serialization.Dynamic;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Map;
|
| 17 | import java.util.function.Function;
|
| 18 |
|
| 19 | public class ChunkBedBlockEntityInjecterFix extends DataFix {
|
| 20 | public ChunkBedBlockEntityInjecterFix(final Schema outputSchema, final boolean changesType) {
|
| 21 | super(outputSchema, changesType);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public TypeRewriteRule makeRule() {
|
| 26 | Type<?> chunkType = this.getOutputSchema().getType(References.CHUNK);
|
| 27 | Type<?> levelType = chunkType.findFieldType("Level");
|
| 28 | if (!(levelType.findFieldType("TileEntities") instanceof ListType<?> tileEntityListType)) {
|
| 29 | throw new IllegalStateException("Tile entity type is not a list type.");
|
| 30 | } else {
|
| 31 | return this.cap(levelType, tileEntityListType);
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | private <TE> TypeRewriteRule cap(final Type<?> levelType, final ListType<TE> tileEntityListType) {
|
| 36 | Type<TE> tileEntityType = tileEntityListType.getElement();
|
| 37 | OpticFinder<?> levelF = DSL.fieldFinder("Level", levelType);
|
| 38 | OpticFinder<List<TE>> tileEntitiesF = DSL.fieldFinder("TileEntities", tileEntityListType);
|
| 39 | int bedId = 416;
|
| 40 | return TypeRewriteRule.seq(
|
| 41 | this.fixTypeEverywhere(
|
| 42 | "InjectBedBlockEntityType",
|
| 43 | this.getInputSchema().findChoiceType(References.BLOCK_ENTITY),
|
| 44 | this.getOutputSchema().findChoiceType(References.BLOCK_ENTITY),
|
| 45 | ops -> v -> v
|
| 46 | ),
|
| 47 | this.fixTypeEverywhereTyped(
|
| 48 | "BedBlockEntityInjecter",
|
| 49 | this.getOutputSchema().getType(References.CHUNK),
|
| 50 | input -> {
|
| 51 | Typed<?> level = input.getTyped(levelF);
|
| 52 | Dynamic<?> levelTag = level.get(DSL.remainderFinder());
|
| 53 | int chunkX = levelTag.get("xPos").asInt(0);
|
| 54 | int chunkZ = levelTag.get("zPos").asInt(0);
|
| 55 | List<TE> tileEntities = Lists.newArrayList(level.getOrCreate(tileEntitiesF));
|
| 56 |
|
| 57 | for (Dynamic<?> sectionTag : levelTag.get("Sections").asList(Function.identity())) {
|
| 58 | int pos = sectionTag.get("Y").asInt(0);
|
| 59 | Streams.mapWithIndex(sectionTag.get("Blocks").asIntStream(), (block, index) -> {
|
| 60 | if (416 == (block & 0xFF) << 4) {
|
| 61 | int p = (int)index;
|
| 62 | int xx = p & 15;
|
| 63 | int yy = p >> 8 & 15;
|
| 64 | int zz = p >> 4 & 15;
|
| 65 | Map<Dynamic<?>, Dynamic<?>> bedTag = Maps.newHashMap();
|
| 66 | bedTag.put(sectionTag.createString("id"), sectionTag.createString("minecraft:bed"));
|
| 67 | bedTag.put(sectionTag.createString("x"), sectionTag.createInt(xx + (chunkX << 4)));
|
| 68 | bedTag.put(sectionTag.createString("y"), sectionTag.createInt(yy + (pos << 4)));
|
| 69 | bedTag.put(sectionTag.createString("z"), sectionTag.createInt(zz + (chunkZ << 4)));
|
| 70 | bedTag.put(sectionTag.createString("color"), sectionTag.createShort((short)14));
|
| 71 | return bedTag;
|
| 72 | } else {
|
| 73 | return null;
|
| 74 | }
|
| 75 | })
|
| 76 | .forEachOrdered(
|
| 77 | bedTag -> {
|
| 78 | if (bedTag != null) {
|
| 79 | tileEntities.add(
|
| 80 | tileEntityType.read(sectionTag.createMap((Map<? extends Dynamic<?>, ? extends Dynamic<?>>)bedTag))
|
| 81 | .result()
|
| 82 | .orElseThrow(() -> new IllegalStateException("Could not parse newly created bed block entity."))
|
| 83 | .getFirst()
|
| 84 | );
|
| 85 | }
|
| 86 | }
|
| 87 | );
|
| 88 | }
|
| 89 |
|
| 90 | return !tileEntities.isEmpty() ? input.set(levelF, level.set(tileEntitiesF, tileEntities)) : input;
|
| 91 | }
|
| 92 | )
|
| 93 | );
|
| 94 | }
|
| 95 | }
|
| 96 |
|