Advertisement
tniemi

fi/nimbus/bukkit/plugin/spawnprotector/ProtectPlayer.java

May 20th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package fi.nimbus.bukkit.plugin.spawnprotector;
  2.  
  3. /**
  4.  *  Protect player against falling in to the ground during (re)spawning
  5.  */
  6. public class ProtectPlayer implements org.bukkit.event.Listener {
  7.     private static final int PROTECT_COUNT_LIMIT = 100;
  8.     private final org.bukkit.entity.Player player;
  9.     private final org.bukkit.Location spawnLocation;
  10.     private int protectCount;
  11.  
  12.     /**
  13.      *  Create protector for the player
  14.      */
  15.     public ProtectPlayer(org.bukkit.entity.Player player) {
  16.         this.player = player;
  17.         spawnLocation = player.getLocation();
  18.         protectCount = 0;
  19.     }
  20.  
  21.     /**
  22.      *  Inspect player movement and decide if him/her requires protection
  23.      */
  24.     @org.bukkit.event.EventHandler(priority = org.bukkit.event.EventPriority.HIGHEST)
  25.     public void playerMoveEvent(org.bukkit.event.player.PlayerMoveEvent event) {
  26.         final org.bukkit.Location location = player.getLocation();
  27.  
  28.         // Stop protecting if we have moved vertically over 1 square since spawn
  29.         if (distanceSquared2D(location) >= 1.0) {
  30.             stopProtecting(event);
  31.         }
  32.  
  33.         // Stop protecting if we have moved upwards since spawn
  34.         else if (location.getY() > spawnLocation.getY()) {
  35.             stopProtecting(event);
  36.         }
  37.  
  38.         // Stop protecting if we have already protected over PROTECT_COUNT_LIMIT times...
  39.         else if (protectCount > PROTECT_COUNT_LIMIT) {
  40.             stopProtecting(event);
  41.             player.sendMessage(org.bukkit.ChatColor.BLUE + "SpawnProtector died of old age");
  42.         }
  43.  
  44.         // Protect us if we are in free fall!
  45.         else if (location.getY() < spawnLocation.getY()) {
  46.             protect(event);
  47.         }
  48.     }
  49.  
  50.     /**
  51.      *  Protect player by teleporting him/her back to the spawn level
  52.      */
  53.     private void protect(final org.bukkit.event.player.PlayerMoveEvent event) {
  54.         final org.bukkit.Location location = event.getTo();
  55.         location.setY(spawnLocation.getY());
  56.         player.teleport(location);
  57.  
  58.         if (protectCount == 0) {
  59.             player.sendMessage(org.bukkit.ChatColor.BLUE + "SpawnProtector active");
  60.         }
  61.  
  62.         ++protectCount;
  63.     }
  64.  
  65.     /**
  66.      *  Stop protecting player by unregistering the protection event
  67.      */
  68.     private void stopProtecting(final org.bukkit.event.player.PlayerMoveEvent event) {
  69.         event.getHandlers().unregister(this);
  70.     }
  71.  
  72.     /**
  73.      *  @return 2D distance moved (squared) since spawning
  74.      */
  75.     private double distanceSquared2D(final org.bukkit.Location location) {
  76.         return Math.abs(spawnLocation.getX() - location.getX()) + Math.abs(spawnLocation.getZ() - location.getZ());
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement