1+ package parallelmc .parallelutils .modules .parallelchat .events ;
2+
3+ import net .kyori .adventure .text .Component ;
4+ import net .kyori .adventure .text .serializer .legacy .LegacyComponentSerializer ;
5+ import org .bukkit .entity .Player ;
6+ import org .bukkit .event .EventHandler ;
7+ import org .bukkit .event .EventPriority ;
8+ import org .bukkit .event .Listener ;
9+ import org .bukkit .event .player .PlayerEditBookEvent ;
10+ import org .bukkit .inventory .meta .BookMeta ;
11+
12+ public class OnBookEdit implements Listener {
13+ @ EventHandler (ignoreCancelled = true , priority = EventPriority .HIGHEST )
14+ public void onBookEdit (PlayerEditBookEvent event ) {
15+ Player player = event .getPlayer ();
16+ BookMeta bookMeta = event .getNewBookMeta ();
17+ // TODO: Add antiswear stuff once it's not jank
18+ if (bookMeta .hasPages ()) {
19+ for (int i = 1 ; i <= bookMeta .getPageCount (); i ++) {
20+ Component newText = bookMeta .page (i ); // Book pages are 1-indexed
21+ // Intellij tells me that newText is never null so /shrug
22+ // Check sign colors permission
23+ if (!player .hasPermission ("parallelutils.book.colors" )) {
24+ newText = newText .replaceText (x -> x .match ("&[[0-9][a-f]]" ).replacement ("" ));
25+ }
26+
27+ // Check sign hex permission
28+ if (!player .hasPermission ("parallelutils.book.hex" )) {
29+ newText = newText .replaceText (x -> x .match ("&#(.{6})" ).replacement ("" ));
30+ }
31+
32+ // Check sign formats permission
33+ if (!player .hasPermission ("parallelutils.book.formats" )) {
34+ newText = newText .replaceText (x -> x .match ("&[[l-o]r]" ).replacement ("" ));
35+ }
36+
37+ // Check sign magic permission
38+ if (!player .hasPermission ("parallelutils.book.magic" )) {
39+ newText = newText .replaceText (x -> x .match ("&k" ).replacement ("" ));
40+ }
41+ //Update bookmeta
42+ bookMeta .page (i , LegacyComponentSerializer .legacyAmpersand ().deserialize (LegacyComponentSerializer .legacyAmpersand ().serialize (newText )));
43+ }
44+ // Set the new bookmeta onto the book
45+ event .setNewBookMeta (bookMeta );
46+ }
47+ }
48+ }
0 commit comments