Solved Armor that gives damages to neabry enemies with a timer

Discussion in 'Spigot Plugin Development' started by MoonNetwork, Jul 7, 2021.

  1. Hello guys. I am trying to make a custom armor that damages neabry enemies in 2 block if player wears full armor. I made it but It makes game laggy because I used playerMoveEvent. How can I make this with another way? Can you guys help to me?

    This is the full code.
    Code (Text):

    @EventHandler
       public void yetiarmor(PlayerMoveEvent event) {

           Player player = event.getPlayer();

           boolean hasarmor = false;
           for (ItemStack armorpiece : player.getInventory().getArmorContents()) {
               hasarmor = armorpiece != null && armorpiece.hasItemMeta() && armorpiece.getItemMeta().hasDisplayName()
                       && armorpiece.getItemMeta().getDisplayName().contains(ChatColor.AQUA + "Gercek");
               if (!hasarmor)
                   break;
           }

           if (hasarmor) {  ------> dont mind this part is different thing

               if (player.hasPotionEffect(PotionEffectType.SLOW)) {
                   player.removePotionEffect(PotionEffectType.SLOW);
               }
           }
           
             for (Entity entity : player.getNearbyEntities(2, 2, 2)) { if (!(entity
             instanceof LivingEntity)) continue; if
             (player.getUniqueId().equals(entity.getUniqueId())) continue; if (entity
             instanceof Damageable) { double amount = 4; ((Damageable)
             entity).damage(amount, player);
             
             }
             }
             }
     
  2. First, avoid using PlayerMoveEvent like wildfire - it VERY heavy to use.
    Here's how I would do this:

    0. Create a player armor equip event (there isn't any, unfortunately, so you have to make your own - check guides online).
    1. Create a runnable that runs on repeat (I suggest every 1 second like vanilla does). This runnable will check a special list of: for each player in the list find the entities (or players or whatever) and damage them accordingly.
    2. When a player equips/unequips an armor check if they have the proper qualifications (in your case: full armor of some kind) - if they do then add them to the list, otherwise remove them from the list (no need to check if they were in it before);
    3. When a player logs in do the same check you do when equipping/unequipping. When a player logs out remove them from the list (again, no need to check if they were in it before).
    4. You're done.​
     
    • Agree Agree x 3
  3. Where can I learn the list thing can you give some advice? I am using inventory close event for armor equip thing. I am checking the players armor containers name. And if players equip the armor the boolean will be true. I can use that. This is a example for that(I used this for another armor). https://paste.md-5.net/apamuforeg.java
     
  4. That works. When the player closes their inventory, check if they are wearing the armorpiece. If they are, set a boolean to true, and start a bukkit runnable that adds all entites within a certain radius to a list, to be damaged later. If the player is not wearing the armorpiece, simply kill the runnable, and set the boolean to false for good measure.
     
  5. You may use this for:
     
  6. Why should I try that if my way works? Thanks for suggestion.
     
  7. I am trying that now.
     
  8. Didn't you come here for advice?...
     
  9. I came here for runnable advice. I mean the inventory close event works with armors. Also I said it with no offense. Sorry if I said something wrong.
     
  10. Next time please be more specific about what you need help with. @DMan16 suggested you a nice way to do what you want to do, and I sent a project that could help you. Besides that, InventoryCloseEvent would only work if the player OPENS the inventory, place armor pieces to correct slots then CLOSES the inventory. There are a few ways to wear armors like: Right-clicking air/block while holding the armor piece. ArmorEquipEvent is fired when player uses these ways instead (see).
     
    #10 TheCaptainSleepy, Jul 7, 2021
    Last edited: Jul 24, 2021 at 1:13 AM
    • Agree Agree x 2
    • Informative Informative x 1
  11. I see. Sorry for my previous comment. I didnt think about other ways.
     
  12. Also, the list should be updated on player 'Join', 'Quit', 'death', 'armor_break' , and world_change.
    If you have not considered those yet?
     
    • Like Like x 1