|
4 | 4 | import com.google.gson.JsonParser; |
5 | 5 | import com.zaxxer.hikari.HikariConfig; |
6 | 6 | import com.zaxxer.hikari.HikariDataSource; |
| 7 | +import net.kyori.adventure.text.Component; |
7 | 8 | import org.bukkit.NamespacedKey; |
8 | 9 | import org.bukkit.OfflinePlayer; |
9 | 10 | import org.bukkit.command.CommandExecutor; |
10 | 11 | import org.bukkit.command.CommandSender; |
11 | 12 | import org.bukkit.command.PluginCommand; |
| 13 | +import org.bukkit.entity.Player; |
12 | 14 | import org.bukkit.event.Listener; |
13 | 15 | import org.bukkit.plugin.java.JavaPlugin; |
14 | 16 | import org.bukkit.scheduler.BukkitTask; |
@@ -105,6 +107,11 @@ public void onEnable() { |
105 | 107 | @Override |
106 | 108 | public void onDisable() { |
107 | 109 | dbSource.close(); |
| 110 | + if (this.invoiceNotificationTask != null) { |
| 111 | + final int taskId = this.invoiceNotificationTask.getTaskId(); |
| 112 | + getServer().getScheduler().cancelTask(taskId); |
| 113 | + this.invoiceNotificationTask = null; |
| 114 | + } |
108 | 115 | } |
109 | 116 |
|
110 | 117 | /** |
@@ -162,6 +169,7 @@ public static void reload() { |
162 | 169 | getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion); |
163 | 170 | })); |
164 | 171 | getInstance().startInterestTimer(); |
| 172 | + getInstance().setupInvoiceNotificationTimer(); |
165 | 173 | } |
166 | 174 |
|
167 | 175 | /** |
@@ -247,6 +255,20 @@ private void startInterestTimer() { |
247 | 255 | }, 0L, 20L*60); |
248 | 256 | } |
249 | 257 |
|
| 258 | + private @Nullable BukkitTask invoiceNotificationTask = null; |
| 259 | + |
| 260 | + private void setupInvoiceNotificationTimer() { |
| 261 | + if (config().invoiceNotifyInterval() <= 0) return; |
| 262 | + this.invoiceNotificationTask = getServer().getScheduler().runTaskTimerAsynchronously(this, () -> { |
| 263 | + for (final @NotNull Player player : getServer().getOnlinePlayers()) { |
| 264 | + final @NotNull Optional<@NotNull Component> message = BankAccounts.getInstance().config().messagesInvoiceNotify(Invoice.countUnpaid(player)); |
| 265 | + if (message.isEmpty()) continue; |
| 266 | + if (player.hasPermission(Permissions.INVOICE_NOTIFY) && Invoice.countUnpaid(player) > 0) |
| 267 | + player.sendMessage(message.get()); |
| 268 | + } |
| 269 | + }, config().invoiceNotifyInterval() * 20L, config().invoiceNotifyInterval() * 20L); |
| 270 | + } |
| 271 | + |
250 | 272 | private void interestPayment(final @NotNull Account account, final @NotNull BigDecimal amount, final double rate, final @NotNull Account serverAccount) { |
251 | 273 | if (account.balance == null) return; |
252 | 274 | if (account.id.equals(serverAccount.id)) return; |
|
0 commit comments