Probably fixed name mentioning

This commit is contained in:
Norbi Peti 2016-09-05 16:46:38 +02:00
parent fe974035aa
commit f34deb7abc
4 changed files with 83 additions and 81 deletions

View file

@ -2,7 +2,7 @@ package buttondevteam.thebuttonmcchat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -12,15 +12,15 @@ public final class ChatFormatter {
private Pattern regex; private Pattern regex;
private Format format; private Format format;
private Color color; private Color color;
private Predicate<String> onmatch; private Function<String, String> onmatch;
private String openlink; private String openlink;
private Priority priority; private Priority priority;
private String replacewith; private String replacewith;
private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue", private static final String[] RainbowPresserColors = new String[] { "red", "gold", "yellow", "green", "blue",
"dark_purple" }; // TODO "dark_purple" }; // TODO: Move out to ChatProcessing
public ChatFormatter(Pattern regex, Format format, Color color, Predicate<String> onmatch, String openlink, public ChatFormatter(Pattern regex, Format format, Color color, Function<String, String> onmatch, String openlink,
Priority priority, String replacewith) { Priority priority, String replacewith) {
this.regex = regex; this.regex = regex;
this.format = format; this.format = format;
@ -138,17 +138,16 @@ public final class ChatFormatter {
section.Formatters.sort((cf1, cf2) -> cf1.priority.compareTo(cf2.priority)); section.Formatters.sort((cf1, cf2) -> cf1.priority.compareTo(cf2.priority));
for (ChatFormatter formatter : section.Formatters) { for (ChatFormatter formatter : section.Formatters) {
DebugCommand.SendDebugMessage("Applying formatter: " + formatter); DebugCommand.SendDebugMessage("Applying formatter: " + formatter);
if (formatter.onmatch == null || formatter.onmatch.test(originaltext)) { if (formatter.onmatch != null)
if (formatter.color != null) originaltext = formatter.onmatch.apply(originaltext);
color = formatter.color; if (formatter.color != null)
if (formatter.format != null) color = formatter.color;
format = formatter.format; if (formatter.format != null)
if (formatter.openlink != null) format = formatter.format;
openlink = formatter.openlink; if (formatter.openlink != null)
if (formatter.replacewith != null) openlink = formatter.openlink;
replacewith = formatter.replacewith; if (formatter.replacewith != null)
} else replacewith = formatter.replacewith;
DebugCommand.SendDebugMessage("Onmatch predicate returned false.");
} }
finalstring.append(",{\"text\":\""); finalstring.append(",{\"text\":\"");
if (replacewith != null) if (replacewith != null)

View file

@ -1,6 +1,6 @@
package buttondevteam.thebuttonmcchat; package buttondevteam.thebuttonmcchat;
import java.util.function.Predicate; import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import buttondevteam.thebuttonmcchat.ChatFormatter.Color; import buttondevteam.thebuttonmcchat.ChatFormatter.Color;
@ -11,7 +11,7 @@ public class ChatFormatterBuilder {
private Pattern regex; private Pattern regex;
private Format format; private Format format;
private Color color; private Color color;
private Predicate<String> onmatch; private Function<String, String> onmatch;
private String openlink; private String openlink;
private Priority priority; private Priority priority;
private String replacewith; private String replacewith;
@ -47,11 +47,11 @@ public class ChatFormatterBuilder {
return this; return this;
} }
public Predicate<String> getOnmatch() { public Function<String, String> getOnmatch() {
return onmatch; return onmatch;
} }
public ChatFormatterBuilder setOnmatch(Predicate<String> onmatch) { public ChatFormatterBuilder setOnmatch(Function<String, String> onmatch) {
this.onmatch = onmatch; this.onmatch = onmatch;
return this; return this;
} }

View file

@ -19,6 +19,13 @@ import buttondevteam.thebuttonmcchat.ChatFormatter.Priority;
import buttondevteam.thebuttonmcchat.commands.UnlolCommand; import buttondevteam.thebuttonmcchat.commands.UnlolCommand;
public class ChatProcessing { public class ChatProcessing {
private static final Pattern CONSOLE_PING_PATTERN = Pattern.compile("(?i)" + Pattern.quote("@console"));
private static final Pattern HASHTAG_PATTERN = Pattern.compile("#(\\w+)");
private static final Pattern URL_PATTERN = Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)");
private static final Pattern ENTIRE_MESSAGE_PATTERN = Pattern.compile(".+");
private static final Pattern UNDERLINED_PATTERN = Pattern.compile("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_");
private static final Pattern ITALIC_PATTERN = Pattern.compile("(?<!\\\\)\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*");
private static final Pattern BOLD_PATTERN = Pattern.compile("(?<!\\\\)\\*\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*\\*");
private static boolean pingedconsole = false; private static boolean pingedconsole = false;
// Returns e.setCancelled // Returns e.setCancelled
@ -69,7 +76,7 @@ public class ChatProcessing {
colormode = ChatFormatter.Color.Green; colormode = ChatFormatter.Color.Green;
// If greentext, ignore channel or player colors // If greentext, ignore channel or player colors
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(".+")).setColor(colormode) formatters.add(new ChatFormatterBuilder().setRegex(ENTIRE_MESSAGE_PATTERN).setColor(colormode)
.setPriority(Priority.Low).build()); .setPriority(Priority.Low).build());
String formattedmessage = message; String formattedmessage = message;
@ -79,19 +86,16 @@ public class ChatProcessing {
String suggestmsg = formattedmessage; String suggestmsg = formattedmessage;
formatters.add(new ChatFormatterBuilder() formatters.add(new ChatFormatterBuilder().setRegex(BOLD_PATTERN).setFormat(ChatFormatter.Format.Bold)
.setRegex(Pattern.compile("(?<!\\\\)\\*\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*\\*")) .setReplacewith("$1").build());
.setFormat(ChatFormatter.Format.Bold).setReplacewith("$1").build()); formatters.add(new ChatFormatterBuilder().setRegex(ITALIC_PATTERN).setFormat(ChatFormatter.Format.Italic)
formatters.add( .setReplacewith("$1").build());
new ChatFormatterBuilder().setRegex(Pattern.compile("(?<!\\\\)\\*((?:\\\\\\*|[^\\*])+[^\\*\\\\])\\*")) formatters.add(new ChatFormatterBuilder().setRegex(UNDERLINED_PATTERN)
.setFormat(ChatFormatter.Format.Italic).setReplacewith("$1").build()); .setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
formatters.add(
new ChatFormatterBuilder().setRegex(Pattern.compile("(?<!\\\\)\\_((?:\\\\\\_|[^\\_])+[^\\_\\\\])\\_"))
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build());
// URLs + Rainbow text // URLs + Rainbow text
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("(http[\\w:/?=$\\-_.+!*'(),]+)")) formatters.add(new ChatFormatterBuilder().setRegex(URL_PATTERN).setFormat(ChatFormatter.Format.Underlined)
.setFormat(ChatFormatter.Format.Underlined).setReplacewith("$1").build()); .setReplacewith("$1").build());
/* /*
* formattedmessage = formattedmessage .replace( item, String.format( * formattedmessage = formattedmessage .replace( item, String.format(
* "\",\"color\":\"%s\"},{\"text\":\"%s\",\"color\":\"%s\",\"underlined\":\"true\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open URL\",\"color\":\"blue\"}]}}},{\"text\":\"" * "\",\"color\":\"%s\"},{\"text\":\"%s\",\"color\":\"%s\",\"underlined\":\"true\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open URL\",\"color\":\"blue\"}]}}},{\"text\":\""
@ -99,20 +103,26 @@ public class ChatProcessing {
*/ */
if (PluginMain.GetPlayers().size() > 0) { if (PluginMain.GetPlayers().size() > 0) {
StringBuilder sb = new StringBuilder(); StringBuilder namesb = new StringBuilder();
sb.append("(?i)("); namesb.append("(?i)(");
for (Player p : PluginMain.GetPlayers()) for (Player p : PluginMain.GetPlayers())
sb.append(p.getName()).append("|"); namesb.append(p.getName()).append("|");
sb.deleteCharAt(sb.length() - 1); namesb.deleteCharAt(namesb.length() - 1);
sb.append(")"); namesb.append(")");
StringBuilder nicksb = new StringBuilder();
nicksb.append("(?i)(");
for (Player p : PluginMain.GetPlayers())
nicksb.append(PlayerListener.nicknames.inverse().get(p.getUniqueId())).append("|");
nicksb.deleteCharAt(nicksb.length() - 1);
nicksb.append(")");
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString())) formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(namesb.toString()))
.setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> {
Player p = Bukkit.getPlayer(match); Player p = Bukkit.getPlayer(match);
if (p == null) { if (p == null) {
PluginMain.Instance.getLogger() PluginMain.Instance.getLogger()
.warning("Error: Can't find player " + match + " but it was reported as online."); .warning("Error: Can't find player " + match + " but it was reported as online.");
return false; return "§c" + match + "§r";
} }
ChatPlayer mpp = ChatPlayer.GetFromPlayer(p); ChatPlayer mpp = ChatPlayer.GetFromPlayer(p);
if (PlayerListener.NotificationSound == null) if (PlayerListener.NotificationSound == null)
@ -122,55 +132,44 @@ public class ChatProcessing {
p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f,
(float) PlayerListener.NotificationPitch); (float) PlayerListener.NotificationPitch);
String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
return true; // TODO return color + p.getName() + "§r";
}).setPriority(Priority.High).build()); }).setPriority(Priority.High).build());
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(sb.toString())) formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile(nicksb.toString()))
.setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { .setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> {
for (String n : PlayerListener.nicknames.keySet()) { if (PlayerListener.nicknames.containsKey(match)) {
String nwithoutformatting = new String(n); Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(match));
int index;
while ((index = nwithoutformatting.indexOf("§k")) != -1)
nwithoutformatting = nwithoutformatting
.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support for one random char
while ((index = nwithoutformatting.indexOf('§')) != -1)
nwithoutformatting = nwithoutformatting
.replace("§" + nwithoutformatting.charAt(index + 1), "");
if (!match.equalsIgnoreCase(nwithoutformatting))
continue; // TODO
Player p = Bukkit.getPlayer(PlayerListener.nicknames.get(n));
if (p == null) { if (p == null) {
PluginMain.Instance.getLogger().warning( PluginMain.Instance.getLogger().warning("Error: Can't find player nicknamed " + match
"Error: Can't find player " + match + " but it was reported as online."); + " but it was reported as online.");
return false; return "§c" + match + "§r";
} }
ChatPlayer mpp = ChatPlayer.GetFromPlayer(p);
if (PlayerListener.NotificationSound == null) if (PlayerListener.NotificationSound == null)
p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO:
// Airhorn // Airhorn
else else
p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f,
(float) PlayerListener.NotificationPitch); (float) PlayerListener.NotificationPitch);
String color = String.format("§%x", return PlayerListener.essentials.getUser(p).getNickname();
(mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor()));
} }
return true; // TODO Bukkit.getServer().getLogger().warning(
"Player nicknamed " + match + " not found in nickname map but was reported as online.");
return "§c" + match + "§r";
}).setPriority(Priority.High).build()); }).setPriority(Priority.High).build());
} }
pingedconsole = false; pingedconsole = false;
formatters.add(new ChatFormatterBuilder().setRegex(Pattern.compile("(?i)" + Pattern.quote("@console"))) formatters.add(new ChatFormatterBuilder().setRegex(CONSOLE_PING_PATTERN).setColor(ChatFormatter.Color.Aqua)
.setColor(ChatFormatter.Color.Aqua).setOnmatch((String match) -> { .setOnmatch((String match) -> {
if (!pingedconsole) { if (!pingedconsole) {
System.out.print("\007"); System.out.print("\007");
pingedconsole = true; pingedconsole = true;
} }
return true; return match;
}).setPriority(Priority.High).build()); }).setPriority(Priority.High).build());
formatters formatters.add(new ChatFormatterBuilder().setRegex(HASHTAG_PATTERN).setColor(ChatFormatter.Color.Blue)
.add(new ChatFormatterBuilder().setRegex(Pattern.compile("#(\\w+)")).setColor(ChatFormatter.Color.Blue) .setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build());
.setOpenlink("https://twitter.com/hashtag/$1").setPriority(Priority.High).build());
/* /*
* if (!hadurls) { if (formattedmessage.matches("(?i).*" + Pattern.quote("@console") + ".*")) { formattedmessage = formattedmessage.replaceAll( "(?i)" + Pattern.quote("@console"), * if (!hadurls) { if (formattedmessage.matches("(?i).*" + Pattern.quote("@console") + ".*")) { formattedmessage = formattedmessage.replaceAll( "(?i)" + Pattern.quote("@console"),

View file

@ -1,7 +1,6 @@
package buttondevteam.thebuttonmcchat; package buttondevteam.thebuttonmcchat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random; import java.util.Random;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -44,6 +43,8 @@ import buttondevteam.bucket.core.TBMCPlayerSaveEvent;
import buttondevteam.thebuttonmcchat.commands.ucmds.KittycannonCommand; import buttondevteam.thebuttonmcchat.commands.ucmds.KittycannonCommand;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Town;
@ -55,7 +56,10 @@ import com.vexsoftware.votifier.model.Vote;
import com.vexsoftware.votifier.model.VotifierEvent; import com.vexsoftware.votifier.model.VotifierEvent;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
public static HashMap<String, UUID> nicknames = new HashMap<>(); /**
* Does not contain format codes
*/
public static BiMap<String, UUID> nicknames = HashBiMap.create();
public static boolean Enable = false; public static boolean Enable = false;
@ -125,7 +129,17 @@ public class PlayerListener implements Listener {
timer.schedule(tt, 15 * 1000); timer.schedule(tt, 15 * 1000);
} }
nicknames.put(essentials.getUser(p).getNickname(), p.getUniqueId()); String nwithoutformatting = essentials.getUser(p).getNickname();
int index;
while ((index = nwithoutformatting.indexOf("§k")) != -1)
nwithoutformatting = nwithoutformatting.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support
// for
// one
// random
// char
while ((index = nwithoutformatting.indexOf('§')) != -1)
nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), "");
nicknames.put(nwithoutformatting, p.getUniqueId());
cp.RPMode = true; cp.RPMode = true;
@ -385,19 +399,9 @@ public class PlayerListener implements Listener {
public void onTabComplete(PlayerChatTabCompleteEvent e) { public void onTabComplete(PlayerChatTabCompleteEvent e) {
String name = e.getLastToken(); String name = e.getLastToken();
for (String nickname : nicknames.keySet()) { for (String nickname : nicknames.keySet()) {
String nwithoutformatting = nickname; if (nickname.startsWith(name)
int index; && !nickname.equals(Bukkit.getPlayer(nicknames.get(nickname)).getName()))
while ((index = nwithoutformatting.indexOf("§k")) != -1) e.getTabCompletions().add(nickname);
nwithoutformatting = nwithoutformatting.replace("§k" + nwithoutformatting.charAt(index + 2), ""); // Support
// for
// one
// random
// char
while ((index = nwithoutformatting.indexOf('§')) != -1)
nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), "");
if (nwithoutformatting.startsWith(name)
&& !nwithoutformatting.equals(Bukkit.getPlayer(nicknames.get(nickname)).getName()))
e.getTabCompletions().add(nwithoutformatting);
} }
} }