Last active
October 1, 2016 09:35
-
-
Save alexisvisco/ef90dd4a9c7b3ba3a44c2416f0e1a38b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.ChatColor; | |
| import org.bukkit.entity.Player; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| /** | |
| * Par Alexis le 26/06/2016. | |
| */ | |
| public class JsonMessageBuilder | |
| { | |
| public List<JComp> jsonComp = new ArrayList<>(); | |
| /** | |
| *Utilisation : | |
| * JsonMessageBuilder jm = new JsonMessageBuilder(); | |
| * jm.newJComp("§eLe chat est").addHoverText("§cIl est bleu en fait :/").build(jm); | |
| * jm.newJComp(" §1noir").addHoverText("§cEt bah non il est noir").addURL("www.chaton.com").build(jm); | |
| * jm.send(Player...) | |
| */ | |
| public JsonMessageBuilder() { | |
| } | |
| public JComp newJComp(String text){ | |
| JComp jc = new JComp(); | |
| jc.addText(text); | |
| return jc; | |
| } | |
| /** | |
| * | |
| * @param c multiple json component | |
| */ | |
| public void addComponent(JComp... c){ | |
| jsonComp.addAll(Arrays.asList(c)); | |
| } | |
| public void send(){ | |
| Bukkit.getOnlinePlayers().forEach(this::send); | |
| } | |
| public void send(Player... player){ | |
| for (Player p : player){ | |
| send(p); | |
| } | |
| } | |
| public void send(List<Player> player){ | |
| player.forEach(this::send); | |
| } | |
| private void send(Player p){ | |
| List<JComp> instanceJsonComp = jsonComp; | |
| StringBuilder temp = new StringBuilder(); | |
| for (JComp jComp : instanceJsonComp) | |
| temp.append(jComp.component); | |
| temp.deleteCharAt(temp.length()-1); | |
| Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + p.getName() + " " + "[\"\"," + str + "]"); | |
| } | |
| public static class JComp{ | |
| private StringBuilder component = new StringBuilder(); | |
| private JsonMessageBuilder jsonMessageBuilder ; | |
| private JComp addText(String message){ | |
| component.append("\"text\":\"").append(message).append("\","); | |
| return this; | |
| } | |
| public JComp addCommandExecutor(String command){ | |
| component.append("\"clickEvent\":{ \"action\":\"run_command\", \"value\":\"").append(command).append("\" },"); | |
| return this; | |
| } | |
| public JComp addCommandSuggestion(String command){ | |
| component.append("\"clickEvent\":{ \"action\":\"suggest_command\", \"value\":\"").append(command).append("\" },"); | |
| return this; | |
| } | |
| public JComp addURL(String link){ | |
| component.append("\"clickEvent\":{ \"action\":\"open_url\", \"value\":\"").append(link).append("\" },"); | |
| return this; | |
| } | |
| public JComp addChatSuggestion(String text){ | |
| component.append("\"insertion\":\"").append(text).append("\"},"); | |
| return this; | |
| } | |
| public JComp setBold(boolean bold){ | |
| component.append(" \"bold\": ").append(bold).append(","); | |
| return this; | |
| } | |
| public JComp setItalic(boolean italic){ | |
| component.append(" \"italic\": ").append(italic).append(","); | |
| return this; | |
| } | |
| public JComp setUnderlined(boolean underlined){ | |
| component.append(" \"underlined\": ").append(underlined).append(","); | |
| return this; | |
| } | |
| public JComp setStrikethrough(boolean strikethrough){ | |
| component.append(" \"strikethrough\": ").append(strikethrough).append(","); | |
| return this; | |
| } | |
| public JComp setObfuscated(boolean obfuscated){ | |
| component.append(" \"obfuscated\": ").append(obfuscated).append(","); | |
| return this; | |
| } | |
| public JComp setColor(ChatColor cc){ | |
| component.append("\"color\": \"").append(cc.name().toLowerCase()).append("\","); | |
| return this; | |
| } | |
| public JComp addHoverText(String... text){ | |
| String txt = ""; | |
| for (int i = 0; i < text.length ; i++) { | |
| if (i + 1 == text.length){ | |
| txt = txt + text[i]; | |
| }else { | |
| txt = txt + text[i] + "\\u000a"; | |
| } | |
| } | |
| component.append("\"hoverEvent\":{ \"action\":\"show_text\", \"value\":\"").append(txt).append("\" },"); | |
| return this; | |
| } | |
| public void build(JsonMessageBuilder jsonMessageBuilder){ | |
| component.deleteCharAt(component.length()-1); | |
| String finalComp = "{" + component + "},"; | |
| component = new StringBuilder(finalComp); | |
| jsonMessageBuilder.jsonComp.add(this); | |
| } | |
| } | |
| } |
Juste un petit problème avec cette solution (j'ai pas tester mais j'en suis sur a 100%), on ne peux pas appeler deux fois la même méthode ;) (duplicata dans le json et donc json invalide)
Nice :')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beaucoup mieux :-)