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 java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Getter
@Setter
@NoArgsConstructor
public abstract class BaseComponent {
public abstract class BaseComponent
{
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
@@ -40,27 +40,32 @@ public abstract class BaseComponent {
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());
public BaseComponent(BaseComponent old)
{
setColor( old.getColorRaw() );
setBold( old.isBoldRaw() );
setItalic( old.isItalicRaw() );
setUnderlined( old.isUnderlined() );
setStrikethrough( old.isStrikethroughRaw() );
setObfuscated( old.isObfuscatedRaw() );
}
public static String toLegacyText(BaseComponent[] components) {
public static String toLegacyText(BaseComponent[] components)
{
StringBuilder builder = new StringBuilder();
for (BaseComponent msg : components) {
builder.append(msg.toLegacyText());
for (BaseComponent msg : components)
{
builder.append( msg.toLegacyText() );
}
return builder.toString();
}
public static String toPlainText(BaseComponent[] components) {
public static String toPlainText(BaseComponent[] components)
{
StringBuilder builder = new StringBuilder();
for (BaseComponent msg : components) {
builder.append(msg.toPlainText());
for (BaseComponent msg : components)
{
builder.append( msg.toPlainText() );
}
return builder.toString();
}
@@ -70,11 +75,15 @@ public abstract class BaseComponent {
* 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) {
public ChatColor getColor()
{
if (color == null)
{
if (parent == null)
{
return ChatColor.WHITE;
}
return parent.getColor();
@@ -85,9 +94,11 @@ public abstract class BaseComponent {
/**
* Returns the color of this component without checking the parents
* color. May return null
*
* @return the color of this component
*/
public ChatColor getColorRaw() {
public ChatColor getColorRaw()
{
return color;
}
@@ -95,10 +106,13 @@ public abstract class BaseComponent {
* 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) {
public boolean isBold()
{
if (bold == null)
{
return parent != null && parent.isBold();
}
return bold;
@@ -107,9 +121,11 @@ public abstract class BaseComponent {
/**
* Returns whether this component is bold without checking
* the parents setting. May return null
*
* @return whether the component is bold
*/
public Boolean isBoldRaw() {
public Boolean isBoldRaw()
{
return bold;
}
@@ -117,10 +133,13 @@ public abstract class BaseComponent {
* 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) {
public boolean isItalic()
{
if (italic == null)
{
return parent != null && parent.isItalic();
}
return italic;
@@ -129,9 +148,11 @@ public abstract class BaseComponent {
/**
* Returns whether this component is italic without checking
* the parents setting. May return null
*
* @return whether the component is italic
*/
public Boolean isItalicRaw() {
public Boolean isItalicRaw()
{
return italic;
}
@@ -139,10 +160,13 @@ public abstract class BaseComponent {
* 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) {
public boolean isUnderlined()
{
if (underlined == null)
{
return parent != null && parent.isUnderlined();
}
return underlined;
@@ -151,9 +175,11 @@ public abstract class BaseComponent {
/**
* Returns whether this component is underlined without checking
* the parents setting. May return null
*
* @return whether the component is underlined
*/
public Boolean isUnderlinedRaw() {
public Boolean isUnderlinedRaw()
{
return underlined;
}
@@ -161,10 +187,13 @@ public abstract class BaseComponent {
* 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) {
public boolean isStrikethrough()
{
if (strikethrough == null)
{
return parent != null && parent.isStrikethrough();
}
return strikethrough;
@@ -173,9 +202,11 @@ public abstract class BaseComponent {
/**
* Returns whether this component is strikethrough without checking
* the parents setting. May return null
*
* @return whether the component is strikethrough
*/
public Boolean isStrikethroughRaw() {
public Boolean isStrikethroughRaw()
{
return strikethrough;
}
@@ -183,10 +214,13 @@ public abstract class BaseComponent {
* 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) {
public boolean isObfuscated()
{
if (obfuscated == null)
{
return parent != null && parent.isObfuscated();
}
return obfuscated;
@@ -195,14 +229,18 @@ public abstract class BaseComponent {
/**
* Returns whether this component is obfuscated without checking
* the parents setting. May return null
*
* @return whether the component is obfuscated
*/
public Boolean isObfuscatedRaw() {
public Boolean isObfuscatedRaw()
{
return obfuscated;
}
public void setExtra(List<BaseComponent> components) {
for (BaseComponent component : components) {
public void setExtra(List<BaseComponent> components)
{
for (BaseComponent component : components)
{
component.parent = this;
}
extra = components;
@@ -211,62 +249,77 @@ public abstract class BaseComponent {
/**
* 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));
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) {
public void addExtra(BaseComponent component)
{
if (extra == null)
{
extra = new ArrayList<>();
}
component.parent = this;
extra.add(component);
extra.add( component );
}
public boolean hasFormatting() {
public boolean hasFormatting()
{
return color != null || bold != null ||
italic != null || underlined != null ||
strikethrough != null || obfuscated != null;
}
public String toPlainText() {
public String toPlainText()
{
StringBuilder builder = new StringBuilder();
toPlainText(builder);
toPlainText( builder );
return builder.toString();
}
protected void toPlainText(StringBuilder builder) {
if (extra != null) {
for (BaseComponent e : extra) {
e.toPlainText(builder);
protected void toPlainText(StringBuilder builder)
{
if (extra != null)
{
for (BaseComponent e : extra)
{
e.toPlainText( builder );
}
}
}
public String toLegacyText() {
public String toLegacyText()
{
StringBuilder builder = new StringBuilder();
toLegacyText(builder);
toLegacyText( builder );
return builder.toString();
}
protected void toLegacyText(StringBuilder builder) {
if (extra != null) {
for (BaseComponent e : extra) {
e.toLegacyText(builder);
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());
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

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

View File

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

View File

@@ -14,123 +14,139 @@ import java.util.regex.Pattern;
@Setter
@AllArgsConstructor
@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<>();
StringBuilder builder = new StringBuilder();
TextComponent component = new TextComponent();
Matcher matcher = url.matcher(message);
Matcher matcher = url.matcher( message );
for ( int i = 0; i < message.length(); i++ ) {
char c = message.charAt(i);
if (c == ChatColor.COLOR_CHAR) {
for (int i = 0; i < message.length(); i++)
{
char c = message.charAt( i );
if (c == ChatColor.COLOR_CHAR)
{
i++;
c = message.charAt(i);
if (c >= 'A' && c <= 'Z') {
c = message.charAt( i );
if (c >= 'A' && c <= 'Z')
{
c += 32;
}
if (builder.length() > 0) {
if (builder.length() > 0)
{
TextComponent old = component;
component = new TextComponent(old);
old.setText(builder.toString());
component = new TextComponent( old );
old.setText( builder.toString() );
builder = new StringBuilder();
components.add(old);
components.add( old );
}
ChatColor format = ChatColor.getByChar(c);
switch (format) {
ChatColor format = ChatColor.getByChar( c );
switch (format)
{
case BOLD:
component.setBold(true);
component.setBold( true );
break;
case ITALIC:
component.setItalic(true);
component.setItalic( true );
break;
case UNDERLINE:
component.setUnderlined(true);
component.setUnderlined( true );
break;
case STRIKETHROUGH:
component.setStrikethrough(true);
component.setStrikethrough( true );
break;
case MAGIC:
component.setObfuscated(true);
component.setObfuscated( true );
break;
case RESET:
format = ChatColor.WHITE;
default:
component = new TextComponent();
component.setColor(format);
component.setColor( format );
break;
}
continue;
}
int pos = message.indexOf(' ', i);
int pos = message.indexOf( ' ', i );
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;
component = new TextComponent(old);
old.setText(builder.toString());
component = new TextComponent( old );
old.setText( builder.toString() );
builder = new StringBuilder();
components.add(old);
components.add( old );
}
TextComponent old = component;
component = new TextComponent(old);
component = new TextComponent( old );
ClickEvent clickEvent = new ClickEvent();
clickEvent.setAction(ClickEvent.Action.OPEN_URL);
String urlString = message.substring(i, pos);
if (urlString.startsWith("http")) {
component.setText(urlString);
clickEvent.setValue(urlString);
} else {
component.setText(urlString);
clickEvent.setValue("http://" + urlString);
clickEvent.setAction( ClickEvent.Action.OPEN_URL );
String urlString = message.substring( i, pos );
if (urlString.startsWith( "http" ))
{
component.setText( urlString );
clickEvent.setValue( urlString );
} else
{
component.setText( urlString );
clickEvent.setValue( "http://" + urlString );
}
component.setClickEvent(clickEvent);
components.add(component);
component.setClickEvent( clickEvent );
components.add( component );
i += pos - i - 1;
component = old;
continue;
}
builder.append(c);
builder.append( c );
}
if (builder.length() > 0) {
component.setText(builder.toString());
components.add(component);
if (builder.length() > 0)
{
component.setText( builder.toString() );
components.add( component );
}
return components.toArray(new BaseComponent[components.size()]);
return components.toArray( new BaseComponent[components.size()] );
}
private String text;
public TextComponent(TextComponent old) {
super(old);
setText(old.getText());
public TextComponent(TextComponent old)
{
super( old );
setText( old.getText() );
}
@Override
protected void toPlainText(StringBuilder builder) {
builder.append(text);
super.toPlainText(builder);
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);
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());
public String toString()
{
return String.format( "TextComponent{text=%s, %s}", text, super.toString() );
}
}

View File

@@ -10,44 +10,54 @@ import java.util.List;
@Getter
@Setter
@NoArgsConstructor
public class TranslatableComponent extends BaseComponent {
public class TranslatableComponent extends BaseComponent
{
private String translate;
private List<BaseComponent> with;
public TranslatableComponent(String translate, Object ...with) {
setTranslate(translate);
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);
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) {
public void setWith(List<BaseComponent> components)
{
for (BaseComponent component : components)
{
component.parent = this;
}
with = components;
}
@Override
protected void toPlainText(StringBuilder builder) {
protected void toPlainText(StringBuilder builder)
{
//TODO
super.toPlainText(builder);
super.toPlainText( builder );
}
@Override
protected void toLegacyText(StringBuilder builder) {
protected void toLegacyText(StringBuilder builder)
{
//TODO
super.toLegacyText(builder);
super.toLegacyText( builder );
}
@Override
public String toString() {
return String.format("TranslatableComponent{translate=%s, with=%s, %s}", translate, with, super.toString());
public String 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
public String getKickReason() {
return BaseComponent.toLegacyText(kickReasonComponent);
return BaseComponent.toLegacyText( kickReasonComponent );
}
@Deprecated
public void setKickReason(String reason) {
kickReasonComponent = TextComponent.fromLegacyText(reason);
kickReasonComponent = TextComponent.fromLegacyText( reason );
}
}