Fix formatting

This commit is contained in:
Thinkofdeath 2013-12-06 18:45:37 +00:00
parent 2c8b15cb1e
commit 35c1b26a20
15 changed files with 436 additions and 287 deletions

View File

@ -7,13 +7,13 @@ import lombok.Setter;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
public abstract class BaseComponent { public abstract class BaseComponent
{
@Getter(AccessLevel.NONE) @Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
@ -40,27 +40,32 @@ public abstract class BaseComponent {
private ClickEvent clickEvent; private ClickEvent clickEvent;
private HoverEvent hoverEvent; private HoverEvent hoverEvent;
public BaseComponent(BaseComponent old) { public BaseComponent(BaseComponent old)
setColor(old.getColorRaw()); {
setBold(old.isBoldRaw()); setColor( old.getColorRaw() );
setItalic(old.isItalicRaw()); setBold( old.isBoldRaw() );
setUnderlined(old.isUnderlined()); setItalic( old.isItalicRaw() );
setStrikethrough(old.isStrikethroughRaw()); setUnderlined( old.isUnderlined() );
setObfuscated(old.isObfuscatedRaw()); setStrikethrough( old.isStrikethroughRaw() );
setObfuscated( old.isObfuscatedRaw() );
} }
public static String toLegacyText(BaseComponent[] components) { public static String toLegacyText(BaseComponent[] components)
{
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (BaseComponent msg : components) { for (BaseComponent msg : components)
builder.append(msg.toLegacyText()); {
builder.append( msg.toLegacyText() );
} }
return builder.toString(); return builder.toString();
} }
public static String toPlainText(BaseComponent[] components) { public static String toPlainText(BaseComponent[] components)
{
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (BaseComponent msg : components) { for (BaseComponent msg : components)
builder.append(msg.toPlainText()); {
builder.append( msg.toPlainText() );
} }
return builder.toString(); return builder.toString();
} }
@ -70,11 +75,15 @@ public abstract class BaseComponent {
* Returns the color of this component. This uses the parent's color * 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} * if this component doesn't have one. {@link net.md_5.bungee.api.ChatColor#WHITE}
* is returned if no color is found. * is returned if no color is found.
*
* @return the color of this component * @return the color of this component
*/ */
public ChatColor getColor() { public ChatColor getColor()
if (color == null) { {
if (parent == null) { if (color == null)
{
if (parent == null)
{
return ChatColor.WHITE; return ChatColor.WHITE;
} }
return parent.getColor(); return parent.getColor();
@ -85,9 +94,11 @@ public abstract class BaseComponent {
/** /**
* Returns the color of this component without checking the parents * Returns the color of this component without checking the parents
* color. May return null * color. May return null
*
* @return the color of this component * @return the color of this component
*/ */
public ChatColor getColorRaw() { public ChatColor getColorRaw()
{
return color; return color;
} }
@ -95,10 +106,13 @@ public abstract class BaseComponent {
* Returns whether this component is bold. This uses the parent's * Returns whether this component is bold. This uses the parent's
* setting if this component hasn't been set. false is returned * setting if this component hasn't been set. false is returned
* if none of the parent chain has been set. * if none of the parent chain has been set.
*
* @return whether the component is bold * @return whether the component is bold
*/ */
public boolean isBold() { public boolean isBold()
if (bold == null) { {
if (bold == null)
{
return parent != null && parent.isBold(); return parent != null && parent.isBold();
} }
return bold; return bold;
@ -107,9 +121,11 @@ public abstract class BaseComponent {
/** /**
* Returns whether this component is bold without checking * Returns whether this component is bold without checking
* the parents setting. May return null * the parents setting. May return null
*
* @return whether the component is bold * @return whether the component is bold
*/ */
public Boolean isBoldRaw() { public Boolean isBoldRaw()
{
return bold; return bold;
} }
@ -117,10 +133,13 @@ public abstract class BaseComponent {
* Returns whether this component is italic. This uses the parent's * Returns whether this component is italic. This uses the parent's
* setting if this component hasn't been set. false is returned * setting if this component hasn't been set. false is returned
* if none of the parent chain has been set. * if none of the parent chain has been set.
*
* @return whether the component is italic * @return whether the component is italic
*/ */
public boolean isItalic() { public boolean isItalic()
if (italic == null) { {
if (italic == null)
{
return parent != null && parent.isItalic(); return parent != null && parent.isItalic();
} }
return italic; return italic;
@ -129,9 +148,11 @@ public abstract class BaseComponent {
/** /**
* Returns whether this component is italic without checking * Returns whether this component is italic without checking
* the parents setting. May return null * the parents setting. May return null
*
* @return whether the component is italic * @return whether the component is italic
*/ */
public Boolean isItalicRaw() { public Boolean isItalicRaw()
{
return italic; return italic;
} }
@ -139,10 +160,13 @@ public abstract class BaseComponent {
* Returns whether this component is underlined. This uses the parent's * Returns whether this component is underlined. This uses the parent's
* setting if this component hasn't been set. false is returned * setting if this component hasn't been set. false is returned
* if none of the parent chain has been set. * if none of the parent chain has been set.
*
* @return whether the component is underlined * @return whether the component is underlined
*/ */
public boolean isUnderlined() { public boolean isUnderlined()
if (underlined == null) { {
if (underlined == null)
{
return parent != null && parent.isUnderlined(); return parent != null && parent.isUnderlined();
} }
return underlined; return underlined;
@ -151,9 +175,11 @@ public abstract class BaseComponent {
/** /**
* Returns whether this component is underlined without checking * Returns whether this component is underlined without checking
* the parents setting. May return null * the parents setting. May return null
*
* @return whether the component is underlined * @return whether the component is underlined
*/ */
public Boolean isUnderlinedRaw() { public Boolean isUnderlinedRaw()
{
return underlined; return underlined;
} }
@ -161,10 +187,13 @@ public abstract class BaseComponent {
* Returns whether this component is strikethrough. This uses the parent's * Returns whether this component is strikethrough. This uses the parent's
* setting if this component hasn't been set. false is returned * setting if this component hasn't been set. false is returned
* if none of the parent chain has been set. * if none of the parent chain has been set.
*
* @return whether the component is strikethrough * @return whether the component is strikethrough
*/ */
public boolean isStrikethrough() { public boolean isStrikethrough()
if (strikethrough == null) { {
if (strikethrough == null)
{
return parent != null && parent.isStrikethrough(); return parent != null && parent.isStrikethrough();
} }
return strikethrough; return strikethrough;
@ -173,9 +202,11 @@ public abstract class BaseComponent {
/** /**
* Returns whether this component is strikethrough without checking * Returns whether this component is strikethrough without checking
* the parents setting. May return null * the parents setting. May return null
*
* @return whether the component is strikethrough * @return whether the component is strikethrough
*/ */
public Boolean isStrikethroughRaw() { public Boolean isStrikethroughRaw()
{
return strikethrough; return strikethrough;
} }
@ -183,10 +214,13 @@ public abstract class BaseComponent {
* Returns whether this component is obfuscated. This uses the parent's * Returns whether this component is obfuscated. This uses the parent's
* setting if this component hasn't been set. false is returned * setting if this component hasn't been set. false is returned
* if none of the parent chain has been set. * if none of the parent chain has been set.
*
* @return whether the component is obfuscated * @return whether the component is obfuscated
*/ */
public boolean isObfuscated() { public boolean isObfuscated()
if (obfuscated == null) { {
if (obfuscated == null)
{
return parent != null && parent.isObfuscated(); return parent != null && parent.isObfuscated();
} }
return obfuscated; return obfuscated;
@ -195,14 +229,18 @@ public abstract class BaseComponent {
/** /**
* Returns whether this component is obfuscated without checking * Returns whether this component is obfuscated without checking
* the parents setting. May return null * the parents setting. May return null
*
* @return whether the component is obfuscated * @return whether the component is obfuscated
*/ */
public Boolean isObfuscatedRaw() { public Boolean isObfuscatedRaw()
{
return obfuscated; return obfuscated;
} }
public void setExtra(List<BaseComponent> components) { public void setExtra(List<BaseComponent> components)
for (BaseComponent component : components) { {
for (BaseComponent component : components)
{
component.parent = this; component.parent = this;
} }
extra = components; extra = components;
@ -211,62 +249,77 @@ public abstract class BaseComponent {
/** /**
* Appends a text element to the component. The text will * Appends a text element to the component. The text will
* inherit this component's formatting * inherit this component's formatting
*
* @param text the text to append * @param text the text to append
*/ */
public void addExtra(String text) { public void addExtra(String text)
addExtra(new TextComponent(text)); {
addExtra( new TextComponent( text ) );
} }
/** /**
* Appends a component to the component. The text will * Appends a component to the component. The text will
* inherit this component's formatting * inherit this component's formatting
*
* @param component the component to append * @param component the component to append
*/ */
public void addExtra(BaseComponent component) { public void addExtra(BaseComponent component)
if (extra == null) { {
if (extra == null)
{
extra = new ArrayList<>(); extra = new ArrayList<>();
} }
component.parent = this; component.parent = this;
extra.add(component); extra.add( component );
} }
public boolean hasFormatting() { public boolean hasFormatting()
{
return color != null || bold != null || return color != null || bold != null ||
italic != null || underlined != null || italic != null || underlined != null ||
strikethrough != null || obfuscated != null; strikethrough != null || obfuscated != null;
} }
public String toPlainText() { public String toPlainText()
{
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
toPlainText(builder); toPlainText( builder );
return builder.toString(); return builder.toString();
} }
protected void toPlainText(StringBuilder builder) { protected void toPlainText(StringBuilder builder)
if (extra != null) { {
for (BaseComponent e : extra) { if (extra != null)
e.toPlainText(builder); {
for (BaseComponent e : extra)
{
e.toPlainText( builder );
} }
} }
} }
public String toLegacyText() { public String toLegacyText()
{
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
toLegacyText(builder); toLegacyText( builder );
return builder.toString(); return builder.toString();
} }
protected void toLegacyText(StringBuilder builder) { protected void toLegacyText(StringBuilder builder)
if (extra != null) { {
for (BaseComponent e : extra) { if (extra != null)
e.toLegacyText(builder); {
for (BaseComponent e : extra)
{
e.toLegacyText( builder );
} }
} }
} }
@Override @Override
public String toString() { 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()); {
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

@ -10,11 +10,13 @@ import lombok.Setter;
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class ClickEvent { public class ClickEvent
{
private Action action; private Action action;
private String value; private String value;
public enum Action { public enum Action
{
OPEN_URL, OPEN_URL,
OPEN_FILE, OPEN_FILE,
RUN_COMMAND, RUN_COMMAND,

View File

@ -5,7 +5,8 @@ import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@NoArgsConstructor @NoArgsConstructor
public class HoverEvent { public class HoverEvent
{
@Getter @Getter
@Setter @Setter
private Action action; private Action action;
@ -13,25 +14,30 @@ public class HoverEvent {
@Getter @Getter
private Object value; private Object value;
public HoverEvent(Action action, String value) { public HoverEvent(Action action, String value)
setAction(action); {
setValue(value); setAction( action );
setValue( value );
} }
public HoverEvent(Action action, BaseComponent value) { public HoverEvent(Action action, BaseComponent value)
setAction(action); {
setValue(value); setAction( action );
setValue( value );
} }
public void setValue(String value) { public void setValue(String value)
{
this.value = value; this.value = value;
} }
public void setValue(BaseComponent value) { public void setValue(BaseComponent value)
{
this.value = value; this.value = value;
} }
public enum Action { public enum Action
{
SHOW_TEXT, SHOW_TEXT,
SHOW_ACHIEVEMENT, SHOW_ACHIEVEMENT,
SHOW_ITEM SHOW_ITEM

View File

@ -14,123 +14,139 @@ import java.util.regex.Pattern;
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class TextComponent extends BaseComponent { public class TextComponent extends BaseComponent
{
private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$"); private static final Pattern url = Pattern.compile( "^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$" );
public static BaseComponent[] fromLegacyText(String message) { public static BaseComponent[] fromLegacyText(String message)
{
ArrayList<BaseComponent> components = new ArrayList<>(); ArrayList<BaseComponent> components = new ArrayList<>();
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent(); TextComponent component = new TextComponent();
Matcher matcher = url.matcher(message); Matcher matcher = url.matcher( message );
for ( int i = 0; i < message.length(); i++ ) { for (int i = 0; i < message.length(); i++)
char c = message.charAt(i); {
if (c == ChatColor.COLOR_CHAR) { char c = message.charAt( i );
if (c == ChatColor.COLOR_CHAR)
{
i++; i++;
c = message.charAt(i); c = message.charAt( i );
if (c >= 'A' && c <= 'Z') { if (c >= 'A' && c <= 'Z')
{
c += 32; c += 32;
} }
if (builder.length() > 0) { if (builder.length() > 0)
{
TextComponent old = component; TextComponent old = component;
component = new TextComponent(old); component = new TextComponent( old );
old.setText(builder.toString()); old.setText( builder.toString() );
builder = new StringBuilder(); builder = new StringBuilder();
components.add(old); components.add( old );
} }
ChatColor format = ChatColor.getByChar(c); ChatColor format = ChatColor.getByChar( c );
switch (format) { switch (format)
{
case BOLD: case BOLD:
component.setBold(true); component.setBold( true );
break; break;
case ITALIC: case ITALIC:
component.setItalic(true); component.setItalic( true );
break; break;
case UNDERLINE: case UNDERLINE:
component.setUnderlined(true); component.setUnderlined( true );
break; break;
case STRIKETHROUGH: case STRIKETHROUGH:
component.setStrikethrough(true); component.setStrikethrough( true );
break; break;
case MAGIC: case MAGIC:
component.setObfuscated(true); component.setObfuscated( true );
break; break;
case RESET: case RESET:
format = ChatColor.WHITE; format = ChatColor.WHITE;
default: default:
component = new TextComponent(); component = new TextComponent();
component.setColor(format); component.setColor( format );
break; break;
} }
continue; continue;
} }
int pos = message.indexOf(' ', i); int pos = message.indexOf( ' ', i );
if (pos == -1) pos = message.length(); if (pos == -1) pos = message.length();
if (matcher.region(i, pos).find()) { //Web link handling if (matcher.region( i, pos ).find())
{ //Web link handling
if (builder.length() > 0) { if (builder.length() > 0)
{
TextComponent old = component; TextComponent old = component;
component = new TextComponent(old); component = new TextComponent( old );
old.setText(builder.toString()); old.setText( builder.toString() );
builder = new StringBuilder(); builder = new StringBuilder();
components.add(old); components.add( old );
} }
TextComponent old = component; TextComponent old = component;
component = new TextComponent(old); component = new TextComponent( old );
ClickEvent clickEvent = new ClickEvent(); ClickEvent clickEvent = new ClickEvent();
clickEvent.setAction(ClickEvent.Action.OPEN_URL); clickEvent.setAction( ClickEvent.Action.OPEN_URL );
String urlString = message.substring(i, pos); String urlString = message.substring( i, pos );
if (urlString.startsWith("http")) { if (urlString.startsWith( "http" ))
component.setText(urlString); {
clickEvent.setValue(urlString); component.setText( urlString );
} else { clickEvent.setValue( urlString );
component.setText(urlString); } else
clickEvent.setValue("http://" + urlString); {
component.setText( urlString );
clickEvent.setValue( "http://" + urlString );
} }
component.setClickEvent(clickEvent); component.setClickEvent( clickEvent );
components.add(component); components.add( component );
i += pos - i - 1; i += pos - i - 1;
component = old; component = old;
continue; continue;
} }
builder.append(c); builder.append( c );
} }
if (builder.length() > 0) { if (builder.length() > 0)
component.setText(builder.toString()); {
components.add(component); component.setText( builder.toString() );
components.add( component );
} }
return components.toArray(new BaseComponent[components.size()]); return components.toArray( new BaseComponent[components.size()] );
} }
private String text; private String text;
public TextComponent(TextComponent old) { public TextComponent(TextComponent old)
super(old); {
setText(old.getText()); super( old );
setText( old.getText() );
} }
@Override @Override
protected void toPlainText(StringBuilder builder) { protected void toPlainText(StringBuilder builder)
builder.append(text); {
super.toPlainText(builder); builder.append( text );
super.toPlainText( builder );
} }
@Override @Override
protected void toLegacyText(StringBuilder builder) { protected void toLegacyText(StringBuilder builder)
builder.append(getColor()); {
if (isBold()) builder.append(ChatColor.BOLD); builder.append( getColor() );
if (isItalic()) builder.append(ChatColor.ITALIC); if (isBold()) builder.append( ChatColor.BOLD );
if (isUnderlined()) builder.append(ChatColor.UNDERLINE); if (isItalic()) builder.append( ChatColor.ITALIC );
if (isStrikethrough()) builder.append(ChatColor.STRIKETHROUGH); if (isUnderlined()) builder.append( ChatColor.UNDERLINE );
if (isObfuscated()) builder.append(ChatColor.MAGIC); if (isStrikethrough()) builder.append( ChatColor.STRIKETHROUGH );
builder.append(text); if (isObfuscated()) builder.append( ChatColor.MAGIC );
super.toLegacyText(builder); builder.append( text );
super.toLegacyText( builder );
} }
@Override @Override
public String toString() { public String toString()
return String.format("TextComponent{text=%s, %s}", text, super.toString()); {
return String.format( "TextComponent{text=%s, %s}", text, super.toString() );
} }
} }

View File

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

View File

@ -61,11 +61,11 @@ public class ServerKickEvent extends Event implements Cancellable
@Deprecated @Deprecated
public String getKickReason() { public String getKickReason() {
return BaseComponent.toLegacyText(kickReasonComponent); return BaseComponent.toLegacyText( kickReasonComponent );
} }
@Deprecated @Deprecated
public void setKickReason(String reason) { public void setKickReason(String reason) {
kickReasonComponent = TextComponent.fromLegacyText(reason); kickReasonComponent = TextComponent.fromLegacyText( reason );
} }
} }

View File

@ -490,19 +490,21 @@ public class BungeeCord extends ProxyServer
public void broadcast(String message) public void broadcast(String message)
{ {
getConsole().sendMessage( message ); getConsole().sendMessage( message );
broadcast(TextComponent.fromLegacyText(message)); broadcast( TextComponent.fromLegacyText( message ) );
} }
@Override @Override
public void broadcast(BaseComponent[] message) { public void broadcast(BaseComponent[] message)
getConsole().sendMessage(BaseComponent.toLegacyText(message)); {
broadcast(new Chat(ComponentSerializer.toString(message))); getConsole().sendMessage( BaseComponent.toLegacyText( message ) );
broadcast( new Chat( ComponentSerializer.toString( message ) ) );
} }
@Override @Override
public void broadcast(BaseComponent message) { public void broadcast(BaseComponent message)
{
getConsole().sendMessage( message.toLegacyText() ); getConsole().sendMessage( message.toLegacyText() );
broadcast( new Chat(ComponentSerializer.toString(message)) ); broadcast( new Chat( ComponentSerializer.toString( message ) ) );
} }
public void addConnection(UserConnection con) public void addConnection(UserConnection con)

View File

@ -43,15 +43,16 @@ public class ServerConnection implements Server
@Override @Override
public synchronized void disconnect(String reason) public synchronized void disconnect(String reason)
{ {
disconnect( TextComponent.fromLegacyText(reason) ); disconnect( TextComponent.fromLegacyText( reason ) );
} }
@Override @Override
public void disconnect(BaseComponent[] reason) { public void disconnect(BaseComponent[] reason)
if ( !ch.isClosed() ) {
if (!ch.isClosed())
{ {
// TODO: Can we just use a future here? // TODO: Can we just use a future here?
unsafe().sendPacket( new Kick(ComponentSerializer.toString(reason) ) ); unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) );
ch.getHandle().eventLoop().schedule( new Runnable() ch.getHandle().eventLoop().schedule( new Runnable()
{ {
@Override @Override
@ -65,8 +66,9 @@ public class ServerConnection implements Server
} }
@Override @Override
public void disconnect(BaseComponent reason) { public void disconnect(BaseComponent reason)
disconnect(new BaseComponent[]{reason}); {
disconnect( new BaseComponent[]{reason} );
} }
@Override @Override

View File

@ -259,27 +259,29 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public synchronized void disconnect(String reason) public synchronized void disconnect(String reason)
{ {
disconnect0( TextComponent.fromLegacyText(reason) ); disconnect0( TextComponent.fromLegacyText( reason ) );
} }
@Override @Override
public void disconnect(BaseComponent[] reason) { public void disconnect(BaseComponent[] reason)
{
disconnect0( reason ); disconnect0( reason );
} }
@Override @Override
public void disconnect(BaseComponent reason) { public void disconnect(BaseComponent reason)
disconnect0(new BaseComponent[]{reason}); {
disconnect0( new BaseComponent[]{reason} );
} }
public synchronized void disconnect0(BaseComponent[] reason) public synchronized void disconnect0(BaseComponent[] reason)
{ {
if ( ch.getHandle().isActive() ) if (ch.getHandle().isActive())
{ {
bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + BaseComponent.toLegacyText(reason) ); bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + BaseComponent.toLegacyText( reason ) );
unsafe().sendPacket( new Kick( ComponentSerializer.toString(reason) ) ); unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) );
ch.close(); ch.close();
if ( server != null ) if (server != null)
{ {
server.disconnect( "Quitting" ); server.disconnect( "Quitting" );
} }
@ -296,25 +298,27 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void sendMessage(String message) public void sendMessage(String message)
{ {
sendMessage(TextComponent.fromLegacyText(message)); sendMessage( TextComponent.fromLegacyText( message ) );
} }
@Override @Override
public void sendMessages(String... messages) public void sendMessages(String... messages)
{ {
for ( String message : messages ) for (String message : messages)
{ {
sendMessage( message ); sendMessage( message );
} }
} }
@Override @Override
public void sendMessage(BaseComponent[] message) { public void sendMessage(BaseComponent[] message)
{
unsafe().sendPacket( new Chat( ComponentSerializer.toString( message ) ) ); unsafe().sendPacket( new Chat( ComponentSerializer.toString( message ) ) );
} }
@Override @Override
public void sendMessage(BaseComponent message) { public void sendMessage(BaseComponent message)
{
unsafe().sendPacket( new Chat( ComponentSerializer.toString( message ) ) ); unsafe().sendPacket( new Chat( ComponentSerializer.toString( message ) ) );
} }

View File

@ -10,86 +10,109 @@ import net.md_5.bungee.api.chat.HoverEvent;
import java.util.Arrays; import java.util.Arrays;
public class BaseComponentSerializer { public class BaseComponentSerializer
{
protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context) { protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context)
if (object.has("color")) { {
component.setColor(ChatColor.valueOf(object.get("color").getAsString().toUpperCase())); if (object.has( "color" ))
{
component.setColor( ChatColor.valueOf( object.get( "color" ).getAsString().toUpperCase() ) );
} }
if (object.has("bold")) { if (object.has( "bold" ))
component.setBold(object.get("bold").getAsBoolean()); {
component.setBold( object.get( "bold" ).getAsBoolean() );
} }
if (object.has("italic")) { if (object.has( "italic" ))
component.setItalic(object.get("italic").getAsBoolean()); {
component.setItalic( object.get( "italic" ).getAsBoolean() );
} }
if (object.has("underlined")) { if (object.has( "underlined" ))
component.setUnderlined(object.get("underlined").getAsBoolean()); {
component.setUnderlined( object.get( "underlined" ).getAsBoolean() );
} }
if (object.has("strikethrough")) { if (object.has( "strikethrough" ))
component.setUnderlined(object.get("strikethrough").getAsBoolean()); {
component.setUnderlined( object.get( "strikethrough" ).getAsBoolean() );
} }
if (object.has("obfuscated")) { if (object.has( "obfuscated" ))
component.setUnderlined(object.get("obfuscated").getAsBoolean()); {
component.setUnderlined( object.get( "obfuscated" ).getAsBoolean() );
} }
if (object.has("extra")) { if (object.has( "extra" ))
component.setExtra(Arrays.asList((BaseComponent[])context.deserialize(object.get("extra"), BaseComponent[].class))); {
component.setExtra( Arrays.asList( (BaseComponent[]) context.deserialize( object.get( "extra" ), BaseComponent[].class ) ) );
} }
//Events //Events
if (object.has("clickEvent")) { if (object.has( "clickEvent" ))
JsonObject event = object.getAsJsonObject("clickEvent"); {
component.setClickEvent(new ClickEvent( JsonObject event = object.getAsJsonObject( "clickEvent" );
ClickEvent.Action.valueOf(event.get("action").getAsString().toUpperCase()), component.setClickEvent( new ClickEvent(
event.get("value").getAsString())); ClickEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase() ),
event.get( "value" ).getAsString() ) );
} }
if (object.has("hoverEvent")) { if (object.has( "hoverEvent" ))
JsonObject event = object.getAsJsonObject("hoverEvent"); {
JsonObject event = object.getAsJsonObject( "hoverEvent" );
HoverEvent hoverEvent = new HoverEvent(); HoverEvent hoverEvent = new HoverEvent();
hoverEvent.setAction(HoverEvent.Action.valueOf(event.get("action").getAsString().toUpperCase())); hoverEvent.setAction( HoverEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase() ) );
Object res = context.deserialize(event.get("value"), BaseComponent.class); Object res = context.deserialize( event.get( "value" ), BaseComponent.class );
if (res instanceof String) { if (res instanceof String)
hoverEvent.setValue((String) res); {
} else { hoverEvent.setValue( (String) res );
hoverEvent.setValue((BaseComponent) res); } else
{
hoverEvent.setValue( (BaseComponent) res );
} }
component.setHoverEvent(hoverEvent); component.setHoverEvent( hoverEvent );
} }
} }
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context) { protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context)
if (component.getColorRaw() != null) { {
object.addProperty("color", component.getColorRaw().getName()); if (component.getColorRaw() != null)
{
object.addProperty( "color", component.getColorRaw().getName() );
} }
if (component.isBoldRaw() != null) { if (component.isBoldRaw() != null)
object.addProperty("bold", component.isBoldRaw()); {
object.addProperty( "bold", component.isBoldRaw() );
} }
if (component.isItalicRaw() != null) { if (component.isItalicRaw() != null)
object.addProperty("italic", component.isItalicRaw()); {
object.addProperty( "italic", component.isItalicRaw() );
} }
if (component.isUnderlinedRaw() != null) { if (component.isUnderlinedRaw() != null)
object.addProperty("underlined", component.isUnderlinedRaw()); {
object.addProperty( "underlined", component.isUnderlinedRaw() );
} }
if (component.isStrikethroughRaw() != null) { if (component.isStrikethroughRaw() != null)
object.addProperty("strikethrough", component.isStrikethroughRaw()); {
object.addProperty( "strikethrough", component.isStrikethroughRaw() );
} }
if (component.isObfuscatedRaw() != null) { if (component.isObfuscatedRaw() != null)
object.addProperty("obfuscated", component.isObfuscatedRaw()); {
object.addProperty( "obfuscated", component.isObfuscatedRaw() );
} }
if (component.getExtra() != null) { if (component.getExtra() != null)
object.add("extra", context.serialize(component.getExtra())); {
object.add( "extra", context.serialize( component.getExtra() ) );
} }
//Events //Events
if (component.getClickEvent() != null) { if (component.getClickEvent() != null)
{
JsonObject clickEvent = new JsonObject(); JsonObject clickEvent = new JsonObject();
clickEvent.addProperty("action", component.getClickEvent().getAction().toString().toLowerCase()); clickEvent.addProperty( "action", component.getClickEvent().getAction().toString().toLowerCase() );
clickEvent.addProperty("value", component.getClickEvent().getValue()); clickEvent.addProperty( "value", component.getClickEvent().getValue() );
} }
if (component.getHoverEvent() != null) { if (component.getHoverEvent() != null)
{
JsonObject clickEvent = new JsonObject(); JsonObject clickEvent = new JsonObject();
clickEvent.addProperty("action", component.getHoverEvent().getAction().toString().toLowerCase()); clickEvent.addProperty( "action", component.getHoverEvent().getAction().toString().toLowerCase() );
clickEvent.add("value", context.serialize(component.getHoverEvent().getValue())); clickEvent.add( "value", context.serialize( component.getHoverEvent().getValue() ) );
} }
} }
} }

View File

@ -1,59 +1,66 @@
package net.md_5.bungee.chat; package net.md_5.bungee.chat;
import com.google.gson.*; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader; import com.google.gson.JsonDeserializationContext;
import com.google.gson.stream.JsonWriter; import com.google.gson.JsonDeserializer;
import net.md_5.bungee.api.ChatColor; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent; import net.md_5.bungee.api.chat.TranslatableComponent;
import javax.xml.soap.Text;
import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonDeserializer<BaseComponent> { public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonDeserializer<BaseComponent>
{
private final static Gson gson = new GsonBuilder(). private final static Gson gson = new GsonBuilder().
registerTypeAdapter(BaseComponent.class, new ComponentSerializer()). registerTypeAdapter( BaseComponent.class, new ComponentSerializer() ).
registerTypeAdapter(TextComponent.class, new TextComponentSerializer()). registerTypeAdapter( TextComponent.class, new TextComponentSerializer() ).
registerTypeAdapter(TranslatableComponent.class, new TranslatableComponentSerializer()). registerTypeAdapter( TranslatableComponent.class, new TranslatableComponentSerializer() ).
create(); create();
public static BaseComponent[] parse(String json) { public static BaseComponent[] parse(String json)
if (json.startsWith("[")) { //Array {
return gson.fromJson(json, BaseComponent[].class); if (json.startsWith( "[" ))
{ //Array
return gson.fromJson( json, BaseComponent[].class );
} }
return new BaseComponent[]{gson.fromJson(json, BaseComponent.class)}; return new BaseComponent[]{gson.fromJson( json, BaseComponent.class )};
} }
public static String toString(BaseComponent component) { public static String toString(BaseComponent component)
return gson.toJson(component); {
return gson.toJson( component );
} }
public static String toString(BaseComponent[] components) { public static String toString(BaseComponent[] components)
return gson.toJson(components); {
return gson.toJson( components );
} }
@Override @Override
public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
if (json.isJsonPrimitive()) { {
return new TextComponent(json.getAsString()); if (json.isJsonPrimitive())
{
return new TextComponent( json.getAsString() );
} }
JsonObject object = json.getAsJsonObject(); JsonObject object = json.getAsJsonObject();
if (object.has("translate")) { if (object.has( "translate" ))
return context.deserialize(json, TranslatableComponent.class); {
return context.deserialize( json, TranslatableComponent.class );
} }
return context.deserialize(json, TextComponent.class); return context.deserialize( json, TextComponent.class );
} }
@Override @Override
public JsonElement serialize(BaseComponent src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(BaseComponent src, Type typeOfSrc, JsonSerializationContext context)
return context.serialize(src, src.getClass()); {
return context.serialize( src, src.getClass() );
} }
} }

View File

@ -1,28 +1,39 @@
package net.md_5.bungee.chat; package net.md_5.bungee.chat;
import com.google.gson.*; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import java.lang.reflect.Type; import java.lang.reflect.Type;
public class TextComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TextComponent> , JsonDeserializer<TextComponent>{ public class TextComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TextComponent>, JsonDeserializer<TextComponent>
{
@Override @Override
public TextComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public TextComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
TextComponent component = new TextComponent(); TextComponent component = new TextComponent();
JsonObject object = json.getAsJsonObject(); JsonObject object = json.getAsJsonObject();
deserialize(object, component, context); deserialize( object, component, context );
component.setText(object.get("text").getAsString()); component.setText( object.get( "text" ).getAsString() );
return component; return component;
} }
@Override @Override
public JsonElement serialize(TextComponent src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(TextComponent src, Type typeOfSrc, JsonSerializationContext context)
if (!src.hasFormatting()) { {
return new JsonPrimitive(src.getText()); if (!src.hasFormatting())
{
return new JsonPrimitive( src.getText() );
} }
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
serialize(object, src, context); serialize( object, src, context );
object.addProperty("text", src.getText()); object.addProperty( "text", src.getText() );
return object; return object;
} }
} }

View File

@ -1,34 +1,43 @@
package net.md_5.bungee.chat; package net.md_5.bungee.chat;
import com.google.gson.*; import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.TranslatableComponent; import net.md_5.bungee.api.chat.TranslatableComponent;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
public class TranslatableComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TranslatableComponent> , JsonDeserializer<TranslatableComponent>{ public class TranslatableComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TranslatableComponent>, JsonDeserializer<TranslatableComponent>
{
@Override @Override
public TranslatableComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public TranslatableComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
TranslatableComponent component = new TranslatableComponent(); TranslatableComponent component = new TranslatableComponent();
JsonObject object = json.getAsJsonObject(); JsonObject object = json.getAsJsonObject();
deserialize(object, component, context); deserialize( object, component, context );
component.setTranslate(object.get("translate").getAsString()); component.setTranslate( object.get( "translate" ).getAsString() );
if (object.has("with")) { if (object.has( "with" ))
component.setWith(Arrays.asList((BaseComponent[])context.deserialize(object.get("with"), BaseComponent[].class))); {
component.setWith( Arrays.asList( (BaseComponent[]) context.deserialize( object.get( "with" ), BaseComponent[].class ) ) );
} }
return component; return component;
} }
@Override @Override
public JsonElement serialize(TranslatableComponent src, Type typeOfSrc, JsonSerializationContext context) { public JsonElement serialize(TranslatableComponent src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
serialize(object, src, context); serialize( object, src, context );
object.addProperty("translate", src.getTranslate()); object.addProperty( "translate", src.getTranslate() );
if (src.getWith() != null) { if (src.getWith() != null)
object.add("with", context.serialize(src.getWith())); {
object.add( "with", context.serialize( src.getWith() ) );
} }
return object; return object;
} }

View File

@ -29,19 +29,21 @@ public class ConsoleCommandSender implements CommandSender
@Override @Override
public void sendMessages(String... messages) public void sendMessages(String... messages)
{ {
for ( String message : messages ) for (String message : messages)
{ {
sendMessage( message ); sendMessage( message );
} }
} }
@Override @Override
public void sendMessage(BaseComponent[] message) { public void sendMessage(BaseComponent[] message)
sendMessage(BaseComponent.toLegacyText(message)); {
sendMessage( BaseComponent.toLegacyText( message ) );
} }
@Override @Override
public void sendMessage(BaseComponent message) { public void sendMessage(BaseComponent message)
{
sendMessage( message.toLegacyText() ); sendMessage( message.toLegacyText() );
} }

View File

@ -398,25 +398,27 @@ public class InitialHandler extends PacketHandler implements PendingConnection
@Override @Override
public synchronized void disconnect(String reason) public synchronized void disconnect(String reason)
{ {
if ( !ch.isClosed() ) if (!ch.isClosed())
{ {
unsafe().sendPacket( new Kick( ComponentSerializer.toString(TextComponent.fromLegacyText(reason)) ) ); unsafe().sendPacket( new Kick( ComponentSerializer.toString( TextComponent.fromLegacyText( reason ) ) ) );
ch.close(); ch.close();
} }
} }
@Override @Override
public void disconnect(BaseComponent[] reason) { public void disconnect(BaseComponent[] reason)
if ( !ch.isClosed() )
{ {
unsafe().sendPacket( new Kick( ComponentSerializer.toString(reason) ) ); if (!ch.isClosed())
{
unsafe().sendPacket( new Kick( ComponentSerializer.toString( reason ) ) );
ch.close(); ch.close();
} }
} }
@Override @Override
public void disconnect(BaseComponent reason) { public void disconnect(BaseComponent reason)
disconnect(new BaseComponent[]{reason}); {
disconnect( new BaseComponent[]{reason} );
} }
@Override @Override