Resource Simplest Json Ever

Discussion in 'Plugin Help/Development/Requests' started by Juancomaster1998, Mar 16, 2015.

Thread Status:
Not open for further replies.
  1. Warning!
    There`s Actually a lib to simply generate Json.
    It`s already implemented on bukkit and you must consider using that one too.

    Tested Working Version 1.0
    Hello Comunity!
    I hope you will find this useful.
    It`s an extremly simple way to send Json messages.
    You Don`t need to use extra jars or something like that,
    just Copy this lightweigth class:
    Code:
    package we.Heiden.gca.Messages;
    
    import net.minecraft.server.v1_8_R1.ChatSerializer;
    import net.minecraft.server.v1_8_R1.IChatBaseComponent;
    import net.minecraft.server.v1_8_R1.PacketPlayOutChat;
    import net.minecraft.server.v1_8_R1.PlayerConnection;
    
    import org.bukkit.ChatColor;
    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    
    /**
     * ********************************************* <p>
     * <b>This has been made by <i>Heiden Team</b>
     * <ul>
     * <li>Don't claim this class as your own
     * <li>Don't remove this disclaimer
     * </ul>
     *  <b>All rights reserved <p>
     *  Heiden Team 2015 <p></b>
     * *********************************************
     **/
    public class JsonMessage {
    
       public static void sendJson(Player p, String... json) {
         PlayerConnection con = ((CraftPlayer)p).getHandle().playerConnection;
         PacketPlayOutChat packet;
         IChatBaseComponent comp;
         for (String s : json) {
           comp = ChatSerializer.a(ChatColor.translateAlternateColorCodes('&', s));
           packet = new PacketPlayOutChat(comp);
           con.sendPacket(packet);
         }
       }
       
       public static String newItem(String... Lines) {
         String lores = "";
         if (Lines.length > 1) {
           boolean first = true;
           int n = 0;
           for (String line : Lines) {
             n++;
             if (n > 1) {
               if (first) first = false;
               else lores += ", ";
               if (line.contains("\"")) line = line.replace("\"", "'");
               lores += "\\\"" + line + "\\\"";
             }
           }
         }
         if (Lines.length == 0) Lines[0] = "";
         if (Lines[0].contains("\"")) Lines[0] = Lines[0].replace("\"", "'");
         return "{id:1, tag:{display:{Name:" + Lines[0] + ", Lore:[" + lores + "]}}}";
       }
       
       @SuppressWarnings("deprecation")
       public static String newEntity(String name, EntityType entity) {
         short id = entity.getTypeId();
         String type = entity.getName();
         return "{id:" + id + ",name:" + name + ",type:" + type + "}";
       }
    
       public static Json newJson() { return new Json(true); }
       public static String newJson(String text) { return new Json(true).add(text).build().build();}
       
       public static final class Json {
         
         public static String json;
         public static String end;
         public static boolean first;
         
         public Builder add() {return new Builder();}
         public Builder add(String text) {return new Builder().setText(text);}
         
         public Json(boolean bol) {
           if (bol) {
             first = true;
             json = "{text:\"\", extra:[";
             end = "]}";
           }
         }
         
         public String build() { return json + end; }
       }
       
       public static final class Builder {
         private String text = null;
         private ClickAction click = null;
         private String clickValue = null;
         private HoverAction hover = null;
         private String hoverValue = null;
         
         public Builder() { }
         
         public Builder setText(String text) { this.text = text; return this; }
         public Builder clickEvent(ClickAction action, String value) { click = action; clickValue = value; return this; }
         public Builder hoverEvent(HoverAction action, String value) { hover = action; hoverValue = value; return this; }
         
         public Json build() {
           String extra = "{text:\"" + text + "\"";
           
           if (click != null) extra += ", clickEvent:{action:" + click.getString() + ", value:\"" + clickValue + "\"}";
           if (hover != null) extra += ", hoverEvent:{action:" + hover.getString() + ", value:\"" + hoverValue + "\"}";
           
           extra += "}";
           
           if (Json.first) Json.first = false;
           else extra = ", " + extra;
           
           Json.json += extra;
           return new Json(false);
         }
       }
       
       public static enum ClickAction {
         Run_Command("run_command"), Suggest_Command("suggest_command"), Open_Url("open_url"), Change_Page("change_page");
         
         private String str;
         private ClickAction(String str) { this.str = str; }
         public String getString() { return str; }
       }
       
       public static enum HoverAction {
         Show_Text("show_text"), Show_Item("show_item"), Show_Entity("show_entity"), Show_Achievement("show_achievement");
         
         private String str;
         private HoverAction(String str) { this.str = str; }
         public String getString() { return str; }
       }
    }
    Tutorial (open)
    Seeing that some of yours won`t know how to use it, Here I`ll explain a bit how to use it.
    Code:
    package we.Heiden.gca.Events;
    
    import org.bukkit.entity.Player;
    
    import we.Heiden.gca.Messages.JsonMessage;
    import we.Heiden.gca.Messages.JsonMessage.ClickAction;
    import we.Heiden.gca.Messages.JsonMessage.HoverAction;
    
    /**
     * ********************************************* <p>
     * <b>This has been made by <i>Heiden Team</b>
     * <ul>
     * <li>Don't claim this class as your own
     * <li>Don't remove this disclaimer
     * </ul>
     *  <b>All rights reserved <p>
     *  Heiden Team 2015 <p></b>
     * *********************************************
     **/
    public class Tutorial {
       
       /**
        * This is just a tutorial.
        * It`s ordered easiest to hardest
        * I`ll recommend you to see them all
        */
    
       public static void Tuto01(Player p) {
         /**
          * sendJson:
          *  type: void
          *  use:
          *  param 1: target (Player)
          *  param 2: String... (Built Json) <-- You can use as many as you like
          */
         /*This will send the json to the player*/
         JsonMessage.sendJson(p,
             /**
              * new Json(String):
              *  type: Json Code (String)
              *  use: Send a string and it will return it as a json String.
              */
             /*The simplest text, does nothing.*/
             JsonMessage.newJson("This is an example"));
       }
       
       public static void Tuto02(Player p) {
         /*As done before, this will send the Json*/
         JsonMessage.sendJson(p,
             /**
              * new Json:
              *  type: Json
              *  use: It allows you to build your json.
              *  
              *  differences beetween "new Json(String)":
              *  * The one used on the previous just
              *  generates an simple json message that
              *  does nothing else than sending a message
              *  * This one opens an Json builder, who
              *  allows you to use multiple texts (same line)
              *  with hoverEvent and clickEvent features
              */
             /*Let`s Start our builder*/
             JsonMessage.newJson()
               /**
                * add:
                *  type: Json
                *  use: Allows you to use different texts on the same line.
                *  example: see under
                */
               /*Here we`re adding the texts*/
               .add("&a&lVisit Google&d! &bClick")
                 /*Please remember this step*/
                 .build()
               .add("&1&o&nHere")
                 /**
                  * clickEvent:
                  *  type: Json
                  *  use: this will be done everytime this text is clicked
                  *  param 1: action (ClickAction)
                   *  This is a enum. Just do ClickAction.ACTION
                   *  param 2: value (String)
                   */
                 /*This will take the player to google*/
                 .clickEvent(ClickAction.Open_Url, "http://www.google.com/")
                 
                 /**
                  * hoverEvent:
                  *  type: Json
                  *  use: this will be shown everytime the player put the mause over this text
                  *  param 1: action (HoverAction)
                  *  This is a enum. Just do HoverAction.ACTION
                  *  param 2: value (String)
                  */
                 /*This will show the url*/
                 .hoverEvent(HoverAction.Show_Text, "&b&o&nhttp://www.google.com/")
                 
                 /*Never forgot to build your Json*/
                 .build()
               
               /*Now that our Json line is ready, let`s build it*/
               .build()
             );
       }
    }


    Compatibility (open)
    This was made for v1.8_R1
    Anyways it should be compatible with every version that supports Json.
    Just organize your imports


    I`m still making the tutorial ;)
     
    Last edited: Mar 17, 2015
  2. Offline

    teej107

    You could also easily generate json with https://code.google.com/p/json-simple/. It's already packaged with the Bukkit API and there is no need to concatenate Strings together inefficiently.
     
  3. @teej107
    That constructor is just a way to access it without reseting it`s values.
    Ikr there`re ways to insert json on bukkit, but for that you should know json. With this class you don`t need any json knowledges to use it, just insert for example the text you want to be displayed, and a message you want to display on hoverEvent. The class will do anything else for you
     
  4. Offline

    teej107

    If you actually used OOP, you wouldn't have to worry about that.
    The simple json library doesn't require you to know Json to use it. It just requires an understanding of how to use Maps/Lists. It also allows for much more complicated Json. I suggest you check it out.
     
  5. @teej107
    What do you mean with OOP?

    Could you give me the link to that library?
     
  6. Offline

    TheEntropy

  7. Offline

    teej107

    Object Oriented Programming.

    And the json-simple library is already packaged in with the Bukkit API anyway. That library requires the same amount of JSON knowledge (or lack of) as your does to use.
    If you want to know how to create a well made JSON library, this is a good resource to look at: http://infernaldevelopers.com/forums/threads/easy-json-for-chat.41/
     
    xTrollxDudex and bwfcwalshy like this.
  8. @teej107
    To be truth if I compare Your resource with mine, It`s the same, only that You set a color to every text and I allow the player to use color codes with "&". And you`re just returning a string, I`m too sending the packet.

    EDIT: Too, your resource is an api, and if you don`t use an external jar, you will have to copy a lot of classes. This is just 1 class and you can simply copy and paste it.
     
    Last edited: Mar 17, 2015
Thread Status:
Not open for further replies.

Share This Page