PwnFilterBookListener extends AbstractBukkitListener { void onBookEdit(PlayerEditBookEvent event) { Player player; String message; if (event.isCancelled()) return; player = event.getPlayer(); if (plugin.getFilterService().getAuthor(player.getUniqueId()).hasPermission("pwnfilter.bypass.book")) return; BookMeta bookMeta = event.getNewBookMeta(); if (bookMeta.hasTitle()) { message = bookMeta.getTitle(); FilterContextImpl filterTask = new FilterContextImpl(new ColoredString(message), filterServiceImpl.getAuthor(player.getUniqueId()), this); ruleChain.execute(filterTask, filterServiceImpl); if (filterTask.isCancelled()) event.setCancelled(true); if (filterTask.messageChanged()) { bookMeta.setTitle(filterTask.getModifiedMessage().getRaw()); event.setNewBookMeta(bookMeta); } } if (bookMeta.hasPages()) { List<String> newPages = new ArrayList<>(); boolean modified = false; for (String page : bookMeta.getPages()) { FilterContextImpl state = new FilterContextImpl(new ColoredString(page), filterServiceImpl.getAuthor(player.getUniqueId()), this); ruleChain.execute(state, filterServiceImpl); if (state.isCancelled()) { event.setCancelled(true); } if (state.messageChanged()) { page = state.getModifiedMessage().getRaw(); modified = true; } newPages.add(page); } if (modified) { bookMeta.setPages(newPages); event.setNewBookMeta(bookMeta); } } } PwnFilterBookListener(PwnFilterPlugin plugin); @Override String getShortName(); @Override void activate();  }
@Test public void testBasicFunctionWorks() throws Exception { final BookMeta bookMeta = new MockBook("TestTitle", mockPlayer.getName()); bookMeta.setPages(testPages); event = new PlayerEditBookEvent(mockPlayer, 1, new MockBook("", ""), bookMeta, false); bookListener.loadRuleChain("blank.txt"); bookListener.onBookEdit(event); for (int i = 0; i < bookMeta.getPageCount() - 1; i++) { assertEquals(testPages[i], event.getNewBookMeta().getPage(i)); } }
@Test public void testBasicReplacementWorks() throws Exception { final BookMeta oldBookMeta = new MockBook("TestTitle", mockPlayer.getName()); oldBookMeta.setPages(testPages); String testPage = "This test should replaceme"; oldBookMeta.addPage(testPage); event = new PlayerEditBookEvent(mockPlayer, 1, new MockBook("", ""), oldBookMeta, false); bookListener.loadRuleChain("replace.txt"); bookListener.onBookEdit(event); int pageCount = oldBookMeta.getPageCount(); BookMeta newBookMeta = event.getNewBookMeta(); for (int i = 0; i < pageCount - 2; i++) { assertEquals(testPages[i], event.getNewBookMeta().getPage(i)); } assertEquals("This test should PASS", newBookMeta.getPage(pageCount - 1)); }