package com.elmakers.mine.bukkit.magic;

import java.io.File;
import java.util.UUID;
import java.util.logging.Level;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

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

    private Long lastModified = null;

    public ConfigCheckTask(MagicController controller) {
        this.controller = controller;
        this.configCheckFile = new File(controller.getPlugin().getDataFolder(), "data/updated.yml");
    }

    @Override
    public void run() {
        if (configCheckFile.exists()) {
            long modified = configCheckFile.lastModified();
            UUID modifiedUserId = null;
            if (lastModified != null && modified > lastModified) {
                try {
                    YamlConfiguration updated = new YamlConfiguration();
                    updated.load(configCheckFile);
                    String userId = updated.getString("user_id");
                    if (userId != null && !userId.isEmpty()) {
                        modifiedUserId = UUID.fromString(userId);
                    }
                } catch (Exception ex) {
                    controller.getLogger().log(Level.WARNING, "Error reading update file", ex);
                }

                final Player player = modifiedUserId == null ? null : Bukkit.getPlayer(modifiedUserId);

                // Don't reload if the modifying player is not online
                if (player != null || modifiedUserId == null) {
                    controller.getLogger().info("Config check file modified, reloading configuration");
                    controller.loadConfiguration(Bukkit.getConsoleSender(), true);

                    // The updating player should get their new spells
                    final Mage mage = player == null ? null : controller.getRegisteredMage(player);
                    if (mage != null) {
                        Bukkit.getScheduler().runTask(controller.getPlugin(), new Runnable() {
                            @Override
                            public void run() {
                                if (!player.hasPermission("Magic.notify")) {
                                    player.sendMessage(ChatColor.AQUA + "Spells reloaded.");
                                }
                                mage.deactivate();
                                mage.checkWand();
                            }
                        });
                    }
                }
            }
            lastModified = modified;
        } else {
            lastModified = 0L;
        }
    }
}

@@ -1739,24 +1739,201 @@
 guration%22);%0A
+                    Bukkit.getScheduler().runTask(controller.getPlugin(), new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A        
             
@@ -2002,16 +2002,66 @@
 , true);
+%0A                        %7D%0A                    %7D);
 %0A%0A      

Fix error reloading sandbox configs