Chat Component API

This commit is contained in:
Thinkofdeath
2013-12-06 17:23:28 +00:00
parent 85c27f30ee
commit c20d8f9cd6
16 changed files with 799 additions and 205 deletions

View File

@@ -1,5 +1,7 @@
package net.md_5.bungee.api;
import net.md_5.bungee.api.chat.BaseComponent;
import java.util.Collection;
public interface CommandSender
@@ -27,6 +29,20 @@ public interface CommandSender
*/
public void sendMessages(String... messages);
/**
* Send a message to this sender.
*
* @param message the message to send
*/
public void sendMessage(BaseComponent[] message);
/**
* Send a message to this sender.
*
* @param message the message to send
*/
public void sendMessage(BaseComponent message);
/**
* Get all groups this user is part of. This returns an unmodifiable
* collection.

View File

@@ -1,5 +1,6 @@
package net.md_5.bungee.api;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.plugin.PluginManager;
import com.google.common.base.Preconditions;
import java.io.File;
@@ -233,6 +234,20 @@ public abstract class ProxyServer
*/
public abstract void broadcast(String message);
/**
* Send the specified message to the console and all connected players.
*
* @param message the message to broadcast
*/
public abstract void broadcast(BaseComponent[] message);
/**
* Send the specified message to the console and all connected players.
*
* @param message the message to broadcast
*/
public abstract void broadcast(BaseComponent message);
/**
* Gets a new instance of this proxies custom tab list.
*

View File

@@ -0,0 +1,256 @@
package net.md_5.bungee.api.chat;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.md_5.bungee.api.ChatColor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Getter
@Setter
@NoArgsConstructor
public abstract class BaseComponent {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
BaseComponent parent;
//Formatting
@Getter(AccessLevel.NONE)
private ChatColor color;
@Getter(AccessLevel.NONE)
private Boolean bold;
@Getter(AccessLevel.NONE)
private Boolean italic;
@Getter(AccessLevel.NONE)
private Boolean underlined;
@Getter(AccessLevel.NONE)
private Boolean strikethrough;
@Getter(AccessLevel.NONE)
private Boolean obfuscated;
//Appended components
private List<BaseComponent> extra;
//Events
private ClickEvent clickEvent;
private HoverEvent hoverEvent;
public BaseComponent(BaseComponent old) {
setColor(old.getColorRaw());
setBold(old.isBoldRaw());
setItalic(old.isItalicRaw());
setUnderlined(old.isUnderlined());
setStrikethrough(old.isStrikethroughRaw());
setObfuscated(old.isObfuscatedRaw());
}
/**
* Returns the color of this component. This uses the parent's color
* if this component doesn't have one. {@link net.md_5.bungee.api.ChatColor#WHITE}
* is returned if no color is found.
* @return the color of this component
*/
public ChatColor getColor() {
if (color == null) {
if (parent == null) {
return ChatColor.WHITE;
}
return parent.getColor();
}
return color;
}
/**
* Returns the color of this component without checking the parents
* color. May return null
* @return the color of this component
*/
public ChatColor getColorRaw() {
return color;
}
/**
* Returns whether this component is bold. This uses the parent's
* setting if this component hasn't been set. false is returned
* if none of the parent chain has been set.
* @return whether the component is bold
*/
public boolean isBold() {
if (bold == null) {
return parent != null && parent.isBold();
}
return bold;
}
/**
* Returns whether this component is bold without checking
* the parents setting. May return null
* @return whether the component is bold
*/
public Boolean isBoldRaw() {
return bold;
}
/**
* Returns whether this component is italic. This uses the parent's
* setting if this component hasn't been set. false is returned
* if none of the parent chain has been set.
* @return whether the component is italic
*/
public boolean isItalic() {
if (italic == null) {
return parent != null && parent.isItalic();
}
return italic;
}
/**
* Returns whether this component is italic without checking
* the parents setting. May return null
* @return whether the component is italic
*/
public Boolean isItalicRaw() {
return italic;
}
/**
* Returns whether this component is underlined. This uses the parent's
* setting if this component hasn't been set. false is returned
* if none of the parent chain has been set.
* @return whether the component is underlined
*/
public boolean isUnderlined() {
if (underlined == null) {
return parent != null && parent.isUnderlined();
}
return underlined;
}
/**
* Returns whether this component is underlined without checking
* the parents setting. May return null
* @return whether the component is underlined
*/
public Boolean isUnderlinedRaw() {
return underlined;
}
/**
* Returns whether this component is strikethrough. This uses the parent's
* setting if this component hasn't been set. false is returned
* if none of the parent chain has been set.
* @return whether the component is strikethrough
*/
public boolean isStrikethrough() {
if (strikethrough == null) {
return parent != null && parent.isStrikethrough();
}
return strikethrough;
}
/**
* Returns whether this component is strikethrough without checking
* the parents setting. May return null
* @return whether the component is strikethrough
*/
public Boolean isStrikethroughRaw() {
return strikethrough;
}
/**
* Returns whether this component is obfuscated. This uses the parent's
* setting if this component hasn't been set. false is returned
* if none of the parent chain has been set.
* @return whether the component is obfuscated
*/
public boolean isObfuscated() {
if (obfuscated == null) {
return parent != null && parent.isObfuscated();
}
return obfuscated;
}
/**
* Returns whether this component is obfuscated without checking
* the parents setting. May return null
* @return whether the component is obfuscated
*/
public Boolean isObfuscatedRaw() {
return obfuscated;
}
public void setExtra(List<BaseComponent> components) {
for (BaseComponent component : components) {
component.parent = this;
}
extra = components;
}
/**
* Appends a text element to the component. The text will
* inherit this component's formatting
* @param text the text to append
*/
public void addExtra(String text) {
addExtra(new TextComponent(text));
}
/**
* Appends a component to the component. The text will
* inherit this component's formatting
* @param component the component to append
*/
public void addExtra(BaseComponent component) {
if (extra == null) {
extra = new ArrayList<>();
}
component.parent = this;
extra.add(component);
}
public boolean hasFormatting() {
return color != null || bold != null ||
italic != null || underlined != null ||
strikethrough != null || obfuscated != null;
}
public String toPlainText() {
StringBuilder builder = new StringBuilder();
toPlainText(builder);
return builder.toString();
}
protected void toPlainText(StringBuilder builder) {
if (extra != null) {
for (BaseComponent e : extra) {
e.toPlainText(builder);
}
}
}
public String toLegacyText() {
StringBuilder builder = new StringBuilder();
toLegacyText(builder);
return builder.toString();
}
protected void toLegacyText(StringBuilder builder) {
if (extra != null) {
for (BaseComponent e : extra) {
e.toLegacyText(builder);
}
}
}
@Override
public String toString() {
return String.format("BaseComponent{color=%s, bold=%b, italic=%b, underlined=%b, strikethrough=%b, obfuscated=%b}", getColor().getName(), isBold(), isItalic(), isUnderlined(), isStrikethrough(), isObfuscated());
}
}

