private static final class MessageFormatter { private static final Pattern variablePattern = Pattern.compile("(?i)\\$(\\{[a-z0-9_]{1,}\\}|[a-z0-9_]{1,})"); private static final Pattern colorPattern = Pattern.compile("(?i)&[0-9A-FK-OR]"); private final Map<String, String> variables; public MessageFormatter() { variables = new HashMap<String, String>(); } public synchronized void setVariable(String variable, String value) { if (value == null) variables.remove(value); else variables.put(variable, value); } public synchronized String format(String message) { Matcher matcher = variablePattern.matcher(message); while (matcher.find()) { String variable = matcher.group(); variable = variable.substring(1); if (variable.startsWith("{") && variable.endsWith("}")) variable = variable.substring(1, variable.length() - 1); String value = variables.get(variable); if (value == null) value = ""; message = message.replaceFirst(Pattern.quote(matcher.group()), Matcher.quoteReplacement(value)); } matcher = colorPattern.matcher(message); while (matcher.find()) message = message.substring(0, matcher.start()) + "\247" + message.substring(matcher.end() - 1); return message; } }
public synchronized String format(String message) { Matcher matcher = variablePattern.matcher(message); while (matcher.find()) { String variable = matcher.group(); variable = variable.substring(1); if (variable.startsWith("{") && variable.endsWith("}")) variable = variable.substring(1, variable.length() - 1); String value = variables.get(variable); if (value == null) value = ""; message = message.replaceFirst(Pattern.quote(matcher.group()), Matcher.quoteReplacement(value)); } matcher = colorPattern.matcher(message); while (matcher.find()) message = message.substring(0, matcher.start()) + "\247" + message.substring(matcher.end() - 1); return message; }
private static List<String> loadAccounts(String fileName) { List<String> accounts = new ArrayList<String>(); try { Pattern pattern = Pattern.compile("[\\w]{1,16}"); BufferedReader reader = new BufferedReader(new FileReader(new File(fileName))); String line; while ((line = reader.readLine()) != null) { Matcher matcher = pattern.matcher(line); if (!matcher.find()) continue; String username = matcher.group(); if (!matcher.find()) continue; String password = matcher.group(); accounts.add(username + ":" + password); } reader.close(); } catch (Exception exception) { throw new RuntimeException(exception); } System.out.println("Loaded " + accounts.size() + " accounts."); return accounts; }
@Override @EventHandler public void onChatReceived(ChatReceivedEvent event) { super.onChatReceived(event); String message = Util.stripColors(event.getMessage()); if (message.startsWith("Please register with \"/register")) { String password = Util.generateRandomString(10 + random.nextInt(6)); bot.say("/register " + password + " " + password); } else if (message.contains("You are not member of any faction.") && spamMessage != null && createFaction) { String msg = "/f create " + Util.generateRandomString(7 + random.nextInt(4)); bot.say(msg); } for (String s : captchaList) { Matcher captchaMatcher = Pattern.compile(s).matcher(message); if (captchaMatcher.matches()) bot.say(captchaMatcher.group(1)); } }