Solved [1.12.2] PlayerItemConsumeEvent not cancelling?

Discussion in 'Spigot Plugin Development' started by Nyquest, Aug 6, 2018.

  1. So, I thought I'd write a super simple gapple cooldown plugin. It's a re-write of one I used on 1.8 but for the 1.12.2 version the server changed to.

    The cooldown gives the correct duration. Adding in some debug, the if statement fires correctly so e.setCancelled(true); does get called. But the Gapple is still being eaten, the buffs are still given, etc.

    I then simply tried to remove the code inside the event and just cancel the event. Still allows me to eat apple after apple despite the event being cancelled. Any ideas?

    Code (Text):
    import java.math.RoundingMode;
    import java.text.DecimalFormat;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffectType;

    import net.md_5.bungee.api.ChatColor;

    public class GappleCooldown extends JavaPlugin implements Listener
    {

      public void onEnable()
      {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        saveDefaultConfig();
      }

      @EventHandler
      public void OnConsumed(PlayerItemConsumeEvent e)
      {
        Player p = e.getPlayer();
        if ((e.getItem().getType() == Material.GOLDEN_APPLE) && (e.getItem().getDurability() == 1)) {
          if (HasCooldown(p)) {
            e.setCancelled(true);
            p.sendMessage(GetCoolDown(p));
            p.updateInventory();
            return;
          }
          p.sendMessage(GetCoolDown(p));
        }
      }

      boolean HasCooldown(Player p)
      {
        int cd = 0;
        if (p.hasPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)) {
          cd = (getConfig().getInt("cooldown")
              - (3000 - (p.getPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)).getDuration()));
        }
        return (cd > 0);
      }

      String GetCoolDown(Player p)
      {
        double cd = 0;
        DecimalFormat df = new DecimalFormat("##.#");
        df.setRoundingMode(RoundingMode.CEILING);
        if (p.hasPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)) {
          cd = ((getConfig().getInt("cooldown")
              - (3000 - (p.getPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)).getDuration())) / 20);
        }
        int min = (int) (cd / 60);
        double sec = cd % 60;
        return ChatColor.translateAlternateColorCodes('&', "&8| &6GApple &7is on cooldown for another &b"
            + (min > 0 ? min + " &7minutes, &b" : "") + df.format(sec) + "&7 seconds.");
      }
    }
     
    #1 Nyquest, Aug 6, 2018
    Last edited: Aug 13, 2018
  2. Took a break from this and came back to it today, still can't figure it out. I get no errors in console, I get the correct messages, it just doesn't seem to cancel the event.
     
  3. Are you sure you tested your code with eating an enchanted golden apple?
    Code (Java):
    @EventHandler
    public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
        if (event.getItem().getType() == Material.GOLDEN_APPLE && event.getItem().getDurability() == 1) {
            event.setCancelled(true);
        }
    }
    I did a quick test on the code above and it cancels eating an enchanted golden apple. Maybe you could try re-coding the plugin from scratch.
     
  4. Your code looks conventionally horrible if you're a human.
    I'm sorry but I won't even try to help you like that.
     
    • Optimistic Optimistic x 2
    • Agree Agree x 1
  5. Code (Text):
    (e.getItem().getType() == Material.GOLDEN_APPLE) && (e.getItem().getDurability() == 1)
    I assume you're trying to cancel regular golden apple eating? If that's the case, change the 1 to a 0. If you want to cancel both golden apple and enchanted golden apple eating, get rid of the durability check entirely.
     
  6. I did test with enchanted golden apples. I have a feeling it's not my code that's the issue. But I can't figure out what else might be interfering. Guess my next test will be to move this to a server that has no other plugins installed and see if the issue is actually some other kind of interference?
    Nope, trying for the enchanted golden apple
    I'm not human, sorry! Next time I'll add extra lines and move my curly bracers in ways that make you less prone to unnecessary response.

    ------------

    Edit - After further testing, it was another plugin interfering and the code worked fine. The other plugin was OldCombatMechanics which altered the effects of the Enchanted Golden Apples by cancelling the event and handling things manually.

    Marked as solved.
     
    #6 Nyquest, Aug 13, 2018
    Last edited: Aug 13, 2018

Share This Page