4747import com .palmergames .util .StringMgmt ;
4848
4949import io .papermc .lib .PaperLib ;
50+
51+ import org .bukkit .Bukkit ;
5052import org .bukkit .ChatColor ;
5153import org .bukkit .Location ;
5254import org .bukkit .Material ;
@@ -1130,7 +1132,9 @@ public void onPlayerTakeLecternBookEvent(PlayerTakeLecternBookEvent event) {
11301132 public void onPlayerUsesCommand (PlayerCommandPreprocessEvent event ) {
11311133 if (plugin .isError () || !TownyAPI .getInstance ().isTownyWorld (event .getPlayer ().getWorld ()))
11321134 return ;
1133-
1135+
1136+ checkForOpDeOpCommand (event );
1137+
11341138 Resident resident = TownyUniverse .getInstance ().getResident (event .getPlayer ().getUniqueId ());
11351139
11361140 // More than likely another plugin using a fake player to run a command or,
@@ -1157,6 +1161,31 @@ public void onPlayerUsesCommand(PlayerCommandPreprocessEvent event) {
11571161 event .setCancelled (true );
11581162 }
11591163
1164+ private void checkForOpDeOpCommand (PlayerCommandPreprocessEvent event ) {
1165+ String [] args = CommandList .normalizeCommand (event .getMessage ()).split (" " );
1166+ String command = args [0 ];
1167+ // Fail early if we aren't looking at /op|deop [playername]
1168+ if ((!command .equalsIgnoreCase ("op" ) && !command .equalsIgnoreCase ("deop" )) || args .length != 2 )
1169+ return ;
1170+
1171+ // Get the target.
1172+ Player target = Bukkit .getPlayer (args [1 ]);
1173+ if (target == null || !target .isOnline ())
1174+ return ;
1175+
1176+ // Make sure they have the permission to run the command.
1177+ if (!event .getPlayer ().hasPermission ("minecraft.command." + command ))
1178+ return ;
1179+
1180+ // Make sure they're not running the command which will have no effect.
1181+ if (target .isOp () == "op" .equalsIgnoreCase (command ))
1182+ return ;
1183+
1184+ // Delete the online player's cache because they have been op'd or deop'd.
1185+ Towny plugin = Towny .getPlugin ();
1186+ plugin .getScheduler ().runLater (target , () -> plugin .deleteCache (target ), 1L );
1187+ }
1188+
11601189 public boolean blockWarPlayerCommand (Player player , Resident resident , String command ) {
11611190 if (resident .hasTown () && resident .getTownOrNull ().hasActiveWar () && blockedWarCommands .containsCommand (command )) {
11621191 TownyMessaging .sendErrorMsg (player , Translatable .of ("msg_command_war_blocked" ));
@@ -1394,7 +1423,7 @@ public void onPlayerPickupItem(EntityPickupItemEvent event) {
13941423 }
13951424 }
13961425
1397- @ EventHandler (priority = EventPriority .MONITOR , ignoreCancelled = true )
1426+ @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
13981427 public void onPlayerChangeGameMode (PlayerGameModeChangeEvent event ) {
13991428 if (!TownyAPI .getInstance ().isTownyWorld (event .getPlayer ().getWorld ()))
14001429 return ;
0 commit comments