use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorldManager method loadWorld.
/**
* Loads a {@link World} for the world entry if possible.
*
* @param worldEntry the world entry
* @return the world, if found
*/
private Optional<World> loadWorld(@Nullable WorldLookupEntry worldEntry) {
if (worldEntry == null) {
return Optional.empty();
}
if (worldEntry.world != null) {
return Optional.of(worldEntry.world);
}
WorldConfigResult result;
try {
result = getOrCreateWorldConfig(worldEntry.properties.getWorldName());
} catch (IOException e) {
this.game.getLogger().error("Unable to read the world config, please fix this issue before loading the world.", e);
return Optional.empty();
}
Scoreboard scoreboard;
try {
scoreboard = ScoreboardIO.read(worldEntry.folder);
} catch (IOException e) {
this.logger.error("Unable to read the scoreboard data.", e);
scoreboard = Scoreboard.builder().build();
}
// Create the world instance
final LanternWorld world = new LanternWorld(this.game, result.config, worldEntry.folder, scoreboard, worldEntry.properties);
// Share the world instance
worldEntry.world = world;
worldEntry.properties.setWorld(world);
// Initialize the world if not done before
world.initialize();
// Generate the spawn if needed
if (worldEntry.properties.doesKeepSpawnLoaded()) {
world.enableSpawnArea(true);
}
// Load the chunk loading tickets, they may load some chunks
try {
world.getChunkManager().loadTickets();
} catch (IOException e) {
this.logger.warn("An error occurred while loading the chunk loading tickets", e);
}
final CauseStack causeStack = CauseStack.currentOrEmpty();
causeStack.pushCause(world);
final LoadWorldEvent event = SpongeEventFactory.createLoadWorldEvent(causeStack.getCurrentCause(), world);
causeStack.popCause();
Sponge.getEventManager().post(event);
if (event.isCancelled()) {
return Optional.empty();
}
// The world is ready for ticks
addWorldTask(world);
return Optional.of(world);
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorldManager method createWorld.
/**
* Creates the world properties for the specified settings and the dimension id.
*
* @param worldArchetype The world archetype
* @param folderName The folder name
* @param dimensionId The dimension id
* @return The new or existing world properties, if creation was successful
*/
private LanternWorldProperties createWorld(WorldArchetype worldArchetype, String folderName, int dimensionId) throws IOException {
final LanternWorldArchetype settings0 = (LanternWorldArchetype) checkNotNull(worldArchetype, "worldArchetype");
final String worldName = worldArchetype.getName();
WorldLookupEntry entry = this.worldByName.get(worldName);
if (entry != null) {
return entry.properties;
}
WorldConfigResult worldConfigResult;
// Create a config
try {
worldConfigResult = getOrCreateWorldConfig(worldName);
} catch (IOException e) {
throw new IOException("Unable to read/write the world config, please fix this issue before" + " creating the world.", e);
}
// Create the world properties
final LanternWorldProperties worldProperties = new LanternWorldProperties(settings0.getName(), worldConfigResult.config);
if (worldConfigResult.newCreated) {
worldProperties.update(settings0);
}
// Get the world folder
final Path worldFolder = getWorldFolder(folderName, dimensionId);
try {
Files.createDirectories(worldFolder);
} catch (IOException e) {
throw new IOException("Unable to create the world folders for " + settings0.getName(), e);
}
// Store the new properties
addWorldProperties(worldProperties, worldFolder, dimensionId);
final CauseStack causeStack = CauseStack.currentOrEmpty();
Sponge.getEventManager().post(SpongeEventFactory.createConstructWorldPropertiesEvent(causeStack.getCurrentCause(), worldArchetype, worldProperties));
// Save the world properties to reserve the world folder
saveWorldProperties(worldProperties);
return worldProperties;
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method enableSpawnArea.
/**
* Enables whether the spawn volume should be generated and keeping it loaded.
*
* @param keepSpawnLoaded keep spawn loaded
*/
void enableSpawnArea(boolean keepSpawnLoaded) {
if (keepSpawnLoaded) {
final Vector3i spawnPoint = this.properties.getSpawnPosition();
if (this.spawnLoadingTicket == null) {
this.spawnLoadingTicket = (ChunkLoadingTicket) this.chunkManager.createTicket(this.game.getMinecraftPlugin()).get();
} else {
this.spawnLoadingTicket.unforceChunks();
}
final int chunkX = spawnPoint.getX() >> 4;
final int chunkZ = spawnPoint.getZ() >> 4;
this.logger.info("Generating spawn volume...");
final CauseStack causeStack = CauseStack.current();
for (int x = chunkX - SPAWN_SIZE; x < chunkX + SPAWN_SIZE; x++) {
for (int z = chunkZ - SPAWN_SIZE; z < chunkZ + SPAWN_SIZE; z++) {
this.chunkManager.getOrCreateChunk(x, z, causeStack, true);
this.spawnLoadingTicket.forceChunk(new Vector2i(x, z));
}
}
this.logger.info("Finished generating spawn volume.");
} else if (this.spawnLoadingTicket != null) {
this.spawnLoadingTicket.unforceChunks();
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method spawnEntity.
@Override
public boolean spawnEntity(Entity entity) {
checkNotNull(entity, "entity");
checkArgument(!entity.isRemoved(), "The entity may not be removed.");
checkArgument(entity.getWorld() == this, "The entity is not be located in this world.");
final CauseStack causeStack = CauseStack.current();
final SpawnEntityEvent.Custom event = SpongeEventFactory.createSpawnEntityEventCustom(causeStack.getCurrentCause(), Lists.newArrayList(entity));
Sponge.getEventManager().post(event);
return !finishSpawnEntityEvent(event).isEmpty();
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method pulse.
public void pulse() {
final CauseStack causeStack = CauseStack.current();
causeStack.pushCause(this);
this.chunkManager.pulse(causeStack);
this.timeUniverse.pulse();
if (this.weatherUniverse != null) {
this.weatherUniverse.pulse(causeStack);
}
// Pulse the entities
pulseEntities();
// Pulse the tile entities
getLoadedChunks().forEach(chunk -> ((LanternChunk) chunk).pulse());
causeStack.popCause();
// TODO: Maybe async?
this.observedChunkManager.pulse();
this.entityProtocolManager.updateTrackers(this.players);
}
Aggregations