package com.elmakers.mine.bukkit.magic;

import java.io.File;

import org.bukkit.Bukkit;

public class ConfigCheckTask implements Runnable {
    private final MagicController controller;
    private final File configCheckFile;

    private long lastModified = 0;

    public ConfigCheckTask(MagicController controller, String configCheckFile) {
        this.controller = controller;
        this.configCheckFile = new File(controller.getPlugin().getDataFolder(), configCheckFile);
    }

    @Override
    public void run() {
        if (configCheckFile.exists()) {
            long modified = configCheckFile.lastModified();
            if (lastModified != 0 && modified > lastModified) {
                controller.getLogger().info("Config check file modified, reloading configuration");
                controller.loadConfiguration(Bukkit.getConsoleSender());
            }
            lastModified = modified;
        }
    }
}

@@ -59,33 +59,292 @@
 le;%0A
-%0Aimport org.bukkit.Bukkit
+import java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Files;%0Aimport java.util.UUID;%0Aimport java.util.logging.Level;%0A%0Aimport org.bukkit.Bukkit;%0Aimport org.bukkit.ChatColor;%0Aimport org.bukkit.entity.Player;%0A%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.stream.JsonReader
 ;%0A%0Ap
@@ -516,16 +516,88 @@
 d = 0;%0A%0A
+    public static class Modified %7B%0A        public String userId;%0A    %7D%0A%0A
     publ
@@ -950,16 +950,56 @@
 fied();%0A
+            UUID modifiedUserId = null;%0A
         
@@ -1174,64 +1174,1523 @@
     
-controller.loadConfiguration(Bukkit.getConsoleSender());
+try %7B%0A                    Gson gson = new Gson();%0A                    JsonReader reader = new JsonReader(Files.newBufferedReader(configCheckFile.toPath(), StandardCharsets.UTF_8));%0A                    Modified modifiedData = gson.fromJson(reader, Modified.class);%0A                    if (modifiedData.userId != null && !modifiedData.userId.isEmpty()) %7B%0A                        modifiedUserId = UUID.fromString(modifiedData.userId);%0A                    %7D%0A                %7D catch (Exception ex) %7B%0A                    controller.getLogger().log(Level.WARNING, %22Error reading update file%22, ex);%0A                %7D%0A                controller.loadConfiguration(Bukkit.getConsoleSender(), true);%0A%0A                // The updating player should get their new spells%0A                final Player player = modifiedUserId == null ? null : Bukkit.getPlayer(modifiedUserId);%0A                final Mage mage = player == null ? null : controller.getRegisteredMage(player);%0A                if (player != null) %7B%0A                    Bukkit.getScheduler().runTask(controller.getPlugin(), new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A                            if (!player.hasPermission(%22Magic.notify%22)) %7B%0A                                player.sendMessage(ChatColor.AQUA + %22Spells reloaded.%22);%0A                            %7D%0A                            mage.deactivate();%0A                            mage.checkWand();%0A                        %7D%0A                    %7D);%0A                %7D
 %0A   

Allow a user id in the modified file, so we can force-update their wand and notify them