From 45fe2df8090cf561a64ec8614cbe11c61bc946fd Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Fri, 29 Nov 2024 18:47:11 +0100 Subject: [PATCH 01/13] wip: AccountBalanceChangeEvent --- .../gestern/gringotts/GringottsAccount.java | 3 + .../event/AccountBalanceChangeEvent.java | 58 +++++++++++++++++++ .../gringotts/event/AccountListener.java | 10 ++++ 3 files changed, 71 insertions(+) create mode 100644 src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java diff --git a/src/main/java/org/gestern/gringotts/GringottsAccount.java b/src/main/java/org/gestern/gringotts/GringottsAccount.java index f7d6a8b..6aa9ed7 100644 --- a/src/main/java/org/gestern/gringotts/GringottsAccount.java +++ b/src/main/java/org/gestern/gringotts/GringottsAccount.java @@ -14,6 +14,7 @@ import org.gestern.gringotts.api.TransactionResult; import org.gestern.gringotts.currency.Denomination; import org.gestern.gringotts.data.DAO; +import org.gestern.gringotts.event.AccountBalanceChangeEvent; import java.util.ArrayList; import java.util.Collection; @@ -204,6 +205,7 @@ public TransactionResult add(long amount) { } if (remaining == 0) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(owner, this.getBalance())); return TransactionResult.SUCCESS; } @@ -316,6 +318,7 @@ public TransactionResult remove(long amount) { dao.storeCents(this, cents - remaining); } + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(owner, this.getBalance())); return TransactionResult.SUCCESS; }; diff --git a/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java b/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java new file mode 100644 index 0000000..c4a4e27 --- /dev/null +++ b/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java @@ -0,0 +1,58 @@ +package org.gestern.gringotts.event; + +import org.bukkit.Bukkit; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.gestern.gringotts.accountholder.AccountHolder; +import org.jetbrains.annotations.NotNull; + +/** + * The type balance change event. + */ +public class AccountBalanceChangeEvent extends Event { + /** + * The constant handlers. + */ + public static final HandlerList handlers = new HandlerList(); + /** + * The Holder. + */ + public final AccountHolder holder; + /** + * Account balance. + */ + public final long balance; + + /** + * Instantiates a new Calculate start balance event. + * + * @param holder the holder + */ + public AccountBalanceChangeEvent(AccountHolder holder, long balance) { + super(!Bukkit.getServer().isPrimaryThread()); + + this.holder = holder; + this.balance = balance; + } + + /** + * Gets handler list. + * + * @return the handler list + */ + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return handlers; + } + + @NotNull + @Override + public HandlerList getHandlers() { + return handlers; + } + + public AccountHolder getHolder() { + return holder; + } + +} diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index dbab486..0527395 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -84,6 +84,7 @@ public void onSignChange(SignChangeEvent event) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onSignBreak(BlockDestroyEvent event) { if (Tag.SIGNS.isTagged(event.getBlock().getType())) { + //TODO: call event Gringotts.instance.getDao().deleteAccountChest( event.getBlock().getWorld().getName(), event.getBlock().getX(), @@ -105,6 +106,7 @@ public void onInventoryClose(InventoryCloseEvent event) { chest.setCachedBalance(chest.balance(true)); Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -117,6 +119,7 @@ public void onInventoryMoveItem(InventoryMoveItemEvent event) { public void run() { chest.setCachedBalance(chest.balance(true)); Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); } }.runTask(Gringotts.instance); } @@ -129,6 +132,7 @@ public void run() { public void run() { chest.setCachedBalance(chest.balance(true)); Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); } }.runTask(Gringotts.instance); } @@ -145,6 +149,7 @@ public void onDispenseEvent(BlockDispenseEvent event) { public void run() { chest.setCachedBalance(chest.balance(true)); Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); } }.runTask(Gringotts.instance); } @@ -162,6 +167,11 @@ public void onSignEdit(PlayerOpenSignEvent event) { } } + @EventHandler + public void onAccountBalanceChange(AccountBalanceChangeEvent event) { + Gringotts.instance.getLogger().info("Balance change: " + event.holder.getId()); + } + /** * Get the AccountChest associated with this {@link InventoryHolder} * @param holder From 47f8116b7d64abba3f20d64f1ce65a29be2a8adf Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sun, 8 Dec 2024 13:27:45 +0100 Subject: [PATCH 02/13] wip: AccountBalanceChangeEvent - implement toString --- .../gestern/gringotts/event/AccountBalanceChangeEvent.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java b/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java index c4a4e27..4a33575 100644 --- a/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java +++ b/src/main/java/org/gestern/gringotts/event/AccountBalanceChangeEvent.java @@ -55,4 +55,9 @@ public AccountHolder getHolder() { return holder; } + @Override + public String toString() { + return this.getClass().getSimpleName() + "{holder=" + this.holder.getId() + ",value=" + this.balance + "}"; + } + } From efd690327691ddd17018a5d356b174e2ef162281 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sun, 8 Dec 2024 20:39:55 +0100 Subject: [PATCH 03/13] wip: AccountBalanceChangeEvent - fire event only if update happens --- .../gringotts/event/AccountListener.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index a622576..867e62b 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -83,7 +83,7 @@ public void onSignChange(SignChangeEvent event) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onSignBreak(BlockDestroyEvent event) { if (Tag.SIGNS.isTagged(event.getBlock().getType())) { - //TODO: call event + //TODO: @Grooble, call event ? Balance Change ? PlayerVaultDestroyEvent ? Gringotts.instance.getDao().deleteAccountChest( event.getBlock().getWorld().getName(), event.getBlock().getX(), @@ -101,8 +101,9 @@ public void onInventoryClose(InventoryCloseEvent event) { if (chest == null) return; chest.setCachedBalance(chest.balance(true)); - Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); - Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + } } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @@ -114,8 +115,9 @@ public void onInventoryMoveItem(InventoryMoveItemEvent event) { @Override public void run() { chest.setCachedBalance(chest.balance(true)); - Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); - Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + } } }.runTask(Gringotts.instance); } @@ -127,8 +129,9 @@ public void run() { @Override public void run() { chest.setCachedBalance(chest.balance(true)); - Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); - Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + } } }.runTask(Gringotts.instance); } @@ -144,8 +147,9 @@ public void onDispenseEvent(BlockDispenseEvent event) { @Override public void run() { chest.setCachedBalance(chest.balance(true)); - Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance()); - Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + } } }.runTask(Gringotts.instance); } From 88d7ab1506e9dd1b76a6d40e53b3795c0575cb33 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sun, 8 Dec 2024 20:54:37 +0100 Subject: [PATCH 04/13] clean up debug --- .../java/org/gestern/gringotts/event/AccountListener.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index 867e62b..a05dd0c 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -167,11 +167,6 @@ public void onSignEdit(PlayerOpenSignEvent event) { } } - @EventHandler - public void onAccountBalanceChange(AccountBalanceChangeEvent event) { - Gringotts.instance.getLogger().info("Balance change: " + event.holder.getId()); - } - /** * Get the AccountChest associated with this {@link InventoryHolder} * @param holder From dfd3f896b19140a5f16162c0ddf32a7e31f47c90 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Mon, 16 Dec 2024 23:51:16 +0100 Subject: [PATCH 05/13] wip: AccountBalanceChangeEvent - fire event on account chest deletion --- .../gringotts/event/AccountListener.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index a05dd0c..9fdafd8 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -6,6 +6,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Tag; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; @@ -21,6 +22,7 @@ import org.gestern.gringotts.AccountChest; import org.gestern.gringotts.Configuration; import org.gestern.gringotts.Gringotts; +import org.gestern.gringotts.GringottsAccount; import org.gestern.gringotts.Util; import com.destroystokyo.paper.event.block.BlockDestroyEvent; @@ -83,13 +85,11 @@ public void onSignChange(SignChangeEvent event) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onSignBreak(BlockDestroyEvent event) { if (Tag.SIGNS.isTagged(event.getBlock().getType())) { - //TODO: @Grooble, call event ? Balance Change ? PlayerVaultDestroyEvent ? - Gringotts.instance.getDao().deleteAccountChest( - event.getBlock().getWorld().getName(), - event.getBlock().getX(), - event.getBlock().getY(), - event.getBlock().getZ() - ); + AccountChest chest = getAccountChestFromLocation(event.getBlock().getLocation()); + if (Gringotts.instance.getDao().deleteAccountChest(chest)) { + GringottsAccount account = chest.getAccount(); + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance())); + } } } @@ -173,13 +173,22 @@ public void onSignEdit(PlayerOpenSignEvent event) { * @return the {@link AccountChest} or null if none was found */ private AccountChest getAccountChestFromHolder(Inventory holder) { + return getAccountChestFromLocation(holder.getLocation()); + } + + /** + * Get the AccountChest from location + * @param location + * @return the {@link AccountChest} or null if none was found + */ + private AccountChest getAccountChestFromLocation(Location location) { for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) { if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded - if (chest.matchesLocation(holder.getLocation())) { + if (chest.matchesLocation(location)) { return chest; } } return null; - } + } } From 746eb0eeee116458b51d3bd7339bc8bb6704eb02 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Thu, 19 Dec 2024 23:43:10 +0100 Subject: [PATCH 06/13] fix onSignBreak NullPointerException on "InventoryHolder.getInventory()" because "holder" is null --- .../org/gestern/gringotts/AccountChest.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/AccountChest.java b/src/main/java/org/gestern/gringotts/AccountChest.java index 57f39f9..79fe34d 100755 --- a/src/main/java/org/gestern/gringotts/AccountChest.java +++ b/src/main/java/org/gestern/gringotts/AccountChest.java @@ -97,15 +97,18 @@ public Location chestLocation() { public boolean matchesLocation(Location loc) { if (this.containerLocations.isEmpty()) { - InventoryHolder holder = chest(); - - if (holder.getInventory() instanceof DoubleChestInventory) { - DoubleChestInventory doubleChest = (DoubleChestInventory) holder.getInventory(); + containerLocations.add(sign.getLocation()); - containerLocations.add(doubleChest.getLeftSide().getLocation()); - containerLocations.add(doubleChest.getRightSide().getLocation()); - } else { - containerLocations.add(holder.getInventory().getLocation()); + InventoryHolder holder = chest(); + if (holder != null) { + if (holder.getInventory() instanceof DoubleChestInventory) { + DoubleChestInventory doubleChest = (DoubleChestInventory) holder.getInventory(); + + containerLocations.add(doubleChest.getLeftSide().getLocation()); + containerLocations.add(doubleChest.getRightSide().getLocation()); + } else { + containerLocations.add(holder.getInventory().getLocation()); + } } } loc = loc.toBlockLocation(); From 34e57a1af55cee0b7c69f99437b00af93c95fc19 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Thu, 19 Dec 2024 23:58:01 +0100 Subject: [PATCH 07/13] fix onInventoryClose NullPointerException on "InventoryHolder.getInventory()" because "holder" is null --- .../gestern/gringotts/event/AccountListener.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index 9fdafd8..de74658 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -8,10 +8,12 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Tag; +import org.bukkit.block.Block; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockDispenseEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.inventory.InventoryCloseEvent; @@ -84,8 +86,17 @@ public void onSignChange(SignChangeEvent event) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onSignBreak(BlockDestroyEvent event) { - if (Tag.SIGNS.isTagged(event.getBlock().getType())) { - AccountChest chest = getAccountChestFromLocation(event.getBlock().getLocation()); + onSignBreak(event.getBlock()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onSignBreak(BlockBreakEvent event) { + onSignBreak(event.getBlock()); + } + + private void onSignBreak(Block block) { + if (Tag.SIGNS.isTagged(block.getType())) { + AccountChest chest = getAccountChestFromLocation(block.getLocation()); if (Gringotts.instance.getDao().deleteAccountChest(chest)) { GringottsAccount account = chest.getAccount(); Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance())); @@ -93,6 +104,7 @@ public void onSignBreak(BlockDestroyEvent event) { } } + @EventHandler(priority = EventPriority.MONITOR) public void onInventoryClose(InventoryCloseEvent event) { if (!Util.isValidInventory(event.getInventory().getType())) return; From 8f2c4f983d12b1b43ecddb52f75545a2573d892d Mon Sep 17 00:00:00 2001 From: GroobleDierne Date: Sun, 22 Dec 2024 13:12:54 +0100 Subject: [PATCH 08/13] Revert "fix onSignBreak NullPointerException on "InventoryHolder.getInventory()" because "holder" is null" This reverts commit 746eb0eeee116458b51d3bd7339bc8bb6704eb02. --- .../org/gestern/gringotts/AccountChest.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/AccountChest.java b/src/main/java/org/gestern/gringotts/AccountChest.java index 79fe34d..57f39f9 100755 --- a/src/main/java/org/gestern/gringotts/AccountChest.java +++ b/src/main/java/org/gestern/gringotts/AccountChest.java @@ -97,18 +97,15 @@ public Location chestLocation() { public boolean matchesLocation(Location loc) { if (this.containerLocations.isEmpty()) { - containerLocations.add(sign.getLocation()); - InventoryHolder holder = chest(); - if (holder != null) { - if (holder.getInventory() instanceof DoubleChestInventory) { - DoubleChestInventory doubleChest = (DoubleChestInventory) holder.getInventory(); - - containerLocations.add(doubleChest.getLeftSide().getLocation()); - containerLocations.add(doubleChest.getRightSide().getLocation()); - } else { - containerLocations.add(holder.getInventory().getLocation()); - } + + if (holder.getInventory() instanceof DoubleChestInventory) { + DoubleChestInventory doubleChest = (DoubleChestInventory) holder.getInventory(); + + containerLocations.add(doubleChest.getLeftSide().getLocation()); + containerLocations.add(doubleChest.getRightSide().getLocation()); + } else { + containerLocations.add(holder.getInventory().getLocation()); } } loc = loc.toBlockLocation(); From eef1b1f44a28fd766ce78971cb7287cf15c19d3e Mon Sep 17 00:00:00 2001 From: GroobleDierne Date: Sun, 22 Dec 2024 13:33:39 +0100 Subject: [PATCH 09/13] Fix getAccountChestFromLocation --- .../gestern/gringotts/event/AccountListener.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index de74658..65e3150 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -185,11 +185,18 @@ public void onSignEdit(PlayerOpenSignEvent event) { * @return the {@link AccountChest} or null if none was found */ private AccountChest getAccountChestFromHolder(Inventory holder) { - return getAccountChestFromLocation(holder.getLocation()); + for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) { + if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded + + if (chest.matchesLocation(holder.getLocation())) { + return chest; + } + } + return null; } /** - * Get the AccountChest from location + * Get the AccountChest from its sign's location * @param location * @return the {@link AccountChest} or null if none was found */ @@ -197,7 +204,7 @@ private AccountChest getAccountChestFromLocation(Location location) { for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) { if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded - if (chest.matchesLocation(location)) { + if (chest.sign.getLocation().equals(location)) { return chest; } } From 75dab396511bf895ff003aa252be7fb63037fa02 Mon Sep 17 00:00:00 2001 From: GroobleDierne Date: Sun, 22 Dec 2024 14:33:20 +0100 Subject: [PATCH 10/13] Perf and readability improvement --- .../gringotts/event/AccountListener.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index 65e3150..0dda6c7 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -18,7 +18,6 @@ import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryMoveItemEvent; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.scheduler.BukkitRunnable; import org.gestern.gringotts.AccountChest; @@ -96,7 +95,7 @@ public void onSignBreak(BlockBreakEvent event) { private void onSignBreak(Block block) { if (Tag.SIGNS.isTagged(block.getType())) { - AccountChest chest = getAccountChestFromLocation(block.getLocation()); + AccountChest chest = getAccountChestFromSign(block.getLocation()); if (Gringotts.instance.getDao().deleteAccountChest(chest)) { GringottsAccount account = chest.getAccount(); Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance())); @@ -109,7 +108,7 @@ private void onSignBreak(Block block) { public void onInventoryClose(InventoryCloseEvent event) { if (!Util.isValidInventory(event.getInventory().getType())) return; - AccountChest chest = getAccountChestFromHolder(event.getInventory()); + AccountChest chest = getAccountChestFromContainer(event.getInventory().getLocation()); if (chest == null) return; chest.setCachedBalance(chest.balance(true)); @@ -121,7 +120,7 @@ public void onInventoryClose(InventoryCloseEvent event) { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryMoveItem(InventoryMoveItemEvent event) { if (Util.isValidInventory(event.getSource().getType())) { - AccountChest chest = getAccountChestFromHolder(event.getSource()); + AccountChest chest = getAccountChestFromContainer(event.getSource().getLocation()); if (chest != null) { new BukkitRunnable() { @Override @@ -135,7 +134,7 @@ public void run() { } } if (event.getDestination() != null && Util.isValidInventory(event.getDestination().getType())) { - AccountChest chest = getAccountChestFromHolder(event.getDestination()); + AccountChest chest = getAccountChestFromContainer(event.getDestination().getLocation()); if (chest != null) { new BukkitRunnable() { @Override @@ -152,19 +151,17 @@ public void run() { @EventHandler public void onDispenseEvent(BlockDispenseEvent event) { - if (event.getBlock().getState() instanceof InventoryHolder) { - AccountChest chest = getAccountChestFromHolder(((InventoryHolder) event.getBlock().getState()).getInventory()); - if (chest != null) { - new BukkitRunnable() { - @Override - public void run() { - chest.setCachedBalance(chest.balance(true)); - if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) { - Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); - } + AccountChest chest = getAccountChestFromContainer(event.getBlock().getLocation()); + if (chest != null) { + new BukkitRunnable() { + @Override + public void run() { + chest.setCachedBalance(chest.balance(true)); + if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); } - }.runTask(Gringotts.instance); - } + } + }.runTask(Gringotts.instance); } } @@ -184,11 +181,11 @@ public void onSignEdit(PlayerOpenSignEvent event) { * @param holder * @return the {@link AccountChest} or null if none was found */ - private AccountChest getAccountChestFromHolder(Inventory holder) { + private AccountChest getAccountChestFromContainer(Location location) { for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) { if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded - if (chest.matchesLocation(holder.getLocation())) { + if (chest.matchesLocation(location)) { return chest; } } @@ -200,7 +197,7 @@ private AccountChest getAccountChestFromHolder(Inventory holder) { * @param location * @return the {@link AccountChest} or null if none was found */ - private AccountChest getAccountChestFromLocation(Location location) { + private AccountChest getAccountChestFromSign(Location location) { for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) { if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded From 288e0b8dff4791985f5c2b20741214c3382994d5 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sun, 22 Dec 2024 15:42:41 +0100 Subject: [PATCH 11/13] fix onSignBreak NullPointerException Cannot read field "sign" because "chest" is null --- src/main/java/org/gestern/gringotts/event/AccountListener.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index 0dda6c7..c694e92 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -96,6 +96,8 @@ public void onSignBreak(BlockBreakEvent event) { private void onSignBreak(Block block) { if (Tag.SIGNS.isTagged(block.getType())) { AccountChest chest = getAccountChestFromSign(block.getLocation()); + if (chest == null) return; + if (Gringotts.instance.getDao().deleteAccountChest(chest)) { GringottsAccount account = chest.getAccount(); Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance())); From 63ee513bb06372623dd960918bb1a9dff2c995e9 Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Sun, 22 Dec 2024 15:52:00 +0100 Subject: [PATCH 12/13] fire AccountBalanceChangeEvent on vault creation when chest not empty --- .../org/gestern/gringotts/event/AccountListener.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index c694e92..f015a6e 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -80,6 +80,18 @@ public void onSignChange(SignChangeEvent event) { final VaultCreationEvent creation = new PlayerVaultCreationEvent(type, event); Bukkit.getServer().getPluginManager().callEvent(creation); + + if (creation.isValid()) { + new BukkitRunnable() { + @Override + public void run() { + AccountChest chest = getAccountChestFromSign(event.getBlock().getLocation()); + if (chest.balance(true) > 0) { + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance())); + } + } + }.runTask(Gringotts.instance); + } } } From 5a68a0c6fedc5a940fa1b04586a6bd248e9722be Mon Sep 17 00:00:00 2001 From: Clem Fern Date: Mon, 23 Dec 2024 19:27:29 +0100 Subject: [PATCH 13/13] fire AccountBalanceChangeEvent when one of double chest vault is destroyed --- .../org/gestern/gringotts/AccountChest.java | 8 +++++++ .../gringotts/event/AccountListener.java | 21 +++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gestern/gringotts/AccountChest.java b/src/main/java/org/gestern/gringotts/AccountChest.java index 57f39f9..9a35382 100755 --- a/src/main/java/org/gestern/gringotts/AccountChest.java +++ b/src/main/java/org/gestern/gringotts/AccountChest.java @@ -96,6 +96,14 @@ public Location chestLocation() { } public boolean matchesLocation(Location loc) { + return matchesLocation(loc, false); + } + + public boolean matchesLocation(Location loc, boolean updateContainerLocations) { + if (updateContainerLocations) { + this.containerLocations.clear(); + } + if (this.containerLocations.isEmpty()) { InventoryHolder holder = chest(); diff --git a/src/main/java/org/gestern/gringotts/event/AccountListener.java b/src/main/java/org/gestern/gringotts/event/AccountListener.java index f015a6e..c3999b4 100755 --- a/src/main/java/org/gestern/gringotts/event/AccountListener.java +++ b/src/main/java/org/gestern/gringotts/event/AccountListener.java @@ -96,16 +96,16 @@ public void run() { } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onSignBreak(BlockDestroyEvent event) { - onSignBreak(event.getBlock()); + public void onBlockDestroy(BlockDestroyEvent event) { + onBlockBreak(event.getBlock()); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onSignBreak(BlockBreakEvent event) { - onSignBreak(event.getBlock()); + public void onBlockBreak(BlockBreakEvent event) { + onBlockBreak(event.getBlock()); } - private void onSignBreak(Block block) { + private void onBlockBreak(Block block) { if (Tag.SIGNS.isTagged(block.getType())) { AccountChest chest = getAccountChestFromSign(block.getLocation()); if (chest == null) return; @@ -114,6 +114,11 @@ private void onSignBreak(Block block) { GringottsAccount account = chest.getAccount(); Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance())); } + } else if (Util.isValidContainer(block.getType())) { + AccountChest chest = getAccountChestFromContainer(block.getLocation(), true); + if (chest == null) return; + GringottsAccount account = chest.getAccount(); + Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance())); } } @@ -196,10 +201,14 @@ public void onSignEdit(PlayerOpenSignEvent event) { * @return the {@link AccountChest} or null if none was found */ private AccountChest getAccountChestFromContainer(Location location) { + return getAccountChestFromContainer(location, false); + } + + private AccountChest getAccountChestFromContainer(Location location, boolean updateContainerLocations) { for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) { if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded - if (chest.matchesLocation(location)) { + if (chest.matchesLocation(location, updateContainerLocations)) { return chest; } }