Solved Cast Potion

Discussion in 'Plugin Development' started by MrGeneralQ, Jun 6, 2016.

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

    MrGeneralQ

    I'm trying to Cast an ItemStack to a potion. But it just doesn't work. Casting it to an item and then to an Potion works in code but crashes when triggering the event .

    I got this so far:

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void playerConsume (PlayerItemConsumeEvent e){
            Player player = e.getPlayer();
            ItemStack item = e.getItem();
           
    
            if(item.getType() == Material.POTION){
               
                Potion potion = (Potion) e.getItem().getType();
               
               
               
            }
     
  2. Offline

    Xerox262

  3. Offline

    MrGeneralQ

    @Xerox262

    What I am trying to do is seperate the different potions. Since all the potions have the same ID and are the same Material type.
     
  4. Offline

    Zombie_Striker

    @MrGeneralQ
    You are not casting an Itemstack to a potion, you are casting the Itemstack's Type to a potion. Even still, you should use PotionMeta (if you are running a 1.9 server) if you want to get potion types.
     
  5. Offline

    MrGeneralQ

    @Zombie_Striker

    Bassicly what I am trying to do is check if a specific potion has been consumed. If it is... then cancel the event.
     
  6. Offline

    Zombie_Striker

    @MrGeneralQ
    1. Check if the itemstack is a potion
    2. Get the itemstacks item meta. Cast it to a PotionMeta.
      Use PotionMeta.getCustomEffects to return a list of all the potion effects.
    3. If the potion has the effect you want, cancel the event.
     
  7. Offline

    MrGeneralQ

    I'll try this! Thank you! I'll keep you up to date.

    @Zombie_Striker

    But how do I check it in a string format. I'm looking for data then I can read from a config file. I would use ItemID but they are all the same.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  8. Offline

    Xerox262

    @MrGeneralQ Check it in string format? You mean the PotionType?

    PotionMeta#getBasePotionData(); to get the PotionData
    PotionData#getType(); to get the PotionType

    P.S. The ItemStack durability shows you which potion it is, however you should use the PotionData
     
  9. Offline

    MrGeneralQ

    Thank you. Im on my Phone atm but I try this tomorrow.

    UPDATE: It worked! Thnx!
     
    Last edited: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page