2014-06-05 46 views
-1

我正在使用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(); 
} 
+3

請將代碼簡化爲演示您的問題的最簡單情況。 –

+0

雖然 – Zahachos

+0

我確實需要幫助...... – Zahachos

回答

0

我認爲:

  1. 您已經測試,在loadBansArrayList不爲空 - 指責的HashMap之前。
  2. 您還檢查了是否正在調用這些方法loadBanssaveBans
  3. 您已手動打開配置文件以查看內容。

您可以使用Debug模式或廣泛使用System.out.println(...)進行調試。每當有東西是null或空的,而它不應該 - 試着回去看看它不是null或空。

+0

所有完成:/我發現了錯誤,現在實際上有一個輸入值,但時間乘以一個特定的屁股數:p(tes francais?) – Zahachos

+0

事實上,你的代碼的一部分說'currentTimeMillis',而另一個部分說'utilCooldown.cooldownMap.get(能力).seconds'例如,有沒有關係? (非,je ne suis pas francais) –