Search code examples
javaminecraftcountdownbukkit

Is there a way to create a player specific countdown, that stops when the player leaves in Java?


I'm looking for a way to create a player-specific countdown for my BankSystem Plugin in Java.

Currently everybody gets their interest at the same time, because I'm using a Bukkit scheduler.

Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {

    @Override
    public void run() {
        try {
            Statement stmt = DatabaseManager.getCon().createStatement();
            String sql = ("SELECT uuid, money FROM Accounts");
            stmt.executeUpdate("USE " + ConfigManager.getConf().getString("Database.DBName"));
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                uids.add(rs.getString(1));
                money.put(rs.getString(1), rs.getInt(2));
            }
            if (!ConfigManager.getConf().getBoolean("Settings.PayInterestOffline")) {
                try {
                    for (String uid : uids) {
                        Player pl = Bukkit.getPlayer(UUID.fromString(uid));
                        if (pl == null) {
                            uids.remove(IndexIdentifier.getIndex(uid, uids));
                            money.remove(uid);
                        }
                    }
                } catch (Exception e) {
                }

            }
            for (int i = 0; i < uids.size(); i++) {
                try {
                    String puid = uids.get(i);
                    double doubleMoney = money.get(puid);
                    if (doubleMoney > ConfigManager.getConf().getInt("Settings.MaximumMoney")) {
                        continue;
                    } else {
                        doubleMoney = (((doubleMoney / 100) * percent) + doubleMoney);
                        int intMoney = (int) Math.ceil(doubleMoney);
                        stmt.executeUpdate("UPDATE Accounts SET money = " + intMoney + " WHERE uuid = '" + puid + "';");
                        Player p = Bukkit.getPlayer(UUID.fromString(puid));
                        if (p.isOnline() && p != null) {
                            p.sendMessage(
                                    "§aYou've credited an interest of §6" + (int) Math.ceil((intMoney / 100) * percent)
                                            + ".0 " + ConfigManager.getConf().getString("Settings.currency"));
                        }
                    }
                    money.remove(puid);
                    uids.remove(i);
                } catch (NullPointerException e) {
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}, 0, period);

Is there a way, to create a countdown for every online player. That means that the countdown stops, when the player leaves the server and resumes after rejoining.


Solution

  • You can associate an integer with a player in a hashmap:

    HashMap<UUID, Integer> playersAndTimes = new HashMap<>();

    To add players to the HashMap when you want to start the countdown:

    playersAndTimes.put(player.getUniqueId(), time)

    Now you just need to run this function when the plugin enables which loops through every player online, if they are in the HashMap (have a countdown on them) it will remove 1 every second from their value:

    Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(Main.class), new Runnable() {
        @Override
        public void run() {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (playersAndTimes.containsKey(player.getUniqueId())) {
                    if (playersAndTimes.get(player.getUniqueId()) >= 1) {
                        playersAndTimes.put(player.getUniqueId(), playersAndTimes.get(player.getUniqueId()) - 1);
                    } else {
                        //The Player's Time Has Expired As The Number Associated With Their UUID In The Hashmap Is Now Equal To 0.
                        //DO SOMETHING
                    }
                }
            }
        }
    }, 0, 20);