Minor cleanup, tweak listener method names

feat/data-edit-commands
William 2 years ago
parent 2b91154ca2
commit c3b339b3dd

@ -53,8 +53,11 @@ public class BukkitEventListener extends EventListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onWorldSave(@NotNull WorldSaveEvent event) { public void onWorldSave(@NotNull WorldSaveEvent event) {
CompletableFuture.runAsync(() -> super.handleAsyncWorldSave(event.getWorld().getPlayers().stream() // Handle saving player data snapshots when the world saves
.map(BukkitPlayer::adapt) if (!plugin.getSettings().saveOnWorldSave) return;
CompletableFuture.runAsync(() -> super.saveOnWorldSave(event.getWorld().getPlayers()
.stream().map(BukkitPlayer::adapt)
.collect(Collectors.toList()))); .collect(Collectors.toList())));
} }
@ -79,6 +82,29 @@ public class BukkitEventListener extends EventListener implements Listener {
}); });
} }
@EventHandler(ignoreCancelled = true)
public void onPlayerDeath(PlayerDeathEvent event) {
final OnlineUser user = BukkitPlayer.adapt(event.getEntity());
// If the player is locked or the plugin disabling, clear their drops
if (cancelPlayerEvent(user)) {
event.getDrops().clear();
return;
}
// Handle saving player data snapshots on death
if (!plugin.getSettings().saveOnDeath) return;
// Truncate the drops list to the inventory size and save the player's inventory
final int maxInventorySize = BukkitInventoryMap.INVENTORY_SLOT_COUNT;
if (event.getDrops().size() > maxInventorySize) {
event.getDrops().subList(maxInventorySize, event.getDrops().size()).clear();
}
BukkitSerializer.serializeItemStackArray(event.getDrops().toArray(new ItemStack[0]))
.thenAccept(serializedDrops -> super.saveOnPlayerDeath(user, new ItemData(serializedDrops)));
}
/* /*
* Events to cancel if the player has not been set yet * Events to cancel if the player has not been set yet
*/ */
@ -132,24 +158,4 @@ public class BukkitEventListener extends EventListener implements Listener {
} }
} }
@EventHandler(ignoreCancelled = true)
public void onPlayerDeath(PlayerDeathEvent event) {
final OnlineUser user = BukkitPlayer.adapt(event.getEntity());
// If the player is locked or the plugin disabling, clear their death drops and return
if (cancelPlayerEvent(user)) {
event.getDrops().clear();
return;
}
// Truncate the drops list to the maximum allowed (44)
if (event.getDrops().size() > BukkitInventoryMap.INVENTORY_SLOT_COUNT) {
event.getDrops().subList(BukkitInventoryMap.INVENTORY_SLOT_COUNT, event.getDrops().size()).clear();
}
// Convert the death drops to a serialized item stack array
BukkitSerializer.serializeItemStackArray(event.getDrops().toArray(new ItemStack[0]))
.thenAccept(serializedDrops -> super.handlePlayerDeath(user, new ItemData(serializedDrops)));
}
} }

@ -164,11 +164,11 @@ public abstract class EventListener {
} }
/** /**
* Asynchronously handles a world save event * Handles the saving of data when the world save event is fired
* *
* @param usersInWorld a list of users in the world that is being saved * @param usersInWorld a list of users in the world that is being saved
*/ */
protected final void handleAsyncWorldSave(@NotNull List<OnlineUser> usersInWorld) { protected final void saveOnWorldSave(@NotNull List<OnlineUser> usersInWorld) {
if (disabling || !plugin.getSettings().saveOnWorldSave) { if (disabling || !plugin.getSettings().saveOnWorldSave) {
return; return;
} }
@ -177,11 +177,12 @@ public abstract class EventListener {
} }
/** /**
* Asynchronously handles a player death * Handles the saving of data when a player dies
* *
* @param user The user who died * @param user The user who died
* @param drops The items that this user would have dropped
*/ */
protected void handlePlayerDeath(@NotNull OnlineUser user, @NotNull ItemData drops) { protected void saveOnPlayerDeath(@NotNull OnlineUser user, @NotNull ItemData drops) {
if (disabling || !plugin.getSettings().saveOnDeath) { if (disabling || !plugin.getSettings().saveOnDeath) {
return; return;
} }

Loading…
Cancel
Save