Best way to check for potion type

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 13, 2017.

  1. Hey

    This is my code:

    Code (Text):
    if (e.getItem().equals(new Potion(PotionType.AWKWARD))) {
    But this doesnt trigger anything when I drink an awkward potion.

    What is wrong?
     
  2. NathanWolf

    Supporter

    You can get the ItemMeta from the ItemStack, check that it's a PotionMeta, then look at getBasePotionData().getType().

    If you have custom potions with additional effects you'll need to check getCustomEffects as well.
     
  3. Okay I tried finding the functions in the docs I found them but I cant use them somehow?

    This is my full code:

    Code (Text):
    package me.kyllian.awkward.listeners;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    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.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.PotionMeta;
    import org.bukkit.potion.Potion;
    import org.bukkit.potion.PotionType;

    import me.kyllian.awkward.Awkward;

    public class OnPlayerItemConsume implements Listener {

        private Awkward main = Awkward.getInstance();

        @EventHandler
        public void onPlayerItemConsumeEvent(PlayerItemConsumeEvent e) {
            if (e.getItem().equals(Material.POTION)) {
                if (e.getPlayer().hasPermission(main.getConfig().getString("PermissionNode"))) {
                    List<String> quotes = main.getConfig().getStringList("AkwardQuotes");

                    Random r = new Random();
                    String toSay = quotes.get(r.nextInt(quotes.size()));
                    if (toSay.contains("%random%")) {
                        ArrayList<String> onlinePl = new ArrayList<String>();
                        for (Player online : Bukkit.getOnlinePlayers()) {
                            if (e.getPlayer() != online) {
                                onlinePl.add(online.getName());
                            }
                        }
                        if (!onlinePl.isEmpty()) {
                            toSay = toSay.replace("%random%", onlinePl.get(r.nextInt(onlinePl.size())));
                        } else {
                            toSay = toSay.replace("%random%", e.getPlayer().getName());
                        }
                    }
                    e.getPlayer().chat(toSay);
                }
            }
        }
    }
     
     
  4. BTW
    Code (Text):
    e.getItem().equals(Material.POTION)
    should be
    Code (Text):
    e.getItem().getType() == Material.POTION
     
  5. Yes thanks but it still cant find the potiondata stuff
     
  6. Something like that i guess
    Code (Text):
    if (event.getItem() != null && event.getItem().hasItemMeta()) {
        if (event.getItem().getItemMeta() instanceof PotionMeta) {
            final PotionMeta meta = (PotionMeta) event.getItem().getItemMeta();
            final PotionData data = meta.getBasePotionData();
            if(data.getType() == PotionType.AWKWARD) {
                ...
            }
         }
    }
     
    • Winner Winner x 2
  7. NathanWolf

    Supporter

    Well .. I mean.. I gave that exact answer minus the spoonfeeding :\

     
  8. Ind
    Indeed but I tried that and I didn't quite know how everything worked