org.bukkit.ChatColor#COLOR_CHAR源码实例Demo

下面列出了org.bukkit.ChatColor#COLOR_CHAR 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: PGM   文件: PlayerComponent.java
/**
 * Get the player's prefix as a {@link Component}
 *
 * @param player The player
 * @return a component with a player's prefix
 */
static TextComponent.Builder getPrefixComponent(Player player) {
  String realName = player.getName();
  String displayName = player.getDisplayName();
  String prefix = displayName.substring(0, displayName.indexOf(realName) - 2);

  TextComponent.Builder prefixComponent = TextComponent.builder();
  boolean isColor = false;
  TextColor color = null;
  for (int i = 0; i < prefix.length(); i++) {
    if (prefix.charAt(i) == ChatColor.COLOR_CHAR) {
      isColor = true;
      continue;
    }

    if (isColor) {
      color = TextFormatter.convert(ChatColor.getByChar(prefix.charAt(i)));
      isColor = false;
    } else {
      prefixComponent.append(
          String.valueOf(prefix.charAt(i)), color != null ? color : TextColor.WHITE);
    }
  }

  return prefixComponent;
}
 
源代码2 项目: Kettle   文件: MapFont.java
/**
 * Get the width of the given text as it would be rendered using this
 * font.
 *
 * @param text The text.
 * @return The width in pixels.
 */
public int getWidth(String text) {
    if (!isValid(text)) {
        throw new IllegalArgumentException("text contains invalid characters");
    }

    if (text.length() == 0) {
        return 0;
    }

    int result = 0;
    for (int i = 0; i < text.length(); ++i) {
        char ch = text.charAt(i);
        if (ch == ChatColor.COLOR_CHAR) continue;
        result += chars.get(ch).getWidth();
    }
    result += text.length() - 1; // Account for 1px spacing between characters

    return result;
}
 
源代码3 项目: Skript   文件: VariableString.java
@Nullable
private static ChatColor getLastColor(final CharSequence s) {
	for (int i = s.length() - 2; i >= 0; i--) {
		if (s.charAt(i) == ChatColor.COLOR_CHAR) {
			final ChatColor c = ChatColor.getByChar(s.charAt(i + 1));
			if (c != null && (c.isColor() || c == ChatColor.RESET))
				return c;
		}
	}
	return null;
}
 
源代码4 项目: NyaaUtils   文件: FuelManager.java
public void updateItem(ItemStack item, int fuelID, int durability) {
    FuelItem fuel = plugin.cfg.fuelConfig.fuel.get(fuelID);
    if (fuel == null) {
        return;
    }
    String hex = toHexString(fuelID) + toHexString(durability) + toHexString(new Random().nextInt(65535));
    String str = "";
    for (int i = 0; i < hex.length(); i++) {
        str += ChatColor.COLOR_CHAR + hex.substring(i, i + 1);
    }
    str += ChatColor.COLOR_CHAR + "r";
    ItemMeta meta = fuel.getItem().getItemMeta();
    List<String> lore;
    if (meta.hasLore()) {
        lore = meta.getLore();
        lore.set(0, lore_prefix + str + lore.get(0));
        lore.add(lore_prefix + I18n.format("user.elytra_enhance.fuel_durability", durability, fuel.getMaxDurability()));
    } else {
        lore = new ArrayList<>();
        lore.add(lore_prefix + str + I18n.format("user.elytra_enhance.fuel_durability", durability, fuel.getMaxDurability()));
    }
    item.setType(fuel.getItem().getType());
    item.setData(fuel.getItem().getData());
    meta.setLore(lore);
    item.setItemMeta(meta);
}
 
源代码5 项目: ScoreboardLib   文件: ScrollableString.java
@Override
public String next() {
    StringBuilder sb = getNext();
    if (sb.charAt(sb.length() - 1) == ChatColor.COLOR_CHAR) {
        sb.setCharAt(sb.length() - 1, ' ');
    }
    if (sb.charAt(0) == ChatColor.COLOR_CHAR) {
        ChatColor c = ChatColor.getByChar(sb.charAt(1));
        if (c != null) {
            color = c;
            sb = getNext();
            if (sb.charAt(0) != ' ')
                sb.setCharAt(0, ' ');
        }
    }
    return color + sb.toString();
}
 
