| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import com.mojang.datafixers.DSL;
|
| 6 | import com.mojang.datafixers.DataFix;
|
| 7 | import com.mojang.datafixers.DataFixUtils;
|
| 8 | import com.mojang.datafixers.OpticFinder;
|
| 9 | import com.mojang.datafixers.TypeRewriteRule;
|
| 10 | import com.mojang.datafixers.schemas.Schema;
|
| 11 | import com.mojang.datafixers.types.Type;
|
| 12 | import com.mojang.datafixers.util.Pair;
|
| 13 | import com.mojang.serialization.Dynamic;
|
| 14 | import com.mojang.serialization.OptionalDynamic;
|
| 15 | import it.unimi.dsi.fastutil.ints.Int2IntFunction;
|
| 16 | import it.unimi.dsi.fastutil.ints.Int2IntLinkedOpenHashMap;
|
| 17 | import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
| 18 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 19 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 20 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 21 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 22 | import java.util.ArrayList;
|
| 23 | import java.util.Arrays;
|
| 24 | import java.util.BitSet;
|
| 25 | import java.util.HashMap;
|
| 26 | import java.util.List;
|
| 27 | import java.util.Map;
|
| 28 | import java.util.Optional;
|
| 29 | import java.util.Set;
|
| 30 | import java.util.function.Function;
|
| 31 | import java.util.function.Supplier;
|
| 32 | import java.util.stream.Collectors;
|
| 33 | import java.util.stream.IntStream;
|
| 34 | import java.util.stream.LongStream;
|
| 35 | import java.util.stream.Stream;
|
| 36 | import net.minecraft.SharedConstants;
|
| 37 | import net.minecraft.util.Util;
|
| 38 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
| 39 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 |
|
| 42 | public class ChunkHeightAndBiomeFix extends DataFix {
|
| 43 | public static final String DATAFIXER_CONTEXT_TAG = "__context";
|
| 44 | private static final String NAME = "ChunkHeightAndBiomeFix";
|
| 45 | private static final int OLD_SECTION_COUNT = 16;
|
| 46 | private static final int NEW_SECTION_COUNT = 24;
|
| 47 | private static final int NEW_MIN_SECTION_Y = -4;
|
| 48 | public static final int BLOCKS_PER_SECTION = 4096;
|
| 49 | private static final int LONGS_PER_SECTION = 64;
|
| 50 | private static final int HEIGHTMAP_BITS = 9;
|
| 51 | private static final long HEIGHTMAP_MASK = 511L;
|
| 52 | private static final int HEIGHTMAP_OFFSET = 64;
|
| 53 | private static final String[] HEIGHTMAP_TYPES = new String[]{
|
| 54 | "WORLD_SURFACE_WG", "WORLD_SURFACE", "WORLD_SURFACE_IGNORE_SNOW", "OCEAN_FLOOR_WG", "OCEAN_FLOOR", "MOTION_BLOCKING", "MOTION_BLOCKING_NO_LEAVES"
|
| 55 | };
|
| 56 | private static final Set<String> STATUS_IS_OR_AFTER_SURFACE = Set.of(
|
| 57 | "surface", "carvers", "liquid_carvers", "features", "light", "spawn", "heightmaps", "full"
|
| 58 | );
|
| 59 | private static final Set<String> STATUS_IS_OR_AFTER_NOISE = Set.of(
|
| 60 | "noise", "surface", "carvers", "liquid_carvers", "features", "light", "spawn", "heightmaps", "full"
|
| 61 | );
|
| 62 | private static final Set<String> BLOCKS_BEFORE_FEATURE_STATUS = Set.of(
|
| 63 | "minecraft:air",
|
| 64 | "minecraft:basalt",
|
| 65 | "minecraft:bedrock",
|
| 66 | "minecraft:blackstone",
|
| 67 | "minecraft:calcite",
|
| 68 | "minecraft:cave_air",
|
| 69 | "minecraft:coarse_dirt",
|
| 70 | "minecraft:crimson_nylium",
|
| 71 | "minecraft:dirt",
|
| 72 | "minecraft:end_stone",
|
| 73 | "minecraft:grass_block",
|
| 74 | "minecraft:gravel",
|
| 75 | "minecraft:ice",
|
| 76 | "minecraft:lava",
|
| 77 | "minecraft:mycelium",
|
| 78 | "minecraft:nether_wart_block",
|
| 79 | "minecraft:netherrack",
|
| 80 | "minecraft:orange_terracotta",
|
| 81 | "minecraft:packed_ice",
|
| 82 | "minecraft:podzol",
|
| 83 | "minecraft:powder_snow",
|
| 84 | "minecraft:red_sand",
|
| 85 | "minecraft:red_sandstone",
|
| 86 | "minecraft:sand",
|
| 87 | "minecraft:sandstone",
|
| 88 | "minecraft:snow_block",
|
| 89 | "minecraft:soul_sand",
|
| 90 | "minecraft:soul_soil",
|
| 91 | "minecraft:stone",
|
| 92 | "minecraft:terracotta",
|
| 93 | "minecraft:warped_nylium",
|
| 94 | "minecraft:warped_wart_block",
|
| 95 | "minecraft:water",
|
| 96 | "minecraft:white_terracotta"
|
| 97 | );
|
| 98 | private static final int BIOME_CONTAINER_LAYER_SIZE = 16;
|
| 99 | private static final int BIOME_CONTAINER_SIZE = 64;
|
| 100 | private static final int BIOME_CONTAINER_TOP_LAYER_OFFSET = 1008;
|
| 101 | public static final String DEFAULT_BIOME = "minecraft:plains";
|
| 102 | private static final Int2ObjectMap<String> BIOMES_BY_ID = new Int2ObjectOpenHashMap<>();
|
| 103 |
|
| 104 | public ChunkHeightAndBiomeFix(final Schema outputSchema) {
|
| 105 | super(outputSchema, true);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected TypeRewriteRule makeRule() {
|
| 110 | Type<?> oldChunkType = this.getInputSchema().getType(References.CHUNK);
|
| 111 | OpticFinder<?> levelFinder = oldChunkType.findField("Level");
|
| 112 | OpticFinder<?> sectionsFinder = levelFinder.type().findField("Sections");
|
| 113 | Schema outputSchema = this.getOutputSchema();
|
| 114 | Type<?> chunkType = outputSchema.getType(References.CHUNK);
|
| 115 | Type<?> levelType = chunkType.findField("Level").type();
|
| 116 | Type<?> sectionsType = levelType.findField("Sections").type();
|
| 117 | return this.fixTypeEverywhereTyped(
|
| 118 | "ChunkHeightAndBiomeFix",
|
| 119 | oldChunkType,
|
| 120 | chunkType,
|
| 121 | chunk -> chunk.updateTyped(
|
| 122 | levelFinder,
|
| 123 | levelType,
|
| 124 | level -> {
|
| 125 | Dynamic<?> tag = level.get(DSL.remainderFinder());
|
| 126 | OptionalDynamic<?> contextTag = chunk.get(DSL.remainderFinder()).get("__context");
|
| 127 | String dimension = contextTag.get("dimension").asString().result().orElse("");
|
| 128 | String generator = contextTag.get("generator").asString().result().orElse("");
|
| 129 | boolean isOverworld = "minecraft:overworld".equals(dimension);
|
| 130 | MutableBoolean wasIncreasedHeight = new MutableBoolean();
|
| 131 | int minSection = isOverworld ? -4 : 0;
|
| 132 | Dynamic<?>[] biomeContainers = getBiomeContainers(tag, isOverworld, minSection, wasIncreasedHeight);
|
| 133 | Dynamic<?> airContainer = makePalettedContainer(
|
| 134 | tag.createList(Stream.of(tag.createMap(ImmutableMap.of(tag.createString("Name"), tag.createString("minecraft:air")))))
|
| 135 | );
|
| 136 | Set<String> blocksInChunk = Sets.newHashSet();
|
| 137 | MutableObject<Supplier<ChunkProtoTickListFix.PoorMansPalettedContainer>> bedrockSectionBlocks = new MutableObject<>(() -> null);
|
| 138 | level = level.updateTyped(sectionsFinder, sectionsType, sections -> {
|
| 139 | IntSet doneSections = new IntOpenHashSet();
|
| 140 | Dynamic<?> dynamic = (Dynamic<?>)sections.write().result().orElseThrow(() -> new IllegalStateException("Malformed Chunk.Level.Sections"));
|
| 141 | List<Dynamic<?>> sectionsList = dynamic.asStream().map(sectionx -> {
|
| 142 | int sectionYx = sectionx.get("Y").asInt(0);
|
| 143 | Dynamic<?> blockStatesContainer = DataFixUtils.orElse(sectionx.get("Palette").result().flatMap(palette -> {
|
| 144 | palette.asStream().map(blockState -> blockState.get("Name").asString("minecraft:air")).forEach(blocksInChunk::add);
|
| 145 | return sectionx.get("BlockStates").result().map(blockStates -> makeOptimizedPalettedContainer(palette, (Dynamic<?>)blockStates));
|
| 146 | }), airContainer);
|
| 147 | Dynamic<?> result = sectionx;
|
| 148 | int sectionYIndex = sectionYx - minSection;
|
| 149 | if (sectionYIndex >= 0 && sectionYIndex < biomeContainers.length) {
|
| 150 | result = result.set("biomes", biomeContainers[sectionYIndex]);
|
| 151 | }
|
| 152 |
|
| 153 | doneSections.add(sectionYx);
|
| 154 | if (sectionx.get("Y").asInt(Integer.MAX_VALUE) == 0) {
|
| 155 | bedrockSectionBlocks.setValue(() -> {
|
| 156 | List<? extends Dynamic<?>> palette = blockStatesContainer.get("palette").asList(Function.identity());
|
| 157 | long[] data = blockStatesContainer.get("data").asLongStream().toArray();
|
| 158 | return new ChunkProtoTickListFix.PoorMansPalettedContainer(palette, data);
|
| 159 | });
|
| 160 | }
|
| 161 |
|
| 162 | return result.set("block_states", blockStatesContainer).remove("Palette").remove("BlockStates");
|
| 163 | }).collect(Collectors.toCollection(ArrayList::new));
|
| 164 |
|
| 165 | for (int sectionIndex = 0; sectionIndex < biomeContainers.length; sectionIndex++) {
|
| 166 | int sectionY = sectionIndex + minSection;
|
| 167 | if (doneSections.add(sectionY)) {
|
| 168 | Dynamic<?> section = tag.createMap(Map.of(tag.createString("Y"), tag.createInt(sectionY)));
|
| 169 | section = section.set("block_states", airContainer);
|
| 170 | section = section.set("biomes", biomeContainers[sectionIndex]);
|
| 171 | sectionsList.add(section);
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | return Util.readTypedOrThrow(sectionsType, tag.createList(sectionsList.stream()));
|
| 176 | });
|
| 177 | return level.update(DSL.remainderFinder(), chunkTag -> {
|
| 178 | if (isOverworld) {
|
| 179 | chunkTag = this.predictChunkStatusBeforeSurface(chunkTag, blocksInChunk);
|
| 180 | }
|
| 181 |
|
| 182 | return updateChunkTag(chunkTag, isOverworld, wasIncreasedHeight.booleanValue(), "minecraft:noise".equals(generator), bedrockSectionBlocks.get());
|
| 183 | });
|
| 184 | }
|
| 185 | )
|
| 186 | );
|
| 187 | }
|
| 188 |
|
| 189 | private Dynamic<?> predictChunkStatusBeforeSurface(final Dynamic<?> chunkTag, final Set<String> blocksInChunk) {
|
| 190 | return chunkTag.update("Status", statusDynamic -> {
|
| 191 | String status = statusDynamic.asString("empty");
|
| 192 | if (STATUS_IS_OR_AFTER_SURFACE.contains(status)) {
|
| 193 | return statusDynamic;
|
| 194 | } else {
|
| 195 | blocksInChunk.remove("minecraft:air");
|
| 196 | boolean hasNonAirBlocks = !blocksInChunk.isEmpty();
|
| 197 | blocksInChunk.removeAll(BLOCKS_BEFORE_FEATURE_STATUS);
|
| 198 | boolean hasFeatureBlocks = !blocksInChunk.isEmpty();
|
| 199 | if (hasFeatureBlocks) {
|
| 200 | return statusDynamic.createString("liquid_carvers");
|
| 201 | } else if ("noise".equals(status) || hasNonAirBlocks) {
|
| 202 | return statusDynamic.createString("noise");
|
| 203 | } else {
|
| 204 | return "biomes".equals(status) ? statusDynamic.createString("structure_references") : statusDynamic;
|
| 205 | }
|
| 206 | }
|
| 207 | });
|
| 208 | }
|
| 209 |
|
| 210 | private static Dynamic<?>[] getBiomeContainers(
|
| 211 | final Dynamic<?> tag, final boolean increaseHeight, final int minSection, final MutableBoolean wasIncreasedHeight
|
| 212 | ) {
|
| 213 | Dynamic<?>[] biomeContainers = new Dynamic[increaseHeight ? 24 : 16];
|
| 214 | int[] oldBiomes = tag.get("Biomes").asIntStreamOpt().result().map(IntStream::toArray).orElse(null);
|
| 215 | if (oldBiomes != null && oldBiomes.length == 1536) {
|
| 216 | wasIncreasedHeight.setValue(true);
|
| 217 |
|
| 218 | for (int sectionYIndex = 0; sectionYIndex < 24; sectionYIndex++) {
|
| 219 | int finalSectionYIndex = sectionYIndex;
|
| 220 | biomeContainers[sectionYIndex] = makeBiomeContainer(tag, ix -> getOldBiome(oldBiomes, finalSectionYIndex * 64 + ix));
|
| 221 | }
|
| 222 | } else if (oldBiomes != null && oldBiomes.length == 1024) {
|
| 223 | for (int sectionY = 0; sectionY < 16; sectionY++) {
|
| 224 | int sectionYIndex = sectionY - minSection;
|
| 225 | int finalSectionY = sectionY;
|
| 226 | biomeContainers[sectionYIndex] = makeBiomeContainer(tag, ix -> getOldBiome(oldBiomes, finalSectionY * 64 + ix));
|
| 227 | }
|
| 228 |
|
| 229 | if (increaseHeight) {
|
| 230 | Dynamic<?> belowWorldBiomes = makeBiomeContainer(tag, ix -> getOldBiome(oldBiomes, ix % 16));
|
| 231 | Dynamic<?> aboveWorldBiomes = makeBiomeContainer(tag, ix -> getOldBiome(oldBiomes, ix % 16 + 1008));
|
| 232 |
|
| 233 | for (int i = 0; i < 4; i++) {
|
| 234 | biomeContainers[i] = belowWorldBiomes;
|
| 235 | }
|
| 236 |
|
| 237 | for (int i = 20; i < 24; i++) {
|
| 238 | biomeContainers[i] = aboveWorldBiomes;
|
| 239 | }
|
| 240 | }
|
| 241 | } else {
|
| 242 | Arrays.fill(biomeContainers, makePalettedContainer(tag.createList(Stream.of(tag.createString("minecraft:plains")))));
|
| 243 | }
|
| 244 |
|
| 245 | return biomeContainers;
|
| 246 | }
|
| 247 |
|
| 248 | private static int getOldBiome(final int[] oldBiomes, final int index) {
|
| 249 | return oldBiomes[index] & 0xFF;
|
| 250 | }
|
| 251 |
|
| 252 | private static Dynamic<?> updateChunkTag(
|
| 253 | Dynamic<?> chunkTag,
|
| 254 | final boolean isOverworld,
|
| 255 | final boolean wasIncreasedHeight,
|
| 256 | final boolean needsBlendingAndUpgrade,
|
| 257 | final Supplier<ChunkProtoTickListFix.@Nullable PoorMansPalettedContainer> bedrockSectionBlocks
|
| 258 | ) {
|
| 259 | chunkTag = chunkTag.remove("Biomes");
|
| 260 | if (!isOverworld) {
|
| 261 | return updateCarvingMasks(chunkTag, 16, 0);
|
| 262 | }
|
| 263 |
|
| 264 | if (wasIncreasedHeight) {
|
| 265 | return updateCarvingMasks(chunkTag, 24, 0);
|
| 266 | }
|
| 267 |
|
| 268 | chunkTag = updateHeightmaps(chunkTag);
|
| 269 | chunkTag = addPaddingEntries(chunkTag, "LiquidsToBeTicked");
|
| 270 | chunkTag = addPaddingEntries(chunkTag, "PostProcessing");
|
| 271 | chunkTag = addPaddingEntries(chunkTag, "ToBeTicked");
|
| 272 | chunkTag = updateCarvingMasks(chunkTag, 24, 4);
|
| 273 | chunkTag = chunkTag.update("UpgradeData", ChunkHeightAndBiomeFix::shiftUpgradeData);
|
| 274 | if (!needsBlendingAndUpgrade) {
|
| 275 | return chunkTag;
|
| 276 | }
|
| 277 |
|
| 278 | Optional<? extends Dynamic<?>> statusOpt = chunkTag.get("Status").result();
|
| 279 | if (statusOpt.isPresent()) {
|
| 280 | Dynamic<?> status = (Dynamic<?>)statusOpt.get();
|
| 281 | String lastStatus = status.asString("");
|
| 282 | if (!"empty".equals(lastStatus)) {
|
| 283 | chunkTag = chunkTag.set(
|
| 284 | "blending_data",
|
| 285 | chunkTag.createMap(ImmutableMap.of(chunkTag.createString("old_noise"), chunkTag.createBoolean(STATUS_IS_OR_AFTER_NOISE.contains(lastStatus))))
|
| 286 | );
|
| 287 | if (!SharedConstants.DEBUG_DISABLE_BELOW_ZERO_RETROGENERATION) {
|
| 288 | ChunkProtoTickListFix.PoorMansPalettedContainer poorMansPalettedContainer = bedrockSectionBlocks.get();
|
| 289 | if (poorMansPalettedContainer != null) {
|
| 290 | BitSet missingBedrock = new BitSet(256);
|
| 291 | boolean hasAnyBedrock = lastStatus.equals("noise");
|
| 292 |
|
| 293 | for (int z = 0; z < 16; z++) {
|
| 294 | for (int x = 0; x < 16; x++) {
|
| 295 | Dynamic<?> blockState = poorMansPalettedContainer.get(x, 0, z);
|
| 296 | boolean isBedrock = blockState != null && "minecraft:bedrock".equals(blockState.get("Name").asString(""));
|
| 297 | boolean isAir = blockState != null && "minecraft:air".equals(blockState.get("Name").asString(""));
|
| 298 | if (isAir) {
|
| 299 | missingBedrock.set(z * 16 + x);
|
| 300 | }
|
| 301 |
|
| 302 | hasAnyBedrock |= isBedrock;
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | if (hasAnyBedrock && missingBedrock.cardinality() != missingBedrock.size()) {
|
| 307 | Dynamic<?> targetStatus = "full".equals(lastStatus) ? chunkTag.createString("heightmaps") : status;
|
| 308 | chunkTag = chunkTag.set(
|
| 309 | "below_zero_retrogen",
|
| 310 | chunkTag.createMap(
|
| 311 | ImmutableMap.of(
|
| 312 | chunkTag.createString("target_status"),
|
| 313 | targetStatus,
|
| 314 | chunkTag.createString("missing_bedrock"),
|
| 315 | chunkTag.createLongList(LongStream.of(missingBedrock.toLongArray()))
|
| 316 | )
|
| 317 | )
|
| 318 | );
|
| 319 | chunkTag = chunkTag.set("Status", chunkTag.createString("empty"));
|
| 320 | }
|
| 321 |
|
| 322 | chunkTag = chunkTag.set("isLightOn", chunkTag.createBoolean(false));
|
| 323 | }
|
| 324 | }
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | return chunkTag;
|
| 329 | }
|
| 330 |
|
| 331 | private static <T> Dynamic<T> shiftUpgradeData(final Dynamic<T> upgradeData) {
|
| 332 | return upgradeData.update("Indices", indices -> {
|
| 333 | Map<Dynamic<?>, Dynamic<?>> shiftedIndices = new HashMap<>();
|
| 334 | indices.getMapValues().ifSuccess(entries -> entries.forEach((index, data) -> {
|
| 335 | try {
|
| 336 | index.asString().result().map(Integer::parseInt).ifPresent(i -> {
|
| 337 | int shiftedIndex = i - -4;
|
| 338 | shiftedIndices.put(index.createString(Integer.toString(shiftedIndex)), (Dynamic<?>)data);
|
| 339 | });
|
| 340 | } catch (NumberFormatException var4) {
|
| 341 | }
|
| 342 | }));
|
| 343 | return indices.createMap(shiftedIndices);
|
| 344 | });
|
| 345 | }
|
| 346 |
|
| 347 | private static Dynamic<?> updateCarvingMasks(final Dynamic<?> chunkTag, final int sectionCount, final int addedSectionsBelow) {
|
| 348 | Dynamic<?> carvingMasks = chunkTag.get("CarvingMasks").orElseEmptyMap();
|
| 349 | carvingMasks = carvingMasks.updateMapValues(pair -> {
|
| 350 | long[] oldValues = BitSet.valueOf(pair.getSecond().asByteBuffer().array()).toLongArray();
|
| 351 | long[] newValues = new long[64 * sectionCount];
|
| 352 | System.arraycopy(oldValues, 0, newValues, 64 * addedSectionsBelow, oldValues.length);
|
| 353 | return Pair.of(pair.getFirst(), chunkTag.createLongList(LongStream.of(newValues)));
|
| 354 | });
|
| 355 | return chunkTag.set("CarvingMasks", carvingMasks);
|
| 356 | }
|
| 357 |
|
| 358 | private static Dynamic<?> addPaddingEntries(final Dynamic<?> chunkTag, final String key) {
|
| 359 | List<Dynamic<?>> list = chunkTag.get(key).orElseEmptyList().asStream().collect(Collectors.toCollection(ArrayList::new));
|
| 360 | if (list.size() == 24) {
|
| 361 | return chunkTag;
|
| 362 | }
|
| 363 |
|
| 364 | Dynamic<?> emptyList = chunkTag.emptyList();
|
| 365 |
|
| 366 | for (int i = 0; i < 4; i++) {
|
| 367 | list.add(0, emptyList);
|
| 368 | list.add(emptyList);
|
| 369 | }
|
| 370 |
|
| 371 | return chunkTag.set(key, chunkTag.createList(list.stream()));
|
| 372 | }
|
| 373 |
|
| 374 | private static Dynamic<?> updateHeightmaps(final Dynamic<?> chunkTag) {
|
| 375 | return chunkTag.update("Heightmaps", heightmapTag -> {
|
| 376 | for (String heightmapType : HEIGHTMAP_TYPES) {
|
| 377 | heightmapTag = heightmapTag.update(heightmapType, ChunkHeightAndBiomeFix::getFixedHeightmap);
|
| 378 | }
|
| 379 |
|
| 380 | return heightmapTag;
|
| 381 | });
|
| 382 | }
|
| 383 |
|
| 384 | private static Dynamic<?> getFixedHeightmap(final Dynamic<?> tag) {
|
| 385 | return tag.createLongList(tag.asLongStream().map(value -> {
|
| 386 | long newValue = 0L;
|
| 387 |
|
| 388 | for (int bitIndex = 0; bitIndex + 9 <= 64; bitIndex += 9) {
|
| 389 | long oldHeight = value >> bitIndex & 511L;
|
| 390 | long newHeight;
|
| 391 | if (oldHeight == 0L) {
|
| 392 | newHeight = 0L;
|
| 393 | } else {
|
| 394 | newHeight = Math.min(oldHeight + 64L, 511L);
|
| 395 | }
|
| 396 |
|
| 397 | newValue |= newHeight << bitIndex;
|
| 398 | }
|
| 399 |
|
| 400 | return newValue;
|
| 401 | }));
|
| 402 | }
|
| 403 |
|
| 404 | private static Dynamic<?> makeBiomeContainer(final Dynamic<?> tag, final Int2IntFunction sourceStorage) {
|
| 405 | Int2IntMap idMap = new Int2IntLinkedOpenHashMap();
|
| 406 |
|
| 407 | for (int i = 0; i < 64; i++) {
|
| 408 | int biomeId = sourceStorage.applyAsInt(i);
|
| 409 | if (!idMap.containsKey(biomeId)) {
|
| 410 | idMap.put(biomeId, idMap.size());
|
| 411 | }
|
| 412 | }
|
| 413 |
|
| 414 | Dynamic<?> palette = tag.createList(
|
| 415 | idMap.keySet().stream().map(biomeId1 -> tag.createString(BIOMES_BY_ID.getOrDefault(biomeId1.intValue(), "minecraft:plains")))
|
| 416 | );
|
| 417 | int bits = ceillog2(idMap.size());
|
| 418 | if (bits == 0) {
|
| 419 | return makePalettedContainer(palette);
|
| 420 | }
|
| 421 |
|
| 422 | int valuesPerLong = 64 / bits;
|
| 423 | int requiredLength = (64 + valuesPerLong - 1) / valuesPerLong;
|
| 424 | long[] bitStorage = new long[requiredLength];
|
| 425 | int cellIndex = 0;
|
| 426 | int bitIndex = 0;
|
| 427 |
|
| 428 | for (int i = 0; i < 64; i++) {
|
| 429 | int biomeId = sourceStorage.applyAsInt(i);
|
| 430 | bitStorage[cellIndex] |= (long)idMap.get(biomeId) << bitIndex;
|
| 431 | bitIndex += bits;
|
| 432 | if (bitIndex + bits > 64) {
|
| 433 | cellIndex++;
|
| 434 | bitIndex = 0;
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | Dynamic<?> storage = tag.createLongList(Arrays.stream(bitStorage));
|
| 439 | return makePalettedContainer(palette, storage);
|
| 440 | }
|
| 441 |
|
| 442 | private static Dynamic<?> makePalettedContainer(final Dynamic<?> palette) {
|
| 443 | return palette.createMap(ImmutableMap.of(palette.createString("palette"), palette));
|
| 444 | }
|
| 445 |
|
| 446 | private static Dynamic<?> makePalettedContainer(final Dynamic<?> palette, final Dynamic<?> storage) {
|
| 447 | return palette.createMap(ImmutableMap.of(palette.createString("palette"), palette, palette.createString("data"), storage));
|
| 448 | }
|
| 449 |
|
| 450 | private static Dynamic<?> makeOptimizedPalettedContainer(Dynamic<?> palette, final Dynamic<?> data) {
|
| 451 | List<Dynamic<?>> paletteList = palette.asStream().collect(Collectors.toCollection(ArrayList::new));
|
| 452 | if (paletteList.size() == 1) {
|
| 453 | return makePalettedContainer(palette);
|
| 454 | }
|
| 455 |
|
| 456 | palette = padPaletteEntries(palette, data, paletteList);
|
| 457 | return makePalettedContainer(palette, data);
|
| 458 | }
|
| 459 |
|
| 460 | private static Dynamic<?> padPaletteEntries(final Dynamic<?> palette, final Dynamic<?> data, final List<Dynamic<?>> paletteList) {
|
| 461 | long dataSizeInBits = data.asLongStream().count() * 64L;
|
| 462 | long estimatedBitsPerBlock = dataSizeInBits / 4096L;
|
| 463 | int paletteSize = paletteList.size();
|
| 464 | int expectedBitsPerBlock = ceillog2(paletteSize);
|
| 465 | if (estimatedBitsPerBlock <= expectedBitsPerBlock) {
|
| 466 | return palette;
|
| 467 | }
|
| 468 |
|
| 469 | Dynamic<?> airPalleteEntry = palette.createMap(ImmutableMap.of(palette.createString("Name"), palette.createString("minecraft:air")));
|
| 470 | int minimumPaletteSizeToMatchData = (1 << (int)(estimatedBitsPerBlock - 1L)) + 1;
|
| 471 | int additionalPaletteEntries = minimumPaletteSizeToMatchData - paletteSize;
|
| 472 |
|
| 473 | for (int i = 0; i < additionalPaletteEntries; i++) {
|
| 474 | paletteList.add(airPalleteEntry);
|
| 475 | }
|
| 476 |
|
| 477 | return palette.createList(paletteList.stream());
|
| 478 | }
|
| 479 |
|
| 480 | public static int ceillog2(final int input) {
|
| 481 | return input == 0 ? 0 : (int)Math.ceil(Math.log(input) / Math.log(2.0));
|
| 482 | }
|
| 483 |
|
| 484 | static {
|
| 485 | BIOMES_BY_ID.put(0, "minecraft:ocean");
|
| 486 | BIOMES_BY_ID.put(1, "minecraft:plains");
|
| 487 | BIOMES_BY_ID.put(2, "minecraft:desert");
|
| 488 | BIOMES_BY_ID.put(3, "minecraft:mountains");
|
| 489 | BIOMES_BY_ID.put(4, "minecraft:forest");
|
| 490 | BIOMES_BY_ID.put(5, "minecraft:taiga");
|
| 491 | BIOMES_BY_ID.put(6, "minecraft:swamp");
|
| 492 | BIOMES_BY_ID.put(7, "minecraft:river");
|
| 493 | BIOMES_BY_ID.put(8, "minecraft:nether_wastes");
|
| 494 | BIOMES_BY_ID.put(9, "minecraft:the_end");
|
| 495 | BIOMES_BY_ID.put(10, "minecraft:frozen_ocean");
|
| 496 | BIOMES_BY_ID.put(11, "minecraft:frozen_river");
|
| 497 | BIOMES_BY_ID.put(12, "minecraft:snowy_tundra");
|
| 498 | BIOMES_BY_ID.put(13, "minecraft:snowy_mountains");
|
| 499 | BIOMES_BY_ID.put(14, "minecraft:mushroom_fields");
|
| 500 | BIOMES_BY_ID.put(15, "minecraft:mushroom_field_shore");
|
| 501 | BIOMES_BY_ID.put(16, "minecraft:beach");
|
| 502 | BIOMES_BY_ID.put(17, "minecraft:desert_hills");
|
| 503 | BIOMES_BY_ID.put(18, "minecraft:wooded_hills");
|
| 504 | BIOMES_BY_ID.put(19, "minecraft:taiga_hills");
|
| 505 | BIOMES_BY_ID.put(20, "minecraft:mountain_edge");
|
| 506 | BIOMES_BY_ID.put(21, "minecraft:jungle");
|
| 507 | BIOMES_BY_ID.put(22, "minecraft:jungle_hills");
|
| 508 | BIOMES_BY_ID.put(23, "minecraft:jungle_edge");
|
| 509 | BIOMES_BY_ID.put(24, "minecraft:deep_ocean");
|
| 510 | BIOMES_BY_ID.put(25, "minecraft:stone_shore");
|
| 511 | BIOMES_BY_ID.put(26, "minecraft:snowy_beach");
|
| 512 | BIOMES_BY_ID.put(27, "minecraft:birch_forest");
|
| 513 | BIOMES_BY_ID.put(28, "minecraft:birch_forest_hills");
|
| 514 | BIOMES_BY_ID.put(29, "minecraft:dark_forest");
|
| 515 | BIOMES_BY_ID.put(30, "minecraft:snowy_taiga");
|
| 516 | BIOMES_BY_ID.put(31, "minecraft:snowy_taiga_hills");
|
| 517 | BIOMES_BY_ID.put(32, "minecraft:giant_tree_taiga");
|
| 518 | BIOMES_BY_ID.put(33, "minecraft:giant_tree_taiga_hills");
|
| 519 | BIOMES_BY_ID.put(34, "minecraft:wooded_mountains");
|
| 520 | BIOMES_BY_ID.put(35, "minecraft:savanna");
|
| 521 | BIOMES_BY_ID.put(36, "minecraft:savanna_plateau");
|
| 522 | BIOMES_BY_ID.put(37, "minecraft:badlands");
|
| 523 | BIOMES_BY_ID.put(38, "minecraft:wooded_badlands_plateau");
|
| 524 | BIOMES_BY_ID.put(39, "minecraft:badlands_plateau");
|
| 525 | BIOMES_BY_ID.put(40, "minecraft:small_end_islands");
|
| 526 | BIOMES_BY_ID.put(41, "minecraft:end_midlands");
|
| 527 | BIOMES_BY_ID.put(42, "minecraft:end_highlands");
|
| 528 | BIOMES_BY_ID.put(43, "minecraft:end_barrens");
|
| 529 | BIOMES_BY_ID.put(44, "minecraft:warm_ocean");
|
| 530 | BIOMES_BY_ID.put(45, "minecraft:lukewarm_ocean");
|
| 531 | BIOMES_BY_ID.put(46, "minecraft:cold_ocean");
|
| 532 | BIOMES_BY_ID.put(47, "minecraft:deep_warm_ocean");
|
| 533 | BIOMES_BY_ID.put(48, "minecraft:deep_lukewarm_ocean");
|
| 534 | BIOMES_BY_ID.put(49, "minecraft:deep_cold_ocean");
|
| 535 | BIOMES_BY_ID.put(50, "minecraft:deep_frozen_ocean");
|
| 536 | BIOMES_BY_ID.put(127, "minecraft:the_void");
|
| 537 | BIOMES_BY_ID.put(129, "minecraft:sunflower_plains");
|
| 538 | BIOMES_BY_ID.put(130, "minecraft:desert_lakes");
|
| 539 | BIOMES_BY_ID.put(131, "minecraft:gravelly_mountains");
|
| 540 | BIOMES_BY_ID.put(132, "minecraft:flower_forest");
|
| 541 | BIOMES_BY_ID.put(133, "minecraft:taiga_mountains");
|
| 542 | BIOMES_BY_ID.put(134, "minecraft:swamp_hills");
|
| 543 | BIOMES_BY_ID.put(140, "minecraft:ice_spikes");
|
| 544 | BIOMES_BY_ID.put(149, "minecraft:modified_jungle");
|
| 545 | BIOMES_BY_ID.put(151, "minecraft:modified_jungle_edge");
|
| 546 | BIOMES_BY_ID.put(155, "minecraft:tall_birch_forest");
|
| 547 | BIOMES_BY_ID.put(156, "minecraft:tall_birch_hills");
|
| 548 | BIOMES_BY_ID.put(157, "minecraft:dark_forest_hills");
|
| 549 | BIOMES_BY_ID.put(158, "minecraft:snowy_taiga_mountains");
|
| 550 | BIOMES_BY_ID.put(160, "minecraft:giant_spruce_taiga");
|
| 551 | BIOMES_BY_ID.put(161, "minecraft:giant_spruce_taiga_hills");
|
| 552 | BIOMES_BY_ID.put(162, "minecraft:modified_gravelly_mountains");
|
| 553 | BIOMES_BY_ID.put(163, "minecraft:shattered_savanna");
|
| 554 | BIOMES_BY_ID.put(164, "minecraft:shattered_savanna_plateau");
|
| 555 | BIOMES_BY_ID.put(165, "minecraft:eroded_badlands");
|
| 556 | BIOMES_BY_ID.put(166, "minecraft:modified_wooded_badlands_plateau");
|
| 557 | BIOMES_BY_ID.put(167, "minecraft:modified_badlands_plateau");
|
| 558 | BIOMES_BY_ID.put(168, "minecraft:bamboo_jungle");
|
| 559 | BIOMES_BY_ID.put(169, "minecraft:bamboo_jungle_hills");
|
| 560 | BIOMES_BY_ID.put(170, "minecraft:soul_sand_valley");
|
| 561 | BIOMES_BY_ID.put(171, "minecraft:crimson_forest");
|
| 562 | BIOMES_BY_ID.put(172, "minecraft:warped_forest");
|
| 563 | BIOMES_BY_ID.put(173, "minecraft:basalt_deltas");
|
| 564 | BIOMES_BY_ID.put(174, "minecraft:dripstone_caves");
|
| 565 | BIOMES_BY_ID.put(175, "minecraft:lush_caves");
|
| 566 | BIOMES_BY_ID.put(177, "minecraft:meadow");
|
| 567 | BIOMES_BY_ID.put(178, "minecraft:grove");
|
| 568 | BIOMES_BY_ID.put(179, "minecraft:snowy_slopes");
|
| 569 | BIOMES_BY_ID.put(180, "minecraft:snowcapped_peaks");
|
| 570 | BIOMES_BY_ID.put(181, "minecraft:lofty_peaks");
|
| 571 | BIOMES_BY_ID.put(182, "minecraft:stony_peaks");
|
| 572 | }
|
| 573 | }
|
| 574 |
|