Skip to content

Commit

Permalink
Allow message parts to be initialized without text
Browse files Browse the repository at this point in the history
  • Loading branch information
mkremins committed Apr 22, 2014
1 parent bcb7365 commit c9eb890
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/main/java/mkremins/fanciful/FancyMessage.java
Expand Up @@ -39,6 +39,23 @@ public FancyMessage(final String firstPartText) {
dirty = false;
}

public FancyMessage() {
messageParts = new ArrayList<MessagePart>();
messageParts.add(new MessagePart());
jsonString = null;
dirty = false;
}

public FancyMessage text(String text) {
MessagePart latest = latest();
if (latest.hasText()) {
throw new IllegalStateException("text for this message part is already set");
}
latest.text = text;
dirty = true;
return this;
}

public FancyMessage color(final ChatColor color) {
if (!color.isColor()) {
throw new IllegalArgumentException(color.name() + " is not a color");
Expand Down Expand Up @@ -175,11 +192,23 @@ public FancyMessage tooltip(final String... lines) {
}

public FancyMessage then(final Object obj) {
if (!latest().hasText()) {
throw new IllegalStateException("previous message part has no text");
}
messageParts.add(new MessagePart(obj.toString()));
dirty = true;
return this;
}

public FancyMessage then() {
if (!latest().hasText()) {
throw new IllegalStateException("previous message part has no text");
}
messageParts.add(new MessagePart());
dirty = true;
return this;
}

public String toJSONString() {
if (!dirty && jsonString != null) {
return jsonString;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/mkremins/fanciful/MessagePart.java
Expand Up @@ -9,11 +9,17 @@ final class MessagePart {
ChatColor[] styles = {};
String clickActionName = null, clickActionData = null,
hoverActionName = null, hoverActionData = null;
final String text;
String text = null;

MessagePart(final String text) {
this.text = text;
}

MessagePart() {}

boolean hasText() {
return text != null;
}

JsonWriter writeJson(JsonWriter json) {
try {
Expand Down

0 comments on commit c9eb890

Please sign in to comment.