【发布时间】: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 <databasename>。相反,您应该将该数据库指定为连接字符串的一部分,或使用Connection.setCatalog(...)。另请参阅 MySQL Connector/J 文档中 6.2 Connection URL Syntax 上 database 下的注释。 -
@MarkRotteveel 我已经按照您的建议进行了更改。谢谢你的帖子。
标签: java minecraft countdown bukkit