Solved Checking if an item is a Potion

Discussion in 'Spigot Plugin Development' started by Roree, May 17, 2016.

  1. Checking if the Material is a potion doesn't work, and I'm unsure what else to do

    I also tried this
    Code (Java):
        private boolean isPotion(ItemStack item) {

            try {
                Potion.fromItemStack(item).getLevel();
            } catch (IllegalArgumentException e) {
                return false;
            }
            return true;
        }
    EDIT: Solved using
    Code (Java):
        private boolean isPotion(ItemStack item) {

            try {
                Potion.fromItemStack(item);
            } catch (IllegalArgumentException e) {
                return false;
            }
            return true;
        }
     
    #1 Roree, May 17, 2016
    Last edited: May 17, 2016
  2. What event are you using to start figuring out it the item is a potion?
     
  3. Try:
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPotionDrink(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if (((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) && (player.getItemInHand().getType() == Material.POTION)) {
    5. int hs = p.getInventory().getHeldItemSlot();
    6. drink(p,hs);
    7. }
    8. }

    9. public void drink(final Player player, int hs) {
    10. plg.getServer().getScheduler().scheduleSyncDelayedTask(plg, new Runnable() {
    11. public void run() {
    12. if (player.getInventory().getItem(hs).getType() == Material.GLASS_BOTTLE) {
    13. // then ...
    14. }
    15. }
    16. }, 35);
    17. }
     
  4. I'm not even using an event this is irrelevant and unrelated to the question. Just so you know checking if an item is a Material.POTION doesn't work as I said. I solved it myself, thanks anyways