Skip to content

Instantly share code, notes, and snippets.

@kelvneo
Last active January 1, 2016 04:39
Show Gist options
  • Save kelvneo/8092899 to your computer and use it in GitHub Desktop.
Save kelvneo/8092899 to your computer and use it in GitHub Desktop.
ChatMsg. For a fast parsing of /tellraw thingy. It does not use JSON builders. So its fast but unreliable but still can be used. Herp derp.
package com.frosty;
import java.util.ArrayList;
import java.util.List;
public class ChatMsg {
public static class ChatPart{
public enum Colour{
BLACK("black"),
DARK_BLUE("dark_blue"),
DARK_GREEN("dark_green"),
DARK_AQUA("dark_aqua"),
DARK_RED("dark_red"),
DARK_PURPLE("dark_purple"),
GOLD("gold"),
GRAY("gray"),
DARK_GRAY("dark_gray"),
BLUE("blue"),
GREEN("green"),
AQUA("aqua"),
RED("red"),
LIGHT_PURPLE("light_purple"),
YELLOW("yellow"),
WHITE("white");
private String colourName;
Colour(String colourName){
this.colourName = colourName;
}
public String getName(){
return this.colourName;
}
public String toString(){
return "color:" + this.colourName;
}
}
public class ClickEvent{
private String action = "";
private String value = "";
private ChatPart part;
private ClickEvent(String action, String value, ChatPart part){
setAction(action);
setValue(value);
this.part = part;
}
public ClickEvent setAction(String action) {
this.action = action;
return this;
}
public ClickEvent setValue(String value) {
this.value = value;
return this;
}
public ChatPart end(){
this.part.click = this;
return this.part;
}
public String toString(){
return "clickEvent:{action:" + action + ",value:\"" + value + "\"}";
}
public static final String OPEN_URL = "open_url";
public static final String SUGGEST_COMMAND = "suggest_command";
public static final String RUN_COMMAND = "run_command";
}
public class HoverEvent{
private String action = "";
private String value = "";
private ChatPart part;
private HoverEvent(String action, String value, ChatPart part){
setAction(action);
setValue(value);
this.part = part;
}
public HoverEvent setAction(String action) {
this.action = action;
return this;
}
public HoverEvent setValue(String value) {
this.value = value;
return this;
}
public ChatPart end(){
this.part.hover = this;
return this.part;
}
public String toString(){
return "hoverEvent:{action:" + action + ",value:\"" + value + "\"}";
}
public static final String SHOW_TEXT = "show_text";
public static final String SHOW_ITEM = "show_item";
public static final String SHOW_ACHIEVEMENT = "show_achievement";
}
private String text;
private Colour colour = Colour.WHITE;
private HoverEvent hover;
private ClickEvent click;
private boolean bold = false;
private boolean italic = false;
private boolean strikethrough = false;
private boolean obfuscated = false;
private boolean underlined = false;
private ChatMsg msg;
private boolean extra;
private ChatPart(ChatMsg msg, boolean extra){
this.msg = msg;
this.extra = extra;
}
public ChatPart text(String text){
this.text = text;
return this;
}
public HoverEvent hoverEvent(){
this.hover = new HoverEvent("action", "", this);
return hover;
}
public ClickEvent clickEvent(){
this.click = new ClickEvent("action", "", this);
return click;
}
public ChatPart colour(Colour colour){
this.colour = colour;
return this;
}
public ChatPart bold(){
this.bold = true;
return this;
}
public ChatPart italic(){
this.italic = true;
return this;
}
public ChatPart strikethrough(){
this.strikethrough = true;
return this;
}
public ChatPart obfuscated(){
this.obfuscated = true;
return this;
}
public ChatPart underlined(){
this.underlined = true;
return this;
}
public ChatMsg end(){
if(extra)
msg.extras.add(this);
return msg;
}
public String toString(){
if(hover == null && click == null)
return ("{text:\"" + text + "\"," + colour.toString() + ",bold:" + bold + ",italic:" + italic + ",strikethrough:" + strikethrough + ",obfuscated:" + obfuscated + ",underlined:" + underlined +"}");
else if(hover != null && click == null)
return ("{text:\"" + text + "\"," + colour.toString() + ",bold:" + bold + ",italic:" + italic + ",strikethrough:" + strikethrough + ",obfuscated:" + obfuscated + ",underlined:" + underlined + "," + hover.toString() + "}");
else if(hover == null && click != null)
return ("{text:\"" + text + "\"," + colour.toString() + ",bold:" + bold + ",italic:" + italic + ",strikethrough:" + strikethrough + ",obfuscated:" + obfuscated + ",underlined:" + underlined + "," + click.toString() + "}");
else
return ("{text:\"" + text + "\"," + colour.toString() + ",bold:" + bold + ",italic:" + italic + ",strikethrough:" + strikethrough + ",obfuscated:" + obfuscated + ",underlined:" + underlined + "," + hover.toString() + "," + click.toString() + "}");
}
}
private ChatPart message;
private List<ChatPart> extras = new ArrayList<ChatPart>();
private ChatMsg(){
this.message = new ChatPart(this,false);
}
public static ChatMsg createNewMessage(){
return new ChatMsg();
}
public ChatPart message(){
return message;
}
public ChatPart extra(){
ChatPart extra = new ChatPart(this,true);
return extra;
}
public String toString(){
if(extras == null || extras.isEmpty())
return message.toString();
String newMessage = message.toString().substring(0, message.toString().length() - 1).concat(",extra:[");
for(int i = 0; i < extras.size(); i ++){
if(extras.get(i) == extras.get(extras.size() - 1))
newMessage += (extras.get(i).toString() + "]}");
else
newMessage += (extras.get(i).toString() + ",");
}
return newMessage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment