Vinetos

MessagesCreators

Feb 2nd, 2016
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. package fr.vinetos.test;
  2.  
  3. import org.json.simple.JSONArray;
  4. import org.json.simple.JSONObject;
  5.  
  6. import java.util.List;
  7.  
  8. /**
  9.  * File <b>MessageCreator</b> located on fr.vinetos.test
  10.  * MessageCreator is a part of Test.
  11.  * <p>
  12.  * Copyright (c) 2016 Vinetos <b>https://vinetos.fr</b> and contributors.
  13.  * <p>
  14.  * Test is a free software: See the MIT License
  15.  * Public License for more details (LICENSE.txt file)
  16.  *
  17.  * @author Vinetos
  18.  *         Created the 26/11/2016 at 22:21
  19.  */
  20. public class MessageCreator {
  21.  
  22.     private static final String[] colors = new String[]{
  23.             "&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f", "&k", "&l", "&m", "&n", "&o", "&r",
  24.             "§0", "§1", "§2", "§3", "§4", "§5", "§6", "§7", "§8", "§9", "§a", "§b", "§c", "§d", "§e", "§f", "§k", "§l", "§m", "§n", "§o", "§r"
  25.     };
  26.     private JSONObject chatObject;
  27.  
  28.     public MessageCreator (String text, Color color, List<ChatFormat> formats) {
  29.         chatObject = new JSONObject();
  30.         chatObject.put("text", text);
  31.         if (color != null) {
  32.             chatObject.put("color", color.getColorString());
  33.         }
  34.         if (formats != null) {
  35.             for (ChatFormat format : formats) {
  36.                 chatObject.put(format.getFormatString(), true);
  37.             }
  38.         }
  39.     }
  40.  
  41.     public MessageCreator addExtra(ChatExtra extraObject) {
  42.         if (!chatObject.containsKey("extra")) {
  43.             chatObject.put("extra", new JSONArray());
  44.         }
  45.         JSONArray extra = (JSONArray) chatObject.get("extra");
  46.         extra.add(extraObject.toJSON());
  47.         chatObject.put("extra", extra);
  48.         return this;
  49.     }
  50.  
  51.     public String toString() {
  52.         return chatObject.toJSONString();
  53.     }
  54.  
  55.     public static String stripColor(String input){
  56.         for(String s : colors){
  57.             input = input.replaceAll(s, "");
  58.         }
  59.         return input;
  60.     }
  61.  
  62.     public static class ChatExtra {
  63.         private JSONObject chatExtra;
  64.  
  65.         public ChatExtra(String text, Color color, List<ChatFormat> formats) {
  66.             chatExtra = new JSONObject();
  67.             chatExtra.put("text", text);
  68.             chatExtra.put("color", color.getColorString());
  69.             if(formats != null){
  70.                 for (ChatFormat format : formats) {
  71.                     chatExtra.put(format.getFormatString(), true);
  72.                 }
  73.             }
  74.         }
  75.  
  76.         public ChatExtra setClickEvent(ClickEventType action, String value) {
  77.             JSONObject clickEvent = new JSONObject();
  78.             clickEvent.put("action", action.getTypeString());
  79.             clickEvent.put("value", value);
  80.             chatExtra.put("clickEvent", clickEvent);
  81.             return this;
  82.         }
  83.  
  84.         public ChatExtra setHoverEvent(HoverEventType action, String value) {
  85.             JSONObject hoverEvent = new JSONObject();
  86.             hoverEvent.put("action", action.getTypeString());
  87.             hoverEvent.put("value", value);
  88.             chatExtra.put("hoverEvent", hoverEvent);
  89.             return this;
  90.         }
  91.  
  92.         public JSONObject toJSON() {
  93.             return chatExtra;
  94.         }
  95.  
  96.         public ChatExtra build(){
  97.             return this;
  98.         }
  99.     }
  100.  
  101.     public static enum Color {
  102.         WHITE("white"),
  103.         YELLOW("yellow"),
  104.         LIGHT_PURPLE("light_purple"),
  105.         RED("red"),
  106.         AQUA("aqua"),
  107.         GREEN("green"),
  108.         BLUE("blue"),
  109.         DARK_GRAY("dark_gray"),
  110.         GRAY("gray"),
  111.         GOLD("gold"),
  112.         DARK_PURPLE("dark_purple"),
  113.         DARK_RED("dark_red"),
  114.         DARK_AQUA("dark_aqua"),
  115.         DARK_GREEN("dark_green"),
  116.         DARK_BLUE("dark_blue"),
  117.         BLACK("black");
  118.         private final String color;
  119.  
  120.         Color(String color) {
  121.             this.color = color;
  122.         }
  123.  
  124.         String getColorString() {
  125.             return color;
  126.         }
  127.     }
  128.  
  129.     public static enum ClickEventType {
  130.         RUN_COMMAND("run_command"),
  131.         SUGGEST_COMMAND("suggest_command"),
  132.         OPEN_URL("open_url"),
  133.         CHANGE_PAGE("change_page");
  134.  
  135.         private final String type;
  136.  
  137.         ClickEventType(String type) {
  138.             this.type = type;
  139.         }
  140.  
  141.         public String getTypeString() {
  142.             return type;
  143.         }
  144.     }
  145.  
  146.     public static enum HoverEventType {
  147.         SHOW_TEXT("show_text"),
  148.         SHOW_ITEM("show_item"),
  149.         SHOW_ACHIEVEMENT("show_achievement");
  150.         private final String type;
  151.  
  152.         HoverEventType(String type) {
  153.             this.type = type;
  154.         }
  155.  
  156.         public String getTypeString() {
  157.             return type;
  158.         }
  159.     }
  160.  
  161.     public static enum ChatFormat {
  162.         BOLD("bold"),
  163.         UNDERLINED("underlined"),
  164.         STRIKETHROUGH("strikethrough"),
  165.         ITALIC("italic"),
  166.         OBFUSCATED("obfuscated");
  167.         private final String format;
  168.  
  169.         ChatFormat(String format) {
  170.             this.format = format;
  171.         }
  172.  
  173.         public String getFormatString() {
  174.             return format;
  175.         }
  176.     }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment