feat: implemented WorldFactory
This commit is contained in:
parent
9638f2eb6f
commit
32b51e8523
31
api/src/main/java/ru/dragonestia/msb3/api/world/World.java
Normal file
31
api/src/main/java/ru/dragonestia/msb3/api/world/World.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package ru.dragonestia.msb3.api.world;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
|
import net.minestom.server.instance.EntityTracker;
|
||||||
|
import net.minestom.server.instance.InstanceContainer;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
@Getter
|
||||||
|
public class World {
|
||||||
|
|
||||||
|
private final InstanceContainer instance;
|
||||||
|
|
||||||
|
World(InstanceContainer instance) {
|
||||||
|
this.instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void unload() {
|
||||||
|
if (!instance.isRegistered()) return;
|
||||||
|
|
||||||
|
log.info("Unloading world {} ({})", instance.getUniqueId(), instance.getChunkLoader().getClass().getSimpleName());
|
||||||
|
|
||||||
|
for (var player: instance.getPlayers()) {
|
||||||
|
if (player.isOnline()) player.kick("World forced to unload");
|
||||||
|
else instance.getEntityTracker().unregister(player, EntityTracker.Target.ENTITIES, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
MinecraftServer.getInstanceManager().unregisterInstance(instance);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
package ru.dragonestia.msb3.api.world;
|
||||||
|
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import net.minestom.server.instance.InstanceContainer;
|
||||||
|
import ru.dragonestia.msb3.api.world.factory.AnvilWorldFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
|
public abstract class WorldFactory {
|
||||||
|
|
||||||
|
public final CompletableFuture<World> createWorld() {
|
||||||
|
return CompletableFuture.supplyAsync(this::loadWorld)
|
||||||
|
.thenApply(world -> {
|
||||||
|
log.info("Created world {} ({})", world.getInstance().getUniqueId(), world.getInstance().getChunkLoader().getClass().getSimpleName());
|
||||||
|
return world;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final World createWorld(InstanceContainer instance) {
|
||||||
|
return new World(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract World loadWorld();
|
||||||
|
|
||||||
|
public static WorldFactory anvil(File worldDir) {
|
||||||
|
return new AnvilWorldFactory(worldDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package ru.dragonestia.msb3.api.world.factory;
|
||||||
|
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
|
import net.minestom.server.instance.anvil.AnvilLoader;
|
||||||
|
import ru.dragonestia.msb3.api.world.World;
|
||||||
|
import ru.dragonestia.msb3.api.world.WorldFactory;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public final class AnvilWorldFactory extends WorldFactory {
|
||||||
|
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
public AnvilWorldFactory(File file) {
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected World loadWorld() {
|
||||||
|
var instance = MinecraftServer.getInstanceManager().createInstanceContainer(new AnvilLoader(file.toPath()));
|
||||||
|
return createWorld(instance);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user