Properly escape regex characters

This commit is contained in:
Aust1n46 2020-03-26 19:38:42 -04:00
parent 65bc1ab49c
commit 5e6988399c
4 changed files with 37 additions and 11 deletions

View File

@ -725,7 +725,7 @@ public class MineverseChat extends JavaPlugin implements PluginMessageListener {
@Override @Override
public void run() { public void run() {
//Create VentureChatEvent //Create VentureChatEvent
VentureChatEvent ventureChatEvent = new VentureChatEvent(null, senderName, nickname, primaryGroup, chatChannelObject, recipients, format, chat, globalJSON, hash, false); VentureChatEvent ventureChatEvent = new VentureChatEvent(null, senderName, nickname, primaryGroup, chatChannelObject, recipients, recipients.size(), format, chat, globalJSON, hash, false);
//Fire event and wait for other plugin listeners to act on it //Fire event and wait for other plugin listeners to act on it
Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent); Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent);
} }

View File

@ -23,13 +23,14 @@ public class VentureChatEvent extends Event {
private final String playerPrimaryGroup; private final String playerPrimaryGroup;
private final ChatChannel channel; private final ChatChannel channel;
private final Set<Player> recipients; private final Set<Player> recipients;
private final int recipientCount; //For not counting vanished players
private final String format; private final String format;
private final String chat; private final String chat;
private final String globalJSON; private final String globalJSON;
private final int hash; private final int hash;
private final boolean bungee; private final boolean bungee;
public VentureChatEvent(MineverseChatPlayer mcp, String username, String nickname, String playerPrimaryGroup, ChatChannel channel, Set<Player> recipients, String format, String chat, String globalJSON, int hash, boolean bungee) { public VentureChatEvent(MineverseChatPlayer mcp, String username, String nickname, String playerPrimaryGroup, ChatChannel channel, Set<Player> recipients, int recipientCount, String format, String chat, String globalJSON, int hash, boolean bungee) {
super(MineverseChat.ASYNC); super(MineverseChat.ASYNC);
this.mcp = mcp; this.mcp = mcp;
this.username = username; this.username = username;
@ -37,6 +38,7 @@ public class VentureChatEvent extends Event {
this.playerPrimaryGroup = playerPrimaryGroup; this.playerPrimaryGroup = playerPrimaryGroup;
this.channel = channel; this.channel = channel;
this.recipients = recipients; this.recipients = recipients;
this.recipientCount = recipientCount;
this.format = format; this.format = format;
this.chat = chat; this.chat = chat;
this.globalJSON = globalJSON; this.globalJSON = globalJSON;
@ -68,6 +70,11 @@ public class VentureChatEvent extends Event {
return this.recipients; return this.recipients;
} }
//Could be lower than the total number of recipients because vanished players are not counted
public int getRecipientCount() {
return this.recipientCount;
}
public String getFormat() { public String getFormat() {
return this.format; return this.format;
} }

View File

@ -61,6 +61,7 @@ public class ChatListener implements Listener {
String chat = event.getMessage(); String chat = event.getMessage();
String format; String format;
Set<Player> recipients = event.getRecipients(); Set<Player> recipients = event.getRecipients();
int recipientCount = recipients.size(); // Don't count vanished players
MineverseChatPlayer mcp = MineverseChatAPI.getOnlineMineverseChatPlayer(event.getPlayer()); MineverseChatPlayer mcp = MineverseChatAPI.getOnlineMineverseChatPlayer(event.getPlayer());
ChatChannel eventChannel = mcp.getCurrentChannel(); ChatChannel eventChannel = mcp.getCurrentChannel();
@ -340,10 +341,12 @@ public class ChatListener implements Listener {
if(p.getPlayer() != mcp.getPlayer()) { if(p.getPlayer() != mcp.getPlayer()) {
if(!p.isListening(eventChannel.getName())) { if(!p.isListening(eventChannel.getName())) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
if(plugin.getConfig().getBoolean("ignorechat", false) && p.getIgnores().contains(mcp.getUUID())) { if(plugin.getConfig().getBoolean("ignorechat", false) && p.getIgnores().contains(mcp.getUUID())) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
if(plugin.getConfig().getBoolean("enable_towny_channel") && pluginManager.isPluginEnabled("Towny")) { if(plugin.getConfig().getBoolean("enable_towny_channel") && pluginManager.isPluginEnabled("Towny")) {
@ -353,28 +356,34 @@ public class ChatListener implements Listener {
if(eventChannel.getName().equalsIgnoreCase("Town")) { if(eventChannel.getName().equalsIgnoreCase("Town")) {
if(!pp.hasTown()) { if(!pp.hasTown()) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
else if(!r.hasTown()) { else if(!r.hasTown()) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
else if(!(r.getTown().getName().equals(pp.getTown().getName()))) { else if(!(r.getTown().getName().equals(pp.getTown().getName()))) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
} }
if(eventChannel.getName().equalsIgnoreCase("Nation")) { if(eventChannel.getName().equalsIgnoreCase("Nation")) {
if(!pp.hasNation()) { if(!pp.hasNation()) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
else if(!r.hasNation()) { else if(!r.hasNation()) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
else if(!(r.getTown().getNation().getName().equals(pp.getTown().getNation().getName()))) { else if(!(r.getTown().getNation().getName().equals(pp.getTown().getNation().getName()))) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
} }
@ -391,12 +400,15 @@ public class ChatListener implements Listener {
if(eventChannel.getName().equalsIgnoreCase("Faction")) { if(eventChannel.getName().equalsIgnoreCase("Faction")) {
if(!mplayer.hasFaction()) { if(!mplayer.hasFaction()) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
} }
else if(!mplayerp.hasFaction()) { else if(!mplayerp.hasFaction()) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
} }
else if(!(mplayer.getFactionName().equals(mplayerp.getFactionName()))) { else if(!(mplayer.getFactionName().equals(mplayerp.getFactionName()))) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
} }
} }
} }
@ -411,20 +423,21 @@ public class ChatListener implements Listener {
diff = locreceip.subtract(locsender); diff = locreceip.subtract(locsender);
if(Math.abs(diff.getX()) > chDistance || Math.abs(diff.getZ()) > chDistance) { if(Math.abs(diff.getX()) > chDistance || Math.abs(diff.getZ()) > chDistance) {
recipients.remove(p.getPlayer()); recipients.remove(p.getPlayer());
recipientCount--;
continue; continue;
} }
if(!mcp.getPlayer().canSee(p.getPlayer())) { if(!mcp.getPlayer().canSee(p.getPlayer())) {
recipients.remove(p.getPlayer()); recipientCount--;
continue; continue;
} }
} }
else { else {
recipients.remove(p.getPlayer()); recipientCount--;
continue; continue;
} }
} }
if(!mcp.getPlayer().canSee(p.getPlayer())) { if(!mcp.getPlayer().canSee(p.getPlayer())) {
recipients.remove(p.getPlayer()); recipientCount--;
continue; continue;
} }
} }
@ -453,7 +466,7 @@ public class ChatListener implements Listener {
int hash = message.hashCode(); int hash = message.hashCode();
//Create VentureChatEvent //Create VentureChatEvent
VentureChatEvent ventureChatEvent = new VentureChatEvent(mcp, mcp.getName(), mcp.getNickname(), MineverseChat.permission.getPrimaryGroup(mcp.getPlayer()), eventChannel, recipients, format, chat, globalJSON, hash, bungee); VentureChatEvent ventureChatEvent = new VentureChatEvent(mcp, mcp.getName(), mcp.getNickname(), MineverseChat.permission.getPrimaryGroup(mcp.getPlayer()), eventChannel, recipients, recipientCount, format, chat, globalJSON, hash, bungee);
//Fire event and wait for other plugin listeners to act on it //Fire event and wait for other plugin listeners to act on it
Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent); Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent);
//Call method to send the processed chat //Call method to send the processed chat
@ -464,6 +477,7 @@ public class ChatListener implements Listener {
MineverseChatPlayer mcp = event.getMineverseChatPlayer(); MineverseChatPlayer mcp = event.getMineverseChatPlayer();
ChatChannel channel = event.getChannel(); ChatChannel channel = event.getChannel();
Set<Player> recipients = event.getRecipients(); Set<Player> recipients = event.getRecipients();
int recipientCount = event.getRecipientCount();
String format = event.getFormat(); String format = event.getFormat();
String chat = event.getChat(); String chat = event.getChat();
String consoleChat = event.getConsoleChat(); String consoleChat = event.getConsoleChat();
@ -479,7 +493,7 @@ public class ChatListener implements Listener {
} }
if(!bungee) { if(!bungee) {
if(recipients.size() == 1) { if(recipientCount == 1) {
if(!plugin.getConfig().getString("emptychannelalert", "&6No one is listening to you.").equals("")) { if(!plugin.getConfig().getString("emptychannelalert", "&6No one is listening to you.").equals("")) {
mcp.getPlayer().sendMessage(Format.FormatStringAll(plugin.getConfig().getString("emptychannelalert", "&6No one is listening to you."))); mcp.getPlayer().sendMessage(Format.FormatStringAll(plugin.getConfig().getString("emptychannelalert", "&6No one is listening to you.")));
} }

View File

@ -34,8 +34,8 @@ public class Format {
String prefix = ""; String prefix = "";
String suffix = ""; String suffix = "";
try { try {
prefix = FormatStringAll(MineverseChat.chat.getPlayerPrefix(sender.getPlayer()).replace("|", "").replace("+", "").replace("*", "")); prefix = FormatStringAll(MineverseChat.chat.getPlayerPrefix(sender.getPlayer()));
suffix = FormatStringAll(MineverseChat.chat.getPlayerSuffix(sender.getPlayer()).replace("|", "").replace("+", "").replace("*", "")); suffix = FormatStringAll(MineverseChat.chat.getPlayerSuffix(sender.getPlayer()));
if(suffix.equals("")) { if(suffix.equals("")) {
suffix = "venturechat_no_suffix_code"; suffix = "venturechat_no_suffix_code";
} }
@ -44,6 +44,7 @@ public class Format {
} }
} }
catch(Exception e) { catch(Exception e) {
System.out.println("Exception?" + e.getLocalizedMessage());
if(plugin.getConfig().getString("loglevel", "info").equals("debug")) { if(plugin.getConfig().getString("loglevel", "info").equals("debug")) {
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&e - Prefix and / or suffix don't exist, setting to nothing.")); Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&e - Prefix and / or suffix don't exist, setting to nothing."));
} }
@ -52,7 +53,7 @@ public class Format {
} }
String nickname = ""; String nickname = "";
if(sender.getPlayer() != null) { if(sender.getPlayer() != null) {
nickname = FormatStringAll(sender.getPlayer().getDisplayName().replace("|", "").replace("+", "").replace("*", "")); nickname = FormatStringAll(sender.getPlayer().getDisplayName());
} }
json += convertPlaceholders(f, JSONformat, prefix, nickname, suffix, sender); json += convertPlaceholders(f, JSONformat, prefix, nickname, suffix, sender);
json += "]}"; json += "]}";
@ -77,7 +78,7 @@ public class Format {
String placeholder = ""; String placeholder = "";
String lastCode = "§f"; String lastCode = "§f";
do { do {
Pattern pattern = Pattern.compile("(" + prefix.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(").replace(")", "\\)") + "|" + nickname.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(").replace(")", "\\)") + "|" + suffix.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(").replace(")", "\\)") + ")"); Pattern pattern = Pattern.compile("(" + escapeAllRegex(prefix) + "|" + escapeAllRegex(nickname) + "|" + escapeAllRegex(suffix) + ")");
Matcher matcher = pattern.matcher(remaining); Matcher matcher = pattern.matcher(remaining);
if(matcher.find()) { if(matcher.find()) {
indexStart = matcher.start(); indexStart = matcher.start();
@ -456,4 +457,8 @@ public class Format {
} }
return bFound; return bFound;
} }
public static String escapeAllRegex(String input) {
return input.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(").replace(")", "\\)").replace("|", "\\|").replace("+", "\\+").replace("*", "\\*");
}
} }