View File

@@ -0,0 +1,23 @@
package net.md_5.bungee.api.chat;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ClickEvent {
private Action action;
private String value;
public enum Action {
OPEN_URL,
OPEN_FILE,
RUN_COMMAND,
SUGGEST_COMMAND
}
}

View File

@@ -0,0 +1,39 @@
package net.md_5.bungee.api.chat;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@NoArgsConstructor
public class HoverEvent {
@Getter
@Setter
private Action action;
@Getter
private Object value;
public HoverEvent(Action action, String value) {
setAction(action);
setValue(value);
}
public HoverEvent(Action action, BaseComponent value) {
setAction(action);
setValue(value);
}
public void setValue(String value) {
this.value = value;
}
public void setValue(BaseComponent value) {
this.value = value;
}
public enum Action {
SHOW_TEXT,
SHOW_ACHIEVEMENT,
SHOW_ITEM
}
}

View File

@@ -0,0 +1,43 @@
package net.md_5.bungee.api.chat;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import net.md_5.bungee.api.ChatColor;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TextComponent extends BaseComponent {
private String text;
public TextComponent(TextComponent old) {
super(old);
setText(old.getText());
}
@Override
protected void toPlainText(StringBuilder builder) {
builder.append(text);
super.toPlainText(builder);
}
@Override
protected void toLegacyText(StringBuilder builder) {
builder.append(getColor());
if (isBold()) builder.append(ChatColor.BOLD);
if (isItalic()) builder.append(ChatColor.ITALIC);
if (isUnderlined()) builder.append(ChatColor.UNDERLINE);
if (isStrikethrough()) builder.append(ChatColor.STRIKETHROUGH);
if (isObfuscated()) builder.append(ChatColor.MAGIC);
builder.append(text);
super.toLegacyText(builder);
}
@Override
public String toString() {
return String.format("TextComponent{text=%s, %s}", text, super.toString());
}
}

View File

@@ -0,0 +1,53 @@
package net.md_5.bungee.api.chat;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
@Getter
@Setter
@NoArgsConstructor
public class TranslatableComponent extends BaseComponent {
private String translate;
private List<BaseComponent> with;
public TranslatableComponent(String translate, Object ...with) {
setTranslate(translate);
this.with = new ArrayList<>();
for (Object w : with) {
if (w instanceof String) {
this.with.add(new TextComponent((String) w));
} else {
this.with.add((BaseComponent) w);
}
}
}
public void setWith(List<BaseComponent> components) {
for (BaseComponent component : components) {
component.parent = this;
}
with = components;
}
@Override
protected void toPlainText(StringBuilder builder) {
//TODO
super.toPlainText(builder);
}
@Override
protected void toLegacyText(StringBuilder builder) {
//TODO
super.toLegacyText(builder);
}
@Override
public String toString() {
return String.format("TranslatableComponent{translate=%s, with=%s, %s}", translate, with, super.toString());
}
}