Skip to content

Commit 750d34f

Browse files
committed
send periodic notifications for unpaid invoices
1 parent bb92846 commit 750d34f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import com.google.gson.JsonParser;
55
import com.zaxxer.hikari.HikariConfig;
66
import com.zaxxer.hikari.HikariDataSource;
7+
import net.kyori.adventure.text.Component;
78
import org.bukkit.NamespacedKey;
89
import org.bukkit.OfflinePlayer;
910
import org.bukkit.command.CommandExecutor;
1011
import org.bukkit.command.CommandSender;
1112
import org.bukkit.command.PluginCommand;
13+
import org.bukkit.entity.Player;
1214
import org.bukkit.event.Listener;
1315
import org.bukkit.plugin.java.JavaPlugin;
1416
import org.bukkit.scheduler.BukkitTask;
@@ -105,6 +107,11 @@ public void onEnable() {
105107
@Override
106108
public void onDisable() {
107109
dbSource.close();
110+
if (this.invoiceNotificationTask != null) {
111+
final int taskId = this.invoiceNotificationTask.getTaskId();
112+
getServer().getScheduler().cancelTask(taskId);
113+
this.invoiceNotificationTask = null;
114+
}
108115
}
109116

110117
/**
@@ -162,6 +169,7 @@ public static void reload() {
162169
getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion);
163170
}));
164171
getInstance().startInterestTimer();
172+
getInstance().setupInvoiceNotificationTimer();
165173
}
166174

167175
/**
@@ -247,6 +255,20 @@ private void startInterestTimer() {
247255
}, 0L, 20L*60);
248256
}
249257

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+
250272
private void interestPayment(final @NotNull Account account, final @NotNull BigDecimal amount, final double rate, final @NotNull Account serverAccount) {
251273
if (account.balance == null) return;
252274
if (account.id.equals(serverAccount.id)) return;

0 commit comments

Comments
 (0)