下面列出了org.bukkit.ChatColor#getByChar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Make a string colorful.
*
* @param string
* @return
*/
public static String rainbowlize(String string) {
int lastColor = 0;
int currColor;
String newString = "";
String colors = "123456789abcde";
for (int i = 0; i < string.length(); i++) {
do {
currColor = new Random().nextInt(colors.length() - 1) + 1;
}
while (currColor == lastColor);
newString += ChatColor.RESET.toString() + ChatColor.getByChar(colors
.charAt(currColor)) + "" + string.charAt(i);
}
return newString;
}
public static String rainbowlize(String string)
{
int lastColor = 0;
int currColor;
String newString = "";
String colors = "123456789abcde";
for(int i = 0; i < string.length(); i++)
{
do
{
currColor = new Random().nextInt(colors.length() - 1) + 1;
} while(currColor == lastColor);
newString += ChatColor.RESET.toString()
+ ChatColor.getByChar(colors.charAt(currColor)) + ""
+ string.charAt(i);
}
return newString;
}
@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;
}
@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();
}
/**
* Loads messages
*
* @throws FatalNovaGuildsException when something goes wrong
*/
public void load() throws FatalNovaGuildsException {
setupDirectories();
try {
detectLanguage();
messages = Lang.loadConfiguration(messagesFile);
//Fork, edit and compile NovaGuilds on your own if you want not to use the original prefix
restorePrefix();
prefix = Message.CHAT_PREFIX.get();
prefixColor = ChatColor.getByChar(ChatColor.getLastColors(prefix).charAt(1));
LoggerUtils.info("Messages loaded: " + Config.LANG_NAME.getString());
}
catch(ScannerException | IOException e) {
throw new FatalNovaGuildsException("Failed to load messages", e);
}
}
private List<JSONObject> parseColors(String text) {
List<JSONObject> jsonList = new ArrayList<>();
for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + ")")) {
JSONObject workingText = new JSONObject();
//fix colors before
filterColors(workingText);
Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
if (match.find()) {
ChatColor color = ChatColor.getByChar(match.group(1).charAt(0));
if (color.isColor()) {
lastColor = color;
last2Color = null;
} else {
// Set a second color if the first color is format
if (lastColor.isColor()) last2Color = lastColor;
lastColor = color;
}
//fix colors from latest
filterColors(workingText);
if (part.length() == 2) continue;
}
//continue if empty
if (ChatColor.stripColor(part).isEmpty()) {
continue;
}
workingText.put("text", ChatColor.stripColor(part));
//fix colors after
filterColors(workingText);
if (!workingText.containsKey("color")) {
workingText.put("color", "white");
}
jsonList.add(workingText);
}
return jsonList;
}
private JSONArray addColorToArray(String text) {
JSONArray extraArr = new JSONArray();
ChatColor color = ChatColor.WHITE;
for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + "[0-9a-fA-Fk-oK-ORr])")) {
JSONObject objExtraTxt = new JSONObject();
Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
if (match.find()) {
color = ChatColor.getByChar(match.group(1).charAt(0));
if (part.length() == 2) continue;
}
objExtraTxt.put("text", ChatColor.stripColor(part));
if (color.isColor()) {
objExtraTxt.put("color", color.name().toLowerCase());
objExtraTxt.remove("obfuscated");
objExtraTxt.remove("underlined");
objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
}
if (color.equals(ChatColor.RESET)) {
objExtraTxt.put("color", "white");
objExtraTxt.remove("obfuscated");
objExtraTxt.remove("underlined");
objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
}
if (color.isFormat()) {
if (color.equals(ChatColor.MAGIC)) {
objExtraTxt.put("obfuscated", true);
} else if (color.equals(ChatColor.UNDERLINE)) {
objExtraTxt.put("underlined", true);
} else {
objExtraTxt.put(color.name().toLowerCase(), true);
}
}
extraArr.add(objExtraTxt);
}
return extraArr;
}
@SuppressWarnings("unchecked")
public static MessagePart deserialize(Map<String, Object> serialized) {
MessagePart part = new MessagePart((TextualComponent) serialized.get("text"));
part.styles = (ArrayList<ChatColor>) serialized.get("styles");
part.color = ChatColor.getByChar(serialized.get("color").toString());
part.hoverActionName = (String) serialized.get("hoverActionName");
part.hoverActionData = (JsonRepresentedObject) serialized.get("hoverActionData");
part.clickActionName = (String) serialized.get("clickActionName");
part.clickActionData = (String) serialized.get("clickActionData");
part.insertionData = (String) serialized.get("insertion");
part.translationReplacements = (ArrayList<JsonRepresentedObject>) serialized.get("translationReplacements");
return part;
}
private List<JSONObject> parseColors(String text) {
List<JSONObject> jsonList = new ArrayList<>();
for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + ")")) {
JSONObject workingText = new JSONObject();
//fix colors before
filterColors(workingText);
Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
if (match.find()) {
ChatColor color = ChatColor.getByChar(match.group(1).charAt(0));
if (color.isColor()) {
lastColor = color;
last2Color = null;
} else {
// Set a second color if the first color is format
if (lastColor.isColor()) last2Color = lastColor;
lastColor = color;
}
//fix colors from latest
filterColors(workingText);
if (part.length() == 2) continue;
}
//continue if empty
if (ChatColor.stripColor(part).isEmpty()) {
continue;
}
workingText.put("text", ChatColor.stripColor(part));
//fix colors after
filterColors(workingText);
if (!workingText.containsKey("color")) {
workingText.put("color", "white");
}
jsonList.add(workingText);
}
return jsonList;
}
private JSONArray addColorToArray(String text) {
JSONArray extraArr = new JSONArray();
ChatColor color = ChatColor.WHITE;
for (String part : text.split("(?=" + ChatColor.COLOR_CHAR + "[0-9a-fA-Fk-oK-ORr])")) {
JSONObject objExtraTxt = new JSONObject();
Matcher match = Pattern.compile("^" + ChatColor.COLOR_CHAR + "([0-9a-fA-Fk-oK-ORr]).*$").matcher(part);
if (match.find()) {
color = ChatColor.getByChar(match.group(1).charAt(0));
if (part.length() == 2) continue;
}
objExtraTxt.put("text", ChatColor.stripColor(part));
if (color.isColor()) {
objExtraTxt.put("color", color.name().toLowerCase());
objExtraTxt.remove("obfuscated");
objExtraTxt.remove("underlined");
objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
}
if (color.equals(ChatColor.RESET)) {
objExtraTxt.put("color", "white");
objExtraTxt.remove("obfuscated");
objExtraTxt.remove("underlined");
objExtraTxt.remove(ChatColor.STRIKETHROUGH.name().toLowerCase());
}
if (color.isFormat()) {
if (color.equals(ChatColor.MAGIC)) {
objExtraTxt.put("obfuscated", true);
} else if (color.equals(ChatColor.UNDERLINE)) {
objExtraTxt.put("underlined", true);
} else {
objExtraTxt.put(color.name().toLowerCase(), true);
}
}
extraArr.add(objExtraTxt);
}
return extraArr;
}
/**
* Takes a string and returns the last colors that can be copied to a new line
*/
public static String getLastColors(String input) {
ChatColor lastColor = null;
List<ChatColor> lastFormats = new ArrayList<>();
int length = input.length();
for (int index = length - 1; index > -1; --index) {
char section = input.charAt(index);
if (section == 167 && index < length - 1) {
char c = input.charAt(index + 1);
ChatColor color = ChatColor.getByChar(c);
if (color != null) {
if (color.equals(ChatColor.RESET)) {
break;
}
if (color.isColor() && lastColor == null) {
lastColor = color;
continue;
}
if (color.isFormat() && !lastFormats.contains(color)) {
lastFormats.add(color);
}
}
}
}
String result = lastFormats.stream()
.map(ChatColor::toString)
.collect(Collectors.joining(""));
if (lastColor != null) {
result = lastColor.toString() + result;
}
return result;
}
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;
}
@SuppressWarnings("unchecked")
public static MessagePart deserialize(Map<String, Object> serialized) {
MessagePart part = new MessagePart((TextualComponent) serialized.get("text"));
part.styles = (ArrayList<ChatColor>) serialized.get("styles");
part.color = ChatColor.getByChar(serialized.get("color").toString());
part.hoverActionName = (String) serialized.get("hoverActionName");
part.hoverActionData = (JsonRepresentedObject) serialized.get("hoverActionData");
part.clickActionName = (String) serialized.get("clickActionName");
part.clickActionData = (String) serialized.get("clickActionData");
part.insertionData = (String) serialized.get("insertion");
part.translationReplacements = (ArrayList<JsonRepresentedObject>) serialized.get("translationReplacements");
return part;
}
public static ChatColor getColor(TextFormatting format) {
return ChatColor.getByChar(format.formattingCode);
}
public static ChatColor convertColor(net.md_5.bungee.api.ChatColor color) {
return ChatColor.getByChar(color.toString().charAt(1));
}
@SuppressWarnings("unchecked")
public PacketWrapper(String name, String prefix, String suffix, int param, Collection<?> players) {
setupDefaults(name, param);
if (param == 0 || param == 2) {
try {
if (PacketAccessor.isLegacyVersion()) {
PacketAccessor.DISPLAY_NAME.set(packet, name);
PacketAccessor.PREFIX.set(packet, prefix);
PacketAccessor.SUFFIX.set(packet, suffix);
} else {
String color = ChatColor.getLastColors(prefix);
String colorCode = null;
if (!color.isEmpty()) {
colorCode = color.substring(color.length() - 1);
String chatColor = ChatColor.getByChar(colorCode).name();
if (chatColor.equalsIgnoreCase("MAGIC"))
chatColor = "OBFUSCATED";
Enum<?> colorEnum = Enum.valueOf(typeEnumChatFormat, chatColor);
PacketAccessor.TEAM_COLOR.set(packet, colorEnum);
}
PacketAccessor.DISPLAY_NAME.set(packet, ChatComponentText.newInstance(name));
PacketAccessor.PREFIX.set(packet, ChatComponentText.newInstance(prefix));
if (colorCode != null)
suffix = ChatColor.getByChar(colorCode) + suffix;
PacketAccessor.SUFFIX.set(packet, ChatComponentText.newInstance(suffix));
}
PacketAccessor.PACK_OPTION.set(packet, 1);
if (PacketAccessor.VISIBILITY != null) {
PacketAccessor.VISIBILITY.set(packet, "always");
}
if (param == 0) {
((Collection) PacketAccessor.MEMBERS.get(packet)).addAll(players);
}
} catch (Exception e) {
error = e.getMessage();
}
}
}
private static boolean isColorOrStyle(char code){
return ChatColor.getByChar(code) != null;
}
@Override
public @NotNull ChatColor getMarriageColor()
{
return ChatColor.getByChar(getColor().getCode());
}