Skip to content
Permalink
Browse files

Throw a new TeleportCancelled - event.

  • Loading branch information...
bmhm committed Apr 6, 2016
1 parent 89cffca commit 3a8916b4718861a3f3b669f25a9841888d7737c0
@@ -0,0 +1,48 @@
/**
* Bukkit-Plugin-Example by Benjamin Marwell.
*
* To the extent possible under law, the person who associated CC0 with
* Bukkit-Plugin-Example has waived all copyright and related or neighboring rights
* to Bukkit-Plugin-Example.
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/

package de.bmarwell.bukkit.events;

import com.google.common.base.Optional;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;

/**
* A Teleport Cancelled Event.
*
* <p>
* Probably not actually useful, but works as an example.
* </p>
*/
public class PlayerTeleportCancelledEvent extends PlayerTeleportEvent {

private Optional<String> reason = Optional.absent();

public PlayerTeleportCancelledEvent(Player player, Location from, Location to, String reason,
TeleportCause cause) {
super(player, from, to, cause);
this.reason = Optional.fromNullable(reason);
}

public Optional<String> getReason() {
return this.reason;
}

@Override
public String toString() {
return "PlayerTeleportCancelledEvent [reason=" + reason.or("No reason defined")
+ ", getCause()=" + getCause().toString()
+ ", getPlayer()=" + getPlayer().getUniqueId() + "/" + getPlayer().getName() + "]";
}

}
@@ -11,8 +11,9 @@

package de.bmarwell.bukkit.listener;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import de.bmarwell.bukkit.events.PlayerTeleportCancelledEvent;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
@@ -37,11 +38,12 @@ public void onPlayerTeleport(PlayerTeleportEvent event) {
return;
}

Player teleportedPlayer = event.getPlayer();
Location origin = event.getFrom();
event.setCancelled(true);

boolean teleportSuccess = teleportedPlayer.teleport(origin, TeleportCause.PLUGIN);
// TODO: log teleportSuccess;
// Call the event cancelled event to notify others.
PlayerTeleportCancelledEvent ptce = new PlayerTeleportCancelledEvent(event.getPlayer(),
event.getFrom(), event.getTo(), "You may not throw ender pearls!!", event.getCause());
Bukkit.getServer().getPluginManager().callEvent(ptce);
}

}

0 comments on commit 3a8916b

Please sign in to comment.
You can’t perform that action at this time.