Code not giving posion to nearby players

Discussion in 'Plugin Development' started by xCyclonez, Jul 31, 2017.

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

    xCyclonez

    Code:
    @EventHandler
        public void onMove(PlayerMoveEvent e) {
          
            Player p = e.getPlayer();
            if(p.getInventory().getBoots().getItemMeta().getLore().contains(ChatColor.RED + "Cyanide")) {
            for (Player other : Bukkit.getOnlinePlayers()) {
                if (other.getLocation().distance(p.getLocation()) <= 5) {
                other.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 40, 2));
                }
                }
            }        
        }
    
    }
    Not sure why this isn't giving players who are in range of 5 blocks posion. Thanks!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @xCyclonez 1. That will cause NullPointerExceptions when the player has no boots or there is not lore on them.
    2. Did you register the event?
    3. If you did register the event, does it work without the inventory check?
     
  3. Offline

    xCyclonez

    @timtower I did register the event, and I commented out the inventory check, but it still does not work.
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    xCyclonez

    Code:
    package com.xcyclonez.chestdrop.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class CustomEnchants implements Listener{
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
          
            Player p = e.getPlayer();
            //if(p.getInventory().getBoots().getItemMeta().getLore().contains("Cyanide")) {
            for (Player other : Bukkit.getOnlinePlayers()) {
                if (other.getLocation().distance(p.getLocation()) <= 5) {
                other.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 40, 2));
                }
                }
            }        
        }
    
    //}
    @timtower
     
    Last edited by a moderator: Jul 31, 2017
  6. Offline

    Machine Maker

    @xCyclonez
    1. This is not the issue, but you should use location.distanceSquared(otherLoc) especially if this is in PlayerMoveEvent which is called very often.
    2. Try printing out the distance inside the for loop to see if the distance if under 5 (25 if you use distance squared).
     
  7. Offline

    xCyclonez

    How would I do this?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    xCyclonez

    public class Main2 extends JavaPlugin{

    public void onEnable(){
    PluginDescriptionFile pdfFile = getDescription();
    Logger logger = getLogger();
    logger.info(pdfFile.getName() + " has been enabled! Created by xCyclonez");


    this.getServer().getPluginManager().registerEvents(new CustomEnchants(), this);

    }
    public void onDisable(){
    PluginDescriptionFile pdfFile = getDescription();
    Logger logger = getLogger();
    logger.info(pdfFile.getName() + " has been disabled!");
    }





    }
    @timtower, this is it.
     
  10. Offline

    Machine Maker

    @xCyclonez
    1. You do not need to log your own plugins. Bukkit does this for you.

    2. Inside the for loop in the CustomEnchants class, before the if statement, do
    Code:
    System.out.println(other.getLocation().distanceSquared(p.getLocation()));
     
  11. Offline

    MrGriefer_

    Wouldn't it cause so many lag since every time a player moves you're looping for all players in the server?
     
Thread Status:
Not open for further replies.

Share This Page