package cat.nyaa.nyaacore;

import cat.nyaa.nyaacore.component.IMessageQueue;
import cat.nyaa.nyaacore.component.NyaaComponent;
import cat.nyaa.nyaacore.configuration.NbtItemStack;
import cat.nyaa.nyaacore.http.client.HttpClient;
import cat.nyaa.nyaacore.timer.TimerManager;
import cat.nyaa.nyaacore.utils.ClickSelectionUtils;
import cat.nyaa.nyaacore.utils.OfflinePlayerUtils;
import org.bukkit.Bukkit;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.craftbukkit.v1_16_R2.util.CraftMagicNumbers;
import org.bukkit.plugin.java.JavaPlugin;

public class NyaaCoreLoader extends JavaPlugin {
    public static final String TARGET_MAPPING = "a69acbca3007d2ae1b4b69881f0ab9ad";
    private static NyaaCoreLoader instance;

    static {
        ConfigurationSerialization.registerClass(NbtItemStack.class);
    }

    public TimerManager timerManager;

    public static NyaaCoreLoader getInstance() {
        return instance;
    }

    @Override
    public void onLoad() {
        instance = this;
        LanguageRepository.initInternalMap(this);
        //timerManager = new TimerManager(this);
    }

    @Override
    public void onEnable() {
        try {
            boolean check = MappingChecker.check();
            if (!check) {
                getLogger().severe("CraftBukkit Mapping changed! Use with caution!");
            }
        } catch (NoSuchMethodError e) {
            getLogger().info("Cannot detect CraftBukkit Mapping!");
        }
        HttpClient.init(0);
        IMessageQueue.DefaultMessageQueue defaultMessageQueue = new IMessageQueue.DefaultMessageQueue();
        Bukkit.getPluginManager().registerEvents(defaultMessageQueue, this);
        Bukkit.getPluginManager().registerEvents(new ClickSelectionUtils._Listener(), this);
        Bukkit.getPluginManager().registerEvents(new OfflinePlayerUtils._Listener(), this);
        NyaaComponent.register(IMessageQueue.class, defaultMessageQueue);
        OfflinePlayerUtils.init();
        //timerManager.load();
    }

    @Override
    public void onDisable() {
        HttpClient.shutdown();
        //timerManager.save();
    }

    private static class MappingChecker {
        static boolean check() {
            String mappingsVersion = ((CraftMagicNumbers) CraftMagicNumbers.INSTANCE).getMappingsVersion();
            return TARGET_MAPPING.equals(mappingsVersion);
        }
    }
}

@@ -679,40 +679,40 @@
  = %22
-a69acbca3007d2ae1b4b69881f0ab9ad
+c2d5d7871edcc4fb0f81d18959c647af
 %22;%0A 
@@ -1312,54 +1312,80 @@
 re(%22
-CraftBukkit Mapping changed! Use with caution!
+Unsupported NMS Mapping version detected. Unexpected error may occurred.
 %22);%0A
@@ -1471,16 +1471,17 @@
 nfo(%22Can
+ 
 not dete
@@ -1499,16 +1499,59 @@
 kit 
+NMS 
 Mapping
-!
+ version. Unexpected error may occurred.
 %22);%0A

update nms mapping version