Skip to content

Commit

Permalink
chore: update to latest minestom (dynamic registry)
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed May 27, 2024
1 parent efa3acd commit ede1588
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
metadata.format.version = "1.1"

[versions]
minestom = "1_20_5-d51c6c75e2"
minestom = "1_20_5-05a4bb77c3"
zstd = "1.5.5-3"
fastutil = "8.5.12"

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/hollowcube/polar/PolarLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.world.biomes.BiomeManager;
import net.minestom.server.world.biomes.VanillaBiome;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.world.biome.Biome;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -39,11 +39,11 @@
@SuppressWarnings("UnstableApiUsage")
public class PolarLoader implements IChunkLoader {
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
private static final BiomeManager BIOME_MANAGER = MinecraftServer.getBiomeManager();
private static final DynamicRegistry<Biome> BIOME_REGISTRY = MinecraftServer.getBiomeRegistry();
private static final ExceptionManager EXCEPTION_HANDLER = MinecraftServer.getExceptionManager();
static final Logger logger = LoggerFactory.getLogger(PolarLoader.class);

private static final int PLAINS_BIOME_ID = BIOME_MANAGER.getId(VanillaBiome.PLAINS);
private static final int PLAINS_BIOME_ID = BIOME_REGISTRY.getId(Biome.PLAINS);

private final Map<String, Integer> biomeReadCache = new ConcurrentHashMap<>();
private final Map<Integer, String> biomeWriteCache = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -200,7 +200,7 @@ private void loadSection(@NotNull PolarSection sectionData, @NotNull Section sec
var biomePalette = new int[rawBiomePalette.length];
for (int i = 0; i < rawBiomePalette.length; i++) {
biomePalette[i] = biomeReadCache.computeIfAbsent(rawBiomePalette[i], name -> {
var biomeId = BIOME_MANAGER.getId(worldAccess.getBiome(name));
var biomeId = BIOME_REGISTRY.getId(worldAccess.getBiome(name));
if (biomeId == -1) {
logger.error("Failed to find biome: {}", name);
biomeId = PLAINS_BIOME_ID;
Expand Down Expand Up @@ -288,10 +288,10 @@ public void unloadChunk(Chunk chunk) {
}

private void updateChunkData(@NotNull Short2ObjectMap<String> blockCache, @NotNull Chunk chunk) {
var dimension = chunk.getInstance().getDimensionType();
var dimension = chunk.getInstance().getCachedDimensionType();

var blockEntities = new ArrayList<PolarChunk.BlockEntity>();
var sections = new PolarSection[dimension.getHeight() / Chunk.CHUNK_SECTION_SIZE];
var sections = new PolarSection[dimension.height() / Chunk.CHUNK_SECTION_SIZE];
assert sections.length == chunk.getSections().size() : "World height mismatch";

var heightmaps = new int[PolarChunk.MAX_HEIGHTMAPS][];
Expand Down
47 changes: 29 additions & 18 deletions src/main/java/net/hollowcube/polar/PolarWorldAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.world.biomes.Biome;
import net.minestom.server.world.biomes.VanillaBiome;
import net.minestom.server.registry.DynamicRegistry;
import net.minestom.server.world.biome.Biome;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

/**
* Provides access to user world data for a {@link PolarLoader} to get and set user
* specific world data such as objects, as well as provides some relevant callbacks.
Expand All @@ -20,7 +21,8 @@
*/
@SuppressWarnings("UnstableApiUsage")
public interface PolarWorldAccess {
PolarWorldAccess DEFAULT = new PolarWorldAccess() {};
PolarWorldAccess DEFAULT = new PolarWorldAccess() {
};

/**
* Called when an instance is created from this chunk loader.
Expand All @@ -30,7 +32,8 @@ public interface PolarWorldAccess {
* @param instance The Minestom instance being created
* @param userData The saved user data, or null if none is present.
*/
default void loadWorldData(@NotNull Instance instance, @Nullable NetworkBuffer userData) {}
default void loadWorldData(@NotNull Instance instance, @Nullable NetworkBuffer userData) {
}

/**
* Called when an instance is being saved.
Expand All @@ -40,33 +43,38 @@ default void loadWorldData(@NotNull Instance instance, @Nullable NetworkBuffer u
* @param instance The Minestom instance being saved
* @param userData A buffer to write user data to save
*/
default void saveWorldData(@NotNull Instance instance, @NotNull NetworkBuffer userData) {}
default void saveWorldData(@NotNull Instance instance, @NotNull NetworkBuffer userData) {
}

/**
* Called when a chunk is created, just before it is added to the world.
* <br/><br/>
* Can be used to initialize the chunk based on saved user data in the world.
*
* @param chunk The Minestom chunk being created
* @param chunk The Minestom chunk being created
* @param userData The saved user data, or null if none is present
*/
default void loadChunkData(@NotNull Chunk chunk, @Nullable NetworkBuffer userData) {}
default void loadChunkData(@NotNull Chunk chunk, @Nullable NetworkBuffer userData) {
}

/**
* Called when a chunk is being saved.
* <br/><br/>
* Can be used to save user data in the chunk by writing it to the buffer.
*
* @param chunk The Minestom chunk being saved
* @param chunk The Minestom chunk being saved
* @param userData A buffer to write user data to save
*/
default void saveChunkData(@NotNull Chunk chunk, @NotNull NetworkBuffer userData) {}
default void saveChunkData(@NotNull Chunk chunk, @NotNull NetworkBuffer userData) {
}

@ApiStatus.Experimental
default void loadHeightmaps(@NotNull Chunk chunk, int[][] heightmaps) {}
default void loadHeightmaps(@NotNull Chunk chunk, int[][] heightmaps) {
}

@ApiStatus.Experimental
default void saveHeightmaps(@NotNull Chunk chunk, int[][] heightmaps) {}
default void saveHeightmaps(@NotNull Chunk chunk, int[][] heightmaps) {
}

/**
* Called when a chunk is being loaded by a {@link PolarLoader} to convert biome ids back to instances.
Expand All @@ -78,20 +86,23 @@ default void saveHeightmaps(@NotNull Chunk chunk, int[][] heightmaps) {}
* @param name The namespace ID of the biome, eg minecraft:plains
* @return The biome instance
*/
default @NotNull Biome getBiome(@NotNull String name) {
var biome = MinecraftServer.getBiomeManager().getByName(NamespaceID.from(name));
default @NotNull DynamicRegistry.Key<Biome> getBiome(@NotNull String name) {
var biomeRegistry = MinecraftServer.getBiomeRegistry();
var key = DynamicRegistry.Key.<Biome>of(name);
var biome = biomeRegistry.get(key);
if (biome == null) {
PolarLoader.logger.error("Failed to find biome: {}", name);
biome = VanillaBiome.PLAINS;
return Biome.PLAINS;
}
return biome;
return key;
}

default @NotNull String getBiomeName(int id) {
var biome = MinecraftServer.getBiomeManager().getById(id);
var biomeRegistry = MinecraftServer.getBiomeRegistry();
var biome = biomeRegistry.getKey(id);
if (biome == null) {
PolarLoader.logger.error("Failed to find biome: {}", id);
biome = VanillaBiome.PLAINS;
return Objects.requireNonNull(biomeRegistry.get(Biome.PLAINS)).name();
}
return biome.name();
}
Expand Down

0 comments on commit ede1588

Please sign in to comment.