String generatorName = worldNode.GENERATOR.getString();
VanillaGenerator generator = VanillaGenerators.byName(generatorName);
if (generator == null) {
throw new IllegalArgumentException("Invalid generator name for world '" + worldNode.getWorldName() + "': " + generatorName);
}
World world = ((Server) getEngine()).loadWorld(worldNode.getWorldName(), generator);
// Apply general settings
final ManagedMap data = world.getData();
data.put(VanillaData.GAMEMODE, GameMode.get(worldNode.GAMEMODE.getString()));
data.put(VanillaData.DIFFICULTY, Difficulty.get(worldNode.DIFFICULTY.getString()));
data.put(VanillaData.DIMENSION, Dimension.get(worldNode.SKY_TYPE.getString()));
world.addLightingManager(VanillaLighting.BLOCK_LIGHT);
world.addLightingManager(VanillaLighting.SKY_LIGHT);
// Add to worlds
worlds.add(world);
}
}
final int radius = VanillaConfiguration.SPAWN_RADIUS.getInt();
final int protectionRadius = VanillaConfiguration.SPAWN_PROTECTION_RADIUS.getInt();
if (worlds.isEmpty()) {
return;
}
//Register protection service used for spawn protection.
getEngine().getServiceManager().register(ProtectionService.class, new VanillaProtectionService(), this, ServiceManager.ServicePriority.Highest);
for (World world : worlds) {
// Keep spawn loaded
WorldConfigurationNode worldConfig = VanillaConfiguration.WORLDS.get(world);
final WorldGenerator generator = world.getGenerator();
boolean newWorld = world.getAge() <= 0;
if (worldConfig.LOADED_SPAWN.getBoolean() || newWorld) {
final Point spawn;
// Grab safe spawn if newly created world and generator is vanilla generator, else get old one.
if (newWorld && generator instanceof VanillaGenerator) {
spawn = ((VanillaGenerator) generator).getSafeSpawn(world);
world.setSpawnPoint(new Transform(spawn, Quaternionf.IDENTITY, Vector3f.ONE));
} else {
spawn = world.getSpawnPoint().getPosition();
}
// Start the protection for the spawn
((VanillaProtectionService) getEngine().getServiceManager().getRegistration(ProtectionService.class).getProvider()).addProtection(new SpawnProtection(world.getName() + " Spawn Protection", world, spawn, protectionRadius));
// Chunks coords of the spawn
int cx = spawn.getBlockX() >> Chunk.BLOCKS.BITS;
int cz = spawn.getBlockZ() >> Chunk.BLOCKS.BITS;
// Load or generate spawn area
int effectiveRadius = newWorld ? (2 * radius) : radius;
// Add observer to spawn to keep loaded if desired
if (worldConfig.LOADED_SPAWN.getBoolean()) {
@SuppressWarnings ("unchecked")
Entity e = world.createAndSpawnEntity(spawn, LoadOption.LOAD_GEN, NetworkComponent.class);
e.get(NetworkComponent.class).setObserver(new FlatIterator(cx, 0, cz, 16, effectiveRadius));
}
}
// Set the appropriate sky for the world type
if (generator instanceof NetherGenerator) {
world.add(NetherSky.class).setHasWeather(false);
} else if (generator instanceof TheEndGenerator) {
world.add(TheEndSky.class).setHasWeather(false);
} else if (generator instanceof SkylandsGenerator) {
world.add(NormalSky.class).setHasWeather(false);
} else {
world.add(NormalSky.class);
}
}
}