Java Code Examples for org.bukkit.ChatColor#COLOR_CHAR

The following examples show how to use org.bukkit.ChatColor#COLOR_CHAR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Align.java    From CardinalPGM with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: MapFont.java    From Kettle with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: FormatUtils.java    From ChestCommands with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: VariableString.java    From Skript with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 5
Source File: FuelManager.java    From NyaaUtils with MIT License 6 votes vote down vote up
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);
}
 
Example 6
Source File: ScrollableString.java    From ScoreboardLib with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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();
}
 
Example 7
Source File: PlayerComponent.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: AbstractScoreboardTeam.java    From ProtocolSupport with GNU Affero General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 9
Source File: LocalChatPaginator.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: Utils.java    From Shopkeepers with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: FormatUtils.java    From ChestCommands with GNU General Public License v3.0 5 votes vote down vote up
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);
	}
}
 
Example 12
Source File: LegacyChat.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: AbstractScoreboardTeam.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: LocalChatPaginator.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: LocalChatPaginator.java    From BetonQuest with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: StringUtils.java    From NovaGuilds with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 17
Source File: MapFont.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: AssembleBoardEntry.java    From Assemble with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 19
Source File: SidebarMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
    }
  }
}
 
Example 20
Source File: ChatMenuAPI.java    From ChatMenuAPI with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}