Remove potion effect on movement

Discussion in 'Plugin Development' started by nowhere, Sep 13, 2017.

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

    nowhere

    So i want that when the user runs /spawn it will give them nausea for 5 seconds which is the amount of time for teleport to spawn but if they move, i want the nausea to be removed instantly

    this is what i worked so far which i believe does the trick but cant figure out the movement part.
    Code:
        @Override
        public boolean onCommand (CommandSender sender, Command cmd, String label, String[] args) {
          
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("spawn")) {
                 p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 200, 100));
                 p.chat("/espawn");
               
            }
          
            return false;
        }
      
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            for (PotionEffect effect : p.getActivePotionEffects())
                p.removePotionEffect(effect.getType());
          
                    return;
                }
     
  2. Offline

    yPedx

    @nowhere
    The for loop only removes one potion effect cause you return in the loop. Did you register the event?
     
    ipodtouch0218 likes this.
  3. Offline

    Edvio

    No need to use p.chat(string), you can use Bukkit.dispatchCommand(p, "espawn")
    In the for loop, you don't have a {, and you're returning inside the foor loop, which will never work, it will just cancel it.
    Also, doing that type of move event is very inefficient and could cause lag, instead I would recommend doing it only when they move 1 block, not every time they move their mouse, cause if I'm correct it doesn't cancel when you just move your mouse in /espawn
     
  4. Offline

    nowhere

    @Edvio @yPedx

    Code:
        ArrayList<Player> myArrayList = new ArrayList<Player>();
            
            @EventHandler
           
            public void onPlayerMove(PlayerMoveEvent event) {
                Player player = event.getPlayer();
                if(myArrayList.contains(player));
                    Location locationPre = event.getFrom();
                    Location locationCur = event.getTo();
                    if ((locationCur.getBlockX() != locationPre.getBlockX()) || locationCur.getBlockY() != locationPre.getBlockY() || locationCur.getBlockZ() != locationPre.getBlockZ()){
                             player.hasPotionEffect(PotionEffectType.CONFUSION);
                    {
                        player.removePotionEffect(PotionEffectType.CONFUSION);
    Now it checks if a player moves a block but doesn't seem to match with essentials spawn cooldown
    Like i dont know if is because of lag but if you move 1 block after you do /espawn which is essentials command the cooldown is still running until you reach 2 blocks or 3.. im not sure if is lag or not.. and im not quite sure how to use its api
     
Thread Status:
Not open for further replies.

Share This Page