feat: actually do biome preloading

This commit is contained in:
Christian Bergschneider
2025-10-07 23:23:54 +02:00
parent 243c523b57
commit 234ff3e49c
5 changed files with 18 additions and 26 deletions
@@ -6,6 +6,8 @@ import com.dfsek.tectonic.api.loader.type.TypeLoader;
import com.dfsek.terra.minestom.api.BiomeFactory; import com.dfsek.terra.minestom.api.BiomeFactory;
import com.dfsek.terra.minestom.biome.MinestomUserDefinedBiomeFactory; import com.dfsek.terra.minestom.biome.MinestomUserDefinedBiomeFactory;
import com.dfsek.terra.minestom.biome.MinestomUserDefinedBiomePool;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.util.RGBLike; import net.kyori.adventure.util.RGBLike;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
@@ -50,14 +52,14 @@ public final class TerraMinestomPlatform extends AbstractPlatform {
private final ItemHandle itemHandle; private final ItemHandle itemHandle;
private final TypeLoader<PlatformBiome> biomeTypeLoader; private final TypeLoader<PlatformBiome> biomeTypeLoader;
private final ArrayList<BaseAddon> platformAddons = new ArrayList<>(List.of(new MinestomAddon(this))); private final ArrayList<BaseAddon> platformAddons = new ArrayList<>(List.of(new MinestomAddon(this)));
private final BiomeFactory biomeFactory; private final MinestomUserDefinedBiomePool biomePool;
public TerraMinestomPlatform(WorldHandle worldHandle, ItemHandle itemHandle, TypeLoader<PlatformBiome> biomeTypeLoader, public TerraMinestomPlatform(WorldHandle worldHandle, ItemHandle itemHandle, TypeLoader<PlatformBiome> biomeTypeLoader,
BiomeFactory biomeFactory, BaseAddon... extraAddons) { BiomeFactory biomeFactory, BaseAddon... extraAddons) {
this.worldHandle = worldHandle; this.worldHandle = worldHandle;
this.itemHandle = itemHandle; this.itemHandle = itemHandle;
this.biomeTypeLoader = biomeTypeLoader; this.biomeTypeLoader = biomeTypeLoader;
this.biomeFactory = biomeFactory; this.biomePool = new MinestomUserDefinedBiomePool(biomeFactory);
this.platformAddons.addAll(List.of(extraAddons)); this.platformAddons.addAll(List.of(extraAddons));
load(); load();
getEventManager().callEvent(new PlatformInitializationEvent()); getEventManager().callEvent(new PlatformInitializationEvent());
@@ -104,11 +106,7 @@ public final class TerraMinestomPlatform extends AbstractPlatform {
public void initializeRegistry() { public void initializeRegistry() {
getRawConfigRegistry() getRawConfigRegistry()
.forEach(pack -> { .forEach(pack -> biomePool.preloadBiomes(pack, pack.getBiomeProvider().getBiomes()));
pack.getBiomeProvider().getBiomes().forEach(biome -> {
});
});
} }
@Override @Override
@@ -141,7 +139,7 @@ public final class TerraMinestomPlatform extends AbstractPlatform {
} }
public TerraMinestomWorldBuilder worldBuilder(Instance instance) { public TerraMinestomWorldBuilder worldBuilder(Instance instance) {
return new TerraMinestomWorldBuilder(this, instance, biomeFactory); return new TerraMinestomWorldBuilder(this, instance, biomePool);
} }
public TerraMinestomWorldBuilder worldBuilder() { public TerraMinestomWorldBuilder worldBuilder() {
@@ -1,5 +1,7 @@
package com.dfsek.terra.minestom.api; package com.dfsek.terra.minestom.api;
import com.dfsek.terra.minestom.biome.MinestomUserDefinedBiomePool;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import java.util.Random; import java.util.Random;
@@ -8,7 +10,6 @@ import java.util.function.Function;
import com.dfsek.terra.api.config.ConfigPack; import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.minestom.TerraMinestomPlatform; import com.dfsek.terra.minestom.TerraMinestomPlatform;
import com.dfsek.terra.minestom.biome.MinestomUserDefinedBiomeFactory;
import com.dfsek.terra.minestom.block.DefaultBlockEntityFactory; import com.dfsek.terra.minestom.block.DefaultBlockEntityFactory;
import com.dfsek.terra.minestom.entity.DefaultEntityFactory; import com.dfsek.terra.minestom.entity.DefaultEntityFactory;
import com.dfsek.terra.minestom.world.TerraMinestomWorld; import com.dfsek.terra.minestom.world.TerraMinestomWorld;
@@ -21,17 +22,17 @@ import org.jspecify.annotations.NonNull;
public class TerraMinestomWorldBuilder { public class TerraMinestomWorldBuilder {
private final TerraMinestomPlatform platform; private final TerraMinestomPlatform platform;
private final Instance instance; private final Instance instance;
private final MinestomUserDefinedBiomePool biomePool;
private ConfigPack pack; private ConfigPack pack;
private long seed = new Random().nextLong(); private long seed = new Random().nextLong();
private EntityFactory entityFactory = new DefaultEntityFactory(); private EntityFactory entityFactory = new DefaultEntityFactory();
private BlockEntityFactory blockEntityFactory; private BlockEntityFactory blockEntityFactory;
private BiomeFactory biomeFactory;
public TerraMinestomWorldBuilder(TerraMinestomPlatform platform, Instance instance, BiomeFactory biomeFactory) { public TerraMinestomWorldBuilder(TerraMinestomPlatform platform, Instance instance, MinestomUserDefinedBiomePool biomePool) {
this.platform = platform; this.platform = platform;
this.instance = instance; this.instance = instance;
this.blockEntityFactory = new DefaultBlockEntityFactory(instance); this.blockEntityFactory = new DefaultBlockEntityFactory(instance);
this.biomeFactory = biomeFactory; this.biomePool = biomePool;
} }
public TerraMinestomWorldBuilder pack(ConfigPack pack) { public TerraMinestomWorldBuilder pack(ConfigPack pack) {
@@ -82,6 +83,6 @@ public class TerraMinestomWorldBuilder {
} }
public TerraMinestomWorld attach() { public TerraMinestomWorld attach() {
return new TerraMinestomWorld(platform, instance, pack, seed, entityFactory, blockEntityFactory, biomeFactory); return new TerraMinestomWorld(platform, instance, pack, seed, entityFactory, blockEntityFactory, biomePool);
} }
} }
@@ -12,14 +12,12 @@ public class MinestomUserDefinedBiomePool {
private final IdentityHashMap<Biome, UserDefinedBiome> biomes = new IdentityHashMap<>(); private final IdentityHashMap<Biome, UserDefinedBiome> biomes = new IdentityHashMap<>();
private final HashSet<String> createdBiomes = new HashSet<>(); private final HashSet<String> createdBiomes = new HashSet<>();
private final BiomeFactory factory; private final BiomeFactory factory;
private final ConfigPack configPack;
public MinestomUserDefinedBiomePool(ConfigPack configPack, BiomeFactory factory) { public MinestomUserDefinedBiomePool(BiomeFactory factory) {
this.configPack = configPack;
this.factory = factory; this.factory = factory;
} }
public UserDefinedBiome getBiome(Biome source) { public UserDefinedBiome getBiome(ConfigPack configPack, Biome source) {
UserDefinedBiome userDefinedBiome = biomes.get(source); UserDefinedBiome userDefinedBiome = biomes.get(source);
if(userDefinedBiome != null) return userDefinedBiome; if(userDefinedBiome != null) return userDefinedBiome;
userDefinedBiome = factory.create(configPack, source); userDefinedBiome = factory.create(configPack, source);
@@ -28,7 +26,7 @@ public class MinestomUserDefinedBiomePool {
return userDefinedBiome; return userDefinedBiome;
} }
public void preloadBiomes(Iterable<Biome> biomesToLoad) { public void preloadBiomes(ConfigPack configPack, Iterable<Biome> biomesToLoad) {
biomesToLoad biomesToLoad
.forEach(biome -> { .forEach(biome -> {
if(!this.createdBiomes.contains(biome.getID())) { if(!this.createdBiomes.contains(biome.getID())) {
@@ -40,7 +40,6 @@ public class MinestomChunkGeneratorWrapper implements Generator, GeneratorWrappe
this.biomePool = biomePool; this.biomePool = biomePool;
this.biomeProvider = pack.getBiomeProvider(); this.biomeProvider = pack.getBiomeProvider();
this.cache = new GeneratedChunkCache(world.getDimensionType(), generator, world, biomeProvider); this.cache = new GeneratedChunkCache(world.getDimensionType(), generator, world, biomeProvider);
preloadBiomes();
} }
public ChunkGenerator getGenerator() { public ChunkGenerator getGenerator() {
@@ -73,6 +72,7 @@ public class MinestomChunkGeneratorWrapper implements Generator, GeneratorWrappe
if(relativeX % 4 == 0 && relativeY % 4 == 0 && relativeZ % 4 == 0) { if(relativeX % 4 == 0 && relativeY % 4 == 0 && relativeZ % 4 == 0) {
UserDefinedBiome userDefinedBiome = biomePool.getBiome( UserDefinedBiome userDefinedBiome = biomePool.getBiome(
pack,
biomeProvider.getBiome(absoluteX, absoluteY, absoluteZ, world.getSeed()) biomeProvider.getBiome(absoluteX, absoluteY, absoluteZ, world.getSeed())
); );
@@ -101,11 +101,6 @@ public class MinestomChunkGeneratorWrapper implements Generator, GeneratorWrappe
this.pack = pack; this.pack = pack;
this.generator = pack.getGeneratorProvider().newInstance(pack); this.generator = pack.getGeneratorProvider().newInstance(pack);
this.biomePool.invalidate(); this.biomePool.invalidate();
preloadBiomes();
}
private void preloadBiomes() {
this.biomePool.preloadBiomes(world.getBiomeProvider().getBiomes());
} }
public void displayStats() { public void displayStats() {
@@ -44,7 +44,7 @@ public final class TerraMinestomWorld implements ServerWorld, WorldProperties {
long seed, long seed,
EntityFactory entityFactory, EntityFactory entityFactory,
BlockEntityFactory blockEntityFactory, BlockEntityFactory blockEntityFactory,
BiomeFactory factory MinestomUserDefinedBiomePool biomePool
) { ) {
this.instance = instance; this.instance = instance;
this.pack = pack; this.pack = pack;
@@ -57,7 +57,7 @@ public final class TerraMinestomWorld implements ServerWorld, WorldProperties {
pack.getGeneratorProvider().newInstance(pack), pack.getGeneratorProvider().newInstance(pack),
this, this,
pack, pack,
new MinestomUserDefinedBiomePool(pack, factory) biomePool
); );
this.entityFactory = entityFactory; this.entityFactory = entityFactory;