Search in sources :

Example 26 with CauseStack

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;
}
Also used : ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) CauseStack(org.lanternpowered.server.event.CauseStack) LanternEntityType(org.lanternpowered.server.entity.LanternEntityType) LanternEntity(org.lanternpowered.server.entity.LanternEntity)

Example 27 with CauseStack

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);
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) TabCompleteEvent(org.spongepowered.api.event.command.TabCompleteEvent) ArrayList(java.util.ArrayList) InvocationCommandException(org.spongepowered.api.command.InvocationCommandException) CommandException(org.spongepowered.api.command.CommandException) InvocationCommandException(org.spongepowered.api.command.InvocationCommandException) CommandPermissionException(org.spongepowered.api.command.CommandPermissionException) TextMessageException(org.spongepowered.api.util.TextMessageException) CommandException(org.spongepowered.api.command.CommandException)

Example 28 with CauseStack

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;
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) CauseStack(org.lanternpowered.server.event.CauseStack) Player(org.spongepowered.api.entity.living.player.Player) Ip(org.spongepowered.api.util.ban.Ban.Ip) Cause(org.spongepowered.api.event.cause.Cause) Event(org.spongepowered.api.event.Event) Ban(org.spongepowered.api.util.ban.Ban) GameProfile(org.spongepowered.api.profile.GameProfile)

Example 29 with CauseStack

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());
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageEvent(org.spongepowered.api.event.message.MessageEvent) ClientConnectionEvent(org.spongepowered.api.event.network.ClientConnectionEvent) LanternWorld(org.lanternpowered.server.world.LanternWorld) Text(org.spongepowered.api.text.Text)

Example 30 with CauseStack

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);
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack)

Aggregations

CauseStack (org.lanternpowered.server.event.CauseStack)54 ItemStack (org.spongepowered.api.item.inventory.ItemStack)18 Entity (org.spongepowered.api.entity.Entity)16 ArrayList (java.util.ArrayList)15 Nullable (javax.annotation.Nullable)15 World (org.spongepowered.api.world.World)15 Optional (java.util.Optional)14 Lantern (org.lanternpowered.server.game.Lantern)13 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)13 LanternWorld (org.lanternpowered.server.world.LanternWorld)13 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)13 Vector3i (com.flowpowered.math.vector.Vector3i)12 List (java.util.List)12 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)12 Keys (org.spongepowered.api.data.key.Keys)12 EventContextKeys (org.spongepowered.api.event.cause.EventContextKeys)12 Sponge (org.spongepowered.api.Sponge)11 Transform (org.spongepowered.api.entity.Transform)11 Player (org.spongepowered.api.entity.living.player.Player)11 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)11