源代码6 项目: ProtocolSupport   文件: AbstractScoreboardTeam.java
protected Any<String, String> formatDisplayLinePrefixSuffix() {
	String prefix = this.prefix.toLegacyText(clientCache.getLocale());

	if ((format != ChatColor.RESET) && !isStringFormatOverride(prefix, format)) {
		prefix = format + prefix;
	}

	if (prefix.length() <= NFIX_LIMIT) {
		return new Any<>(prefix, "");
	}

	int limit = prefix.charAt(NFIX_LIMIT_FCUT) == ChatColor.COLOR_CHAR ? NFIX_LIMIT_FCUT : NFIX_LIMIT;

	String sPrefix = prefix.substring(0, limit);

	String sSuffix = ChatColor.getLastColors(sPrefix) + prefix.substring(limit);
	if (!isStringFormatOverride(sSuffix, ChatColor.RESET)) {
		sSuffix = ChatColor.RESET + sSuffix;
	}
	sSuffix = LegacyChat.clampLegacyText(sSuffix, NFIX_LIMIT);

	return new Any<>(sPrefix, sSuffix);
}
 
源代码7 项目: ChestCommands   文件: FormatUtils.java
public static List<String> colorizeLore(List<String> input) {
	if (input == null || input.isEmpty()) return input;

	for (int i = 0; i < input.size(); i++) {

		String line = input.get(i);

		if (line.isEmpty()) continue;

		if (line.charAt(0) != ChatColor.COLOR_CHAR) {
			input.set(i, ChestCommands.getSettings().default_color__lore + addColors(line));
		} else {
			input.set(i, addColors(line));
		}
	}
	return input;
}
 
源代码8 项目: CardinalPGM   文件: Align.java
/**
 * Gets the total width of some text in font-pixels, the sum of its characters.
 *
 * @param str some text
 * @return the width of the text
 */
public static int getStringWidth(String str) {
    int length = 0;
    boolean bold = false;
    for (int i = 0; i < str.length(); i++)
        if (str.charAt(i) != ChatColor.COLOR_CHAR)
            if (i == 0)
                length += getCharWidth(str.charAt(i), bold);
            else if (str.charAt(i - 1) != ChatColor.COLOR_CHAR)
                length += getCharWidth(str.charAt(i), bold);
            else if (str.charAt(i) == 'l')
                bold = true;
            else if (!Lists.newArrayList('m', 'n', 'o').contains(str.charAt(i)))
                bold = false;
    return length;
}
 
源代码9 项目: PGM   文件: SidebarMatchModule.java
private void setRow(int maxScore, int row, @Nullable String text) {
  if (row < 0 || row >= MAX_ROWS) return;

  int score = text == null ? -1 : maxScore - row - 1;
  if (this.scores[row] != score) {
    this.scores[row] = score;

    if (score == -1) {
      this.scoreboard.resetScores(this.players[row]);
    } else {
      this.objective.getScore(this.players[row]).setScore(score);
    }
  }

  if (!Objects.equals(this.rows[row], text)) {
    this.rows[row] = text;

    if (text != null) {
      /*
       Split the row text into prefix and suffix, limited to 16 chars each. Because the player name
       is a color code, we have to restore the color at the split in the suffix. We also have to be
       careful not to split in the middle of a color code.
      */
      int split = MAX_PREFIX - 1; // Start by assuming there is a color code right on the split
      if (text.length() < MAX_PREFIX || text.charAt(split) != ChatColor.COLOR_CHAR) {
        // If there isn't, we can fit one more char in the prefix
        split++;
      }

      // Split and truncate the text, and restore the color in the suffix
      String prefix = StringUtils.substring(text, 0, split);
      String lastColors = ChatColor.getLastColors(prefix);
      String suffix =
          lastColors
              + StringUtils.substring(text, split, split + MAX_SUFFIX - lastColors.length());
      this.teams[row].setPrefix(prefix);
      this.teams[row].setSuffix(suffix);
    }
  }
}
 
源代码10 项目: Assemble   文件: AssembleBoardEntry.java
public void send(int position) {
	if (this.text.length() > 16) {
		String prefix = this.text.substring(0, 16);
		String suffix;

		if (prefix.charAt(15) == ChatColor.COLOR_CHAR) {
			prefix = prefix.substring(0, 15);
			suffix = this.text.substring(15, this.text.length());
		} else if (prefix.charAt(14) == ChatColor.COLOR_CHAR) {
			prefix = prefix.substring(0, 14);
			suffix = this.text.substring(14, this.text.length());
		} else {
			if (ChatColor.getLastColors(prefix).equalsIgnoreCase(ChatColor.getLastColors(this.identifier))) {
				suffix = this.text.substring(16, this.text.length());
			} else {
				suffix = ChatColor.getLastColors(prefix) + this.text.substring(16, this.text.length());
			}
		}

		if (suffix.length() > 16) {
			suffix = suffix.substring(0, 16);
		}

		this.team.setPrefix(prefix);
		this.team.setSuffix(suffix);
	} else {
		this.team.setPrefix(this.text);
		this.team.setSuffix("");
	}

	Score score = this.board.getObjective().getScore(this.identifier);
	score.setScore(position);
}
 
