123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package de.Linus122.TimeIsMoney.tools;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.entity.Player;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * Utility class.
- *
- * @author Linus122
- * @since 1.9.6.1
- */
- public class Utils {
- /**
- * @throws RuntimeException utils class should not be instantiated.
- */
- public Utils() {
- throw new RuntimeException("Utils class should not be instantiated!");
- }
-
- /**
- * Converts &color to {@link org.bukkit.ChatColor}.
- *
- * @param s The string to convert to {@link org.bukkit.ChatColor}.
- * @return The converted string with {@link org.bukkit.ChatColor}.
- */
- public static String CC(String s) {
- // return an empty string if given string is null
- if(s == null) {
- return "";
- }
- return ChatColor.translateAlternateColorCodes('&', parseRGB(s));
- }
-
- public static String applyPlaceholders(Player player, String s) {
- if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
- s = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, s);
- }
- return s;
- }
- private static final Pattern rgbColor = Pattern.compile("(?<!\\\\)(&#[a-fA-F0-9]{6})");
- public static String parseRGB(String msg) {
- Matcher matcher = rgbColor.matcher(msg);
- while (matcher.find()) {
- String color = msg.substring(matcher.start(), matcher.end());
- String hex = color.replace("&", "");
- try {
- msg = msg.replace(color, String.valueOf(net.md_5.bungee.api.ChatColor.of(hex)));
- } catch(NoSuchMethodError e) {
- // older spigot versions
- msg = msg.replace(color, "");
- }
- matcher = rgbColor.matcher(msg);
- }
- return msg;
- }
- }
|