Guest User

JSONApi

a guest
Aug 17th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1. import java.lang.reflect.Constructor;
  2. import java.util.ArrayList;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.entity.Player;
  8.  
  9. public class JSONMessage {
  10.  
  11.     public enum ClickAction{
  12.         RUN_COMMAND, SUGGEST_COMMAND, OPEN_URL
  13.     }
  14.  
  15.     public enum HoverAction{
  16.         SHOW_TEXT
  17.     }
  18.  
  19.     private static final String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
  20.     private static final Pattern pattern = Pattern.compile("([&|§][a-fA-F0-9k-oK-orR]){1}");
  21.     private ArrayList<String> partes;
  22.     private String json = "";
  23.  
  24.     public JSONMessage(){
  25.         json += "[\"\",";
  26.         partes = new ArrayList<>();
  27.     }
  28.  
  29.     public JSONMessage parseToJSON(String text){
  30.         Matcher matcher = pattern.matcher(text);
  31.         if(matcher.find()){
  32.             String cor = "";
  33.             String palavra = "";
  34.             String geral = "";
  35.             char[] array = text.toCharArray();
  36.             for(int j = 0; j<array.length; j++){
  37.                 if(j+1 != array.length && pattern.matcher(String.valueOf(array[j]) + String.valueOf(array[j+1])).matches()){
  38.                     cor += String.valueOf(array[j]) + String.valueOf(array[j+1]);
  39.                     j+=1;
  40.                 }else{
  41.                     palavra += array[j];
  42.                     if(j+1 != array.length){
  43.                         if(String.valueOf(array[j+1]).matches("&|§")){
  44.                             /*json += "{\"text\":\"" + palavra + "\"";
  45.                             json += getFormat(cor);
  46.                             json += "},";*/
  47.                             geral += "{\"text\":\"" + palavra + "\"" + getFormat(cor) + "},";
  48.                             partes.add(geral);
  49.                             cor = "";
  50.                             palavra = "";
  51.                             geral = "";
  52.                         }
  53.                     }else{
  54.                         /*json += "{\"text\":\"" + palavra + "\"";
  55.                         json += getFormat(cor);
  56.                         json += "},";*/
  57.                         geral += "{\"text\":\"" + palavra + "\"" + getFormat(cor) + "},";
  58.                         partes.add(geral);
  59.                         geral = "";
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         return this;
  65.     }
  66.  
  67.     private String getFormat(String cor){
  68.         String retornar = "";
  69.         if(cor.matches("(.+)?([&§]0)(.+)?")) retornar += ",\"color\":\"black\"";
  70.         if(cor.matches("(.+)?([&§]1)(.+)?")) retornar += ",\"color\":\"dark_blue\"";
  71.         if(cor.matches("(.+)?([&§]2)(.+)?")) retornar += ",\"color\":\"dark_green\"";
  72.         if(cor.matches("(.+)?([&§]3)(.+)?")) retornar += ",\"color\":\"dark_aqua\"";
  73.         if(cor.matches("(.+)?([&§]4)(.+)?")) retornar += ",\"color\":\"dark_red\"";
  74.         if(cor.matches("(.+)?([&§]5)(.+)?")) retornar += ",\"color\":\"dark_purple\"";
  75.         if(cor.matches("(.+)?([&§]6)(.+)?")) retornar += ",\"color\":\"gold\"";
  76.         if(cor.matches("(.+)?([&§]7)(.+)?")) retornar += ",\"color\":\"gray\"";
  77.         if(cor.matches("(.+)?([&§]8)(.+)?")) retornar += ",\"color\":\"dark_gray\"";
  78.         if(cor.matches("(.+)?([&§]9)(.+)?")) retornar += ",\"color\":\"blue\"";
  79.         if(cor.matches("(.+)?([&§]a)(.+)?")) retornar += ",\"color\":\"green\"";
  80.         if(cor.matches("(.+)?([&§]b)(.+)?")) retornar += ",\"color\":\"aqua\"";
  81.         if(cor.matches("(.+)?([&§]c)(.+)?")) retornar += ",\"color\":\"red\"";
  82.         if(cor.matches("(.+)?([&§]d)(.+)?")) retornar += ",\"color\":\"light_purple\"";
  83.         if(cor.matches("(.+)?([&§]e)(.+)?")) retornar += ",\"color\":\"yellow\"";
  84.         if(cor.matches("(.+)?([&§]f)(.+)?")) retornar += ",\"color\":\"white\"";
  85.         if(cor.matches("(.+)?([&§]k)(.+)?")) retornar += ",\"obfuscated\":true";
  86.         if(cor.matches("(.+)?([&§]l)(.+)?")) retornar += ",\"bold\":true";
  87.         if(cor.matches("(.+)?([&§]m)(.+)?")) retornar += ",\"strikethrough\":true";
  88.         if(cor.matches("(.+)?([&§]n)(.+)?")) retornar += ",\"underlined\":true";
  89.         if(cor.matches("(.+)?([&§]o)(.+)?")) retornar += ",\"italic\":true";
  90.         return retornar;
  91.     }
  92.  
  93.     public JSONMessage addText(String text){
  94.         if(partes.size()>0){
  95.             for(String parte : partes){
  96.                 json += parte;
  97.             }
  98.             partes.clear();
  99.         }
  100.         Matcher matcher = pattern.matcher(text);
  101.         if(matcher.find()){
  102.             parseToJSON(text);
  103.         }else{
  104.             json += "{\"text\":\"" + text + "\"},";
  105.         }
  106.         return this;
  107.     }
  108.  
  109.     public JSONMessage withHoverAction(HoverAction hoveraction, String value){
  110.         for(int i = 0; i<partes.size(); i++){
  111.             String parte = partes.get(i);
  112.             if(parte.endsWith("},")) parte = parte.substring(0, parte.length()-2);
  113.             parte += ",\"hoverEvent\":{\"action\":\"" + hoveraction.name().toLowerCase() + "\",\"value\":\"" + value + "\"}},";
  114.             partes.set(i, parte);
  115.         }
  116.         return this;
  117.     }
  118.  
  119.     public JSONMessage withClickAction(ClickAction clickaction, String value){
  120.         for(int i = 0; i<partes.size(); i++){
  121.             String parte = partes.get(i);
  122.             if(parte.endsWith("},")) parte = parte.substring(0, parte.length()-2);
  123.             parte += ",\"clickEvent\":{\"action\":\"" + clickaction.name().toLowerCase() + "\",\"value\":\"" + value + "\"}},";
  124.             partes.set(i, parte);
  125.         }
  126.         return this;
  127.     }
  128.  
  129.     public JSONMessage removeLastElement(){
  130.         if(partes.size()>0){
  131.             for(String parte : partes){
  132.                 json += parte;
  133.             }
  134.             partes.clear();
  135.         }
  136.         this.json = json.substring(0, json.lastIndexOf("{\"text\":\""));
  137.         return this;
  138.     }
  139.  
  140.     @Override
  141.     public String toString(){
  142.         return this.json;
  143.     }
  144.  
  145.     public void sendJson(Player p){
  146.         try {
  147.             if(partes.size()>0){
  148.                 for(String parte : partes){
  149.                     json += parte;
  150.                 }
  151.                 partes.clear();
  152.             }
  153.             if(json.endsWith(",")) json = json.substring(0, json.length()-1);
  154.             if(!json.endsWith("]")) json += "]";
  155.             Class<?> chatSerializer = Class.forName("net.minecraft.server." + version + ".IChatBaseComponent$ChatSerializer");
  156.             Class<?> chatComponent = Class.forName("net.minecraft.server." + version + ".IChatBaseComponent");
  157.             Class<?> packet = Class.forName("net.minecraft.server." + version + ".PacketPlayOutChat");
  158.             Constructor<?> constructor = packet.getConstructor(chatComponent);
  159.  
  160.             Object text = chatSerializer.getMethod("a", String.class).invoke(chatSerializer, json);
  161.             Object packetFinal = constructor.newInstance(text);
  162.  
  163.             Object handle = p.getClass().getMethod("getHandle").invoke(p);
  164.             Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
  165.             playerConnection.getClass().getMethod("sendPacket", Class.forName("net.minecraft.server." + version + ".Packet")).invoke(playerConnection, packetFinal);
  166.         } catch (Exception e) {
  167.             e.printStackTrace();
  168.         }
  169.     }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment