[Method] Title JSON Tool

Discussion in 'Resources' started by werter318, Oct 4, 2014.

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

    werter318

    [​IMG]

    Hello,

    I was playing with the new Title feature in 1.8 and decided to make a method to generate the json.

    Example: You have a deathmatch minigame, and when someone kills another player you want to send the players a Title for that, this is what you have to do to get the Json (IMPORTANT: You still need a library to send the actual packet or you can write that yourself):

    Code:
    getJSON(ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + killer.getName() + ChatColor.WHITE + " killed " + ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + player.getName());

    This is what you'll get:

    Code:
    {text:"Notch ",color:gold,bold:true,underlined:false,italic:false,strikethrough:false,obfuscated:false,extra:[{text:"killed ",color:white,bold:false,underlined:false,italic:false,strikethrough:false,obfuscated:false,extra:[{text:"Jeb",color:gold,bold:true,underlined:false,italic:false,strikethrough:false,obfuscated:false}]}]}
    
    So here is the method:
    Code:java
    1. /**
    2. * @author werter318
    3. * @param title
    4. * @return String
    5. */
    6. public static String getJSON(String title) {
    7. char colorChar = ChatColor.COLOR_CHAR;
    8.  
    9. String template = "{text:\"TEXT\",color:COLOR,bold:BOLD,underlined:UNDERLINED,italic:ITALIC,strikethrough:STRIKETHROUGH,obfuscated:OBFUSCATED,extra:[EXTRA]}";
    10. String json = "";
    11.  
    12. List<String> parts = new ArrayList<String>();
    13.  
    14. int first = 0;
    15. int last = 0;
    16.  
    17. while ((first = title.indexOf(colorChar, last)) != -1) {
    18. int offset = 2;
    19. while ((last = title.indexOf(colorChar, first + offset)) - 2 == first) {
    20. offset += 2;
    21. }
    22.  
    23. if (last == -1) {
    24. parts.add(title.substring(first));
    25. break;
    26. } else {
    27. parts.add(title.substring(first, last));
    28. }
    29. }
    30.  
    31. if (parts.isEmpty()) {
    32. parts.add(title);
    33. }
    34.  
    35. Pattern colorFinder = Pattern.compile("(" + colorChar + "([a-f0-9]))");
    36. for (String part : parts) {
    37. json = (json.isEmpty() ? template : json.replace("EXTRA", template));
    38.  
    39. Matcher matcher = colorFinder.matcher(part);
    40. ChatColor color = (matcher.find() ? ChatColor.getByChar(matcher.group().charAt(1)) : ChatColor.WHITE);
    41.  
    42. json = json.replace("COLOR", color.name().toLowerCase());
    43. json = json.replace("BOLD", String.valueOf(part.contains(ChatColor.BOLD.toString())));
    44. json = json.replace("ITALIC", String.valueOf(part.contains(ChatColor.ITALIC.toString())));
    45. json = json.replace("UNDERLINED", String.valueOf(part.contains(ChatColor.UNDERLINE.toString())));
    46. json = json.replace("STRIKETHROUGH", String.valueOf(part.contains(ChatColor.STRIKETHROUGH.toString())));
    47. json = json.replace("OBFUSCATED", String.valueOf(part.contains(ChatColor.MAGIC.toString())));
    48.  
    49. json = json.replace("TEXT", part.replaceAll("(" + colorChar + "([a-z0-9]))", ""));
    50. }
    51.  
    52. json = json.replace(",extra:[EXTRA]", "");
    53.  
    54. return json;
    55. }
    Thanks for reading/using! <3
     
    woutwoot, Skionz and Skyost like this.
  2. Offline

    Skionz

    werter318 Very nice I will definitely be using this in the future :D
     
Thread Status:
Not open for further replies.

Share This Page