gpt4 book ai didi

java - 有没有办法创建一个特定于玩家的倒计时,当玩家离开时停止在Java中?

转载 作者:行者123 更新时间:2023-12-02 09:10:47 26 4
gpt4 key购买 nike

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

目前每个人都会同时感兴趣,因为我正在使用 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);

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

最佳答案

您可以将整数与 HashMap 中的玩家关联起来:

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

当要开始倒计时时将玩家添加到 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);

关于java - 有没有办法创建一个特定于玩家的倒计时,当玩家离开时停止在Java中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59439062/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com