本文整理汇总了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;
}