Solved Get dyed armor color

Discussion in 'Plugin Development' started by galaipa, Jun 23, 2014.

Thread Status:
Not open for further replies.
  1. Hi! I want to get a armors color. There are several posts in Bukkit but I haven't found the solution. This is what I have tryed
    Code:java
    1. if (e.getCurrentItem () == new ItemStack(Material.LEATHER_HELMET)){
    2. if (e.getCurrentItem().getItemMeta().getColor() == Color.RED)){
    3.  
    4. }


    Thx
     
  2. Offline

    AoH_Ruthless

    galaipa
    Well duh that's not going to work. You are checking if the color is red then you aren't doing anything with it! You aren't even printing or anything.
     
  3. galaipa, I'm confused as to what you're looking for. Do you want to dye the armor or check if the armor is a certain color?
    Never the less, I'd use .equals() instead of ==
     
  4. AoH_Ruthless I know Its not working. I check it adding a log.info. I removed it to post it here.
    RAFA_GATO_FOFO I want to check it yes
     
  5. Offline

    xTigerRebornx

    RAFA_GATO_FOFO Enum#equals() calls ==, so use == to be null-safe :p
    galaipa Have you tried casting the ItemMeta to LeatherArmorMeta?
     
    Zupsub likes this.
  6. xTigerRebornx
    If you mean this, is not working
    Code:java
    1. else if (e.getCurrentItem().getType() == Material.LEATHER_HELMET){
    2. LeatherArmorMeta leather = (LeatherArmorMeta) e.getCurrentItem().getItemMeta();
    3. if (leather.getColor() == Color.RED){
    4. player.sendMessage("Gorria");
    5. }
    6. }
     
  7. Offline

    xTigerRebornx

    galaipa What about it doesn't work? Does it throw errors? Does it not execute?
     
  8. Offline

    Zupsub

    Debug your plugin to see, what if statement cause false.
     
  9. Zupsub Sorry I don't know how to debug it :eek:
     
  10. Offline

    np15isntboss

    galaipa I would first off make sure that that else if statement is being hit. If you are debugging this piece of code, make sure the prior if statement(s) return false so that this code is hit. You could also just comment them out for the time being. Then, like xTigerRebornx, suggested, I would change the


    Code:java
    1. else if (e.getCurrentItem().getType() == Material.LEATHER_HELMET)
    2. {
    3. }


    to:

    Code:java
    1. else if (e.getCurrentItem().getType().equals(Material.LEATHER_HELMET))
    2. {
    3. }



    and the

    Code:java
    1. if (leather.getColor() == Color.RED)
    2. {
    3. }


    to:

    Code:java
    1. if (leather.getColor().equals(Color.RED))
    2. {
    3. }


    That will ensure the a logically correct return from those statements, and I am not saying that what you did was wrong, but it can return something that you would not expect. Also, it is just overall better code.

    The next thing I would do to debug is add an else statement after your inner if statement that just sends a message to the player to let you know that the inner if statement returned false. It is good to know that the inner statement is being hit and if it is, you can assume that your above code is working properly, but if you don't get a message then you can assume that your prior code is not working properly. So, doing that would look something like this:

    Code:java
    1. if (leather.getColor() == Color.RED)
    2. {
    3. player.sendMessage("Your item is a leather helmet and it is red!");
    4. }
    5. else
    6. {
    7. player.sendMessage("Your item is a leather helmet, but it is not red!");
    8. }


    Then, just to be proper and so you get results for any situation I would add an else statement to the outer if statement as well. That would look like this:

    Code:java
    1. else if (e.getCurrentItem().getType() == Material.LEATHER_HELMET)
    2. {
    3.  
    4. }
    5. else
    6. {
    7. player.sendMessage("Your item is not a leather helmet!");
    8. }


    Give these changes a try and let me know the results. I don't expect this to fix your problem, but it should help you find where your plugin is screwing up. If you cannot find the problem, post more of your code here and tell me the results of what I suggested. Good luck :)!

    - Np15isntboss
     
  11. Offline

    Necrodoom

    Zupsub likes this.
  12. Offline

    np15isntboss

    Necrodoom My mistake, I see where I am wrong when it comes to that, but I still endorse the other things I said in order to help debug.
     
  13. np15isntboss Zupsub Necrodoom xTigerRebornx
    Here is the code:
    Code:java
    1. else if (e.getCurrentItem().getType() == Material.LEATHER_HELMET){
    2. player.sendMessage("Helmet yes");
    3. LeatherArmorMeta leather = (LeatherArmorMeta) e.getCurrentItem().getItemMeta();
    4. if (leather.getColor() == Color.RED){
    5. player.sendMessage("Red helmet yes");
    6. }else {
    7. player.sendMessage("Red helmet no");
    8. }
    9.  
    10. }


    And the result is:
    -Helmet yes, Red helmet no
    So something is wrong here
    Code:java
    1. LeatherArmorMeta leather = (LeatherArmorMeta) e.getCurrentItem().getItemMeta();
    2. if (leather.getColor() == Color.RED){


    Thanks to everybody
     
  14. Offline

    Zupsub

    Print out what getColor returns instead.
     
  15. Zupsub
    It returns: Color: [rgb0x993333]
     
  16. Offline

    xTigerRebornx

  17. xTigerRebornx
    I used this from your link but I dont know why It dosn't work.
    Code:java
    1. leather.getColor() == Color.fromRGB(0x993333)
     
  18. Offline

    xTigerRebornx

    galaipa You didn't use the method that was shown by that link.
    Use DyeColor#getByColor() and compare that to the DyeColor you want (or just call DyeColor#getByColor() on Color.RED and compare the converted DyeColor to that)
     
  19. @xTigerReborn
    Sorry but I dont understand how can I get the dye color.

    xTigerRebornx
    Code fixed!
    Here is the final code:
    Code:java
    1. else if (e.getCurrentItem().getType() == Material.LEATHER_HELMET){
    2. LeatherArmorMeta leather = (LeatherArmorMeta) e.getCurrentItem().getItemMeta();
    3. Color kolorea = Color.fromRGB(0x993333);
    4. Color kolorea2 = leather.getColor();
    5. if (kolorea2.equals(kolorea)){
    6. player.sendMessage("Red helmet yes");
    7. }else{
    8. player.sendMessage("Red helmet no");
    9. Color a = leather.getColor();
    10.  
    11. }
    12.  
    13. }


    Thanks to everybody

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page