Solved Get color from colored blocks

Discussion in 'Plugin Development' started by TheA13X, Dec 2, 2013.

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

    TheA13X

    Hello people. I have a bunch of questions again but I think I will just ask this one.

    The title says everything. I have to get a color from blocks like wool, clay, or the new stained glass.
    With wool it's easy, because it has it's own class, with colorable implemented. So I just have to write
    Code:java
    1. if(((Wool)woolblock.getState().getData()).getColor.equals(DyeColor.RED)){
    2. //it IS red
    3. }

    .
    But how about the other ones? they don't have any classes or something.
    How can I get the color from colored blocks?

    Hoping for some help!
     
  2. Offline

    fireblast709

    TheA13X Might be added to the API on a later state. For now you could write an enumeration as a temporary replacement.
     
  3. Offline

    TheA13X

    okay. Thanks, but how can an enumeration help?
    Shouldn't I have a way to assign the color of the blocks first?
     
  4. Offline

    fireblast709

    TheA13X Just to make your code more readable :p.
    Code:java
    1. public class ColourUtil
    2. {
    3. private static Set<Material> colourable = EnumSet.of(Material.WOOL, Material.STAINED_GLASS, Material.STAINED_CLAY, Material.STAINED_GLASS_PANE);
    4.  
    5. public static ItemStack colourIt(Material mat, Wool colour) throws IllegalArgumentException
    6. {
    7. if(!colourable.contains(mat))
    8. throw new IllegalArgumentException("Material is not colourable!");
    9. return new ItemStack(mat, 1, (short)colour.getData());
    10. }
    11.  
    12. public static void colourIt(Block block, Wool colour) throws IllegalArgumentException
    13. {
    14. if(!colourable.contains(block.getType()))
    15. throw new IllegalArgumentException("Material is not colourable!");
    16. block.setData(colour.getData());
    17. block.getState().update(true);
    18. }
    19.  
    20. public DyeColor getColour(Block block)
    21. {
    22. if(!colourable.contains(block.getType()))
    23. throw new IllegalArgumentException("Material is not colourable!");
    24. byte data = block.getData();
    25. return DyeColor.getByWoolData(data);
    26. }
    27.  
    28. public DyeColor getColour(ItemStack stack)
    29. {
    30. if(!colourable.contains(stack.getType()))
    31. throw new IllegalArgumentException("Material is not colourable!");
    32. byte data = stack.getDurability();
    33. return DyeColor.getByWoolData(data);
    34. }
    35. }
    A small utility class for colours. The colouring seems to be the same as for wool (how surprising).
     
Thread Status:
Not open for further replies.

Share This Page