【问题标题】:Is there a way to create a player specific countdown, that stops when the player leaves in Java?有没有办法创建一个特定于玩家的倒计时,当玩家离开 Java 时该倒计时停止?
【发布时间】:2019-12-21 19:03:30
【问题描述】:

我正在寻找一种方法来为我的银行系统插件在 Java 中创建特定于玩家的倒计时。

目前每个人都同时感兴趣,因为我使用的是 Bukkit 调度程序。

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);

有没有办法为每个在线玩家创建倒计时。这意味着当玩家离开服务器并在重新加入后恢复时,倒计时停止。

【问题讨论】:

  • 与您的问题无关,但您不应使用 JDBC 执行 USE &lt;databasename&gt;。相反,您应该将该数据库指定为连接字符串的一部分,或使用Connection.setCatalog(...)。另请参阅 MySQL Connector/J 文档中 6.2 Connection URL Syntaxdatabase 下的注释。
  • @MarkRotteveel 我已经按照您的建议进行了更改。谢谢你的帖子。

标签: java minecraft countdown bukkit


【解决方案1】:

您可以将整数与哈希图中的玩家相关联:

HashMap&lt;UUID, Integer&gt; playersAndTimes = new HashMap&lt;&gt;();

当你想开始倒计时时将玩家添加到 HashMap:

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

现在你只需要在插件启用循环遍历每个在线玩家时运行此函数,如果他们在 HashMap 中(对他们进行倒计时),它将每秒从他们的值中删除 1:

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);

【讨论】:

  • 太棒了!经过一个小时的测试,它就像一个魅力。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多