Search in sources :

Example 1 with AboutToTeleportEvent

use of io.github.nucleuspowered.nucleus.modules.teleport.events.AboutToTeleportEvent in project Nucleus by NucleusPowered.

the class NucleusTeleportHandler method teleportPlayer.

private TeleportResult teleportPlayer(Player player, Location<World> locationToTeleportTo, Vector3d rotation, TeleportMode teleportMode, Cause cause, boolean addOffset) {
    Optional<Location<World>> targetLocation = getSafeLocation(player, locationToTeleportTo, teleportMode);
    if (targetLocation.isPresent() && Util.isLocationInWorldBorder(targetLocation.get())) {
        AboutToTeleportEvent event = new AboutToTeleportEvent(cause, new Transform<>(targetLocation.get().getExtent(), targetLocation.get().getPosition(), rotation), player);
        if (Sponge.getEventManager().post(event)) {
            event.getCancelMessage().ifPresent(x -> {
                Object o = cause.root();
                if (o instanceof MessageReceiver) {
                    ((MessageReceiver) o).sendMessage(x);
                }
            });
            return TeleportResult.FAILED_CANCELLED;
        }
        Optional<Entity> oe = player.getVehicle();
        if (oe.isPresent()) {
            player.setVehicle(null);
        }
        // Do it, tell the routine if it worked.
        TeleportResult tr;
        if (addOffset) {
            tr = result(player.setLocationAndRotation(targetLocation.get().add(0.5, 0.5, 0.5), rotation));
        } else {
            tr = result(player.setLocationAndRotation(targetLocation.get(), rotation));
        }
        if (tr.isSuccess()) {
            player.setSpectatorTarget(null);
        } else {
            oe.ifPresent(player::setVehicle);
        }
        return tr;
    }
    return TeleportResult.FAILED_NO_LOCATION;
}
Also used : Entity(org.spongepowered.api.entity.Entity) MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) AboutToTeleportEvent(io.github.nucleuspowered.nucleus.modules.teleport.events.AboutToTeleportEvent) Location(org.spongepowered.api.world.Location)

Aggregations

AboutToTeleportEvent (io.github.nucleuspowered.nucleus.modules.teleport.events.AboutToTeleportEvent)1 Entity (org.spongepowered.api.entity.Entity)1 MessageReceiver (org.spongepowered.api.text.channel.MessageReceiver)1 Location (org.spongepowered.api.world.Location)1