How do I make a scheduled time event to prevent spamming potion effect when equipping armor?

Discussion in 'Spigot Plugin Development' started by Xaziss, Feb 14, 2018 at 6:32 PM.

  1. Hi sry for confusing title, would you be able to help me with a plugin I'm coding using Borlea's ArmorEquipEvent libraries? I'm just equipping diamond boots, then giving a potioneffect, then dequipping to take that potioneffect away. Everything works fine, but when you spam click the effect stays even when the boots aren't on.

    I noticed someone said you could fix it with a scheduled time event to prevent the pot effect spamming, would someone be able to help me by showing me an example of schedule time event programming with this code? i'm sorry, i'm fairly new at java.
    Here is my code:

    @EventHandler
    public void onDequip(ArmorEquipEvent e) {
    if(e.getOldArmorPiece() != null && e.getOldArmorPiece().getType() != Material.AIR && e.getOldArmorPiece().getType() == Material.DIAMOND_BOOTS && e.getOldArmorPiece().getItemMeta().getLore().contains("Test")) {
    e.getPlayer().removePotionEffect(PotionEffectType.SPEED);
    }
    }

    @EventHandler
    public void onEquip(ArmorEquipEvent e) {
    if(e.getNewArmorPiece().getType() == Material.DIAMOND_BOOTS && e.getNewArmorPiece().getItemMeta().getLore().contains("Test")) {
    e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 99999, 2));
    }
    }

    How could i time it to make it so that when they spam it doesn't accidentally leave the effects, or if there is a better method?
    I looked at the wiki but i'm not sure how i would make a scheduled event for this code. thank you so much again, i'm sorry i'm fairly inexperienced so thank you for your patience :)
     
  2. Check if they de-equip armor and then remove the effect that the armor gives. That should fix it.
     
  3. That is what onDequip does, it works. The problem is when they open up the inventory and spam the armor on and off really fast it lags the tick updates and doesn't call onDequip sometimes and leaves the potion effect on. I'm wondering if there is a way to delay adding the armor effects for a certain amount of time when they dequip the armor so when they spam it on it doesn't lag, but i'm not sure how to.
     
  4. Why lib for this one?
     
  5. @Xaziss
    Code (Text):
    By the way you can use this to put your code in next time ;)
     
  6. Set the Scheduler in void onEnable(){
     
  7. I actually have a test build that should hopefully fix this.
    Related links are:
    https://github.com/Borlea/ArmorEquipEvent/releases/tag/1.7.0
    https://github.com/Borlea/ArmorEquipEvent/commit/ac706517d6d5af30c53a19ed5c43c00c3b7802f1

    Also I suggest not making 2 separate methods for 1 event and if you want to clean stuff up just do some checks in the event handler and call a method based on equip/unequip.

    If the issue persists even while using the 1.7 build I'll look into potentially other causes.
     
  8. Wow thank you so much Borlea, your 1.7 build seems to have fixed the spamming issue for me :D the armor effects are taken away even with the spamming with this build, and I combined the two methods into one. thx so much

    Cheers <3


    EDIT -- There actually is still a small chance that the effects are still applied when the armor is not equipped from spamming the armor slot still. It's a small chance but i got it to happen, the addition still reduces the chance by a large margin tho.
     
    #8 Xaziss, Feb 15, 2018 at 12:49 AM
    Last edited: Feb 15, 2018 at 1:32 AM
  9. Check if they have the armor equiped an then give them the effect for like 5 seconds and repeat it.
     

Share This Page