| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFix;
|
| 4 | import com.mojang.datafixers.DataFixUtils;
|
| 5 | import com.mojang.datafixers.TypeRewriteRule;
|
| 6 | import com.mojang.datafixers.schemas.Schema;
|
| 7 | import com.mojang.serialization.Dynamic;
|
| 8 | import it.unimi.dsi.fastutil.shorts.ShortArrayList;
|
| 9 | import it.unimi.dsi.fastutil.shorts.ShortList;
|
| 10 | import java.util.Arrays;
|
| 11 | import java.util.List;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import java.util.stream.IntStream;
|
| 14 |
|
| 15 | public class ChunkToProtochunkFix extends DataFix {
|
| 16 | private static final int NUM_SECTIONS = 16;
|
| 17 |
|
| 18 | public ChunkToProtochunkFix(final Schema outputSchema, final boolean changesType) {
|
| 19 | super(outputSchema, changesType);
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public TypeRewriteRule makeRule() {
|
| 24 | return this.writeFixAndRead(
|
| 25 | "ChunkToProtoChunkFix",
|
| 26 | this.getInputSchema().getType(References.CHUNK),
|
| 27 | this.getOutputSchema().getType(References.CHUNK),
|
| 28 | chunk -> chunk.update("Level", ChunkToProtochunkFix::fixChunkData)
|
| 29 | );
|
| 30 | }
|
| 31 |
|
| 32 | private static <T> Dynamic<T> fixChunkData(final Dynamic<T> tag) {
|
| 33 | boolean terrainPopulated = tag.get("TerrainPopulated").asBoolean(false);
|
| 34 | boolean lightPopulated = tag.get("LightPopulated").asNumber().result().isEmpty() || tag.get("LightPopulated").asBoolean(false);
|
| 35 | String status;
|
| 36 | if (terrainPopulated) {
|
| 37 | if (lightPopulated) {
|
| 38 | status = "mobs_spawned";
|
| 39 | } else {
|
| 40 | status = "decorated";
|
| 41 | }
|
| 42 | } else {
|
| 43 | status = "carved";
|
| 44 | }
|
| 45 |
|
| 46 | return repackTicks(repackBiomes(tag)).set("Status", tag.createString(status)).set("hasLegacyStructureData", tag.createBoolean(true));
|
| 47 | }
|
| 48 |
|
| 49 | private static <T> Dynamic<T> repackBiomes(final Dynamic<T> tag) {
|
| 50 | return tag.update("Biomes", biomes -> DataFixUtils.orElse(biomes.asByteBufferOpt().result().map(buffer -> {
|
| 51 | int[] newBiomes = new int[256];
|
| 52 |
|
| 53 | for (int i = 0; i < newBiomes.length; i++) {
|
| 54 | if (i < buffer.capacity()) {
|
| 55 | newBiomes[i] = buffer.get(i) & 255;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | return tag.createIntList(Arrays.stream(newBiomes));
|
| 60 | }), biomes));
|
| 61 | }
|
| 62 |
|
| 63 | private static <T> Dynamic<T> repackTicks(final Dynamic<T> tag) {
|
| 64 | return DataFixUtils.orElse(
|
| 65 | tag.get("TileTicks")
|
| 66 | .asStreamOpt()
|
| 67 | .result()
|
| 68 | .map(
|
| 69 | ticks -> {
|
| 70 | List<ShortList> toBeTickedTag = IntStream.range(0, 16).mapToObj(i -> new ShortArrayList()).collect(Collectors.toList());
|
| 71 | ticks.forEach(pendingTickTag -> {
|
| 72 | int x = pendingTickTag.get("x").asInt(0);
|
| 73 | int y = pendingTickTag.get("y").asInt(0);
|
| 74 | int z = pendingTickTag.get("z").asInt(0);
|
| 75 | short packedOffset = packOffsetCoordinates(x, y, z);
|
| 76 | toBeTickedTag.get(y >> 4).add(packedOffset);
|
| 77 | });
|
| 78 | return tag.remove("TileTicks")
|
| 79 | .set("ToBeTicked", tag.createList(toBeTickedTag.stream().map(l -> tag.createList(l.intStream().mapToObj(v -> tag.createShort((short)v))))));
|
| 80 | }
|
| 81 | ),
|
| 82 | tag
|
| 83 | );
|
| 84 | }
|
| 85 |
|
| 86 | private static short packOffsetCoordinates(final int x, final int y, final int z) {
|
| 87 | return (short)(x & 15 | (y & 15) << 4 | (z & 15) << 8);
|
| 88 | }
|
| 89 | }
|
| 90 |
|