Solved Getting custom potion type on PlayerItemConsumeEvent

Discussion in 'Plugin Development' started by torpkev, Nov 14, 2018.

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

    torpkev

    I've created a new recipe on a potion, which works:
    Code:
    ItemStack item = new ItemStack(Material.POTION);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName("§aSoda");
    List<String> lore = new ArrayList<String>();
    lore.add("Soda Name here");
    meta.setLore(lore);
    item.setItemMeta(meta);
    PotionMeta hpotmeta = (PotionMeta) meta;
    hpotmeta.setColor(Color.PURPLE);
    item.setItemMeta(hpotmeta);
    NamespacedKey key = new NamespacedKey(this, "soda_1");
    ShapedRecipe recipe = new ShapedRecipe(key, item);
    recipe.shape(" D ", "CPC", " D ");
    recipe.setIngredient('P', Material.POTION);
    recipe.setIngredient('C', Material.COAL);
    recipe.setIngredient('D', Material.PURPLE_DYE);
    Bukkit.addRecipe(recipe);
    
    The crafted potion has my custom name (in green) and the lore.

    I'd like to detect when this particular potion is consumed, so I'm using the PlayerItemConsumeEvent
    Code:
    public class OnConsume implements Listener {
        @EventHandler
        public void onConsume(PlayerItemConsumeEvent evt) {  
            ItemStack istack = evt.getItem();
            ItemMeta imeta = istack.getItemMeta();
            Player player = evt.getPlayer();
            player.sendMessage(istack.getType().name());
            player.sendMessage(istack.getType().getKey().toString());
            player.sendMessage(imeta.getDisplayName());
        }
    }
    
    This sends the following:

    POTION
    minecraft: potion
    Soda (but in green)

    I would have expected the key value to be soda_1 rather than minecraft: potion, so instead I tried getting the type from the name

    So I would expect the following to work:
    Code:
            if (imeta.getDisplayName() == "§aSoda")
            {
                player.sendMessage("1");
            }
    
    But the message is never sent.
    I've also tried just "Soda" and ChatColor.GREEN + "Soda" without success.

    I can't really use PotionEffect as I do not want any effects on the potion, I want to run some other code when the player drinks the potion.

    Is there some better approach I could be using to identify which custom potion it is?

    Thanks
     
    Last edited by a moderator: Nov 14, 2018
  2. Offline

    timtower Administrator Administrator Moderator

    @torpkev Strings need to be compared with equals or equalsIgnoreCase, == will just return false for Strings.
     
  3. Offline

    torpkev

    Well that would explain a lot.. my experience with other programming languages and just learning java showing through.

    Thanks - that did the trick
     
  4. Offline

    timtower Administrator Administrator Moderator

    The difference between all the programming languages doesn't make it any easier indeed :p
    Marked as solved.
     
Thread Status:
Not open for further replies.

Share This Page