Solved Onmove event Else not working

Discussion in 'Plugin Development' started by witkwakje, Dec 1, 2017.

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

    witkwakje

    I have a problem, im making a plugin that checks if u got that enchant on ur boots and if u do it will give u a effect.
    It works just fine but for some reason the Else doesnt work does anyone know the anwser please comment down below.
    Code:
    @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player player = (Player) e.getPlayer();
            if(player.getEquipment().getBoots().getItemMeta().getLore().contains(ChatColor.GOLD + "Gears III")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 2));
            }
            if(player.getEquipment().getBoots().getItemMeta().getLore().contains(ChatColor.BLUE + "Jump III")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 2));
            }
            else {
                player.removePotionEffect(PotionEffectType.SPEED);
                player.removePotionEffect(PotionEffectType.JUMP);
            }
           
           
           
        }
     
  2. Offline

    RunsWithShovels

    Code:
      if(player.getEquipment().getBoots().getItemMeta().getLore().contains(ChatColor.GOLD + "Gears III")) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 2));
            }else{
                player.removePotionEffect(PotionEffectType.SPEED);
    }
    place an else catch for each if statement to handle the boolean for it.
     
  3. Offline

    witkwakje

    Code:
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            Player player = (Player) e.getPlayer();
                if(player.getEquipment().getBoots().getItemMeta().getLore().contains(ChatColor.GOLD + "Gears III")) {
                            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 2));
                        }else{
                            player.removePotionEffect(PotionEffectType.SPEED);
                }
               
               
               
            }
    Still doesnt Work ;(
     
  4. Offline

    Caderape2

    @witkwakje What does not work ? do you have an error ? nothing happen ? Did you try to debug ?
    -Your code might cuz a lot of errors since not every boot has a displayname.
    - And you should use a repeating task for this.
     
  5. Offline

    witkwakje

    i get this console error
    and in game it doesnt remove the effects if i deEquip the boots
    sorry for disturbing u im not that good at coding yet
     
  6. Offline

    Caderape2

    @witkwakje Yes it's not removing effect cuz your code throw a NPE before the effect is removed. As i said, you don't check if the item is null, if it has itemmeta, or a lore.

    Don't use that event, since it can be called 10x by second for each player. Go for a repeating task every one or two second and loop online players. this will be less laggy.
     
  7. Offline

    witkwakje

    This is my new code so i dont have all those errors
    Code:
    if(player.getInventory().getBoots() != null) {
                if(player.getEquipment().getBoots().getItemMeta().getLore().contains(ChatColor.GOLD + "Gears III")) {
                            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, 2));
                }else {
                    player.removePotionEffect(PotionEffectType.SPEED);
                }
                if(player.getEquipment().getBoots().getItemMeta().getLore().contains(ChatColor.BLUE + "Jump III")) {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1000000, 2));
                } else {
                    player.removePotionEffect(PotionEffectType.JUMP);
                }
    }
    yes it works just when the boots has Gears III
     
  8. Offline

    mehboss

    You still haven't null checked to see if the item even has an ItemMeta or a Lore..

    After you fix this:
    • Any errors in console?
    • Did you try debugging?
    Also, as @Caderape2 said, it could potentially be laggy. I'd advise you setup a delayed repeating task, or even better, use something like this: https://www.spigotmc.org/resources/lib-armorequipevent.5478/
     
    Last edited: Dec 5, 2017
Thread Status:
Not open for further replies.

Share This Page