Solved Get wool colour associated with durability?

Discussion in 'Plugin Development' started by elementalgodz11, Jun 26, 2014.

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

    elementalgodz11

    I need to parse a string of an ItemStack with a full formatted name of the item:
    This will also include the colour at the front of the name, e.g: 'Lime Wool'
    Currently getting the name of a Material such as Wool would just return 'Wool'.

    I am using the following method currently to get the full name of a Wool:
    Code:java
    1.  
    2. Short durability = getDurability(stack);
    3. String name = "";
    4. if (stack.getType() == Material.STAINED_GLASS) {
    5.  
    6. switch(durability) {
    7. case 0: name = "Stained Glass";
    8. break;
    9. case 1: name = "Orange Stained Glass";
    10. break;
    11. case 2: name = "Magenta Stained Glass";
    12. break;
    13. case 3: name = "Light Blue Stained Glass";
    14. break;
    15. case 4: name = "Yellow Stained Glass";
    16. break;
    17. case 5: name = "Lime Stained Glass";
    18. break;
    19. case 6: name = "Pink Stained Glass";
    20. break;
    21. case 7: name = "Gray Stained Glass";
    22. break;
    23. case 8: name = "Light Gray Stained Glass";
    24. break;
    25. case 9: name = "Cyan Stained Glass";
    26. break;
    27. case 10: name = "Purple Stained Glass";
    28. break;
    29. case 11: name = "Blue Stained Glass";
    30. break;
    31. case 12: name = "Brown Stained Glass";
    32. break;
    33. case 13: name = "Green Stained Glass";
    34. break;
    35. case 14: name = "Red Stained Glass";
    36. break;
    37. case 15: name = "Black Stained Glass";
    38. break;
    39. default: name = "Stained Glass";
    40. break;
    41. }
    42.  
    43. }


    However, this is very tedious as it is not just the material of Wool I need to do this for, but also every material, such as Wood, Wood Stairs, etc.

    Is there an alternative method of doing what I am trying to do?

    Thanks.
     
  2. Offline

    desht

    elementalgodz11 I created this utility class just to solve problems like the above: https://github.com/desht/dhutils/blob/master/Lib/src/main/java/me/desht/dhutils/ItemNames.java

    Given an item stack, it returns a string which is the item description, as the client displays it. E.g.:
    PHP:
    ItemStack stack = new ItemStack(Material.STAINED_GLASS1DyeColor.RED.getWoolData());
    player.sendMessage("This is " ItemNames.lookup(stack));
    will tell the player "This is Red Stained Glass".

    It even includes names for the new MC 1.8 items :)

    You're welcome to use that class; there aren't any dependencies on the rest of dhutils so you should be able to use it as a standalone, changing the package of course. Or if you use Maven, add dhutils as a shaded Maven dependency.
     
    AoH_Ruthless and elementalgodz11 like this.
  3. Offline

    elementalgodz11

    desht

    That's amazing haha.
    What will happen when magic id's are removed though?
     
  4. Offline

    desht

    It'll break horribly (and I'll rewrite it somehow to work) :) Till then, though, it's good.
     
  5. Offline

    elementalgodz11

    desht

    Screenshot_2.png

    Error occuring at ItemNames class Line 506, which is .build();

    Method I am using to return the error:
    Code:java
    1. public String toFriendlyName(ItemStack stack) {
    2.  
    3. return ItemNames.lookup(stack);
    4.  
    5. }


    The ItemStack isn't null also.

    Managed to fix it by removing one of the duplicate entries in your Map:
    .put("1:4", "Polished Diorite")
    .put("1:4", "Polished Andesite")

    I have also found a bug where if the durability is too high, the string will return null, how about return it's default name instead of throwing the exception?



    Code:java
    1. try {
    2. return result;
    3. } catch (Exception ex) {
    4. return mat.name();
    5. }
     
  6. Offline

    desht

    I'll get Polished Andesite fixed (should be "1:6"). My bad for not testing after changing over to ImmutableMap.builder() :)

    Regarding the durability issue, what item did you use to trigger that?
     
  7. Offline

    elementalgodz11

    desht

    The error occurs in any case where the durability of the itemstack is higher than the max durability of the itemstack for me;
    as an example though: wool with durability of 67.
     
  8. Offline

    desht

    I updated the class to gracefully handle out-of-range colour data - e.g. wool with data 67 will now return just "Wool" since 67 isn't a valid dye data value.
     
    elementalgodz11 likes this.
  9. Offline

    elementalgodz11

    desht

    Awesome thank you.

    Would there be any way to reverse your method and get an ItemStack based on a String input? For example, if my input string was 'limewool', it would parse an itemstack of wool with the data value of 5.
     
  10. Offline

    viper_monster

Thread Status:
Not open for further replies.

Share This Page