001package fr.aumgn.bukkitutils.util;
002
003import java.util.List;
004import java.util.Locale;
005import java.util.regex.Matcher;
006import java.util.regex.Pattern;
007
008import org.bukkit.Bukkit;
009import org.bukkit.ChatColor;
010import org.bukkit.Material;
011import org.bukkit.entity.Player;
012import org.bukkit.event.Event;
013
014import fr.aumgn.bukkitutils.itemtype.ItemTypeDataParser;
015
016public final class Util {
017
018    private static final Pattern COLORS_PATTERN =
019            Pattern.compile("\\{([A-Za-z]+)\\}");
020    private static final Random RANDOM = new Random();
021
022    private Util() {
023    }
024
025    /**
026     * Gets the shared Random objects.
027     */
028    public static Random getRandom() {
029        return RANDOM;
030    }
031
032    /**
033     * Picks a random object in the list.
034     */
035    public static <T> T pickRandom(List<T> from) {
036        if (from.isEmpty()) {
037            return null;
038        }
039        return from.get(getRandom().nextInt(from.size()));
040    }
041
042    /**
043     * Call an event.
044     */
045    public static void callEvent(Event event) {
046        Bukkit.getPluginManager().callEvent(event);
047    }
048
049    /**
050     * Broadcast the message to all players.
051     * Do not send it to Console contrary to
052     * {@link Bukkit#broadcastMessage(String)}
053     */
054    public static void broadcast(String message) {
055        for (Player player : Bukkit.getOnlinePlayers()) {
056            player.sendMessage(message);
057        }
058    }
059
060    /**
061     * Broadcast the message to all players 
062     * who have the given permission.
063     * Do not send it to Console contrary to
064     * {@link Bukkit#broadcastMessage(String)}
065     */
066    public static void broadcast(String permission, String message) {
067        for (Player player : Bukkit.getOnlinePlayers()) {
068            if (player.hasPermission(permission)) {
069                player.sendMessage(message);
070            }
071        }
072    }
073
074    /**
075     * Match a material. Wraps 
076     * {@link Material#matchMaterial(String)}
077     * and add an support for `dye`.
078     */
079    public static Material matchMaterial(String pattern) {
080        if (pattern.equalsIgnoreCase("dye")) {
081            return Material.INK_SACK;
082        }
083
084        return Material.matchMaterial(pattern);
085    }
086
087    /**
088     * Parse the data for the given material
089     * and the given token.
090     * This supports things such as colors name
091     * for wools, etc..
092     */
093    public static Short parseDataFor(Material material, String token) {
094        try {
095            return Short.parseShort(token);
096        } catch (NumberFormatException exc) {
097        }
098
099        ItemTypeDataParser parser = ItemTypeDataParser.getFor(material);
100        if (parser == null) {
101            return null;
102        }
103
104        return parser.parse(token);
105    }
106
107    /**
108     * Replaces all {ColorName} in the string by
109     * the corresponding color code.
110     */
111    public static String parseColorsMarkup(String message) {
112        StringBuffer parsed = new StringBuffer();
113        Matcher matcher = COLORS_PATTERN.matcher(message);
114        while (matcher.find()) {
115            try {
116                ChatColor color = ChatColor.valueOf(matcher.group(1)
117                        .toUpperCase());
118                matcher.appendReplacement(parsed, color.toString());
119            } catch (IllegalArgumentException exc) {
120            }
121        }
122        matcher.appendTail(parsed);
123        return parsed.toString();
124    }
125
126    /**
127     * Parse a locale identifier into a Locale.
128     * Returns the system default Locale if 
129     * the string is invalid. 
130     */
131    public static Locale parseLocale(String localeStr) {
132        return parseLocale(localeStr, "_");
133    }
134
135    /**
136     * Parse a locale identifier into a Locale using 
137     * the given delimiter. Returns the system 
138     * default Locale if the string is invalid. 
139     */
140    public static Locale parseLocale(String localeStr, String delimiter) {
141        String[] splitted = localeStr.split(delimiter);
142        if (splitted.length == 0) {
143            return Locale.getDefault();
144        } else if (splitted.length == 1) {
145            return new Locale(splitted[0]);
146        } else {
147            return new Locale(splitted[0], splitted[1]);
148        }
149    }
150}