Skip to content

Commit

Permalink
merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Leymooo committed Jun 29, 2020
2 parents a578031 + 26f538d commit c7bcae2
Show file tree
Hide file tree
Showing 38 changed files with 793 additions and 179 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Java ${{ matrix.java }}

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
Expand Down
4 changes: 2 additions & 2 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-parent</artifactId>
<version>1.15-SNAPSHOT</version>
<version>1.16-R0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.15-SNAPSHOT</version>
<version>1.16-R0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BungeeCord-API</name>
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-parent</artifactId>
<version>1.15-SNAPSHOT</version>
<version>1.16-R0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>net.md-5</groupId>
<artifactId>bungeecord-bootstrap</artifactId>
<version>1.15-SNAPSHOT</version>
<version>1.16-R0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BungeeCord-Bootstrap</name>
Expand Down
4 changes: 2 additions & 2 deletions chat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-parent</artifactId>
<version>1.15-SNAPSHOT</version>
<version>1.16-R0.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.15-SNAPSHOT</version>
<version>1.16-R0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BungeeCord-Chat</name>
Expand Down
231 changes: 185 additions & 46 deletions chat/src/main/java/net/md_5/bungee/api/ChatColor.java
Original file line number Diff line number Diff line change
@@ -1,145 +1,192 @@
package net.md_5.bungee.api;

import com.google.common.base.Preconditions;
import java.awt.Color;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import lombok.Getter;

