[UTIL]Material Name Formatter

Discussion in 'Resources' started by creatorfromhell, Jul 11, 2013.

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

    creatorfromhell

    Hello, this is my first post in the resource section so please excuse anything I do wrong.

    Basically, I was creating an economy plugin, and I needed a way to format, and deformat a Material's Name for my sign shops.

    Here's an example.

    Let's say I get the Material Name using the ID of 53. It'll return WOOD_STAIRS. Now, when I format the name using my formatter it'll make it "WoodStairs". Then I can deformat "WoodStairs" using my formatter, and it'll return WOOD_STAIRS, which I can use to get the ID again.


    Format Method:
    Code:java
    1. public static String formatMaterialName(Material material) {
    2. String[] wordsSplit = material.name().split("_");
    3. String sReturn = "";
    4. for(String w: wordsSplit) {
    5. String word = w.toUpperCase().replace(w.substring(1), w.substring(1).toLowerCase());
    6. sReturn += word;
    7. }
    8. return sReturn;
    9. }


    Deformat Method:
    Code:java
    1. public static String deformatMaterialName(String name) {
    2. String[] split = name.split("(?=[A-Z])");
    3. String sReturn = "";
    4. int count = 1;
    5. for(String s : split) {
    6. sReturn += s.toUpperCase();
    7. if(count < split.length && count > 1) {
    8. sReturn += "_";
    9. }
    10. count++;
    11. }
    12. return sReturn;
    13. }



    Alternative Methods

    The following methods are an alternative to the ones above, and include a space in the formatted Material Name. For example, instead of being formatted to WoodStairs it would become Wood Stairs.

    Format Method:
    Code:java
    1. public static String formatMaterialNameWithSpace(Material material) {
    2. String[] wordsSplit = material.name().split("_");
    3. String sReturn = "";
    4. int count = 1;
    5. for(String w: wordsSplit) {
    6. String word = w.toUpperCase().replace(w.substring(1), w.substring(1).toLowerCase());
    7. sReturn += word;
    8. if(count < wordsSplit.length) {
    9. sReturn += " ";
    10. }
    11. }
    12. return sReturn;
    13. }


    DeFormat Method:
    Code:java
    1. public static String deformatMaterialNameWithSpace(String name) {
    2. String upperCase = name.toUpperCase();
    3. String formatted = upperCase.replace(" ", "_");
    4. return formatted;
    5. }


    Please leave any comments, and/or suggestions you have. Also, sorry if anyone has posted something like this before as I could not find anything like it. You are free to use the methods as long as you credit me for them.
     
  2. Offline

    hawkfalcon

    A space would be nice
     
  3. Offline

    creatorfromhell

    I updated the post with alternative methods that add a space to the formatted Material Name(as suggested by hawkfalcon), and I also modified the original methods a bit.
     
    hawkfalcon likes this.
  4. Format:
    Code:java
    1. public static String format(Material material{
    2. return WordUtils.capatilize(material.name().toLowerCase().replaceAll("_", " "));
    3. }


    Un-Format:
    Code:java
    1. public static Material unFormat(String string){
    2. return Material.valueOf(string.toUpperCase().replaceAll(" ", "_"));
    3. }


    Sorry if I mis-understood what you were attempting to do but my method is a lot shorter and simpler. If any code is incorrect its because I don't have an IDE on me atm.
     
    hawkfalcon and zachoooo like this.
  5. Offline

    Ultimate_n00b

    I think he didn't want a space also.
     

  6. Well then that makes the code even shorter...

    Format:
    Code:java
    1. public static String format(Material material{
    2. return WordUtils.capatilize(material.name().toLowerCase());
    3. }


    Un-Format:
    Code:java
    1. public static Material unFormat(String string){
    2. return Material.valueOf(string.toUpperCase());
    3. }
     
  7. Offline

    lycano

    iKeirNez this would not work as capitalize from common-lang only capitalizes white space delimited words in a string.

    What the OP wanted to share is "how he converted Material.WOOD_STAIRS to WoodStairs" which i really appreciate.

    What you do is using comman lang which is a dependency of bukkit and could be used for that. But you would have to make sure that you always import the right lib that bukkit imports if you rely on depency dependencies. Currently 2.6 and not use lang3 ...

    Anyways if you could change

    WordUtils.capitalize(material.name().toLowerCase());
    to
    WordUtils.capitalize(material.name(), '_').replace('_', '');

    Then it would convert WOOD_STAIRS to Wood Stairs and replace _ with blank which is what the OP wanted. Also it would not use regex for such a simple task ;)
     
    Ultimate_n00b likes this.
  8. Oops, didn't think of that lol. Yeah that would work.
     
Thread Status:
Not open for further replies.

Share This Page