<commit_before>package com.github.games647.changeskin.bungee.listener;

import com.github.games647.changeskin.bungee.ChangeSkinBungee;
import com.github.games647.changeskin.core.model.UserPreference;
import com.github.games647.changeskin.core.model.skin.SkinModel;

import java.util.Optional;

import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.AsyncEvent;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;

public class ConnectListener extends AbstractSkinListener {

    public ConnectListener(ChangeSkinBungee plugin) {
        super(plugin);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onPostLogin(LoginEvent loginEvent) {
        if (loginEvent.isCancelled() || !core.getConfig().getStringList("server-blacklist").isEmpty()) {
            return;
        }

        PendingConnection connection = loginEvent.getConnection();
        String playerName = connection.getName().toLowerCase();

        if (core.getConfig().getBoolean("restoreSkins")) {
            refetchSkin(connection, playerName, loginEvent);
        }
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerLogin(PostLoginEvent postLoginEvent) {
        ProxiedPlayer player = postLoginEvent.getPlayer();

        //updates to the chosen one
        UserPreference preferences = plugin.getLoginSession(player.getPendingConnection());
        if (preferences == null) {
            return;
        }

        Optional<SkinModel> optSkin = preferences.getTargetSkin();
        if (!optSkin.isPresent()) {
            setRandomSkin(preferences, player);
        }
    }

    @EventHandler
    public void onDisconnect(PlayerDisconnectEvent disconnectEvent) {
        PendingConnection pendingConnection = disconnectEvent.getPlayer().getPendingConnection();
        UserPreference preference = plugin.endSession(pendingConnection);

        if (preference != null) {
            save(preference);
        }
    }

    private void refetchSkin(final PendingConnection conn, final String playerName, final AsyncEvent<?> loginEvent) {
        loginEvent.registerIntent(plugin);

        ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> {
            try {
                UserPreference preferences = plugin.getStorage().getPreferences(conn.getUniqueId());
                plugin.startSession(conn, preferences);

                Optional<SkinModel> optSkin = preferences.getTargetSkin();
                if (optSkin.isPresent()) {
                    SkinModel targetSkin = optSkin.get();
                    if (!preferences.isKeepSkin()) {
                        targetSkin = core.checkAutoUpdate(targetSkin);
                    }

                    preferences.setTargetSkin(targetSkin);
                } else {
                    refetchSkin(playerName, preferences);
                }
            } finally {
                loginEvent.completeIntent(plugin);
            }
        });
    }
}<commit_msg>Fix default skin loading if restoreSkins is disabled in BungeeCord (Fixes #107)<commit_after>package com.github.games647.changeskin.bungee.listener;

import com.github.games647.changeskin.bungee.ChangeSkinBungee;
import com.github.games647.changeskin.core.model.UserPreference;
import com.github.games647.changeskin.core.model.skin.SkinModel;

import java.util.Optional;

import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.AsyncEvent;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;

public class ConnectListener extends AbstractSkinListener {

    public ConnectListener(ChangeSkinBungee plugin) {
        super(plugin);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onPostLogin(LoginEvent loginEvent) {
        if (loginEvent.isCancelled() || !core.getConfig().getStringList("server-blacklist").isEmpty()) {
            return;
        }

        PendingConnection connection = loginEvent.getConnection();
        String playerName = connection.getName().toLowerCase();

        loadProfile(connection, playerName, loginEvent);
    }

    @EventHandler(priority = EventPriority.HIGH)
    public void onPlayerLogin(PostLoginEvent postLoginEvent) {
        ProxiedPlayer player = postLoginEvent.getPlayer();

        //updates to the chosen one
        UserPreference preferences = plugin.getLoginSession(player.getPendingConnection());
        if (preferences == null) {
            return;
        }

        Optional<SkinModel> optSkin = preferences.getTargetSkin();
        if (!optSkin.isPresent()) {
            setRandomSkin(preferences, player);
        }
    }

    @EventHandler
    public void onDisconnect(PlayerDisconnectEvent disconnectEvent) {
        PendingConnection pendingConnection = disconnectEvent.getPlayer().getPendingConnection();
        UserPreference preference = plugin.endSession(pendingConnection);

        if (preference != null) {
            save(preference);
        }
    }

    private void loadProfile(final PendingConnection conn, final String playerName, final AsyncEvent<?> loginEvent) {
        loginEvent.registerIntent(plugin);

        ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> {
            try {
                UserPreference preferences = plugin.getStorage().getPreferences(conn.getUniqueId());
                plugin.startSession(conn, preferences);

                Optional<SkinModel> optSkin = preferences.getTargetSkin();
                if (optSkin.isPresent()) {
                    SkinModel targetSkin = optSkin.get();
                    if (!preferences.isKeepSkin()) {
                        targetSkin = core.checkAutoUpdate(targetSkin);
                    }

                    preferences.setTargetSkin(targetSkin);
                } else if (core.getConfig().getBoolean("restoreSkins")) {
                    refetchSkin(playerName, preferences);
                }
            } finally {
                loginEvent.completeIntent(plugin);
            }
        });
    }
}
{'fraction_non_alphanumeric': 0.09981768459434823, 'fraction_numerical': 0.0068368277119416595, 'mean_word_length': 2.8008083140877598, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6991794', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1785, 'n_words': 287}