spawn protection

Discussion in 'Plugin Development' started by vtg_the_kid, Dec 10, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    if i wanted to code spawn protection where if a player steps off spawn, they lose it even if they go back in,
    would i have to use playermoveevent, and what would be the most effecient way of doing it
  2. Offline


    What is "it" that they lose?
    Please make an antecedent to your pronoun before using it...
    Grammar 101
  3. Offline


  4. Offline


    PlayerMoveEvent is the only way I can think of, so there wouldn't really be a more efficient way. Don't forget to listen to PlayerTeleportEvent tho ;3.

    Also you could keep a Set<String> with playernames that have protection and should be checked. That Set could be used for the protection itself as well, while you remove them as they leave the protection.
  5. Offline


    ok thanks

    is this code right
    am i missing anything, guys?
    1. ArrayList<String> spawn = new ArrayList<String>();
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent event) {
    4. Player player = event.getPlayer();
    5. if (!player.hasPlayedBefore()){
    6. spawn.add(player.getName());
    7. }
    8. }
    9. @EventHandler
    10. public void spawnProtection(PlayerTeleportEvent event){
    11. Player player = event.getPlayer();
    12. if (Math.abs(player.getLocation().getBlockX()) <= 20
    13. && Math.abs(player.getLocation().getBlockZ()) <= 20){
    14. if (!spawn.contains(player.getName())){
    15. spawn.add(player.getName());
    16. player.sendMessage(ChatColor.LIGHT_PURPLE + "You have regained spawn protection.");
    17. }
    18. }
    19. else{
    20. if (spawn.contains(player.getName())){
    21. player.sendMessage(ChatColor.LIGHT_PURPLE + "You no longer have spawn protection.");
    22. }
    23. }
    24. }
    25. @EventHandler
    26. public void spawnProtection(PlayerMoveEvent event){
    27. Player player = event.getPlayer();
    28. if (Math.abs(player.getLocation().getBlockX()) >= 20
    29. || Math.abs(player.getLocation().getBlockZ()) >= 20){
    30. if (spawn.contains(player.getName())){
    31. spawn.remove(player.getName());
    32. player.sendMessage(ChatColor.LIGHT_PURPLE + "You no longer have spawn protection.");
    33. }
    34. }
    35. }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 5, 2016
  6. Offline


    vtg_the_kid Seems about right on the first glance. Not sure if you need the hasPlayedBefore() check, since they would spawn in spawn anyway.

    Also, you might want to integrate the current spawnpoint so you could freely set spawn without worries, but that is optional.
  7. Offline


  8. Offline


  9. Offline


  10. Offline


    Not particularly.
  11. Offline


    vtg_the_kid No need to worry about efficiency, especially in a plugin as small as this with minimal scalability.
Thread Status:
Not open for further replies.

Share This Page