我正在使用Bukkit Api製作Minecraft Server插件。我使用onDisable()將兩個hashmap內容存儲到配置中,然後當服務器啓動時,我使用onEnable()從配置中獲取該信息,並將其放回到HashMaps這樣。這是行不通的。將數據輸入到HashMap中
這裏是我的方法:saveBans是在禁止接通()和loadBans在onEnable():
public class utilReloadSave {
static settingsmanager settings = settingsmanager.getInstance();
public static void saveBans() {
ArrayList<String> bans = new ArrayList<String>();
for (UUID play : Cooldown.cooldownPlayers.keySet()) {
settings.getConfig().set("bans." + play, Cooldown.getTime(play, "TempBan"));
bans.add(play.toString());
}
settings.getConfig().set("banlist", bans);
settings.saveConfig();
}
public static void loadBans() {
FileConfiguration config = settings.getConfig();
ArrayList<String> bans = (ArrayList<String>) config.getStringList("banlist");
for (String uuid : bans) {
Cooldown.cooldownPlayers.put(UUID.fromString(uuid), new utilCooldown(UUID.fromString(uuid), config.getInt("bans." + uuid), System.currentTimeMillis()));
}
config.set("bans", null);
config.set("banlist", null);
settings.saveConfig();
}
}
,似乎工作。問題是我使用onPlayerJoin事件:
@EventHandler
public void onLogin(PlayerLoginEvent e) {
Player play = e.getPlayer();
if (play.isBanned()) {
if (Cooldown.isCooling(play.getUniqueId(), "TempBan")) {
File player = new File(basic.plugin.getDataFolder() + "/players/" + play.getUniqueId() + ".yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(player);
List<String> list = config.getStringList("banned.temp.reason");
String reason = list.get(list.size()-1);
if (Cooldown.getTime(play.getUniqueId(), "TempBan") < 60000L) {
e.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.YELLOW + "" + ChatColor.BOLD + "You are still banned for " + Cooldown.getRemaining(play.getUniqueId(), "TempBan") + " seconds." + ChatColor.RED + " Reason: " + reason);
return;}
if (Cooldown.getTime(play.getUniqueId(), "TempBan") < 3600000L) {
e.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.YELLOW + "" + ChatColor.BOLD + "You are still banned for " + Cooldown.getRemaining(play.getUniqueId(), "TempBan") + " minutes." + ChatColor.RED + " Reason: " + reason);
return;}
if (Cooldown.getTime(play.getUniqueId(), "TempBan") < 86400000L) {
e.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.YELLOW + "" + ChatColor.BOLD + "You are still banned for " + Cooldown.getRemaining(play.getUniqueId(), "TempBan") + " hours." + ChatColor.RED + " Reason: " + reason);
return;}
e.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.YELLOW + "" + ChatColor.BOLD + "You are still banned for " + Cooldown.getRemaining(play.getUniqueId(), "TempBan") + " days." + ChatColor.RED + " Reason: " + reason);
return;
} else {
File player = new File(basic.plugin.getDataFolder() + "/players/" + play.getUniqueId() + ".yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(player);
List<String> list = config.getStringList("banned.perm.reason");
String reason = list.get(list.size()-1);
e.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.YELLOW + "" + ChatColor.BOLD + "You are permanentely banned! " + ChatColor.RED + "" + ChatColor.BOLD + "Reason: " + reason);
return;
}
}
}
這將檢查是否有東西在HashMap中卻沒有,因爲它返回else語句。
public static HashMap<UUID, utilCooldown> cooldownPlayers = new HashMap<UUID, utilCooldown>();
public static void add(UUID player, String ability, long seconds, long systime) {
if(!cooldownPlayers.containsKey(player)) cooldownPlayers.put(player, new utilCooldown(player));
if(isCooling(player, ability)) return;
cooldownPlayers.get(player);
utilCooldown.cooldownMap.put(ability, new utilCooldown(player, seconds * 1000, System.currentTimeMillis()));
}
public static boolean isCooling(UUID player, String ability) {
if(!cooldownPlayers.containsKey(player)) return false;
if(!utilCooldown.cooldownMap.containsKey(ability)) return false;
return true;
}
public static double getRemaining(UUID player, String ability) {
if(!cooldownPlayers.containsKey(player)) return 0.0;
if(!utilCooldown.cooldownMap.containsKey(ability)) return 0.0;
return utilTime.convert((utilCooldown.cooldownMap.get(ability).seconds + utilCooldown.cooldownMap.get(ability).systime) - System.currentTimeMillis(), TimeUnit.BEST, 1);
}
public static void removeCooldown(UUID key, String ability) {
if(!cooldownPlayers.containsKey(key)) {
return;
}
if(!utilCooldown.cooldownMap.containsKey(ability)) {
return;
}
utilCooldown.cooldownMap.remove(ability);
}
@SuppressWarnings("deprecation")
public static void handleCooldowns() {
if(cooldownPlayers.isEmpty()) {
return;
}
for(Iterator<UUID> it = cooldownPlayers.keySet().iterator(); it.hasNext();) {
UUID key = it.next();
cooldownPlayers.get(key);
for(Iterator<String> iter = utilCooldown.cooldownMap.keySet().iterator(); iter.hasNext();) {
String name = iter.next();
if(getRemaining(key, name) <= 0.0) {
OfflinePlayer p = Bukkit.getOfflinePlayer(key);
p.setBanned(false);
removeCooldown(key, name);
}
}
}
}
public static long getTime(UUID player, String ability) {
return (utilCooldown.cooldownMap.get(ability).seconds + utilCooldown.cooldownMap.get(ability).systime) - System.currentTimeMillis();
}
請將代碼簡化爲演示您的問題的最簡單情況。 –
雖然 – Zahachos
我確實需要幫助...... – Zahachos