Solved Changing DyeColor of already placed Wool

Discussion in 'Spigot Plugin Development' started by WAS, Apr 26, 2017.

  1. WAS

    WAS

    How do I go about this?

    Code (Text):
           Block block = e.getTo().getBlock().getRelative(BlockFace.DOWN, 1);
           block.setType(Material.WOOL);
           Wool wool = (Wool) block.getState();
           wool.setColor(colors.get(cur));
    Results in a CraftBlockState cannot be cast to org.bukkit.material.Wool

    Code (Text):
           Block block = e.getTo().getBlock().getRelative(BlockFace.DOWN, 1);
           block.setType(Material.WOOL);
           Wool wool = (Wool) block;
           wool.setColor(colors.get(cur));
    Results in a CraftBlock cannot be cast to org.bukkit.material.Wool

    Goal:
    Achieve this without using deprecated methods. Bukkit/Spigot really needs to get in the habit of not marking things deprecated without a solid replacement.

    Swear I've done this before in someones plugin. Can't find any reference though grepping my projects.
     
    #1 WAS, Apr 26, 2017
    Last edited: Apr 26, 2017
  2. Just, that not hard, a little look in the JavaDoc and everything is good
    Code (Text):
    block.setData(DyeColor.COLOR);
     
  3. WAS

    WAS

    This is a old a deprecated method. I know there is a way to do this without such nonsense. This is why we have the Wool class now.
     
  4. You're right. You can do that:

    Code (Text):
    block.setType(new Wool(DyeColor.COLOR));
     
  5. WAS

    WAS

    Uhmm, setType takes a Material class, and Wool class cannot be cast to Material.
     
  6. Oh, my bad...
    Code (Text):

    block.setType(new Wool(DyeColor.COLOR).getType());
     
     
  7. not 100% sure but I'm pretty sure you set it like this, as you would a sign
    Code (Text):
    Wool wool = (Wool) block.getState();
    While you have
    Code (Text):
    Wool wool = (Wool) block;
     
  8. He tryed both.
     
  9. WAS

    WAS

    I think that would be getItemType(). This gives you plain wool.

    ... There's two examples, the first is using getState()
     
  10. cast the block to wool, making sure it is wool first, that is. then, change the color in its block state.

    Code (Java):

    Block block = ...;
    if (block instanceof Wool) {
         ((Wool)block.getState().getData()).setColor(DyeColor...);
    }
     
    you may need to update the block state after doing that, not too sure, i dont work with blockstates too often.
     
    • Agree Agree x 1
  11. WAS

    WAS

    It can only be wool if you look at the code.
     
  12. yes, then cast wool to it, and manipulate the block state's data (color in this case). as i said above
     
    • Agree Agree x 1
  13. WAS

    WAS

    Did you even read the original post?
     
    • Optimistic Optimistic x 1
  14. you're trying to set the color of a wool that is already placed in the world, aren't you? what i said, does just that..
     
    • Agree Agree x 2
  15. no just the title
     
    • Agree Agree x 1
    • Funny Funny x 1
  16. WAS

    WAS

    First of all, we can't cast to wool (that's the inherent problem I was having), second of all the method setColor(DyeColor) is undefined for the type MaterialData. If you force cast with parenthesis; there is a Type mismatch: cannot convert from void to Wool

    And again, trying to avoid deprecated methods. Pretty sure the reason we have the Wool class and setColor.

    Update: The method ChefJava provides does work, I was implementing it wrong.

    Update to the Update: This method actually does not work and is not changing the color of the wool, while providing no error. With updating state as well.

    Code (Text):
           block.setType(Material.WOOL);
           ((Wool)block.getState().getData()).setColor(colors.get(cur));
     
    #16 WAS, Apr 26, 2017
    Last edited: Apr 26, 2017
    • Like Like x 2
  17. to any future users, the solution is as follows:

    Code (Java):

    MaterialData materialData = location.getBlock().getState().getData();
    BlockState state = location.getBlock().getState();
    if (materialData instanceof Wool) {
        Wool wool = (Wool) materialData;
        wool.setColor(DyeColor...);
        state.setData(materialData);
        state.update();
    }
     
    • Informative Informative x 1