Skip to content

Commit

Permalink
feat: support reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Bloeckchengrafik committed Jan 3, 2025
1 parent 35bdc99 commit 7711e67
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
import com.dfsek.terra.minestom.entity.MinestomEntityType;
import com.dfsek.terra.minestom.item.MinestomItemHandle;

import com.dfsek.terra.minestom.world.MinestomChunkGeneratorWrapper;
import com.dfsek.terra.minestom.world.MinestomWorldHandle;

import net.minestom.server.MinecraftServer;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;


public final class MinestomPlatform extends AbstractPlatform {
private static final Logger LOGGER = LoggerFactory.getLogger(MinestomPlatform.class);
private static MinestomPlatform INSTANCE = null;
private final MinestomWorldHandle worldHandle = new MinestomWorldHandle();
private final MinestomItemHandle itemHandle = new MinestomItemHandle();
Expand All @@ -45,7 +51,20 @@ public void register(TypeRegistry registry) {

@Override
public boolean reload() {
return false;
getTerraConfig().load(this);
getRawConfigRegistry().clear();
boolean succeed = getRawConfigRegistry().loadAll(this);

MinecraftServer.getInstanceManager().getInstances().forEach(world -> {
if(world.generator() instanceof MinestomChunkGeneratorWrapper wrapper) {
getConfigRegistry().get(wrapper.getPack().getRegistryKey()).ifPresent(pack -> {
wrapper.setPack(pack);
LOGGER.info("Replaced pack in chunk generator for instance {}", world.getUniqueId());
});
}
});

return succeed;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package com.dfsek.terra.minestom;

import com.dfsek.terra.minestom.api.filter.ChunkFilter;
import com.dfsek.terra.minestom.api.filter.EvenChunkFilter;
import com.dfsek.terra.minestom.api.filter.NoFeaturesFilter;
import com.dfsek.terra.minestom.api.filter.NoTerrainFilter;
import com.dfsek.terra.minestom.api.filter.SpecificChunkFilter;
import com.dfsek.terra.minestom.api.filter.XFilter;
import com.dfsek.terra.minestom.api.filter.ZFilter;
import com.dfsek.terra.minestom.world.TerraMinestomWorld;
import com.dfsek.terra.minestom.world.TerraMinestomWorldBuilder;

import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.arguments.ArgumentLiteral;
import net.minestom.server.command.builder.arguments.number.ArgumentInteger;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.GameMode;
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
import net.minestom.server.event.player.PlayerSpawnEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.LightingChunk;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,11 +31,9 @@ public void createNewInstance() {
instance.setChunkSupplier(LightingChunk::new);
}

public void attachTerra(ChunkFilter filter) {
public void attachTerra() {
world = TerraMinestomWorldBuilder.from(instance)
.defaultPack()
// .seed(0)
.filtered(filter)
.attach();
}

Expand Down Expand Up @@ -117,7 +106,7 @@ public void bind() {
public static void main(String[] args) {
TerraMinestomExample example = new TerraMinestomExample();
example.createNewInstance();
example.attachTerra(null);
example.attachTerra();
example.preloadWorldAndMeasure();
example.addScheduler();
example.addListeners();
Expand All @@ -128,27 +117,13 @@ public static void main(String[] args) {
public class RegenerateCommand extends Command {
public RegenerateCommand() {
super("regenerate");

ArgumentInteger cx = new ArgumentInteger("cx");
ArgumentInteger cz = new ArgumentInteger("cz");

setDefaultExecutor((sender, context) -> regenerate(null));
addSyntax((sender, context) -> regenerate(new NoFeaturesFilter()), new ArgumentLiteral("noFeatures"));
addSyntax((sender, context) -> regenerate(new NoTerrainFilter()), new ArgumentLiteral("noTerrain"));
addSyntax((sender, context) -> regenerate(new EvenChunkFilter()), new ArgumentLiteral("evenChunks"));
addSyntax((sender, context) -> regenerate(new SpecificChunkFilter(context.get(cx), context.get(cz))), new ArgumentLiteral("chunk"), cx, cz);
addSyntax((sender, context) -> regenerate(new XFilter(context.get(cx))), new ArgumentLiteral("x"), cx);
addSyntax((sender, context) -> regenerate(new ZFilter(context.get(cz))), new ArgumentLiteral("z"), cz);
setDefaultExecutor((sender, context) -> regenerate());
}

private void regenerate(@Nullable ChunkFilter filter) {
if (filter == null) {
instance.sendMessage(Component.text("Regenerating world without filter "));
} else {
instance.sendMessage(Component.text("Regenerating world with filter " + filter.getClass().getSimpleName()));
}
private void regenerate() {
instance.sendMessage(Component.text("Regenerating world"));
createNewInstance();
attachTerra(filter);
attachTerra();
preloadWorldAndMeasure();
MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player ->
player.setInstance(instance, new Pos(0, 100, 0))
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.dfsek.terra.minestom.world;

import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;

import com.dfsek.terra.api.world.chunk.generation.stage.GenerationStage;
import com.dfsek.terra.minestom.api.filter.ChunkFilter;
import com.dfsek.terra.minestom.chunk.CachedChunk;
import com.dfsek.terra.minestom.chunk.GeneratedChunkCache;

Expand All @@ -15,14 +15,14 @@

public class MinestomChunkGeneratorWrapper implements Generator {
private final GeneratedChunkCache cache;
private final ChunkGenerator generator;
private ChunkGenerator generator;
private final TerraMinestomWorld world;
private final ChunkFilter filter;
private ConfigPack pack;

public MinestomChunkGeneratorWrapper(ChunkGenerator generator, TerraMinestomWorld world, ChunkFilter filter) {
public MinestomChunkGeneratorWrapper(ChunkGenerator generator, TerraMinestomWorld world, ConfigPack pack) {
this.generator = generator;
this.world = world;
this.filter = filter;
this.pack = pack;
this.cache = new GeneratedChunkCache(world.getDimensionType(), generator, world);
}

Expand All @@ -36,18 +36,24 @@ public void generate(@NotNull GenerationUnit unit) {
int x = start.chunkX();
int z = start.chunkZ();
CachedChunk chunk = cache.at(x, z);
if(filter == null || filter.shouldPlaceTerrain(x, z))
chunk.writeRelative(unit.modifier());

if(filter == null || filter.shouldPlaceFeatures(x, z)) {
unit.fork(setter -> {
MinestomProtoWorld protoWorld = new MinestomProtoWorld(cache, x, z, world, setter);

for(GenerationStage stage : world.getPack().getStages()) {
stage.populate(protoWorld);
}
});
}
chunk.writeRelative(unit.modifier());

unit.fork(setter -> {
MinestomProtoWorld protoWorld = new MinestomProtoWorld(cache, x, z, world, setter);

for(GenerationStage stage : world.getPack().getStages()) {
stage.populate(protoWorld);
}
});
}

public ConfigPack getPack() {
return pack;
}

public void setPack(ConfigPack pack) {
this.pack = pack;
this.generator = pack.getGeneratorProvider().newInstance(pack);
}

public void displayStats() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.dfsek.terra.minestom.api.BlockEntityFactory;
import com.dfsek.terra.minestom.api.EntityFactory;
import com.dfsek.terra.minestom.block.MinestomBlockState;
import com.dfsek.terra.minestom.api.filter.ChunkFilter;
import com.dfsek.terra.minestom.entity.MinestomEntity;

import net.minestom.server.MinecraftServer;
Expand All @@ -43,7 +42,6 @@ public TerraMinestomWorld(
ConfigPack pack,
long seed,
EntityFactory entityFactory,
ChunkFilter filter,
BlockEntityFactory blockEntityFactory
) {
this.instance = instance;
Expand All @@ -56,7 +54,7 @@ public TerraMinestomWorld(
this.wrapper = new MinestomChunkGeneratorWrapper(
pack.getGeneratorProvider().newInstance(pack),
this,
filter
pack
);
this.entityFactory = entityFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import com.dfsek.terra.minestom.MinestomPlatform;
import com.dfsek.terra.minestom.api.BlockEntityFactory;
import com.dfsek.terra.minestom.api.EntityFactory;
import com.dfsek.terra.minestom.api.filter.ChunkFilter;
import com.dfsek.terra.minestom.block.DefaultBlockEntityFactory;
import com.dfsek.terra.minestom.entity.DefaultEntityFactory;
import net.minestom.server.MinecraftServer;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.Nullable;

import java.util.Random;
import java.util.function.Function;
Expand All @@ -23,7 +21,6 @@ public class TerraMinestomWorldBuilder {
private ConfigPack pack;
private long seed = new Random().nextLong();
private EntityFactory entityFactory = new DefaultEntityFactory();
private ChunkFilter filter;
private BlockEntityFactory blockEntityFactory = new DefaultBlockEntityFactory();

private TerraMinestomWorldBuilder(Instance instance) { this.instance = instance; }
Expand Down Expand Up @@ -75,12 +72,7 @@ public TerraMinestomWorldBuilder blockEntityFactory(BlockEntityFactory factory)
return this;
}

public TerraMinestomWorldBuilder filtered(@Nullable ChunkFilter filter) {
this.filter = filter;
return this;
}

public TerraMinestomWorld attach() {
return new TerraMinestomWorld(instance, pack, seed, entityFactory, filter, blockEntityFactory);
return new TerraMinestomWorld(instance, pack, seed, entityFactory, blockEntityFactory);
}
}

0 comments on commit 7711e67

Please sign in to comment.