Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fi.nimbus.bukkit.plugin.spawnprotector;
- /**
- * Protect player against falling in to the ground during (re)spawning
- */
- public class ProtectPlayer implements org.bukkit.event.Listener {
- private static final int PROTECT_COUNT_LIMIT = 100;
- private final org.bukkit.entity.Player player;
- private final org.bukkit.Location spawnLocation;
- private int protectCount;
- /**
- * Create protector for the player
- */
- public ProtectPlayer(org.bukkit.entity.Player player) {
- this.player = player;
- spawnLocation = player.getLocation();
- protectCount = 0;
- }
- /**
- * Inspect player movement and decide if him/her requires protection
- */
- @org.bukkit.event.EventHandler(priority = org.bukkit.event.EventPriority.HIGHEST)
- public void playerMoveEvent(org.bukkit.event.player.PlayerMoveEvent event) {
- final org.bukkit.Location location = player.getLocation();
- // Stop protecting if we have moved vertically over 1 square since spawn
- if (distanceSquared2D(location) >= 1.0) {
- stopProtecting(event);
- }
- // Stop protecting if we have moved upwards since spawn
- else if (location.getY() > spawnLocation.getY()) {
- stopProtecting(event);
- }
- // Stop protecting if we have already protected over PROTECT_COUNT_LIMIT times...
- else if (protectCount > PROTECT_COUNT_LIMIT) {
- stopProtecting(event);
- player.sendMessage(org.bukkit.ChatColor.BLUE + "SpawnProtector died of old age");
- }
- // Protect us if we are in free fall!
- else if (location.getY() < spawnLocation.getY()) {
- protect(event);
- }
- }
- /**
- * Protect player by teleporting him/her back to the spawn level
- */
- private void protect(final org.bukkit.event.player.PlayerMoveEvent event) {
- final org.bukkit.Location location = event.getTo();
- location.setY(spawnLocation.getY());
- player.teleport(location);
- if (protectCount == 0) {
- player.sendMessage(org.bukkit.ChatColor.BLUE + "SpawnProtector active");
- }
- ++protectCount;
- }
- /**
- * Stop protecting player by unregistering the protection event
- */
- private void stopProtecting(final org.bukkit.event.player.PlayerMoveEvent event) {
- event.getHandlers().unregister(this);
- }
- /**
- * @return 2D distance moved (squared) since spawning
- */
- private double distanceSquared2D(final org.bukkit.Location location) {
- return Math.abs(spawnLocation.getX() - location.getX()) + Math.abs(spawnLocation.getZ() - location.getZ());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement