Don't run chat filtering on the main thread

This commit is contained in:
samczsun 2016-06-16 15:53:35 -04:00
parent 01409af5bd
commit 1893e98036
No known key found for this signature in database
GPG Key ID: B0AFDBD87206805D

View File

@ -22,6 +22,7 @@ import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang3.Validate;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -59,7 +60,7 @@ public class Chat extends MiniPlugin
private PreferencesManager _preferences;
private AchievementManager _achievements;
private IncognitoManager _incognitoManager;
private String[] _hackusations = {"hack", "hax", "hacker", "hacking", "cheat", "cheater", "cheating", "forcefield", "flyhack", "flyhacking", "autoclick", "aimbot"};
private String _filterUrl = "https://chat.mineplex.com:8003/content/item/moderate";
private String _appId = "34018d65-466d-4a91-8e92-29ca49f022c4";
@ -69,22 +70,22 @@ public class Chat extends MiniPlugin
private int _chatSlow = 0;
private long _silenced = 0;
private boolean _threeSecondDelay = true;
private List<Function<AsyncPlayerChatEvent, Boolean>> _highPriorityFilters = new ArrayList<>();
private List<Function<AsyncPlayerChatEvent, Boolean>> _lowPriorityFilters = new ArrayList<>();
private HashMap<UUID, MessageData> _playerLastMessage = new HashMap<UUID, MessageData>();
public Chat(JavaPlugin plugin, IncognitoManager incognitoManager, CoreClientManager clientManager, PreferencesManager preferences, AchievementManager achievements, String serverName)
{
super("Chat", plugin);
_incognitoManager = incognitoManager;
_clientManager = clientManager;
_serverName = serverName;
_preferences = preferences;
_achievements = achievements;
try
{
trustCert();
@ -207,7 +208,7 @@ public class Chat extends MiniPlugin
{
if (event.isCancelled())
return;
if (event.isAsynchronous())
{
for (Iterator<Player> playerIterator = event.getRecipients().iterator(); playerIterator.hasNext();)
@ -230,24 +231,41 @@ public class Chat extends MiniPlugin
return;
}
for (int i = 0; i < event.getLines().length; i++)
String[] lines = event.getLines();
runAsync(() ->
{
String line = event.getLine(i);
if (line != null && line.length() > 0)
for (int i = 0; i < lines.length; i++)
{
String filteredLine = getFilteredMessage(event.getPlayer(), line);
if (filteredLine != null)
event.setLine(i, filteredLine);
String line = lines[i];
if (line != null && line.length() > 0)
{
String filteredLine = getFilteredMessage(event.getPlayer(), line);
if (filteredLine != null)
{
lines[i] = filteredLine;
}
}
}
}
runSync(() ->
{
Sign sign = (Sign) event.getBlock().getState();
for (int i = 0; i < lines.length; i++)
{
sign.setLine(i, lines[i]);
}
sign.update();
});
});
}
@EventHandler(priority = EventPriority.LOWEST)
public void filterChat(AsyncPlayerChatEvent event)
{
if (event.isCancelled())
return;
if (event.isAsynchronous())
{
event.setMessage(getFilteredMessage(event.getPlayer(), event.getMessage()));
@ -266,7 +284,7 @@ public class Chat extends MiniPlugin
JSONObject message = buildJsonChatObject(filterType, displayName, playerName, originalMessage, _serverName, 1);
String response = getResponseFromCleanSpeak(message, filterType);
if (response == null)
{
System.out.println("[ERROR] Unable to filter chat message...thanks a lot CleanSpeak.");
@ -284,7 +302,7 @@ public class Chat extends MiniPlugin
System.out.println(o.toString());
}
*/
String filteredMsg = "";
filteredMsg = ((JSONObject) JSONValue.parse(response)).get("content").toString();
@ -293,7 +311,7 @@ public class Chat extends MiniPlugin
filteredMsg = ((JSONObject) JSONValue.parse(filteredMsg)).get("parts").toString();
filteredMsg = filteredMsg.replace('[', ' ').replace(']', ' ').trim();
filteredMsg = ((JSONObject) JSONValue.parse(filteredMsg)).get("replacement").toString();
return filteredMsg;
}
else
@ -301,7 +319,7 @@ public class Chat extends MiniPlugin
return originalMessage;
}
}
@EventHandler(priority = EventPriority.LOW)
public void HandleChat(AsyncPlayerChatEvent event)
{
@ -309,14 +327,14 @@ public class Chat extends MiniPlugin
return;
Player sender = event.getPlayer();
if (_incognitoManager != null && _incognitoManager.Get(sender).Status)
{
UtilPlayer.message(sender, C.cYellow + "You can not chat while incognito.");
event.setCancelled(true);
return;
}
for (Function<AsyncPlayerChatEvent, Boolean> filter : _highPriorityFilters)
{
if (filter.apply(event).booleanValue())
@ -325,7 +343,7 @@ public class Chat extends MiniPlugin
return;
}
}
if (SilenceCheck(sender))
{
event.setCancelled(true);
@ -349,7 +367,7 @@ public class Chat extends MiniPlugin
else if (!_clientManager.Get(sender).GetRank().has(Rank.HELPER) &&
msgContainsHack(event.getMessage()))
{
UtilPlayer.message(sender, F.main("Chat",
UtilPlayer.message(sender, F.main("Chat",
"Accusing players of cheating in-game is against the rules."
+ "If you think someone is cheating, please gather evidence and report it at "
+ F.link("www.mineplex.com/reports")));
@ -372,10 +390,10 @@ public class Chat extends MiniPlugin
event.setCancelled(true);
}
}
if (!event.isCancelled())
_playerLastMessage.put(sender.getUniqueId(), new MessageData(event.getMessage()));
for (Function<AsyncPlayerChatEvent, Boolean> filter : _lowPriorityFilters)
{
if (filter.apply(event).booleanValue())
@ -442,17 +460,17 @@ public class Chat extends MiniPlugin
JSONObject content = new JSONObject();
content.put("content", msg);
content.put("type", "text");
JSONArray parts = new JSONArray();
parts.add(content);
JSONObject mainContent = new JSONObject();
mainContent.put("applicationId", _appId);
mainContent.put("createInstant", System.currentTimeMillis());
mainContent.put("parts", parts);
mainContent.put("senderDisplayName", name);
mainContent.put("senderId", player);
message.put("content", mainContent);
break;
case "username":
@ -558,7 +576,7 @@ public class Chat extends MiniPlugin
e.printStackTrace();
}
}
if (inputStream != null)
{
try
@ -573,10 +591,10 @@ public class Chat extends MiniPlugin
}
String pmresponse = null;
if (response != null)
pmresponse = response.toString();
return pmresponse;
}
@ -627,18 +645,18 @@ public class Chat extends MiniPlugin
_playerLastMessage.remove(event.getPlayer().getUniqueId());
}
public void setThreeSecondDelay(boolean b)
public void setThreeSecondDelay(boolean b)
{
_threeSecondDelay = b;
}
/**
* If the function returns Boolean.TRUE then the message will be CANCELLED.
*/
public void AddFilter(Function<AsyncPlayerChatEvent, Boolean> restriction, FilterPriority priority)
{
Validate.isTrue(priority != null, "Priority must not be null.");
switch (priority)
{
case HIGH:
@ -648,5 +666,5 @@ public class Chat extends MiniPlugin
_lowPriorityFilters.add(restriction);
}
}
}