/**
* Simplistic enumeration of all supported color values for chat.
*/
public enum ChatColor
public final class ChatColor
{

/**
* The special character which prefixes all chat colour codes. Use this if
* you need to dynamically convert colour codes from your custom format.
*/
public static final char COLOR_CHAR = '\u00A7';
public static final String ALL_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx";
/**
* Pattern to remove all colour codes.
*/
public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile( "(?i)" + String.valueOf( COLOR_CHAR ) + "[0-9A-FK-ORX]" );
/**
* Colour instances keyed by their active character.
*/
private static final Map<Character, ChatColor> BY_CHAR = new HashMap<Character, ChatColor>();
/**
* Colour instances keyed by their name.
*/
private static final Map<String, ChatColor> BY_NAME = new HashMap<String, ChatColor>();
/**
* Represents black.
*/
BLACK( '0', "black" ),
public static final ChatColor BLACK = new ChatColor( '0', "black", new Color( 0x000000 ) );
/**
* Represents dark blue.
*/
DARK_BLUE( '1', "dark_blue" ),
public static final ChatColor DARK_BLUE = new ChatColor( '1', "dark_blue", new Color( 0x0000AA ) );
/**
* Represents dark green.
*/
DARK_GREEN( '2', "dark_green" ),
public static final ChatColor DARK_GREEN = new ChatColor( '2', "dark_green", new Color( 0x00AA00 ) );
/**
* Represents dark blue (aqua).
*/
DARK_AQUA( '3', "dark_aqua" ),
public static final ChatColor DARK_AQUA = new ChatColor( '3', "dark_aqua", new Color( 0x00AAAA ) );
/**
* Represents dark red.
*/
DARK_RED( '4', "dark_red" ),
public static final ChatColor DARK_RED = new ChatColor( '4', "dark_red", new Color( 0xAA0000 ) );
/**
* Represents dark purple.
*/
DARK_PURPLE( '5', "dark_purple" ),
public static final ChatColor DARK_PURPLE = new ChatColor( '5', "dark_purple", new Color( 0xAA00AA ) );
/**
* Represents gold.
*/
GOLD( '6', "gold" ),
public static final ChatColor GOLD = new ChatColor( '6', "gold", new Color( 0xFFAA00 ) );
/**
* Represents gray.
*/
GRAY( '7', "gray" ),
public static final ChatColor GRAY = new ChatColor( '7', "gray", new Color( 0xAAAAAA ) );
/**
* Represents dark gray.
*/
DARK_GRAY( '8', "dark_gray" ),
public static final ChatColor DARK_GRAY = new ChatColor( '8', "dark_gray", new Color( 0x555555 ) );
/**
* Represents blue.
*/
BLUE( '9', "blue" ),
public static final ChatColor BLUE = new ChatColor( '9', "blue", new Color( 0x05555FF ) );
/**
* Represents green.
*/
GREEN( 'a', "green" ),
public static final ChatColor GREEN = new ChatColor( 'a', "green", new Color( 0x55FF55 ) );
/**
* Represents aqua.
*/
AQUA( 'b', "aqua" ),
public static final ChatColor AQUA = new ChatColor( 'b', "aqua", new Color( 0x55FFFF ) );
/**
* Represents red.
*/
RED( 'c', "red" ),
public static final ChatColor RED = new ChatColor( 'c', "red", new Color( 0xFF5555 ) );
/**
* Represents light purple.
*/
LIGHT_PURPLE( 'd', "light_purple" ),
public static final ChatColor LIGHT_PURPLE = new ChatColor( 'd', "light_purple", new Color( 0xFF55FF ) );
/**
* Represents yellow.
*/
YELLOW( 'e', "yellow" ),
public static final ChatColor YELLOW = new ChatColor( 'e', "yellow", new Color( 0xFFFF55 ) );
/**
* Represents white.
*/
WHITE( 'f', "white" ),
public static final ChatColor WHITE = new ChatColor( 'f', "white", new Color( 0xFFFFFF ) );
/**
* Represents magical characters that change around randomly.
*/
MAGIC( 'k', "obfuscated" ),
public static final ChatColor MAGIC = new ChatColor( 'k', "obfuscated" );
/**
* Makes the text bold.
*/
BOLD( 'l', "bold" ),
public static final ChatColor BOLD = new ChatColor( 'l', "bold" );
/**
* Makes a line appear through the text.
*/
STRIKETHROUGH( 'm', "strikethrough" ),
public static final ChatColor STRIKETHROUGH = new ChatColor( 'm', "strikethrough" );
/**
* Makes the text appear underlined.
*/
UNDERLINE( 'n', "underline" ),
public static final ChatColor UNDERLINE = new ChatColor( 'n', "underline" );
/**
* Makes the text italic.
*/
ITALIC( 'o', "italic" ),
public static final ChatColor ITALIC = new ChatColor( 'o', "italic" );
/**
* Resets all previous chat colors or formats.
*/
RESET( 'r', "reset" );
/**
* The special character which prefixes all chat colour codes. Use this if
* you need to dynamically convert colour codes from your custom format.
*/
public static final char COLOR_CHAR = '\u00A7';
public static final String ALL_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRr";
/**
* Pattern to remove all colour codes.
*/
public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile( "(?i)" + String.valueOf( COLOR_CHAR ) + "[0-9A-FK-OR]" );
/**
* Colour instances keyed by their active character.
*/
private static final Map<Character, ChatColor> BY_CHAR = new HashMap<Character, ChatColor>();
public static final ChatColor RESET = new ChatColor( 'r', "reset" );
/**
* The code appended to {@link #COLOR_CHAR} to make usable colour.
* Count used for populating legacy ordinal.
*/
private final char code;
private static int count = 0;
/**
* This colour's colour char prefixed by the {@link #COLOR_CHAR}.
*/
private final String toString;
@Getter
private final String name;
private final int ordinal;
/**
* The RGB color of the ChatColor. null for non-colors (formatting)
*/
@Getter
private final Color color;

static
private ChatColor(char code, String name)
{
for ( ChatColor colour : values() )
{
BY_CHAR.put( colour.code, colour );
}
this( code, name, null );
}

private ChatColor(char code, String name)
private ChatColor(char code, String name, Color color)
{
this.code = code;
this.name = name;
this.toString = new String( new char[]
{
COLOR_CHAR, code
} );
this.ordinal = count++;
this.color = color;

BY_CHAR.put( code, this );
BY_NAME.put( name.toUpperCase( Locale.ROOT ), this );
}

private ChatColor(String name, String toString, int rgb)
{
this.name = name;
this.toString = toString;
this.ordinal = -1;
this.color = new Color( rgb );
}

@Override
public int hashCode()
{
int hash = 7;
hash = 53 * hash + Objects.hashCode( this.toString );
return hash;
}

@Override
public boolean equals(Object obj)
{
if ( this == obj )
{
return true;
}
if ( obj == null || getClass() != obj.getClass() )
{
return false;
}
final ChatColor other = (ChatColor) obj;

return Objects.equals( this.toString, other.toString );
}

@Override
Expand Down Expand Up @@ -188,4 +235,96 @@ public static ChatColor getByChar(char code)
{
return BY_CHAR.get( code );
}

public static ChatColor of(Color color)
{
return of( "#" + Integer.toHexString( color.getRGB() ).substring( 2 ) );
}

public static ChatColor of(String string)
{
Preconditions.checkArgument( string != null, "string cannot be null" );
if ( string.startsWith( "#" ) && string.length() == 7 )
{
int rgb;
try
{
rgb = Integer.parseInt( string.substring( 1 ), 16 );
} catch ( NumberFormatException ex )
{
throw new IllegalArgumentException( "Illegal hex string " + string );
}

StringBuilder magic = new StringBuilder( COLOR_CHAR + "x" );
for ( char c : string.substring( 1 ).toCharArray() )
{
magic.append( COLOR_CHAR ).append( c );
}

return new ChatColor( string, magic.toString(), rgb );
}

ChatColor defined = BY_NAME.get( string.toUpperCase( Locale.ROOT ) );
if ( defined != null )
{
return defined;
}

throw new IllegalArgumentException( "Could not parse ChatColor " + string );
}

/**
* See {@link Enum#valueOf(java.lang.Class, java.lang.String)}.
*
* @param name color name
* @return ChatColor
* @deprecated holdover from when this class was an enum
*/
@Deprecated
public static ChatColor valueOf(String name)
{
Preconditions.checkNotNull( name, "Name is null" );

ChatColor defined = BY_NAME.get( name );
Preconditions.checkArgument( defined != null, "No enum constant " + ChatColor.class.getName() + "." + name );

return defined;
}

/**
* Get an array of all defined colors and formats.
*
* @return copied array of all colors and formats
* @deprecated holdover from when this class was an enum
*/
@Deprecated
public static ChatColor[] values()
{
return BY_CHAR.values().toArray( new ChatColor[ BY_CHAR.values().size() ] );
}

/**
* See {@link Enum#name()}.
*
* @return constant name
* @deprecated holdover from when this class was an enum
*/
@Deprecated
public String name()
{
return getName().toUpperCase( Locale.ROOT );
}

/**
* See {@link Enum#ordinal()}.
*
* @return ordinal
* @deprecated holdover from when this class was an enum
*/
@Deprecated
public int ordinal()
{
Preconditions.checkArgument( ordinal >= 0, "Cannot get ordinal of hex color" );
return ordinal;
}
}
Loading

0 comments on commit c7bcae2

Please sign in to comment.