Search in sources :

Example 1 with SendToSpawnEvent

use of io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent in project Nucleus by NucleusPowered.

the class SpawnCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    boolean force = args.hasAny("f");
    GlobalSpawnConfig gsc = sc.getGlobalSpawn();
    WorldProperties wp = args.<WorldProperties>getOne(key).orElseGet(() -> gsc.isOnSpawnCommand() ? gsc.getWorld().orElse(src.getWorld().getProperties()) : src.getWorld().getProperties());
    Optional<World> ow = Sponge.getServer().loadWorld(wp.getUniqueId());
    if (!ow.isPresent()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.noworld"));
    } else if (sc.isPerWorldPerms() && !permissions.testSuffix(src, "worlds." + ow.get().getName().toLowerCase())) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.nopermsworld", ow.get().getName()));
    }
    Transform<World> worldTransform = SpawnHelper.getSpawn(wp, plugin, src);
    SendToSpawnEvent event = new SendToSpawnEvent(worldTransform, src, CauseStackHelper.createCause(src));
    if (Sponge.getEventManager().post(event)) {
        if (event.getCancelReason().isPresent()) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.self.failed.reason", event.getCancelReason().get()));
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.self.failed.noreason"));
    }
    // If we don't have a rotation, then use the current rotation
    NucleusTeleportHandler.TeleportResult result = this.plugin.getTeleportHandler().teleportPlayer(src, SpawnHelper.getSpawn(ow.get().getProperties(), this.plugin, src), !force && this.sc.isSafeTeleport());
    if (result.isSuccess()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawn.success", wp.getWorldName()));
        return CommandResult.success();
    }
    if (result == NucleusTeleportHandler.TeleportResult.FAILED_NO_LOCATION) {
        throw ReturnMessageException.fromKey("command.spawn.fail", wp.getWorldName());
    }
    throw ReturnMessageException.fromKey("command.spawn.cancelled", wp.getWorldName());
}
Also used : GlobalSpawnConfig(io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig) NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) SendToSpawnEvent(io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 2 with SendToSpawnEvent

use of io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent in project Nucleus by NucleusPowered.

the class SpawnOtherCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    User target = args.<User>getOne(otherKey).get();
    WorldProperties world = this.getWorldProperties(src, worldKey, args).orElseGet(() -> gsc.isOnSpawnCommand() ? gsc.getWorld().get() : Sponge.getServer().getDefaultWorld().get());
    Transform<World> worldTransform = SpawnHelper.getSpawn(world, plugin, target.getPlayer().orElse(null));
    SendToSpawnEvent event = new SendToSpawnEvent(worldTransform, target, CauseStackHelper.createCause(src));
    if (Sponge.getEventManager().post(event)) {
        if (event.getCancelReason().isPresent()) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.other.failed.reason", target.getName(), event.getCancelReason().get()));
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.other.failed.noreason", target.getName()));
    }
    if (!target.isOnline()) {
        return isOffline(src, target, worldTransform);
    }
    // If we don't have a rotation, then use the current rotation
    Player player = target.getPlayer().get();
    NucleusTeleportHandler.TeleportResult result = plugin.getTeleportHandler().teleportPlayer(player, worldTransform, this.safeTeleport);
    if (result.isSuccess()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.success.source", target.getName(), world.getWorldName()));
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.spawnother.success.target", world.getWorldName()));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.spawnother.fail", target.getName(), world.getWorldName());
}
Also used : NucleusTeleportHandler(io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) SendToSpawnEvent(io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)2 NucleusTeleportHandler (io.github.nucleuspowered.nucleus.internal.teleport.NucleusTeleportHandler)2 SendToSpawnEvent (io.github.nucleuspowered.nucleus.modules.spawn.events.SendToSpawnEvent)2 World (org.spongepowered.api.world.World)2 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)2 GlobalSpawnConfig (io.github.nucleuspowered.nucleus.modules.spawn.config.GlobalSpawnConfig)1 Player (org.spongepowered.api.entity.living.player.Player)1 User (org.spongepowered.api.entity.living.player.User)1