Minor performance improvement to event cancelling, add checks against inventory clicks

feat/data-edit-commands
William278 2 years ago
parent efcb36d345
commit 945b65e1bc

@ -17,6 +17,7 @@ import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
@ -55,7 +56,7 @@ public class BukkitEventListener extends EventListener implements BukkitJoinEven
final OnlineUser user = BukkitPlayer.adapt(event.getEntity()); final OnlineUser user = BukkitPlayer.adapt(event.getEntity());
// If the player is locked or the plugin disabling, clear their drops // If the player is locked or the plugin disabling, clear their drops
if (cancelPlayerEvent(user)) { if (cancelPlayerEvent(user.uuid)) {
event.getDrops().clear(); event.getDrops().clear();
return; return;
} }
@ -89,43 +90,47 @@ public class BukkitEventListener extends EventListener implements BukkitJoinEven
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onDropItem(@NotNull PlayerDropItemEvent event) { public void onDropItem(@NotNull PlayerDropItemEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer()))); event.setCancelled(cancelPlayerEvent(event.getPlayer().getUniqueId()));
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPickupItem(@NotNull EntityPickupItemEvent event) { public void onPickupItem(@NotNull EntityPickupItemEvent event) {
if (event.getEntity() instanceof Player player) { if (event.getEntity() instanceof Player player) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(player))); event.setCancelled(cancelPlayerEvent(player.getUniqueId()));
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerInteract(@NotNull PlayerInteractEvent event) { public void onPlayerInteract(@NotNull PlayerInteractEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer()))); event.setCancelled(cancelPlayerEvent(event.getPlayer().getUniqueId()));
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(@NotNull BlockPlaceEvent event) { public void onBlockPlace(@NotNull BlockPlaceEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer()))); event.setCancelled(cancelPlayerEvent(event.getPlayer().getUniqueId()));
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBreak(@NotNull BlockBreakEvent event) { public void onBlockBreak(@NotNull BlockBreakEvent event) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(event.getPlayer()))); event.setCancelled(cancelPlayerEvent(event.getPlayer().getUniqueId()));
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryOpen(@NotNull InventoryOpenEvent event) { public void onInventoryOpen(@NotNull InventoryOpenEvent event) {
if (event.getPlayer() instanceof Player player) { if (event.getPlayer() instanceof Player player) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(player))); event.setCancelled(cancelPlayerEvent(player.getUniqueId()));
} }
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryClick(@NotNull InventoryClickEvent event) {
event.setCancelled(cancelPlayerEvent(event.getWhoClicked().getUniqueId()));
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerTakeDamage(@NotNull EntityDamageEvent event) { public void onPlayerTakeDamage(@NotNull EntityDamageEvent event) {
if (event.getEntity() instanceof Player player) { if (event.getEntity() instanceof Player player) {
event.setCancelled(cancelPlayerEvent(BukkitPlayer.adapt(player))); event.setCancelled(cancelPlayerEvent(player.getUniqueId()));
} }
} }

@ -122,8 +122,8 @@ public abstract class EventListener {
private void handleSynchronisationCompletion(@NotNull OnlineUser user, boolean succeeded) { private void handleSynchronisationCompletion(@NotNull OnlineUser user, boolean succeeded) {
if (succeeded) { if (succeeded) {
plugin.getLocales().getLocale("synchronisation_complete").ifPresent(user::sendActionBar); plugin.getLocales().getLocale("synchronisation_complete").ifPresent(user::sendActionBar);
lockedPlayers.remove(user.uuid);
plugin.getDatabase().ensureUser(user).join(); plugin.getDatabase().ensureUser(user).join();
lockedPlayers.remove(user.uuid);
plugin.getEventCannon().fireSyncCompleteEvent(user); plugin.getEventCannon().fireSyncCompleteEvent(user);
} else { } else {
plugin.getLocales().getLocale("synchronisation_failed") plugin.getLocales().getLocale("synchronisation_failed")
@ -154,7 +154,8 @@ public abstract class EventListener {
optionalUserData -> optionalUserData.ifPresent(userData -> plugin.getRedisManager() optionalUserData -> optionalUserData.ifPresent(userData -> plugin.getRedisManager()
.setUserData(user, userData).thenRun(() -> plugin.getDatabase() .setUserData(user, userData).thenRun(() -> plugin.getDatabase()
.setUserData(user, userData, DataSaveCause.DISCONNECT))))) .setUserData(user, userData, DataSaveCause.DISCONNECT)))))
.thenRun(() -> lockedPlayers.remove(user.uuid)).exceptionally(throwable -> { .thenRun(() -> lockedPlayers.remove(user.uuid))
.exceptionally(throwable -> {
plugin.getLoggingAdapter().log(Level.SEVERE, plugin.getLoggingAdapter().log(Level.SEVERE,
"An exception occurred handling a player disconnection"); "An exception occurred handling a player disconnection");
throwable.printStackTrace(); throwable.printStackTrace();
@ -196,11 +197,11 @@ public abstract class EventListener {
/** /**
* Determine whether a player event should be cancelled * Determine whether a player event should be cancelled
* *
* @param user {@link OnlineUser} performing the event * @param userUuid The UUID of the user to check
* @return Whether the event should be cancelled * @return Whether the event should be cancelled
*/ */
protected final boolean cancelPlayerEvent(@NotNull OnlineUser user) { protected final boolean cancelPlayerEvent(@NotNull UUID userUuid) {
return disabling || lockedPlayers.contains(user.uuid); return disabling || lockedPlayers.contains(userUuid);
} }
/** /**

Loading…
Cancel
Save