/*
 * This file is part of Nucleus, licensed under the MIT License (MIT). See the LICENSE.txt file
 * at the root of this project for more details.
 */
package io.github.nucleuspowered.nucleus.modules.core.listeners;

import io.github.nucleuspowered.nucleus.config.loaders.UserConfigLoader;
import io.github.nucleuspowered.nucleus.internal.ListenerBase;
import io.github.nucleuspowered.nucleus.internal.interfaces.InternalNucleusUser;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.time.Instant;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class CoreListener extends ListenerBase {

    @Listener(order = Order.FIRST)
    public void onPlayerLogin(final ClientConnectionEvent.Login event) {
        Sponge.getScheduler().createAsyncExecutor(plugin).execute(() -> {
            try {
                InternalNucleusUser qsu = this.plugin.getUserLoader().getUser(event.getTargetUser());
                qsu.setLastLogin(Instant.now());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    @Listener(order = Order.DEFAULT)
    public void onPlayerJoin(final ClientConnectionEvent.Join event) {
        try {
            InternalNucleusUser qsu = this.plugin.getUserLoader().getUser(event.getTargetEntity());

            // If we have a location to send them to in the config, send them there now!
            Optional<Location<World>> olw = qsu.getLocationOnLogin();
            if (olw.isPresent()) {
                event.getTargetEntity().setLocation(olw.get());
                qsu.removeLocationOnLogin();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Listener
    public void onPlayerQuit(final ClientConnectionEvent.Disconnect event) {
        Sponge.getScheduler().createAsyncExecutor(plugin).schedule(() -> {
            UserConfigLoader ucl = this.plugin.getUserLoader();
            try {
                InternalNucleusUser qsu = this.plugin.getUserLoader().getUser(event.getTargetEntity());
                qsu.setOnLogout();
                ucl.purgeNotOnline();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 200, TimeUnit.MILLISECONDS);
    }
}

@@ -211,16 +211,62 @@
 eners;%0A%0A
+import io.github.nucleuspowered.nucleus.Util;%0A
 import i
@@ -326,24 +326,24 @@
 nfigLoader;%0A
-
 import io.gi
@@ -511,16 +511,68 @@
 Sponge;%0A
+import org.spongepowered.api.command.CommandSource;%0A
 import o
@@ -646,24 +646,81 @@
 vent.Order;%0A
+import org.spongepowered.api.event.game.GameReloadEvent;%0A
 import org.s
@@ -769,24 +769,117 @@
 ctionEvent;%0A
+import org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.format.TextColors;%0A
 import org.s
@@ -2732,11 +2732,488 @@
 );%0A    %7D
+%0A%0A    @Listener%0A    public void onGameReload(final GameReloadEvent event) %7B%0A        plugin.reload();%0A        CommandSource requester = event.getCause().first(CommandSource.class).orElse(Sponge.getServer().getConsole());%0A        requester.sendMessage(Text.of(TextColors.YELLOW, %22%5BNucleus%5D %22, Util.getTextMessageWithFormat(%22command.reload.one%22)));%0A        requester.sendMessage(Text.of(TextColors.YELLOW, %22%5BNucleus%5D %22, Util.getTextMessageWithFormat(%22command.reload.two%22)));%0A    %7D
 %0A%7D%0A

Add support for /sponge plugins reload