将数据输入到HashMap

时间:2014-06-05 21:26:53

标签: java minecraft bukkit

我正在使用Bukkit Api制作Minecraft服务器插件。

基本上当我的服务器停止时,我使用onDisable()将两个hashmap内容存储到配置中,然后当服务器启动时,我使用onEnable()从配置中获取该信息并将其放回HashMaps中。这不起作用。

以下是我的方法:saveBans位于onDisable()中,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();
}

1 个答案:

答案 0 :(得分:0)

我假设:

  1. 在指责HashMap之前,您已经测试过ArrayList中的loadBans不为空。
  2. 您还检查了是否正在调用这些方法loadBanssaveBans
  3. 您已手动打开配置文件以查看其中的内容。
  4. 您可以使用Debug - 模式或广泛使用System.out.println(...)进行调试。只要某些内容为null或空,但它不应该是 - 请尝试返回并查看它不是null或空的位置。