ClassCastException

Discussion in 'Spigot Plugin Development' started by BlueMond, Apr 20, 2017.

  1. Hello, I have a small problem with trying to cast a MaterialData to Wool for some reason, even though I thought Wool was a MaterialData.

    Code (Text):
     
    return ((Wool)block.getState().getData()).getColor() == dyeColor;
     
    In the case I tested it is giving this issue with a carpet block, but I'm pretty sure the data is handled the same as a wool block.
     
    • Funny Funny x 1
  2. Erm, help?
    EDIT: If you need more details please ask.
     
  3. Print the class from the MaterialData, that'll tell you what it is
     
    • Like Like x 1
  4. Wool the and result from getData doesn't have a good inheritance...
     
    • Like Like x 1
  5. @ramidzk
    I must be misinterpreting what the Wool class is. Do you have an idea as to how else I would check what the color of a wool block is?
     
  6. I will try that.
     
  7. I even added a check to make sure it's an instance of Wool in case it was doubted, but it is still giving me a classcastexception and I cannot understand why and have no idea how I would get what color the wool is without such a cast.

    Code (Text):

    MaterialData blockData = block.getState().getData();

    if(blockData instanceof Wool){
       return ( ((Wool) blockData).getColor() == dyeColor );

    }else{
       return false;
    }
     
     
  8. @DarkSeraphim
    My mistake. I see what you were saying now. You can use the actual value of the MaterialData, which turns out to be a byte, as the data of the block which resolves to the corresponding byte value for the color if the block is wool.

    Here is the new, working code in the case of white carpet in case anyone wants it for future reference.
    Code (Text):

    MaterialData blockData = block.getState().getData();

    System.out.println(blockData.getData() == (byte)0);
    return blockData.getData() == (byte)0;
     
    EDIT: I strayed from using the data because I was afraid to use deprecated functions.