|
| 1 | +/* |
| 2 | +* Copyright 2022 BuiltByBit |
| 3 | +* All rights reserved. |
| 4 | +*/ |
| 5 | +package com.builtbybit; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Locale; |
| 10 | + |
| 11 | +import org.bukkit.Bukkit; |
| 12 | +import org.bukkit.ChatColor; |
| 13 | +import org.bukkit.Server; |
| 14 | +import org.bukkit.entity.Player; |
| 15 | +import org.json.simple.JSONObject; |
| 16 | + |
| 17 | +public class JsonMessage { |
| 18 | + |
| 19 | + private String msg; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a new json message! |
| 23 | + */ |
| 24 | + public JsonMessage() { |
| 25 | + msg = "[{\"text\":\"\",\"extra\":[{\"text\": \"\"}"; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Send the json string to all players on the server. |
| 30 | + */ |
| 31 | + public void send() { |
| 32 | + List<Object> players = new ArrayList<>(); |
| 33 | + for (Player p : Bukkit.getOnlinePlayers()) players.add(p); |
| 34 | + send(players.toArray(new Player[players.size()])); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Send the json string to specified player(s) |
| 39 | + * @param player to send the message to. |
| 40 | + */ |
| 41 | + public void send(Player... player) { |
| 42 | + sendRawJson(msg + "]}]", player); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Send a raw json string to specified players. |
| 47 | + * @param json string to send |
| 48 | + * @param player to send the message to. |
| 49 | + */ |
| 50 | + public static void sendRawJson(String json, Player... player) { |
| 51 | + Server server = Bukkit.getServer(); |
| 52 | + for (Player p : player) |
| 53 | + server.dispatchCommand(server.getConsoleSender(), "tellraw " + p.getName() + " " + json); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Append text to the json message. |
| 58 | + * @param text to be appended |
| 59 | + * @return json string builder |
| 60 | + */ |
| 61 | + public JsonStringBuilder append(String text) { |
| 62 | + return new JsonStringBuilder(this, esc(text)); |
| 63 | + } |
| 64 | + |
| 65 | + private static String esc(String s) { |
| 66 | + return JSONObject.escape(s); |
| 67 | + } |
| 68 | + /** |
| 69 | + * |
| 70 | + * @author JustisR |
| 71 | + * |
| 72 | + */ |
| 73 | + public static class JsonStringBuilder { |
| 74 | + |
| 75 | + private final JsonMessage message; |
| 76 | + private final String string = ",{\"text\":\"\",\"extra\":["; |
| 77 | + private final String[] strings; |
| 78 | + private String hover = "", click = ""; |
| 79 | + |
| 80 | + /** |
| 81 | + * Settings for the json message's text |
| 82 | + * @param jsonMessage the original message |
| 83 | + * @param text the text to be appended to the message. |
| 84 | + */ |
| 85 | + private JsonStringBuilder(JsonMessage jsonMessage, String text) { |
| 86 | + message = jsonMessage; |
| 87 | + strings = colorized(text); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Set the hover event's action as showing a tooltip with the given text |
| 92 | + * @param lore the text to be displayed in the tooltip |
| 93 | + * @return the json string builder to which you are applying settings |
| 94 | + */ |
| 95 | + public JsonStringBuilder setHoverAsTooltip(String... lore) { |
| 96 | + StringBuilder builder = new StringBuilder(); |
| 97 | + for (int i = 0; i < lore.length; i++) |
| 98 | + if (i + 1 == lore.length) builder.append(lore[i]); |
| 99 | + else builder.append(lore[i] + "\n"); |
| 100 | + hover = ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":\"" + esc(builder.toString()) + "\"}"; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Set the click event's action as redirecting to a URL |
| 106 | + * @param link to redirect to |
| 107 | + * @return the json string builder to which you are applying settings. |
| 108 | + */ |
| 109 | + public JsonStringBuilder setClickAsURL(String link) { |
| 110 | + click = ",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"" + esc(link) + "\"}"; |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Set the click event's action as suggesting a command |
| 116 | + * @param cmd to suggest |
| 117 | + * @return the json string builder to which you are applying settings; |
| 118 | + */ |
| 119 | + public JsonStringBuilder setClickAsSuggestCmd(String cmd) { |
| 120 | + click = ",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"" + esc(cmd) + "\"}"; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Set the click event's action as executing a command |
| 126 | + * @param cmd |
| 127 | + * @return |
| 128 | + */ |
| 129 | + public JsonStringBuilder setClickAsExecuteCmd(String cmd) { |
| 130 | + click = ",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + esc(cmd) + "\"}"; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Finalize the appending of the text, with settings. |
| 136 | + * @return |
| 137 | + */ |
| 138 | + public JsonMessage save() { |
| 139 | + StringBuilder builder = new StringBuilder(message.msg + string); |
| 140 | + for (String string : strings) { |
| 141 | + builder.append(string); |
| 142 | + } |
| 143 | + builder.append("]" + hover + click + "}"); |
| 144 | + message.msg = builder.toString(); |
| 145 | + return message; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static final String[] colorized(String text) { |
| 150 | + String[] colors = text.split(String.valueOf(ChatColor.COLOR_CHAR)); |
| 151 | + boolean bold = false, italic = false, magic = false, underlined = false, strikethrough = false; |
| 152 | + ChatColor color = ChatColor.WHITE; |
| 153 | + for (int i = 0; i < colors.length; i++) { |
| 154 | + if (i == 0 && !text.startsWith(String.valueOf(ChatColor.COLOR_CHAR))) { |
| 155 | + colors[i] = "{\"text\":\"" + colors[i] + "\"}"; |
| 156 | + } else if (colors[i].length() < 1) { |
| 157 | + continue; |
| 158 | + } else { |
| 159 | + ChatColor decoded = ChatColor.getByChar(colors[i].substring(0, 1)); |
| 160 | + switch (decoded) { |
| 161 | + case RESET: |
| 162 | + bold = false; |
| 163 | + italic = false; |
| 164 | + magic = false; |
| 165 | + underlined = false; |
| 166 | + strikethrough = false; |
| 167 | + color = ChatColor.WHITE; |
| 168 | + break; |
| 169 | + case BOLD: |
| 170 | + bold = true; |
| 171 | + break; |
| 172 | + case ITALIC: |
| 173 | + italic = true; |
| 174 | + break; |
| 175 | + case MAGIC: |
| 176 | + magic = true; |
| 177 | + break; |
| 178 | + case UNDERLINE: |
| 179 | + underlined = true; |
| 180 | + break; |
| 181 | + case STRIKETHROUGH: |
| 182 | + strikethrough = true; |
| 183 | + break; |
| 184 | + default: |
| 185 | + color = decoded; |
| 186 | + } |
| 187 | + StringBuilder builder = new StringBuilder("{\"text\":\"" + colors[i].substring(1, colors[i].length()) + "\""); |
| 188 | + if (color != ChatColor.WHITE) builder.append(",\"color\":\"" + color.name().toLowerCase(Locale.US) + "\""); |
| 189 | + if (bold) builder.append(",\"bold\":\"" + bold + "\""); |
| 190 | + if (italic) builder.append(",\"italic\":\"" + italic + "\""); |
| 191 | + if (magic) builder.append(",\"obfuscated\":\"" + magic + "\""); |
| 192 | + if (underlined) builder.append(",\"underlined\":\"" + underlined + "\""); |
| 193 | + if (strikethrough) builder.append(",\"strikethrough\":\"" + strikethrough + "\""); |
| 194 | + colors[i] = builder.append("}").toString(); |
| 195 | + } |
| 196 | + if (i + 1 < colors.length) colors[i] = colors[i] + ","; |
| 197 | + } |
| 198 | + return colors; |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments