Util JsonConstructor

Discussion in 'Resources' started by ByteZ, Sep 7, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    ByteZ

    Hello! I made this class a couple of months ago and I thought i would share it with you guys ;) enjoy!
    Code:
    public class JsonConstructor {
        /**
         *
         * @author ByteZ
         *
         */
        public enum ClickEvent{
            RUN_COMMAND, SUGGEST_COMMAND, OPEN_URL
        }
        ArrayList<String> text = new ArrayList<>();
        String json = "{text:\"";
        public JsonConstructor(String text){
            json +=text+"\"";
        }
        public JsonConstructor addText(String text){
            if(this.text.size() == 0){
                json +=",extra:[{text:\"" + text + "\"}";
            }else{
                json +=",{text:\"" + text + "\"}";
            }
            this.text.add(text);
            return this;
        }
        public JsonConstructor includesColor(ChatColor color){
            if(text.size() > 0){
                json = json.substring(0, json.length()-1);
                if(color.isColor()){
                    json+=",color:"+color.name().toLowerCase()+"}";
                }else{
                    json+=","+color.name().toLowerCase()+":true}";
                }
            }else{
                if(color.isColor()){
                    json+=",color:"+color.name().toLowerCase()+"";
                }else{
                    json+=","+color.name().toLowerCase()+":true";
                }
            }
            return this;
        }
        public JsonConstructor includesClickEvent(ClickEvent Action,String value){
            if(text.size() > 0){
                json = json.substring(0, json.length()-1);
                json+=",clickEvent:{action:" + Action.toString().toLowerCase()+",value:\"" + value + "\"}}";
            }else{
                json+=",clickEvent:{action:" + Action.toString().toLowerCase()+",value:\"" + value + "\"}";
            }
            return this;
        }
        public JsonConstructor includesHoverTextEvent(String value){
            if(text.size() > 0){
                json = json.substring(0, json.length()-1);
                json+=",hoverEvent:{action:show_text,value:\"" + value + "\"}}";
            }else{
                json+=",hoverEvent:{action:show_text,value:\"" + value + "\"}";
            }
            return this;
        }
        public JsonConstructor includesHoverAchievmentEvent(Achievement ach){
            if(text.size() > 0){
                json = json.substring(0, json.length()-1);
                json+=",hoverEvent:{action:show_achievement,value:achievement."+ach.name().toLowerCase().replace("_", "")+"}}";
            }else{
                json+=",hoverEvent:{action:show_achievement,value:achievement."+ach.name().toLowerCase().replace("_", "")+"}";
            }
            return this;
        }
        @Override
        public String toString() {
            if(text.size() > 0){
                return json+"]}";
            }else{
                return json+"}";
            }
        }
        public void send(Player p){
            ((CraftPlayer)p).getHandle().playerConnection.sendPacket(new PacketPlayOutChat(ChatSerializer.a(toString())));
        }
    }
    How to use:
    Code:
            new JsonConstructor("You have received a friend request!")
            .includesColor(ChatColor.AQUA)
            .addText(" Click to show the request.")
            .includesColor(ChatColor.GRAY)
            .includesHoverTextEvent("§7Click me§8!")
            .includesClickEvent(ClickEvent.RUN_COMMAND, "/friend requestlist")
            .send(player);
    I will add reflections soon!
     
Thread Status:
Not open for further replies.

Share This Page