McPVP Spawn Style Plugin

Discussion in 'Plugin Development' started by mydeblob, Aug 17, 2013.

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

    mydeblob

    I am working on making a plugin that is like mcpvp where if you leave the spawn and come back in you are still vulnerable to pvp. I was going to hook world gaurd into it, and use the regions from worldgaurd. But as I was thinking how would I override a flag? I would assume if a player leaves and comes back in I would have to ovverride a no pvp flag to pvp allow for that player only. Is there something in the worldgaurd api for this?
     
  2. Offline

    GaaTavares


    You can use getLocation, and add the player that leave the spawn in arraylist/hashmap, check entitydamagebyentity if the player is on a hashmap.... i dont know..
     
  3. Offline

    Quantix

    I made something like this some time ago and I think it would be much simpler to code this yourself instead of hooking into WorldGuward. First of all I'd make a list and save player names and booleans (if they have protection or not) in it.
    Code:java
    1. public HashMap<String, Boolean> protectedPlayers = new HashMap<String, Boolean>();
    Next you need a method which returns a boolean if a location (of a player for example) is in spawn. You can define the area of the spawn yourself. I used the dimensions of the spawns on the MCPVP hardcore servers (128x128).
    Code:java
    1. public boolean isInSpawn(Location location) {
    2. double x = location.getX();
    3. double z = location.getZ();
    4. if (x <= 64 && x >= -64 && z <= 64 && z >= -64) {
    5. return true;
    6. }
    7. return false;
    8. }
    Also, here's a quick method to check if the player has spawn protection:
    Code:java
    1. public boolean hasSpawnProtection(Player player) {
    2. return (protectedPlayers.containsKey(player.getName())) ? protectedPlayers.get(player.getName()) : false;
    3. }
    We should also give players that log in for the first time since the server restarted spawn protection:
    Code:java
    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onJoin(PlayerJoinEvent event) {
    3. Player player = event.getPlayer();
    4. if (!protectedPlayers.containsKey(player.getName())) {
    5. if (isInSpawn(player.getLocation())) {
    6. protectedPlayers.put(player.getName(), true);
    7. }
    8. }
    9. }
    This code removes a player's spawn protection if they walk out of spawn:
    Code:java
    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onMove (PlayerMoveEvent event) {
    3. Player player = event.getPlayer();
    4. if (isInSpawn(event.getFrom()) && !isInSpawn(event.getTo())) {
    5. if (hasSpawnProtection(player)) {
    6. protectedPlayers.put(player.getName(), false);
    7. player.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    8. }
    9. }
    10. }
    Last but not least, this cancels the damage if the player has spawn protection and also removes the spawn protection if a player with spawn protection attacks a player without (like in MCPVP):
    Code:java
    1. @EventHandler (priority = EventPriority.NORMAL)
    2. public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof Player) {
    4. Player damaged = (Player) event.getEntity();
    5. if (hasSpawnProtection(damaged)) {
    6. event.setCancelled(true);
    7. } else if (event.getDamager() instanceof Player) {
    8. Player attacker = (Player) event.getDamager();
    9. if (hasSpawnProtection(attacker)) {
    10. protectedPlayers.put(attacker.getName(), false);
    11. attacker.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    12. }
    13. }
    14. }
    15. }


    One more note: My method above has one flaw which is that if you /reload the server all players loose spawn protection, which is only an issue for players in spawn who have protection. What you could do is kick all players, forcing them to log in again and obtain their spawn protection (if they are in spawn).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Quantix
    And how can you claim the area becouse I coded almost the exact same thing and it's not working. When I exit the 64x64 area it doesn't show a message :p
    I think I failed miserable...
     
  5. Offline

    Loogeh

    Quantix You don't need to use a HashMap for that. Instead, you could just use some sort if list and you could make a method like this.

    Code:
    private ArrayList<String> protectedPlayers = new ArrayList<String>();
       
        private boolean isProtected(String player) {
            return protectedPlayers.contains(player);
        }
     
  6. Offline

    Quantix

    What do you mean claim the area? Could you show me the code from your main plugin class, your listener and any other relevant classes? Were there any stack traces in the console that you could post?


    No, the HashMap is used for a reason. If one were to use a list or unordered set, I wouldn't know whether the player is not in the list because he either lost the spawn protection or hasn't logged in the server since it was restarted. This matters because players that newly log in and spawn in the spawn protected area should be given protection, while players that just exit the spawn, loose their protection, re-enter and log out and back in again should not be given spawn protection to prevent players from abusing the spawn protection and escaping or avoiding combat.
     
  7. Offline

    Twisted_Panda

    It also didnt work for me when I tried it.
     
  8. Offline

    Quantix

    And I'll tell you same thing I told jojolinul :D Please do post your code from your listener class and main plugin class and post any stack traces in the console (if there are any). Thanks!
     
  9. Offline

    Twisted_Panda

    Main class:
    Code:
    package me.panda.spawn;
     
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin  {
       
       
        public void onEnable() {
            System.out.println("alive");
            Bukkit.getPluginManager().registerEvents(new spawnprot(), this);
        }
       
        public void onDisable() {
        }
     
     
    }
    
    Listener:
    Code:
    public class spawnprot implements Listener {
       
        public HashMap<String, Boolean> protectedPlayers = new HashMap<String, Boolean>();
       
        public boolean hasSpawnProtection(Player player) {
            return (protectedPlayers.containsKey(player.getName())) ? protectedPlayers.get(player.getName()) : false;
        }
       
        public boolean isInSpawn(Location location) {
            double x = location.getX();
            double z = location.getZ();
            if (x <= 64 && x >= -64 && z <= 64 && z >= -64) {
                return true;
            } 
            return false;
        }
       
        @EventHandler (priority = EventPriority.NORMAL)
        public void onMove (PlayerMoveEvent event) {
            Player player = event.getPlayer();
            if (isInSpawn(event.getFrom()) && !isInSpawn(event.getTo())) {
                if (hasSpawnProtection(player)) {
                    protectedPlayers.put(player.getName(), false);
                    player.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
                }
            }
        }
       
        @EventHandler (priority = EventPriority.NORMAL)
        public void onEntityDamageByEntity (EntityDamageByEntityEvent event) {
            if (event.getEntity() instanceof Player) {
                Player damaged = (Player) event.getEntity();
                if (hasSpawnProtection(damaged)) {
                    event.setCancelled(true);
                } else if (event.getDamager() instanceof Player) {
                    Player attacker = (Player) event.getDamager();
                    if (hasSpawnProtection(attacker)) {
                        protectedPlayers.put(attacker.getName(), false);
                        attacker.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
                    }
                }
            }
        }
       
        @EventHandler (priority = EventPriority.NORMAL)
        public void onJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            if (!protectedPlayers.containsKey(player.getName())) {
                if (isInSpawn(player.getLocation())) {
                    protectedPlayers.put(player.getName(), true);       
                }
            }
        }
    }
    
     
  10. Offline

    Quantix

    Okay, after testing my/your posted code I noticed that the code cancels damage only from entities (EntityDamageByEntityEvent) such as players, mobs and arrows. I updated my code so that environmental damage such as falling, cacti and lava is also canceled if the player has spawn protection. I also added a listener for the respawn event so that players who are killed and respawn are given protection again. Here's the updated code similar to the one I posted above but with the corrections:
    Code:java
    1. public HashMap<String, Boolean> protectedPlayers = new HashMap<String, Boolean>();
    2.  
    3. public boolean hasSpawnProtection(Player player) {
    4. return (protectedPlayers.containsKey(player.getName())) ?
    5. protectedPlayers.get(player.getName()) : false;
    6. }
    7.  
    8. public boolean isInSpawn(Location location) {
    9. double x = location.getX();
    10. double z = location.getZ();
    11. if (x <= 64 && x >= -64 && z <= 64 && z >= -64) {
    12. return true;
    13. }
    14. return false;
    15. }
    16.  
    17. @EventHandler (priority = EventPriority.NORMAL)
    18. public void onMove (PlayerMoveEvent event) {
    19. Player player = event.getPlayer();
    20. if (isInSpawn(event.getFrom()) && !isInSpawn(event.getTo())) {
    21. if (hasSpawnProtection(player)) {
    22. protectedPlayers.put(player.getName(), false);
    23. player.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    24. }
    25. }
    26. }
    27.  
    28. @EventHandler (priority = EventPriority.NORMAL)
    29. public void onJoin(PlayerJoinEvent event) {
    30. Player player = event.getPlayer();
    31. if (!protectedPlayers.containsKey(player.getName())) {
    32. if (isInSpawn(player.getLocation())) {
    33. protectedPlayers.put(player.getName(), true);
    34. player.sendMessage(ChatColor.GRAY + "You've received spawn protection.");
    35. }
    36. }
    37. }
    38.  
    39. @EventHandler (priority = EventPriority.NORMAL)
    40. public void onDamage (EntityDamageEvent damage) {
    41. if (damage.getEntity() instanceof Player) {
    42. Player damaged = (Player) damage.getEntity();
    43. if (hasSpawnProtection(damaged)) {
    44. damage.setCancelled(true);
    45. } else if (damage instanceof EntityDamageByEntityEvent) {
    46. EntityDamageByEntityEvent entityDamage = (EntityDamageByEntityEvent) damage;
    47. if (entityDamage.getDamager() instanceof Player) {
    48. Player attacker = (Player) entityDamage.getDamager();
    49. if (hasSpawnProtection(attacker)) {
    50. protectedPlayers.put(attacker.getName(), false);
    51. attacker.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    52. }
    53. }
    54. }
    55. }
    56. }
    57.  
    58. @EventHandler (priority = EventPriority.NORMAL)
    59. public void onRespawn (PlayerRespawnEvent event) {
    60. if (isInSpawn(event.getRespawnLocation())) {
    61. Player player = event.getPlayer();
    62. protectedPlayers.put(player.getName(), true);
    63. player.sendMessage(ChatColor.GRAY + "You've received spawn protection.");
    64. }
    65. }
     
  11. Offline

    Twisted_Panda

  12. Quantix
    For me aswell :p Thanks man.
     
  13. Offline

    marshmallowz

    Quantix
    How would you do this with World Guard?
     
  14. Offline

    naorpeled

    Get the region boundries. But I think there`s a function in WorldGuardAPI.
     
  15. Offline

    BetrayedQU

    I am having trouble adding protection to players who /spawn or teleport to spawn
    Code:java
    1. public boolean teleport (PlayerTeleportEvent event) {
    2.  
    3. Player player = event.getPlayer();
    4.  
    5. double x = 0;
    6. double y = 65;
    7. double z = 0;
    8. World w = player.getWorld();
    9. Location loc = new Location(w, x, y, z);
    10. if (player.teleport(loc)) { //loc is the spawn
    11.  
    12. player.sendMessage("you have protection (temp message)");
    13. //ill add the spawn protection adding here, this code doesnt work, the message doesnt get displayed
    14. }
    15. return false;
    16.  
    17.  
    18. }
     
  16. Offline

    Mang0eZPvP

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3. Player p = e.getPlayer();
    4. double prot = 14;
    5. if (spawnprot.contains(p.getName())) {
    6. if (p.getLocation().getX() >= prot) {
    7. spawnprot.remove(p.getName());
    8. p.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    9. }
    10. if (p.getLocation().getZ() >= prot) {
    11. spawnprot.remove(p.getName());
    12. p.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    13. }
    14. if (p.getLocation().getX() <= -prot) {
    15. spawnprot.remove(p.getName());
    16. p.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    17. }
    18. if (p.getLocation().getZ() <= -prot) {
    19. spawnprot.remove(p.getName());
    20. p.sendMessage(ChatColor.GRAY + "You no longer have spawn protection.");
    21. }
    22. }
    23. }
     
  17. Offline

    BetrayedQU

    I want them to be able to /spawn and regain spawn prot. But /spawn interfers with essentials so when they teleport in
     
  18. Offline

    Doodledew

    BetrayedQU
    You can disable the /spawn command of Essentials in the Essentials config. As far as my experiences goes, all of the plugin's I've made with /spawn overwrites the EssentialsSpawn command.. Or you can just remove the EssentialsSpawn.jar from your server's plugin's! :D
     
  19. Offline

    BetrayedQU

    Oh no nvm, I figured it out a while ago :)

    BTW: I'm sub :)
     
  20. Offline

    Doodledew


    :D
     
Thread Status:
Not open for further replies.

Share This Page