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
public void run() {
//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
Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent);
}

View File

@ -23,13 +23,14 @@ public class VentureChatEvent extends Event {
private final String playerPrimaryGroup;
private final ChatChannel channel;
private final Set<Player> recipients;
private final int recipientCount; //For not counting vanished players
private final String format;
private final String chat;
private final String globalJSON;
private final int hash;
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);
this.mcp = mcp;
this.username = username;
@ -37,6 +38,7 @@ public class VentureChatEvent extends Event {
this.playerPrimaryGroup = playerPrimaryGroup;
this.channel = channel;
this.recipients = recipients;
this.recipientCount = recipientCount;
this.format = format;
this.chat = chat;
this.globalJSON = globalJSON;
@ -68,6 +70,11 @@ public class VentureChatEvent extends Event {
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() {
return this.format;
}

View File

@ -61,6 +61,7 @@ public class ChatListener implements Listener {
String chat = event.getMessage();
String format;
Set<Player> recipients = event.getRecipients();
int recipientCount = recipients.size(); // Don't count vanished players
MineverseChatPlayer mcp = MineverseChatAPI.getOnlineMineverseChatPlayer(event.getPlayer());
ChatChannel eventChannel = mcp.getCurrentChannel();
@ -340,10 +341,12 @@ public class ChatListener implements Listener {
if(p.getPlayer() != mcp.getPlayer()) {
if(!p.isListening(eventChannel.getName())) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
if(plugin.getConfig().getBoolean("ignorechat", false) && p.getIgnores().contains(mcp.getUUID())) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
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(!pp.hasTown()) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
else if(!r.hasTown()) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
else if(!(r.getTown().getName().equals(pp.getTown().getName()))) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
}
if(eventChannel.getName().equalsIgnoreCase("Nation")) {
if(!pp.hasNation()) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
else if(!r.hasNation()) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
else if(!(r.getTown().getNation().getName().equals(pp.getTown().getNation().getName()))) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
}
@ -391,12 +400,15 @@ public class ChatListener implements Listener {
if(eventChannel.getName().equalsIgnoreCase("Faction")) {
if(!mplayer.hasFaction()) {
recipients.remove(p.getPlayer());
recipientCount--;
}
else if(!mplayerp.hasFaction()) {
recipients.remove(p.getPlayer());
recipientCount--;
}
else if(!(mplayer.getFactionName().equals(mplayerp.getFactionName()))) {
recipients.remove(p.getPlayer());
recipientCount--;
}
}
}
@ -411,20 +423,21 @@ public class ChatListener implements Listener {
diff = locreceip.subtract(locsender);
if(Math.abs(diff.getX()) > chDistance || Math.abs(diff.getZ()) > chDistance) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
if(!mcp.getPlayer().canSee(p.getPlayer())) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
}
else {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
}
if(!mcp.getPlayer().canSee(p.getPlayer())) {
recipients.remove(p.getPlayer());
recipientCount--;
continue;
}
}
@ -453,7 +466,7 @@ public class ChatListener implements Listener {
int hash = message.hashCode();
//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
Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent);
//Call method to send the processed chat
@ -464,6 +477,7 @@ public class ChatListener implements Listener {
MineverseChatPlayer mcp = event.getMineverseChatPlayer();
ChatChannel channel = event.getChannel();
Set<Player> recipients = event.getRecipients();
int recipientCount = event.getRecipientCount();
String format = event.getFormat();
String chat = event.getChat();
String consoleChat = event.getConsoleChat();
@ -479,7 +493,7 @@ public class ChatListener implements Listener {
}
if(!bungee) {
if(recipients.size() == 1) {
if(recipientCount == 1) {
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.")));
}

View File

@ -34,8 +34,8 @@ public class Format {
String prefix = "";
String suffix = "";
try {
prefix = FormatStringAll(MineverseChat.chat.getPlayerPrefix(sender.getPlayer()).replace("|", "").replace("+", "").replace("*", ""));
suffix = FormatStringAll(MineverseChat.chat.getPlayerSuffix(sender.getPlayer()).replace("|", "").replace("+", "").replace("*", ""));
prefix = FormatStringAll(MineverseChat.chat.getPlayerPrefix(sender.getPlayer()));
suffix = FormatStringAll(MineverseChat.chat.getPlayerSuffix(sender.getPlayer()));
if(suffix.equals("")) {
suffix = "venturechat_no_suffix_code";
}
@ -44,6 +44,7 @@ public class Format {
}
}
catch(Exception e) {
System.out.println("Exception?" + e.getLocalizedMessage());
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."));
}
@ -52,7 +53,7 @@ public class Format {
}
String nickname = "";
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 += "]}";
@ -77,7 +78,7 @@ public class Format {
String placeholder = "";
String lastCode = "§f";
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);
if(matcher.find()) {
indexStart = matcher.start();
@ -456,4 +457,8 @@ public class Format {
}
return bFound;
}
public static String escapeAllRegex(String input) {
return input.replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}").replace("(", "\\(").replace(")", "\\)").replace("|", "\\|").replace("+", "\\+").replace("*", "\\*");
}
}