2013-05-01 21:47:41 +02:00
|
|
|
package com.dre.brewery;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2013-05-02 10:06:07 +02:00
|
|
|
import java.util.Map;
|
2013-05-01 21:47:41 +02:00
|
|
|
|
2013-05-02 10:06:07 +02:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
2013-05-01 21:47:41 +02:00
|
|
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
|
|
|
|
|
|
|
import com.dre.brewery.BPlayer;
|
|
|
|
|
|
|
|
public class Words {
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// represends Words and letters, that are replaced in drunk players messages
|
2013-05-01 21:47:41 +02:00
|
|
|
|
2013-05-02 10:06:07 +02:00
|
|
|
public static ArrayList<Words> words = new ArrayList<Words>();
|
|
|
|
public static FileConfiguration config;
|
2013-05-01 21:47:41 +02:00
|
|
|
|
|
|
|
private String from;
|
|
|
|
private String to;
|
|
|
|
private String[] pre;
|
2013-05-02 10:06:07 +02:00
|
|
|
private Boolean match = false;
|
|
|
|
private int alcohol = 1;
|
|
|
|
private int percentage = 100;
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
public Words(Map<?, ?> part) {
|
|
|
|
for (Map.Entry<?, ?> wordPart : part.entrySet()) {
|
2013-05-02 10:06:07 +02:00
|
|
|
String key = (String) wordPart.getKey();
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (wordPart.getValue() instanceof String) {
|
2013-05-02 10:06:07 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (key.equalsIgnoreCase("replace")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
this.from = (String) wordPart.getValue();
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (key.equalsIgnoreCase("to")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
this.to = (String) wordPart.getValue();
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (key.equalsIgnoreCase("pre")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
String fullPre = (String) wordPart.getValue();
|
|
|
|
this.pre = fullPre.split(",");
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (wordPart.getValue() instanceof Boolean) {
|
2013-05-02 10:06:07 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (key.equalsIgnoreCase("match")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
this.match = (Boolean) wordPart.getValue();
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (wordPart.getValue() instanceof Integer) {
|
2013-05-02 10:06:07 +02:00
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (key.equalsIgnoreCase("alcohol")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
this.alcohol = (Integer) wordPart.getValue();
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (key.equalsIgnoreCase("percentage")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
this.percentage = (Integer) wordPart.getValue();
|
|
|
|
}
|
|
|
|
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
}
|
2013-05-03 16:08:05 +02:00
|
|
|
if (this.from != null && this.to != null) {
|
2013-05-01 21:47:41 +02:00
|
|
|
words.add(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// Distort players words when he talks
|
|
|
|
public static void playerChat(AsyncPlayerChatEvent event) {
|
2013-05-02 10:06:07 +02:00
|
|
|
BPlayer bPlayer = BPlayer.get(event.getPlayer().getName());
|
2013-05-03 16:08:05 +02:00
|
|
|
if (bPlayer != null) {
|
|
|
|
if (words.isEmpty()) {
|
|
|
|
// load when first drunk player talks
|
2013-05-01 21:47:41 +02:00
|
|
|
load();
|
|
|
|
}
|
2013-05-03 16:08:05 +02:00
|
|
|
if (!words.isEmpty()) {
|
2013-05-01 21:47:41 +02:00
|
|
|
String message = event.getMessage();
|
2013-05-28 16:25:06 +02:00
|
|
|
for (Words word : words) {
|
|
|
|
if (word.alcohol <= bPlayer.getDrunkeness()) {
|
|
|
|
message = word.distort(message);
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
event.setMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// replace "percent"% of "from" -> "to" in "words", when the string before
|
|
|
|
// each "from" "match"es "pre"
|
|
|
|
// Not yet ignoring case :(
|
2013-05-28 16:25:06 +02:00
|
|
|
public String distort(String words) {
|
2013-07-01 01:07:56 +02:00
|
|
|
String from = this.from;
|
|
|
|
String to = this.to;
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (from.equalsIgnoreCase("-end")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
from = words;
|
2013-05-03 16:08:05 +02:00
|
|
|
to = words + to;
|
|
|
|
} else if (from.equalsIgnoreCase("-start")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
from = words;
|
2013-05-03 16:08:05 +02:00
|
|
|
to = to + words;
|
2013-07-03 00:31:34 +02:00
|
|
|
} else if (from.equalsIgnoreCase("-all")) {
|
|
|
|
from = words;
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (from.equalsIgnoreCase("-space")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
from = " ";
|
2013-05-03 16:08:05 +02:00
|
|
|
} else if (from.equalsIgnoreCase("-random")) {
|
|
|
|
// inserts "to" on a random position in "words"
|
2013-07-01 01:07:56 +02:00
|
|
|
int charIndex = (int) (Math.random() * (words.length() - 1));
|
|
|
|
if (charIndex < words.length() / 2) {
|
2013-05-02 10:06:07 +02:00
|
|
|
from = words.substring(charIndex);
|
2013-05-03 16:08:05 +02:00
|
|
|
to = to + from;
|
2013-05-02 10:06:07 +02:00
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
from = words.substring(0, charIndex);
|
|
|
|
to = from + to;
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
if (words.contains(from)) {
|
|
|
|
// some characters (*,?) disturb split() which then throws
|
|
|
|
// PatternSyntaxException
|
|
|
|
try {
|
2013-05-28 16:25:06 +02:00
|
|
|
if (pre == null && percentage == 100) {
|
2013-05-03 16:08:05 +02:00
|
|
|
// All occurences of "from" need to be replaced
|
|
|
|
return words.replaceAll(from, to);
|
2013-05-02 10:06:07 +02:00
|
|
|
}
|
|
|
|
String newWords = "";
|
2013-05-03 16:08:05 +02:00
|
|
|
if (words.endsWith(from)) {
|
|
|
|
// add space to end to recognize last occurence of "from"
|
|
|
|
words = words + " ";
|
2013-05-02 10:06:07 +02:00
|
|
|
}
|
2013-05-03 16:08:05 +02:00
|
|
|
// remove all "from" and split "words" there
|
2013-05-02 10:06:07 +02:00
|
|
|
String[] splitted = words.split(from);
|
|
|
|
int index = 0;
|
|
|
|
String part = null;
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// 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) {
|
2013-05-02 10:06:07 +02:00
|
|
|
part = splitted[index];
|
2013-05-03 16:08:05 +02:00
|
|
|
// add current part of "words" to the output
|
|
|
|
newWords = newWords + part;
|
|
|
|
// check if the part ends with correct string
|
|
|
|
|
2013-05-28 16:25:06 +02:00
|
|
|
if (doesPreMatch(part) && Math.random() * 100.0 <= percentage) {
|
2013-05-03 16:08:05 +02:00
|
|
|
// add replacement
|
|
|
|
newWords = newWords + to;
|
2013-05-02 10:06:07 +02:00
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
// add original
|
|
|
|
newWords = newWords + from;
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
index++;
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
2013-05-03 16:08:05 +02:00
|
|
|
// add the last part to finish the sentence
|
2013-05-02 10:06:07 +02:00
|
|
|
part = splitted[index];
|
2013-05-03 16:08:05 +02:00
|
|
|
if (part.equals(" ")) {
|
|
|
|
// dont add the space to the end
|
2013-05-02 10:06:07 +02:00
|
|
|
return newWords;
|
2013-05-01 21:47:41 +02:00
|
|
|
} else {
|
2013-05-02 10:06:07 +02:00
|
|
|
return newWords + part;
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
} catch (java.util.regex.PatternSyntaxException e) {
|
2013-05-03 16:08:05 +02:00
|
|
|
// e.printStackTrace();
|
2013-05-02 10:06:07 +02:00
|
|
|
return words;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return words;
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:25:06 +02:00
|
|
|
public boolean doesPreMatch(String part) {
|
2013-05-02 10:06:07 +02:00
|
|
|
boolean isBefore = !match;
|
2013-05-03 16:08:05 +02:00
|
|
|
if (pre != null) {
|
|
|
|
for (String pr : pre) {
|
|
|
|
if (match == true) {
|
|
|
|
// if one is correct, it is enough
|
|
|
|
if (part.endsWith(pr) == match) {
|
2013-05-02 10:06:07 +02:00
|
|
|
isBefore = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-01 21:47:41 +02:00
|
|
|
} else {
|
2013-05-03 16:08:05 +02:00
|
|
|
// if one is wrong, its over
|
|
|
|
if (part.endsWith(pr) != match) {
|
2013-05-02 10:06:07 +02:00
|
|
|
isBefore = false;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
} else {
|
|
|
|
isBefore = true;
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
2013-05-02 10:06:07 +02:00
|
|
|
return isBefore;
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
|
|
|
|
2013-05-03 16:08:05 +02:00
|
|
|
// load from config file
|
|
|
|
public static void load() {
|
|
|
|
if (config != null) {
|
|
|
|
for (Map<?, ?> map : config.getMapList("words")) {
|
2013-05-02 10:06:07 +02:00
|
|
|
new Words(map);
|
2013-05-01 21:47:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|