Solved Check if ItemStack is a dye and turn it into the dye

Discussion in 'Plugin Development' started by PDKnight, Apr 21, 2015.

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

    PDKnight

    Hi, Bukkit,
    I'm playing around the InventoryClickEvent and I don't know how to compare two ItemStacks. Here's my code:
    Code:java
    1. // cur is first ItemStack, dyeX is the second ItemStack
    2. Dye dye = new Dye();
    3. dye.setColor(DyeColor.RED);
    4. ItemStack dye4 = dye.toItemStack();
    5. dye4.setAmount(1);
    6. dye.setColor(DyeColor.ORANGE);
    7. ItemStack dye3 = dye.toItemStack();
    8. dye4.setAmount(1);
    9. dye.setColor(DyeColor.PURPLE);
    10. ItemStack dye2 = dye.toItemStack();
    11. dye4.setAmount(1);
    12. dye.setColor(DyeColor.CYAN);
    13. ItemStack dye1 = dye.toItemStack();
    14. dye4.setAmount(1);
    15.  
    16. if(cur.equals(dye1)
    17. || cur.equals(dye2)
    18. || cur.equals(dye3)
    19. || cur.equals(dye4)
    20. ) {
    21. int type = 4;
    22. if(cur.equals(dye4) ) type=4;
    23. if(cur.equals(dye3) ) type=3;
    24. if(cur.equals(dye2) ) type=2;
    25. if(cur.equals(dye1) ) type=1;
    26. ((Player)event.getWhoClicked()).sendMessage(""+type); //still gives "4" or nothing
    27. }

    So I have 2 questions together:
    1. How to compare two dyes?
    2. How to turn ItemStack into a dye (after checking it with condition, if it's a dye)?
    Thanks in advance. Cheers :D
     
  2. Offline

    Zombie_Striker

    There is a better, older, way that still works but is deprecated.

    ItemStack.getData();/.setData();

    Each returns a byte, And if you scroll down to dyes here, you can just cast the dyeID to a byte.
     
    PDKnight likes this.
  3. Offline

    PDKnight

    Oh yes, it works now! Thanks @Zombie_Striker !! The final code:
    Code:java
    1. Dye dye = new Dye();
    2. dye.setColor(DyeColor.RED);
    3. ItemStack dye4 = dye.toItemStack();
    4. dye4.setAmount(1);
    5. dye.setColor(DyeColor.ORANGE);
    6. ItemStack dye3 = dye.toItemStack();
    7. dye4.setAmount(1);
    8. dye.setColor(DyeColor.PURPLE);
    9. ItemStack dye2 = dye.toItemStack();
    10. dye4.setAmount(1);
    11. dye.setColor(DyeColor.CYAN);
    12. ItemStack dye1 = dye.toItemStack();
    13. dye4.setAmount(1);
    14.  
    15. if(cur.getType() == Material.INK_SACK) {
    16.  
    17. MaterialData d = cur.getData();
    18. if(d.equals(dye4.getData())
    19. || d.equals(dye3.getData())
    20. || d.equals(dye2.getData())
    21. || d.equals(dye1.getData())
    22. ) {
    23. int type = 4;
    24. if(d.equals(dye4.getData()) ) type=4;
    25. if(d.equals(dye3.getData()) ) type=3;
    26. if(d.equals(dye2.getData()) ) type=2;
    27. if(d.equals(dye1.getData()) ) type=1;
    28. ((Player)event.getWhoClicked()).sendMessage(""+type);
    29. }
    30. }
     
  4. Offline

    Zombie_Striker

    @PDKnight I works, but a cleaner way would be to do this.
    Code:
    public int getColor(ItemStack is){
        return (int) is.getData();
    }
    
    /* This is just a table just so you know which color is which
            0    White
        1     Orange
        2     Magenta
        3     Light Blue
        4     Yellow
        5     Lime
        6     Pink
        7     Gray
        8     Light Gray
        9     Cyan
        10     Purple
        11     Blue
        12     Brown
        13     Green
        14     Red
        15     Black
    */
     
    PDKnight likes this.
  5. Offline

    PDKnight

    Great, thanks!
     
Thread Status:
Not open for further replies.

Share This Page