本文整理匯總了Java中org.bukkit.ChatColor.COLOR_CHAR屬性的典型用法代碼示例。如果您正苦於以下問題:Java ChatColor.COLOR_CHAR屬性的具體用法?Java ChatColor.COLOR_CHAR怎麽用?Java ChatColor.COLOR_CHAR使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類org.bukkit.ChatColor
的用法示例。
在下文中一共展示了ChatColor.COLOR_CHAR屬性的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getFirstColors
public static String getFirstColors(String input) {
StringBuilder result = new StringBuilder();
int length = input.length();
for (int index = 0; index < length; index++) {
char section = input.charAt(index);
if (section == ChatColor.COLOR_CHAR && index < length - 1) {
char c = input.charAt(index + 1);
ChatColor color = ChatColor.getByChar(c);
if (color != null) {
result.insert(0, color.toString());
if (color.isColor() || color.equals(ChatColor.RESET)) {
break;
}
}
}
}
return result.toString();
}
示例2: stringToColors
private static String stringToColors(String normal) {
if (normal == null) return null;
byte[] bytes = normal.getBytes(Charset.forName("UTF-8"));
char[] chars = new char[bytes.length * 4];
for (int i = 0; i < bytes.length; i++) {
char[] hex = byteToHex(bytes[i]);
chars[i * 4] = ChatColor.COLOR_CHAR;
chars[i * 4 + 1] = hex[0];
chars[i * 4 + 2] = ChatColor.COLOR_CHAR;
chars[i * 4 + 3] = hex[1];
}
return new String(chars);
}
示例3: getWidth
/**
* Calculates the width of the provided text.
* <br>
* Works with formatting codes such as bold.
*
* @param text the text to calculate the width for
* @return the number of pixels in chat the text takes up
*/
public static int getWidth(@Nonnull String text)
{
if(text.contains("\n"))
throw new IllegalArgumentException("Cannot get width of text containing newline");
int width = 0;
boolean isBold = false;
char[] chars = text.toCharArray();
for(int i = 0; i < chars.length; i++)
{
char c = chars[i];
int charWidth = getCharacterWidth(c);
if(c == ChatColor.COLOR_CHAR && i < chars.length - 1)
{
c = chars[++i];
if(c != 'l' && c != 'L')
{
if(c == 'r' || c == 'R')
{
isBold = false;
}
}else
{
isBold = true;
}
charWidth = 0;
}
if(isBold && c != ' ' && charWidth > 0)
{
width++;
}
width += charWidth;
}
return width;
}