Solved spawn protection help

Discussion in 'Spigot Plugin Development' started by TerroDoor, Jul 7, 2019.

  1. im stuck, if im making a spawn protection for players inside spawn. do i place them into a hashmap on a join event and cancel entity damage by entities?
     
  2. i have code included if someone can show me how to post code correctly, thanks.
     
  3. Check if they are inside your area, then cancel.
     
  4. I have done that, but if the attacker has protection and the defender doesn't, how do i remove the attaker protection if they hit the defender?
     
  5. Are you making it so players can toggle between PvP protection in spawn? If so, i would guess something like this works:
    Code (Java):
    on entitydamagebyentityevent:
    if (defender is in area and has pvp disabled) cancel event
    else if (attacker is in area and has pvp disabled) cancel event
     
  6. @KarimAKL Thankyou, i'll try seeing if i can get something working. maybe if i try: !protection.contains(defender) protection.remove(attacker) ??
     
  7. how's this:

    Code (Text):


    public class Protection implements Listener {

        public static List<UUID> protection = new ArrayList<UUID>();

        @EventHandler
        public void onProtection(EntityDamageByEntityEvent e) {

            Player d = (Player)e.getEntity();
            Entity a = (Entity)e.getDamager();

            if (protection.contains(a.getType())) {

                e.setCancelled(true);
               
                return;
            }
           
            if (!protection.contains(d.getUniqueId())) {
                 
                protection.remove(a.getType());
            }
        }
    }

     
     
  8. im using Zombies as my test Entities for attackers
     
  9. You can simply use a List or a Set.
    EDIT: Nvm, you are already doing that. I just assumed you used a Map because of the mention of a HashMap in the OG post.
    EDIT2: Could you explain what you want to happen exactly? I don't quite understand.
     
  10. @KarimAKL when the players join, they teleport to spawn with protection(added them to list on join event). if they attack someone without protection they will lose their's, if someone with or without protection tries hitting someone with protection, they can't do any damage.
     
  11. Here's an update:

    Code (Text):


    public class Protection implements Listener {

        public static List<UUID> protection = new ArrayList<UUID>();

        @EventHandler
        public void onLoseProt(PlayerMoveEvent e) {

            Player p = (Player)e.getPlayer();

            if (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType().equals(Material.SLIME_BLOCK)) {

                if (Protection.protection.contains(p.getUniqueId())) {

                    Protection.protection.remove(p.getUniqueId());

                    p.sendMessage(ChatColor.GRAY + "you have lost protection!");
               
                } else {
                   
                    return;
                   
                }
            }
        }
        @EventHandler
        public void onProtection(EntityDamageByEntityEvent e) {

            Player d = (Player)e.getEntity();
            Entity a = (Entity)e.getDamager();


            if (protection.contains(d.getUniqueId())) {

                e.setCancelled(true);

            } else {

                e.setCancelled(false);

                return;
            }  
        }
    }

     
     
    #11 TerroDoor, Jul 7, 2019
    Last edited: Jul 7, 2019