有没有办法创建特定于玩家的倒计时,当玩家在 Java 离开时停止?
Is there a way to create a player specific countdown, that stops when the player leaves in Java?
我正在寻找一种方法来为 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<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 中的 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<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);