Solved Cast Itemstack to type Potion

Discussion in 'Plugin Development' started by John Cameron, May 21, 2013.

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

    John Cameron

    Ok, here is what I got so far...
    Code:
    if(material == Material.POTION){
                    Item tmp = (Item) item;
                    Potion potion = (Potion) tmp;
                    //Do stuff with the potion
    }
    And it gives me this error
    Code:
    2013-05-21 17:27:09 [SEVERE] java.lang.ClassCastException: org.bukkit.craftbukkit.v1_5_R3.inventory.CraftItemStack cannot be cast to org.bukkit.entity.Item
    So, how do I do it?
    Thanks! :)

    Bump

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

    Omerrg

    The "item" that you used is importen with bukkit,craftbukkit.v1_5_R3.inventory.CraftItemStack instead of org.bukkit.inventory.ItemStack (Im not sure about the second import) and you can't cast a craftbukkit type to an bukkit api type. Import the right ItemStack and than try again.
     
  3. Offline

    John Cameron

    Are
    Are these imports correct?
    Code:
    import org.bukkit.entity.Item;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.Potion;
    
     
  4. Offline

    evilmidget38

    That's actually completely wrong. The real issue, is that he's trying to cast an ItemStack to an Item. An Item is the entity used when an Item is on the ground, an ItemStack is what you have in your inventory. They're incomparable types.

    The reason it shows as being CraftItemStack is because CraftItemStack extends ItemStack, and that ItemStack happens to be a CraftItemStack(it was probably retrieved from an Inventory).

    As for what the OP should do, I have no idea, as I have no idea what they're trying to do or achieve.
     
    1 person likes this.
  5. Offline

    John Cameron

    Thanks for the info!
    but anyway that I can cast an ItemStack to a potion?
     
  6. Offline

    Omerrg

    Thx for correcting me, Sorry for the mistake. Now that i rewatch your topic, He's right. The Item is the entity fo a dropped item as much as i know, And you can't just cast an itemstack to an item, Its like making an item in inventory to a dropped item IN THE INVENTORY. well, you can drop an item in a specific location with the same itemstack and you will get an Item entity returned with world.dropItem(loc,itemstack) where loc is the location, itemstack is the itemstack and world is the specific world. Hope i helped, And sorry for the mistake.
     
  7. Offline

    John Cameron

    Ok I'll re word it,

    Lets say that I have a potion in my inventory.
    But I want to get the potion type, duration, etc.
    However, this can only be done by using the potion type variable.
    So, how to get potion data from an itemstack?
     
  8. Offline

    evilmidget38

  9. Offline

    John Cameron

Thread Status:
Not open for further replies.

Share This Page