Skip to content

Commit

Permalink
send periodic notifications for unpaid invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git committed Sep 15, 2024
1 parent bb92846 commit 750d34f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/pro/cloudnode/smp/bankaccounts/BankAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.google.gson.JsonParser;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import net.kyori.adventure.text.Component;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
Expand Down Expand Up @@ -105,6 +107,11 @@ public void onEnable() {
@Override
public void onDisable() {
dbSource.close();
if (this.invoiceNotificationTask != null) {
final int taskId = this.invoiceNotificationTask.getTaskId();
getServer().getScheduler().cancelTask(taskId);
this.invoiceNotificationTask = null;
}
}

/**
Expand Down Expand Up @@ -162,6 +169,7 @@ public static void reload() {
getInstance().getLogger().warning("Update details: https://modrinth.com/plugin/bankaccounts/version/" + latestVersion);
}));
getInstance().startInterestTimer();
getInstance().setupInvoiceNotificationTimer();
}

/**
Expand Down Expand Up @@ -247,6 +255,20 @@ private void startInterestTimer() {
}, 0L, 20L*60);
}

private @Nullable BukkitTask invoiceNotificationTask = null;

private void setupInvoiceNotificationTimer() {
if (config().invoiceNotifyInterval() <= 0) return;
this.invoiceNotificationTask = getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
for (final @NotNull Player player : getServer().getOnlinePlayers()) {
final @NotNull Optional<@NotNull Component> message = BankAccounts.getInstance().config().messagesInvoiceNotify(Invoice.countUnpaid(player));
if (message.isEmpty()) continue;
if (player.hasPermission(Permissions.INVOICE_NOTIFY) && Invoice.countUnpaid(player) > 0)
player.sendMessage(message.get());
}
}, config().invoiceNotifyInterval() * 20L, config().invoiceNotifyInterval() * 20L);
}

private void interestPayment(final @NotNull Account account, final @NotNull BigDecimal amount, final double rate, final @NotNull Account serverAccount) {
if (account.balance == null) return;
if (account.id.equals(serverAccount.id)) return;
Expand Down

0 comments on commit 750d34f

Please sign in to comment.