use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method spawnEntities.
@Override
public Collection<Entity> spawnEntities(Iterable<? extends Entity> entities) {
for (Entity entity : entities) {
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(entities));
Sponge.getEventManager().post(event);
return finishSpawnEntityEvent(event);
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method interactBlock.
private boolean interactBlock(int x, int y, int z, Direction side, @Nullable GameProfile profile, @Nullable ItemStack itemStack) {
checkNotNull(side, "side");
final LanternBlockType blockType = ((LanternBlockType) getBlockType(x, y, z));
final BehaviorPipeline<Behavior> pipeline = blockType.getPipeline();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(ContextKeys.INTERACTION_FACE, side);
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(this, x, y, z));
frame.addContext(ContextKeys.BLOCK_TYPE, blockType);
if (profile != null) {
frame.addContext(EventContextKeys.PLAYER_SIMULATED, profile);
}
if (itemStack != null) {
frame.addContext(ContextKeys.USED_ITEM_STACK, itemStack);
}
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
// Just pass an object trough to make sure that a value is present when successful
if (context.process(pipeline.pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(pipeline, ctx)).isSuccess()) {
context.accept();
return true;
}
context.revert();
return false;
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method handleEntitySpawning.
public static void handleEntitySpawning(Iterable<EntitySpawningEntry> entries, BiFunction<Cause, List<Entity>, SpawnEntityEvent> spawnEventConstructor) {
final CauseStack causeStack = CauseStack.current();
final List<Entity> entities = handlePreEntitySpawning(causeStack, entries);
if (entities.isEmpty()) {
return;
}
final SpawnEntityEvent spawnEvent = spawnEventConstructor.apply(causeStack.getCurrentCause(), entities);
// Post the spawn event
Sponge.getEventManager().post(spawnEvent);
// Spawn the entities in the world
finishSpawnEntityEvent(spawnEvent);
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorldManager method unloadWorld.
/**
* Unloads a {@link World}, if there are any connected players in the given
* world then no operation will occur.
*
* <p>A world which is unloaded will be removed from memory. However if it
* is still enabled according to {@link WorldProperties#isEnabled()} then it
* will be loaded again if the server is restarted or an attempt is made by
* a plugin to transfer an entity to the world using
* {@link org.spongepowered.api.entity.Entity#transferToWorld(World, Vector3d)}.</p>
*
* @param world the world to unload
* @return whether the operation was successful
*/
public boolean unloadWorld(World world) {
checkNotNull(world, "world");
final LanternWorld world0 = (LanternWorld) world;
// still players active
if (!world0.getPlayers().isEmpty()) {
return false;
}
final CauseStack causeStack = CauseStack.currentOrEmpty();
causeStack.pushCause(world);
// Post the unload world event
this.game.getEventManager().post(SpongeEventFactory.createUnloadWorldEvent(causeStack.getCurrentCause(), world));
causeStack.popCause();
// Save all the world data
world0.shutdown();
// Remove the tick task
removeWorldTask(world0);
// Get the lookup entry and properties to remove all the references
final LanternWorldProperties properties = world0.getProperties();
final WorldLookupEntry entry = this.worldByProperties.get(properties);
properties.setWorld(null);
entry.world = null;
// Save the world properties
this.saveWorldProperties(properties);
try {
ScoreboardIO.write(entry.folder, world0.getScoreboard());
} catch (IOException e) {
Lantern.getLogger().warn("An error occurred while saving the scoreboard data.", e);
}
return true;
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternChunkManager method doChunkLoad.
private void doChunkLoad(Vector2i coords) {
Set<ChunkLoadingTicket> tickets = this.ticketsByPos.get(coords);
if (tickets == null) {
return;
}
tickets = new HashSet<>(tickets);
final CauseStack causeStack = CauseStack.current();
tickets.forEach(causeStack::pushCause);
// Chunk may be null if's already being loaded by a different thread.
getOrCreateChunk(coords, causeStack, true, false);
causeStack.popCauses(tickets.size());
}
Aggregations