package com.dre.brewery; import java.util.List; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.lang.Character; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.block.SignChangeEvent; import com.dre.brewery.BPlayer; public class Words { // represends Words and letters, that are replaced in drunk players messages public static ArrayList words = new ArrayList(); public static List commands; public static FileConfiguration config; public static Boolean doSigns; public static Boolean log; private static Map waitPlayers = new HashMap(); private String from; private String to; private String[] pre; private Boolean match = false; private int alcohol = 1; private int percentage = 100; public Words(Map part) { for (Map.Entry wordPart : part.entrySet()) { String key = (String) wordPart.getKey(); if (wordPart.getValue() instanceof String) { if (key.equalsIgnoreCase("replace")) { this.from = (String) wordPart.getValue(); } else if (key.equalsIgnoreCase("to")) { this.to = (String) wordPart.getValue(); } else if (key.equalsIgnoreCase("pre")) { String fullPre = (String) wordPart.getValue(); this.pre = fullPre.split(","); } } else if (wordPart.getValue() instanceof Boolean) { if (key.equalsIgnoreCase("match")) { this.match = (Boolean) wordPart.getValue(); } } else if (wordPart.getValue() instanceof Integer) { if (key.equalsIgnoreCase("alcohol")) { this.alcohol = (Integer) wordPart.getValue(); } else if (key.equalsIgnoreCase("percentage")) { this.percentage = (Integer) wordPart.getValue(); } } } if (this.from != null && this.to != null) { words.add(this); } } private static boolean loadWords() { if (words.isEmpty()) { // load when first drunk player talks load(); } return !words.isEmpty(); } // Distort players words when he uses a command public static void playerCommand(PlayerCommandPreprocessEvent event) { String name = event.getPlayer().getName(); BPlayer bPlayer = BPlayer.get(name); if (bPlayer != null) { if (!commands.isEmpty() && loadWords()) { if (!waitPlayers.containsKey(name) || waitPlayers.get(name) + 500 < System.currentTimeMillis()) { String chat = event.getMessage(); for (String command : commands) { if (command.length() + 1 < chat.length()) { if (Character.isSpaceChar(chat.charAt(command.length()))) { if (chat.toLowerCase().startsWith(command.toLowerCase())) { if (log) { P.p.log(P.p.languageReader.get("Player_TriedToSay", name, chat)); } String message = chat.substring(command.length() + 1); for (Words word : words) { if (word.alcohol <= bPlayer.getDrunkeness()) { message = word.distort(message); } } event.setMessage(chat.substring(0, command.length() + 1) + message); waitPlayers.put(name, System.currentTimeMillis()); return; } } } } } } } } // Distort players words when he uses a command public static void signWrite(SignChangeEvent event) { BPlayer bPlayer = BPlayer.get(event.getPlayer().getName()); if (bPlayer != null) { if (loadWords()) { int index = 0; for (String message : event.getLines()) { if (message.length() > 1) { for (Words word : words) { if (word.alcohol <= bPlayer.getDrunkeness()) { message = word.distort(message); } } if (message.length() > 15) { message = message.substring(0, 14); } event.setLine(index, message); } index++; } } } } // Distort players words when he talks public static void playerChat(AsyncPlayerChatEvent event) { BPlayer bPlayer = BPlayer.get(event.getPlayer().getName()); if (bPlayer != null) { if (loadWords()) { String message = event.getMessage(); if (log) { P.p.log(P.p.languageReader.get("Player_TriedToSay", event.getPlayer().getName(), message)); } for (Words word : words) { if (word.alcohol <= bPlayer.getDrunkeness()) { message = word.distort(message); } } event.setMessage(message); } } } // replace "percent"% of "from" -> "to" in "words", when the string before // each "from" "match"es "pre" // Not yet ignoring case :( public String distort(String words) { String from = this.from; String to = this.to; if (from.equalsIgnoreCase("-end")) { from = words; to = words + to; } else if (from.equalsIgnoreCase("-start")) { from = words; to = to + words; } else if (from.equalsIgnoreCase("-all")) { from = words; } else if (from.equalsIgnoreCase("-space")) { from = " "; } else if (from.equalsIgnoreCase("-random")) { // inserts "to" on a random position in "words" int charIndex = (int) (Math.random() * (words.length() - 1)); if (charIndex < words.length() / 2) { from = words.substring(charIndex); to = to + from; } else { from = words.substring(0, charIndex); to = from + to; } } if (words.contains(from)) { // some characters (*,?) disturb split() which then throws // PatternSyntaxException try { if (pre == null && percentage == 100) { // All occurences of "from" need to be replaced return words.replaceAll(from, to); } String newWords = ""; if (words.endsWith(from)) { // add space to end to recognize last occurence of "from" words = words + " "; } // remove all "from" and split "words" there String[] splitted = words.split(from); int index = 0; String part = null; // if there are occurences of "from" if (splitted.length > 1) { // - 1 because dont add "to" to the end of last part while (index < splitted.length - 1) { part = splitted[index]; // add current part of "words" to the output newWords = newWords + part; // check if the part ends with correct string if (doesPreMatch(part) && Math.random() * 100.0 <= percentage) { // add replacement newWords = newWords + to; } else { // add original newWords = newWords + from; } index++; } // add the last part to finish the sentence part = splitted[index]; if (part.equals(" ")) { // dont add the space to the end return newWords; } else { return newWords + part; } } } catch (java.util.regex.PatternSyntaxException e) { // e.printStackTrace(); return words; } } return words; } public boolean doesPreMatch(String part) { boolean isBefore = !match; if (pre != null) { for (String pr : pre) { if (match == true) { // if one is correct, it is enough if (part.endsWith(pr) == match) { isBefore = true; break; } } else { // if one is wrong, its over if (part.endsWith(pr) != match) { isBefore = false; break; } } } } else { isBefore = true; } return isBefore; } // load from config file public static void load() { if (config != null) { for (Map map : config.getMapList("words")) { new Words(map); } } } }