日期解析操作使服务器崩溃

Date Parsing Operation Crashes Server

我正在创建一个 Spigot 插件,但是这段代码总是让我的服务器崩溃。

public List<String> getReports(OfflinePlayer p) {
    FileConfiguration config = getData(p);
    List<String> reports = new ArrayList<String>();
    for (String item : config.getConfigurationSection("reports").getKeys(false)) {
        try {
            Date created = dateFormat.parse(config.getString("reports." + item + ".date"));
            if (!reports.isEmpty()) {
                boolean added = false;
                for (int i = 0; i < reports.size(); i++) {
                    try {
                        String e = reports.get(i);
                        String edate = config.getString("reports." + e + ".date");
                        Date eday = dateFormat.parse(edate);
                        if (created.after(eday) || created.equals(eday)) {
                            reports.add(i, item);
                            added = true;
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
                if (!added) {
                    reports.add(item);
                }
            } else {
                reports.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return reports;
}

我知道无效的日期字符串会抛出 ParseException,但不会抛出任何错误。我什至尝试改为捕获 Throwable,但那没有任何作用。这些字符串是有效的,并且由我的插件正确导入(我检查过),并且此方法适用于 1-2 个项目,但由于某种原因,更多的项目会破坏它。

这是我的数据 (YAML):

reports:
  '1':
    type: Misc
    source: 6db6fde9-802f-4bef-a40a-fe516a7a8309
    reason: Idk
    date: 28/10/2016 19:09:16
  '2':
    type: Grief
    source: 6db6fde9-802f-4bef-a40a-fe516a7a8309
    reason: He destroyed my building! And he did really nasty stuff too meh, ban him now!
    date: 28/10/2016 19:14:56
  '3':
    type: Hacking
    source: 6db6fde9-802f-4bef-a40a-fe516a7a8309
    reason: He was flying like mad all over the place. This guy needs to be banned!
    date: 28/10/2016 19:29:33

该方法试图做的是从玩家的历史记录中导入数据并按倒序排列。我以前从未见过这样的事情发生,甚至没有记录这样的事情发生。我知道我一定是在做一件非常愚蠢的事情,但我需要帮助找出那是什么。

编辑: 这是我的日期格式字段

public final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.US);

编辑:尽管我已经说过没有抛出具体错误,但这里的堆栈跟踪没有帮助

[10:08:18] [Spigot Watchdog Thread/ERROR]: The server has stopped responding!
[10:08:18] [Spigot Watchdog Thread/ERROR]: Please report this to http://www.spigotmc.org/
[10:08:18] [Spigot Watchdog Thread/ERROR]: Be sure to include ALL relevant console errors and Minecraft crash reports
[10:08:18] [Spigot Watchdog Thread/ERROR]: Spigot version: git-Spigot-5f38d38-18fbb24 (MC: 1.8.8)
[10:08:18] [Spigot Watchdog Thread/ERROR]: ------------------------------
[10:08:18] [Spigot Watchdog Thread/ERROR]: Server thread dump (Look for plugins here before reporting to Spigot!):
[10:08:18] [Spigot Watchdog Thread/ERROR]: ------------------------------
[10:08:18] [Spigot Watchdog Thread/ERROR]: Current Thread: Server thread
[10:08:18] [Spigot Watchdog Thread/ERROR]:  PID: 16 | Suspended: false | Native: false | State: RUNNABLE
[10:08:18] [Spigot Watchdog Thread/ERROR]:  Stack:
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.text.DecimalFormat.parse(Unknown Source)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.text.SimpleDateFormat.subParse(Unknown Source)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.text.SimpleDateFormat.parse(Unknown Source)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.text.DateFormat.parse(Unknown Source)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      com.rictacius.punishSystem.system.History.getReports(History.java:155)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      com.rictacius.punishSystem.gui.PunishMenu.getReportsBar(PunishMenu.java:57)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      com.rictacius.punishSystem.gui.PunishMenu.getMainMenu(PunishMenu.java:41)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      com.rictacius.punishSystem.command.PunishCommand.onCommand(PunishCommand.java:62)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.PlayerConnectionUtils.run(SourceFile:13)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.util.concurrent.FutureTask.run(Unknown Source)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
[10:08:18] [Spigot Watchdog Thread/ERROR]:      java.lang.Thread.run(Unknown Source)

堆栈中最后一项与我有关的是这一行

日期 = dateFormat.parse(edate);

我的服务器不断崩溃的原因是内存不足。

(From: @Jon Skeet) The fact that you're looping over a collection and conditionally adding to it looks suspect to me. It's possible that it's just looping forever, because it adds an item, then finds that item and adds another, then adds another etc. The stack trace you've got may just be one that was taken within the infinite loop, or possibly on running out of memory. You should be adding more diagnostics so you can see how the code is executing.

循环遍历我要添加值的集合是不正确的,因为最终会发生的是对象将永远继续添加到同一位置。

相反,我应该做的是在填充数组后对其进行排序。一个简单的穿梭排序就可以解决这个问题。