use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method createEntity.
public Entity createEntity(EntityType type, Vector3d position, Consumer<Entity> entityConsumer) {
checkNotNull(position, "position");
final LanternEntityType entityType = (LanternEntityType) checkNotNull(type, "type");
final LanternEntity entity = (LanternEntity) entityType.getEntityConstructor().apply(UUID.randomUUID());
entity.setPositionAndWorld(this, position);
entityConsumer.accept(entity);
final CauseStack causeStack = CauseStack.current();
// Only throw the post event, the pre event will
// only be called in specific cases.
final ConstructEntityEvent.Post event = SpongeEventFactory.createConstructEntityEventPost(causeStack.getCurrentCause(), entity, type, entity.getTransform());
Sponge.getEventManager().post(event);
return entity;
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternCommandManager method getSuggestions.
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition, boolean usingBlock) {
final CauseStack causeStack = CauseStack.currentOrEmpty();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(source);
final List<String> suggestions;
final String[] argSplit = arguments.split(" ", 2);
// 'argSplit.length == 1' the check '&& !arguments.endsWith(" ")'
if (argSplit.length == 1 && !arguments.endsWith(" ")) {
suggestions = this.dispatcher.getSuggestions(source, arguments, targetPosition);
} else {
final Optional<? extends CommandMapping> cmdOptional = this.dispatcher.get(argSplit[0], source);
if (!cmdOptional.isPresent()) {
suggestions = ImmutableList.of();
} else {
suggestions = cmdOptional.get().getCallable().getSuggestions(source, argSplit[1], targetPosition);
}
}
final List<String> rawSuggestions = new ArrayList<>(suggestions);
final TabCompleteEvent.Command event = SpongeEventFactory.createTabCompleteEventCommand(frame.getCurrentCause(), ImmutableList.copyOf(suggestions), rawSuggestions, argSplit.length > 1 ? argSplit[1] : "", argSplit[0], arguments, Optional.ofNullable(targetPosition), usingBlock);
Sponge.getGame().getEventManager().post(event);
if (event.isCancelled()) {
return ImmutableList.of();
} else {
return ImmutableList.copyOf(event.getTabCompletions());
}
} catch (CommandException e) {
source.sendMessage(error(t("Error getting suggestions: %s", e.getText())));
return ImmutableList.of();
} catch (Exception e) {
throw new RuntimeException(String.format("Error occurred while tab completing '%s'", arguments), e);
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class BanConfig method addBan.
@Override
public Optional<? extends Ban> addBan(Ban ban) {
checkNotNull(ban, "ban");
final Optional<Ban> oldBan;
if (ban instanceof Ban.Ip) {
oldBan = (Optional) getBanFor(((Ban.Ip) ban).getAddress());
} else {
oldBan = (Optional) getBanFor(((Ban.Profile) ban).getProfile());
}
oldBan.ifPresent(this.entries0::remove);
this.entries0.add((BanEntry) ban);
if (!oldBan.isPresent() || !oldBan.get().equals(ban)) {
final CauseStack causeStack = CauseStack.currentOrEmpty();
// Post the ban events
final Event event;
final Cause cause = causeStack.getCurrentCause();
if (ban instanceof Ban.Ip) {
event = SpongeEventFactory.createBanIpEvent(cause, (Ban.Ip) ban);
} else {
final Ban.Profile profileBan = (Ban.Profile) ban;
// Check if the pardoned player is online (not yet been kicked)
final Optional<Player> optTarget = Sponge.getServer().getPlayer(profileBan.getProfile().getUniqueId());
if (optTarget.isPresent()) {
event = SpongeEventFactory.createBanUserEventTargetPlayer(cause, profileBan, optTarget.get(), optTarget.get());
} else {
event = SpongeEventFactory.createBanUserEvent(cause, profileBan, Lantern.getGame().getServiceManager().provideUnchecked(UserStorageService.class).getOrCreate(profileBan.getProfile()));
}
}
// Just ignore for now the fact that they may be cancellable,
// only the PardonIpEvent seems to be cancellable
// TODO: Should they all be cancellable or none of them?
Sponge.getEventManager().post(event);
}
return oldBan;
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class NetworkSession method leavePlayer.
/**
* Is called when the {@link LanternPlayer} leaves the
* server and needs to be cleaned up.
*/
private void leavePlayer() {
checkState(this.player != null, "The player must first be available!");
final LanternWorld world = this.player.getWorld();
if (world != null) {
final CauseStack causeStack = CauseStack.current();
causeStack.pushCause(this.player);
// Close the open container
this.player.getContainerSession().setRawOpenContainer(causeStack, null);
final MessageChannel messageChannel = this.player.getMessageChannel();
final Text quitMessage = t("multiplayer.player.left", this.player.getName());
final ClientConnectionEvent.Disconnect event = SpongeEventFactory.createClientConnectionEventDisconnect(causeStack.getCurrentCause(), messageChannel, Optional.of(messageChannel), new MessageEvent.MessageFormatter(quitMessage), this.player, false);
Sponge.getEventManager().post(event);
if (!event.isMessageCancelled()) {
event.getChannel().ifPresent(channel -> channel.send(this.player, event.getMessage()));
}
causeStack.popCause();
// Remove the proxy user from the player and save the player data
this.player.getProxyUser().setInternalUser(null);
// Destroy the player entity
this.player.remove(LanternEntity.RemoveState.DESTROYED);
// Detach the player from the world
this.player.setWorld(null);
// Release the players entity id
EntityProtocolManager.releaseEntityId(this.player.getNetworkId());
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class SyncScheduler method executeTaskRunnable.
@Override
protected void executeTaskRunnable(ScheduledTask task, Runnable runnable) {
final CauseStack causeStack = CauseStack.current();
causeStack.pushCause(task.getOwner());
causeStack.pushCause(task);
runnable.run();
causeStack.popCauses(2);
}
Aggregations