Util Fancy json messages

Discussion in 'Resources' started by The_Spaceman, Jul 10, 2017.

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

    The_Spaceman

    With this you can simply create json messages and send them to players
    its supports: normal text, hover with text, and only 1 clickEvent per added text. supported clickEvents: command, url and suggestions

    example:
    Code:
    FancyMessageUtil fm = new FancyMessageUtil();
    fm.addText("hover\n", FancyMessageUtil.Colors.AQUA);
    fm.addHover(new String[]{"hover", "\nhover", "\nhover"}, new FancyMessageUtil.Colors[]{FancyMessageUtil.Colors.DARK_PURPLE, FancyMessageUtil.Colors.GREEN, FancyMessageUtil.Colors.BLUE}, new FancyMessageUtil.Attributes[]{FancyMessageUtil.Attributes.BOLD, FancyMessageUtil.Attributes.ITALIC, FancyMessageUtil.Attributes.UNDERLINE});
    fm.addText("command\n", FancyMessageUtil.Colors.RED);
    fm.addCommand("/say hai");
    fm.addText("url\n", FancyMessageUtil.Colors.DARK_BLUE);
    fm.addUrl("https://google.com");
    fm.addText("suggestion", FancyMessageUtil.Colors.DARK_GRAY);
    fm.addSuggest("suggestion");
    fm.sendMessage(player);
    fm.clearMessage();
    fm.addText("subtitle", FancyMessageUtil.Colors.DARK_GRAY, FancyMessageUtil.Attributes.BOLD);
    fm.sendSubtitle(player);fm.clearMessage();
    fm.addText("title", FancyMessageUtil.Colors.DARK_GREEN);
    fm.sendTitle(player);
    fm.clearMessage();
    fm.addText("actionbar", FancyMessageUtil.Colors.GOLD);
    fm.sendActionbar(player);
    
    if you like this you or find a error (probably not) can comment this.
    I like it when I know when its used :D

    pls don't mind the bad formatting
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    public class FancyMessageUtil {
    
    private String message = "";
    
    @SuppressWarnings("unused")
    public enum Colors {
    BLACK,DARK_BLUE,DARK_GREEN,DARK_AQUA,DARK_RED,DARK_PURPLE,GOLD,GRAY,DARK_GRAY,BLUE,GREEN,AQUA,RED,LIGHT_PURPLE,YELLOW,WHITE
    }
    
    @SuppressWarnings("unused")
    public enum Attributes {
    OBFUSCATED,BOLD,ITALIC,STRIKETHROUGH,UNDERLINE
    }
    
    private static String removeLastChar(String str) {
        return str.substring(0, str.length() - 1);
    }
    
    @SuppressWarnings("unused")
    public void sendMessage(Player player) {
    
    boolean b = false; 
     if (Bukkit.getServer().getWorld(player.getWorld().getName()).getGameRuleValue("sendCommandFeedback").equals("true")) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "false");
    b = true;
    }
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"\"," + removeLastChar(message) + "]");
    if (b) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "true");
    }
    }
    
    @SuppressWarnings("unused")
    public void sendTitle(Player player) {
    boolean b = false; 
     if (Bukkit.getServer().getWorld(player.getWorld().getName()).getGameRuleValue("sendCommandFeedback").equals("true")) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "false");
    b = true;
    }
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "title " + player.getName() + " title [\"\"," + removeLastChar(message) + "]"); 
    if (b) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "true");
    }
    }
    
    @SuppressWarnings("unused")
    public void sendSubtitle(Player player) {
    boolean b = false;
    if (Bukkit.getServer().getWorld(player.getWorld().getName()).getGameRuleValue("sendCommandFeedback").equals("true")) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "false");b = true;
    }
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "title " + player.getName() + " subtitle [\"\"," + removeLastChar(message) + "]");
    if (b) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "true");
    }
    }
    
    @SuppressWarnings("unused")
    public void sendActionbar(Player player) {
    boolean b = false; if (Bukkit.getServer().getWorld(player.getWorld().getName()).getGameRuleValue("sendCommandFeedback").equals("true")) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "false");
    b = true;
    }
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "title " + player.getName() + " actionbar [\"\"," + removeLastChar(message) + "]");
    if (b) {
    Bukkit.getServer().getWorld(player.getWorld().getName()).setGameRuleValue("sendCommandFeedback", "true");
    }
    }
    
    @SuppressWarnings("unused")
    public final void clearMessage() {
        message = "";
    }
    
    @SuppressWarnings("unused")
    public final void addText(String text, Colors color) {
    message += "{\"text\":\"" + text + "\",\"color\":\"" + color.toString().toLowerCase() + "\"},";
    }
    
    @SuppressWarnings("unused")
    public final void addText(String text, Colors color, Attributes attribute1) {
    message += "{\"text\":\"" + text + "\",\"color\":\"" + color.toString().toLowerCase() + "\",\"" + attribute1 + "\":\"true\"},";
    }
    
    @SuppressWarnings("unused")
    public final void addText(String text, Colors color, Attributes attribute1,Attributes attribute2) {
    message += "{\"text\":\"" + text + "\",\"color\":\"" + color.toString().toLowerCase() + "\",\"" + attribute1 + "\":\"true\",\"" +
    attribute2 + "\":\"true\"},";
    }
    
    @SuppressWarnings("unused")
    public final void addText(String text, Colors color, Attributes attribute1,Attributes attribute2, Attributes attribute3) {
    message += "{\"text\":\"" + text + "\",\"color\":\"" + color.toString().toLowerCase() + "\",\"" + attribute1 + "\":\"true\",\"" +
    attribute2 + "\":\"true\",\"" + attribute3 + "\":\"true\"},";
    }
    
    @SuppressWarnings("unused")
    public final void addText(String text, Colors color, Attributes attribute1,Attributes attribute2, Attributes attribute3, Attributes attribute4) {
    
    message += "{\"text\":\"" + text + "\",\"color\":\"" + color.toString().toLowerCase() + "\",\"" + attribute1 + "\":\"true\",\"" +
    attribute2 + "\":\"true\",\"" + attribute3 + "\":\"true\",\"" + attribute4 + "\":\"true\"},";
    }
    
    @SuppressWarnings("unused")
    public final void addText(String text, Colors color, Attributes attribute1,Attributes attribute2, Attributes attribute3, Attributes attribute4, Attributes attribute5) {
    message += "{\"text\":\"" + text + "\",\"color\":\"" + color.toString().toLowerCase() + "\",\"" + attribute1 + "\":\"true\",\"" +
    attribute2 + "\":\"true\",\"" + attribute3 + "\":\"true\",\"" + attribute4 + "\":\"true\",\"" + attribute5 + "\":\"true\"},";
    }
    
    @SuppressWarnings("unused")
    public final void addHover(String[] hoverText, Colors[] hoverColor) {
    
    message = removeLastChar(removeLastChar(message));message += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":[\"\",";StringBuilder str = new StringBuilder().append(message); 
     for (int i = 0; i < hoverText.length; i++) {
    str.append("{\"text\":\"");str.append(hoverText[i]);str.append("\",\"color\":\"");str.append(hoverColor[i].toString().toLowerCase());
    str.append("\"},");
    }
    
    message = removeLastChar(str.toString()) + "]}},";
    }
    
    @SuppressWarnings("unused")
    public final void addHover(String[] hoverText, Colors[] hoverColor,Attributes[] attribute1) {
    
    message = removeLastChar(removeLastChar(message));
    message += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":[\"\",";
    StringBuilder str = new StringBuilder().append(message); 
     for (int i = 0; i < hoverText.length; i++) {
    str.append("{\"text\":\"");str.append(hoverText[i]);
    str.append("\",\"color\":\"");
    str.append(hoverColor[i].toString().toLowerCase());
    str.append("\",\"");str.append(attribute1[i]);
    str.append("\":\"true\"},");
    }
    
    message = removeLastChar(str.toString()) + "]}},";
    }
    
    @SuppressWarnings("unused")
    public final void addHover(String[] hoverText, Colors[] hoverColor,Attributes[] attribute1, Attributes[] attribute2) {
    
    message = removeLastChar(removeLastChar(message));
    message += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":[\"\",";
    StringBuilder str = new StringBuilder().append(message); 
     for (int i = 0; i < hoverText.length; i++) {
    str.append("{\"text\":\"");
    str.append(hoverText[i]);
    str.append("\",\"color\":\"");
    str.append(hoverColor[i].toString().toLowerCase());
    str.append("\",\"");str.append(attribute1[i]);
    str.append("\":\"true\",\"");
    str.append(attribute2[i]);
    str.append("\":\"true\"},");}
    
    message = removeLastChar(str.toString()) + "]}},";}
    
    @SuppressWarnings("unused")
    public final void addHover(String[] hoverText, Colors[] hoverColor,Attributes[] attribute1, Attributes[] attribute2, Attributes[] attribute3) {
    
    message = removeLastChar(removeLastChar(message));
    message += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":[\"\",";
    StringBuilder str = new StringBuilder().append(message); 
     for (int i = 0; i < hoverText.length; i++) {
    str.append("{\"text\":\"");
    str.append(hoverText[i]);
    str.append("\",\"color\":\"");
    str.append(hoverColor[i].toString().toLowerCase());
    str.append("\",\"");
    str.append(attribute1[i]);
    str.append("\":\"true\",\"");
    str.append(attribute2[i]);
    str.append("\":\"true\",\"");
    str.append(attribute3[i]);
    str.append("\":\"true\"},");
    }
    
    message = removeLastChar(str.toString()) + "]}},";
    }
    
    @SuppressWarnings("unused")
    public final void addHover(String text, String[] hoverText, Colors[] hoverColor,Attributes[] attribute1, Attributes[] attribute2, Attributes[] attribute3, Attributes[] attribute4) {
    
    message = removeLastChar(removeLastChar(message));
    message += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":[\"\",";
    StringBuilder str = new StringBuilder().append(message); 
     for (int i = 0; i < hoverText.length; i++) {
    str.append("{\"text\":\"");
    str.append(hoverText[i]);
    str.append("\",\"color\":\"");
    str.append(hoverColor[i].toString().toLowerCase());
    str.append("\",\"");
    str.append(attribute1[i]);
    str.append("\":\"true\",\"");
    str.append(attribute2[i]);
    str.append("\":\"true\",\"");
    str.append(attribute3[i]);
    str.append("\":\"true\",\"");
    str.append(attribute4[i]);
    str.append("\":\"true\"},");}
    
    message = removeLastChar(str.toString()) + "]}},";}
    
    @SuppressWarnings("unused")
    public final void addHover(String[] hoverText, Colors[] hoverColor,Attributes[] attribute1, Attributes[] attribute2, Attributes[] attribute3, Attributes[] attribute4, Attributes[] attribute5) {
    
    message = removeLastChar(removeLastChar(message));
    message += ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":[\"\",";
    StringBuilder str = new StringBuilder().append(message); 
    for (int i = 0; i < hoverText.length; i++) {
    str.append("{\"text\":\"");str.append(hoverText[i]);str.append("\",\"color\":\"");str.append(hoverColor[i].toString().toLowerCase());str.append("\",\"");str.append(attribute1[i]);str.append("\":\"true\",\"");str.append(attribute2[i]);str.append("\":\"true\",\"");str.append(attribute3[i]);str.append("\":\"true\",\"");str.append(attribute4[i]);
    str.append("\":\"true\",\"");str.append(attribute5[i]);str.append("\":\"true\"},");}
    
    message = removeLastChar(str.toString()) + "]}},";}
    
    @SuppressWarnings("unused")
    public final void addUrl(String url) {
    message = removeLastChar(removeLastChar(message));
    message += ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + url + "\"}},";
    }
    
    @SuppressWarnings("unused")
    public final void addCommand(String command) {
    message = removeLastChar(removeLastChar(message));
    message += ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + command + "\"}},";
    }
    
    @SuppressWarnings("unused")
    public final void addSuggest(String suggestion) {
    message = removeLastChar(removeLastChar(message));
    message += ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + suggestion + "\"}},";
    }
    }
    
     
    timtower and Zombie_Striker like this.
  2. Offline

    PhantomUnicorns

    @The_Spaceman Good job! But you might want to update your code to include:
    • Reflection
    • Format it (I'll put a link here to a formatted version of it so you can update it)
    • More functionality (Obviously :p)
    • Make the code better! (Your code backtracks to go forward with each method! Compile the message at the end when they would like to use it! If you don't know what I mean, look at my util linked below)
    • Remove all of those annotations! If it's public, it will never give a warning for being unused. If you use the formatted version I put above, I have removed them.
    If you don't know how to do the reflection part, you can look at this which is a reflection class I made that actually includes JSON Messaging and more. You can also look how I did the clickEvents and hoverEvents that you don't have
     
    Last edited: Jul 11, 2017
  3. Online

    timtower Administrator Administrator Moderator

    @PhantomUnicorns Why reflection for something that doesn't use NMS to begin with?
     
  4. Offline

    PhantomUnicorns

    @timtower If you look at the util, it uses some methods would use NMS! (If he doesn't route it through the console, I believe that routing it through the console is worse then actually sending the packet (It uses more code, it's definitely less efficient his way)) It's not only a JSON Messagener, it has hotbar, titles, ect...
     
  5. Offline

    The_Spaceman

    when I doens't use the console it neads to be updated every Bukkit update, and if I use nms it uses more code. i had it first the nms way, but that would break every time a new update comes.
     
  6. Offline

    PhantomUnicorns

    @The_Spaceman That's why I said reflection... So it doesn't break every update.
     
  7. Offline

    The_Spaceman

    Well, this works. So why should I change it?
     
  8. Offline

    PhantomUnicorns

    Because it is inefficient, you should use NMS/Reflection NMS not route it through the console. There is no point to this if you don't update to do so because there is already ones that use Reflection NMS, like mine. Not trying to hate on your resource but like if you don't want to change it to be better then it's not going to be good compared to others.
     
Thread Status:
Not open for further replies.

Share This Page