PwnFilterPlayerListener extends AbstractBukkitListener { void onPlayerChat(AsyncPlayerChatEvent event) { if (event.isCancelled()) { return; } MessageAuthor minecraftPlayer = plugin.getFilterService().getAuthor((event.getPlayer().getUniqueId())); if (minecraftPlayer instanceof UnknownAuthor) { plugin.getLogger().info("Filtering Aborted. Unable to lookup player in Chat Event. PlayerUUID: " + event.getPlayer().getUniqueId()); plugin.getLogger().info("Message: " + event.getMessage()); plugin.getLogger().info("AuthorServices: " + filterServiceImpl.getAuthorServices()); plugin.getLogger().info("Bukkit player online: " + event.getPlayer().isOnline()); return; } if (minecraftPlayer.hasPermission("pwnfilter.bypass.chat")) { return; } String message = event.getMessage(); if ((plugin.getApi().globalMute()) && (!minecraftPlayer.hasPermission("pwnfilter.bypass.mute"))) { event.setCancelled(true); return; } if (BukkitConfig.spamfilterEnabled() && !minecraftPlayer.hasPermission("pwnfilter.bypass.spam") && checkIfSpam(minecraftPlayer, message, event)) { return; } FilterContextImpl state = new FilterContextImpl(new ColoredString(message), minecraftPlayer, this); if ((BukkitConfig.decolor()) && !(minecraftPlayer.hasPermission("pwnfilter.color"))) { state.setModifiedMessage(new SimpleString(state.getModifiedMessage().toString())); } plugin.getLogger().finer("Applying '" + ruleChain.getConfigName() + "' to message: " + state.getModifiedMessage()); ruleChain.execute(state, filterServiceImpl); if (state.messageChanged()) { event.setMessage(state.getModifiedMessage().getRaw()); } if (state.isCancelled()) { event.setCancelled(true); } } PwnFilterPlayerListener(PwnFilterPlugin plugin); String getShortName(); @Override void activate();  }
@Test public void testBasicFunctionWorks() throws Exception { String input = "Test Chat Message"; chatEvent = new AsyncPlayerChatEvent(true, mockPlayer, input, new HashSet<>()); playerListener.loadRuleChain("blank.txt"); playerListener.onPlayerChat(chatEvent); assertEquals(chatEvent.getMessage(), input); }
@Test public void testExecutesRules() throws Exception { String input = "Test replaceme Message"; chatEvent = new AsyncPlayerChatEvent(true, mockPlayer, input, new HashSet<>()); playerListener.loadRuleChain("replace.txt"); playerListener.onPlayerChat(chatEvent); assertEquals(chatEvent.getMessage(), "Test PASS Message"); }
@Test public void testGlobalMuteCancelsMessage() throws Exception { String input = "Test Message"; api.setMutStatus(true); chatEvent = new AsyncPlayerChatEvent(true, mockPlayer, input, new HashSet<>()); playerListener.loadRuleChain("blank.txt"); playerListener.onPlayerChat(chatEvent); assertTrue(chatEvent.isCancelled()); }
@Test public void testDecolorMessage() throws Exception { String input = "Test&4 Message"; testConfig.set("decolor", true); BukkitConfig.loadConfiguration(testConfig, resourcesDir, filterServiceImpl); chatEvent = new AsyncPlayerChatEvent(true, mockPlayer, input, new HashSet<>()); playerListener.loadRuleChain("blank.txt"); playerListener.onPlayerChat(chatEvent); assertEquals(chatEvent.getMessage(), "Test Message"); testConfig.set("decolor", false); }
@Test public void testLowerMessage() throws Exception { String input = "HEY! THIS SHOULD ALL GET LOWERED."; BukkitConfig.loadConfiguration(testConfig, resourcesDir, filterServiceImpl); chatEvent = new AsyncPlayerChatEvent(true, mockPlayer, input, new HashSet<>()); playerListener.loadRuleChain("actionTests.txt"); playerListener.onPlayerChat(chatEvent); assertFalse(chatEvent.isCancelled()); assertEquals(chatEvent.getMessage(), "HEY! this should all get lowered."); }