From 1bebdb56028132151b921cf276c4977278ea8719 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Fri, 22 Sep 2023 23:06:05 -0400 Subject: [PATCH] Add OpenPlayerSaveEvent Closes #156 --- api/pom.xml | 2 +- .../openinv/event/OpenPlayerSaveEvent.java | 74 +++++++++++++++++++ assembly/pom.xml | 2 +- internal/v1_19_R3/pom.xml | 2 +- internal/v1_20_R1/pom.xml | 2 +- plugin/pom.xml | 2 +- .../com/lishid/openinv/InventoryListener.java | 4 +- .../main/java/com/lishid/openinv/OpenInv.java | 20 +++-- pom.xml | 6 +- 9 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java diff --git a/api/pom.xml b/api/pom.xml index fe0552d..eeee22d 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -21,7 +21,7 @@ openinvparent com.lishid - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT openinvapi diff --git a/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java b/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java new file mode 100644 index 0000000..a36a6c9 --- /dev/null +++ b/api/src/main/java/com/lishid/openinv/event/OpenPlayerSaveEvent.java @@ -0,0 +1,74 @@ +package com.lishid.openinv.event; + +import com.lishid.openinv.internal.ISpecialInventory; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * Event fired before OpenInv saves a player's data. + */ +public class OpenPlayerSaveEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private final Player player; + private final ISpecialInventory inventory; + private boolean cancelled = false; + + public OpenPlayerSaveEvent(@NotNull Player player, @NotNull ISpecialInventory inventory) { + this.player = player; + this.inventory = inventory; + } + + /** + * Get the {@link Player} whose data is being saved. + * + * @return player the Player whose data is being saved + */ + public @NotNull Player getPlayer() { + return player; + } + + /** + * Get the {@link ISpecialInventory} that triggered the save by being closed. + * + * @return the special inventory + */ + public @NotNull ISpecialInventory getInventory() { + return inventory; + } + + /** + * Get whether the event is cancelled. + * + * @return true if the event is cancelled + */ + @Override + public boolean isCancelled() { + return cancelled; + } + + /** + * Set whether the event is cancelled. + * + * @param cancel whether the event is cancelled + */ + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLERS; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + +} diff --git a/assembly/pom.xml b/assembly/pom.xml index 54f94a1..2a69091 100644 --- a/assembly/pom.xml +++ b/assembly/pom.xml @@ -21,7 +21,7 @@ com.lishid openinvparent - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT openinvassembly diff --git a/internal/v1_19_R3/pom.xml b/internal/v1_19_R3/pom.xml index 8e940b6..56acad9 100644 --- a/internal/v1_19_R3/pom.xml +++ b/internal/v1_19_R3/pom.xml @@ -23,7 +23,7 @@ openinvparent com.lishid ../../pom.xml - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT openinvadapter1_19_R3 diff --git a/internal/v1_20_R1/pom.xml b/internal/v1_20_R1/pom.xml index 5f004c5..acb7f21 100644 --- a/internal/v1_20_R1/pom.xml +++ b/internal/v1_20_R1/pom.xml @@ -23,7 +23,7 @@ openinvparent com.lishid ../../pom.xml - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT openinvadapter1_20_R1 diff --git a/plugin/pom.xml b/plugin/pom.xml index 77fb5af..dbaccf9 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -21,7 +21,7 @@ openinvparent com.lishid - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT openinvplugincore diff --git a/plugin/src/main/java/com/lishid/openinv/InventoryListener.java b/plugin/src/main/java/com/lishid/openinv/InventoryListener.java index 7101fa4..ef15931 100644 --- a/plugin/src/main/java/com/lishid/openinv/InventoryListener.java +++ b/plugin/src/main/java/com/lishid/openinv/InventoryListener.java @@ -63,11 +63,11 @@ record InventoryListener(OpenInv plugin) implements Listener { ISpecialInventory specialInventory = InventoryAccess.getEnderChest(event.getInventory()); if (specialInventory != null) { - this.plugin.handleCloseInventory(event.getPlayer(), specialInventory); + this.plugin.handleCloseInventory(specialInventory); } else { specialInventory = InventoryAccess.getPlayerInventory(event.getInventory()); if (specialInventory != null) { - this.plugin.handleCloseInventory(event.getPlayer(), specialInventory); + this.plugin.handleCloseInventory(specialInventory); } } } diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index 25b28aa..9ff7ee5 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -23,6 +23,7 @@ import com.lishid.openinv.commands.OpenInvCommand; import com.lishid.openinv.commands.SearchContainerCommand; import com.lishid.openinv.commands.SearchEnchantCommand; import com.lishid.openinv.commands.SearchInvCommand; +import com.lishid.openinv.event.OpenPlayerSaveEvent; import com.lishid.openinv.internal.IAnySilentContainer; import com.lishid.openinv.internal.ISpecialEnderChest; import com.lishid.openinv.internal.ISpecialInventory; @@ -544,7 +545,7 @@ public class OpenInv extends JavaPlugin implements IOpenInv { } } - void handleCloseInventory(@NotNull HumanEntity exViewer, @NotNull ISpecialInventory inventory) { + void handleCloseInventory(@NotNull ISpecialInventory inventory) { Map map = inventory instanceof ISpecialPlayerInventory ? inventories : enderChests; UUID key = inventory.getPlayer().getUniqueId(); @Nullable ISpecialInventory loaded = map.get(key); @@ -576,11 +577,18 @@ public class OpenInv extends JavaPlugin implements IOpenInv { // Re-fetch from map - prevents duplicate saves on multi-close. ISpecialInventory current = map.remove(key); - if (!disableSaving() - && current != null - && current.getPlayer() instanceof Player player - && !player.isOnline()) { - this.accessor.getPlayerDataManager().inject(player).saveData(); + if (disableSaving() + || current == null + || !(current.getPlayer() instanceof Player player) + || player.isOnline()) { + return; + } + + OpenPlayerSaveEvent event = new OpenPlayerSaveEvent(player, current); + getServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + this.accessor.getPlayerDataManager().inject(player).saveData(); } }); } diff --git a/pom.xml b/pom.xml index f910362..f7271e0 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ openinvparent OpenInv http://dev.bukkit.org/bukkit-plugins/openinv/ - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT pom @@ -84,13 +84,13 @@ openinvapi com.lishid compile - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT openinvplugincore com.lishid compile - 4.3.2-SNAPSHOT + 4.4.0-SNAPSHOT com.lishid