Solved Can't cast Dark Oak or Acacia logs to type Tree

Discussion in 'Plugin Help/Development/Requests' started by Scullyking, Apr 11, 2015.

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

    Scullyking

    Error:
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.material.MaterialData cannot be cast to org.bukkit.material.Tree
    This doesn't make sense, is this a known bug?
    If so I need another way of knowing if a log broken was Dark Oak or Acacia.

    Thanks
     
  2. Offline

    GODofCRAFTERS

    The problem is you're trying to cast MaterialData to a Material, which in theory can't be done. If you post the part of the code that's generating the error, I'd be happy to tell you what's wrong.
     
  3. Offline

    Scullyking

    @GODofCRAFTERS
    Code:
    Tree woodType = (Tree) woodChopped.getState().getData();
    woodChopped is a block.
    This error only occurs for acacia and dark oak logs.
     
  4. Offline

    GODofCRAFTERS

    well Acacia and Dark Oak are newer types. They aren't Trees themselves. So it would be better if you assigns them to a Material or ItemStack and give the MetaData for it. I'd still need a broader code to understand whatever you're plugin is doing, if you're at liberty to disclose such information.
     
  5. Offline

    Scullyking

    @GODofCRAFTERS
    1. Get the block broken by the player and store it in woodChopped
    2. Cast to a Tree type
    3. Get TreeSpecies

    Is the type of log anywhere in the item meta data? I'm really not sure how to get at it.
    In other words: given just the block broken, and not casting it to a Tree, how can I get the type of log?
     
  6. Offline

    GODofCRAFTERS

    Code:
    Tree woodType = (Tree) e.getClickedBlock().getState();
    You're getting the data, don't do that. You need to only get the block. Just try and catch the above statement and don't print exceptions, simple :)
     
  7. Offline

    Scullyking

    @GODofCRAFTERS
    Will try in the morning, time to sleep now - thanks

    @GODofCRAFTERS
    Ok this doesn't work. I have getData() on the blockstate to be able to cast to a tree or I get this error:
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.block.CraftBlockState cannot be cast to org.bukkit.material.Tree
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  8. Offline

    Xerox262

    Try .getState().getType()
     
  9. Offline

    Scullyking

    @Xerox262
    Nope, that doesn't even compile.

    I want to make it clear that this is possible with the other types of logs. And for that reason I assume this is because the Tree class hasn't yet been updated to deal with dark oak and acacia logs.

    I am therefore asking for an alternative method of getting the tree species without having to cast to a Tree.
     
  10. Offline

    Xerox262

    if(e.getClickedBlock().getType() == Material.LOG_2){
    it's a darkoak or acacia
    }

    ? :p
     
  11. Offline

    Scullyking

    @Xerox262
    Ok very funny, and now how about getting which one is which
     
  12. Offline

    Xerox262

    What exactly are you trying to do with this anyway? give the player the block they broken into their inventory?
     
  13. Offline

    Scullyking

    @Xerox262
    Give the player the plank version of the log.
     
  14. Offline

    Xerox262

  15. Offline

    Scullyking

Thread Status:
Not open for further replies.

Share This Page