源代码11 项目: Kettle   文件: MapFont.java
/**
 * Check whether the given text is valid.
 *
 * @param text The text.
 * @return True if the string contains only defined characters, false
 * otherwise.
 */
public boolean isValid(String text) {
    for (int i = 0; i < text.length(); ++i) {
        char ch = text.charAt(i);
        if (ch == ChatColor.COLOR_CHAR || ch == '\n') continue;
        if (chars.get(ch) == null) return false;
    }
    return true;
}
 
源代码12 项目: NovaGuilds   文件: StringUtils.java
/**
 * Reverses color character replacement
 *
 * @param msg colored message
 * @return raw message
 */
public static String unTranslateAlternateColorCodes(String msg) {
	char altColorChar = ChatColor.COLOR_CHAR;

	char[] b = msg.toCharArray();
	for(int i = 0; i < b.length - 1; i++) {
		if(b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) {
			b[i] = '&';
			b[i + 1] = Character.toLowerCase(b[i + 1]);
		}
	}

	return new String(b);
}
 
源代码13 项目: BetonQuest   文件: LocalChatPaginator.java
/**
 * Return the width of text taking into account variable font size and ignoring hidden characters
 */
public static int getWidth(String input) {
    int ret = 0;
    char[] rawChars = input.toCharArray();

    for (int i = 0; i < rawChars.length; i++) {
        if (rawChars[i] == ChatColor.COLOR_CHAR) {
            i += 1;
            continue;
        }
        ret += getWidth(rawChars[i]);
    }
    return ret;
}
 
源代码14 项目: BetonQuest   文件: LocalChatPaginator.java
/**
 * Return the length of the line minus hidden characters
 */
public static int lineLength(String input) {
    int ret = 0;
    char[] rawChars = input.toCharArray();
    for (int i = 0; i < rawChars.length; i++) {
        if (rawChars[i] == ChatColor.COLOR_CHAR) {
            i += 1;
            continue;
        }
        ret++;
    }
    return ret;
}
 
源代码15 项目: BetonQuest   文件: LocalChatPaginator.java
/**
 * Return the number of hidden characters in input
 */
public static int hiddenCount(String input) {
    char[] rawChars = input.toCharArray();
    int count = 0;
    for (int i = 0; i < rawChars.length; i++) {
        char c = rawChars[i];

        if (c == ChatColor.COLOR_CHAR) {
            count += 2;
            i++;
        }

    }
    return count;
}
 
源代码16 项目: ProtocolSupport   文件: AbstractScoreboardTeam.java
protected static boolean isStringFormatOverride(String string, ChatColor format) {
	if ((string.length() >= 2) && (string.charAt(0) == ChatColor.COLOR_CHAR)) {
		ChatColor formatStringColor = ChatColor.getByChar(string.charAt(1));
		if ((formatStringColor != null) && (formatStringColor.isColor() || (formatStringColor == format))) {
			return true;
		}
	}
	return false;
}
 
源代码17 项目: ProtocolSupport   文件: LegacyChat.java
public static String clampLegacyText(String text, int limit) {
	if (text.length() <= limit) {
		return text;
	}
	int lastNotColorCharIndex = limit - 1;
	while (text.charAt(lastNotColorCharIndex) == ChatColor.COLOR_CHAR) {
		lastNotColorCharIndex--;
	}
	return text.substring(0, lastNotColorCharIndex + 1);
}
 
源代码18 项目: ChestCommands   文件: FormatUtils.java
public static String colorizeName(String input) {
	if (input == null || input.isEmpty()) return input;

	if (input.charAt(0) != ChatColor.COLOR_CHAR) {
		return ChestCommands.getSettings().default_color__name + addColors(input);
	} else {
		return addColors(input);
	}
}
 
源代码19 项目: Shopkeepers   文件: Utils.java
public static String translateColorCodesToAlternative(char altColorChar, String textToTranslate) {
	char[] b = textToTranslate.toCharArray();
	for (int i = 0; i < b.length - 1; i++) {
		if (b[i] == ChatColor.COLOR_CHAR && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) {
			b[i] = altColorChar;
			b[i + 1] = Character.toLowerCase(b[i + 1]);
		}
	}
	return new String(b);
}
 
源代码20 项目: ChatMenuAPI   文件: ChatMenuAPI.java
/**
 * 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(@NotNull 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;
}