Search in sources :

Example 1 with CooldownStorage

use of skinsrestorer.shared.storage.CooldownStorage in project SkinsRestorerX by DoNotSpamPls.

the class SkinsRestorer method onEnable.

public void onEnable() {
    ConsoleCommandSender console = getServer().getConsoleSender();
    @SuppressWarnings("unused") MetricsLite metrics = new MetricsLite(this);
    SpigetUpdate updater = new SpigetUpdate(this, 2124);
    updater.setVersionComparator(VersionComparator.EQUAL);
    updater.setVersionComparator(VersionComparator.SEM_VER);
    instance = this;
    factory = new UniversalSkinFactory();
    Config.load(getResource("config.yml"));
    Locale.load();
    console.sendMessage("§e[§2SkinsRestorer§e] §aDetected Minecraft §e" + ReflectionUtil.serverVersion + "§a, using §e" + factory.getClass().getSimpleName() + "§a.");
    // Detect ChangeSkin
    if (getServer().getPluginManager().getPlugin("ChangeSkin") != null) {
        console.sendMessage("§e[§2SkinsRestorer§e] §cWe have detected ChangeSkin on your server, disabling SkinsRestorer.");
        Bukkit.getPluginManager().disablePlugin(this);
    }
    // Bungeecord stuff
    try {
        bungeeEnabled = getServer().spigot().getConfig().getBoolean("settings.bungeecord");
    } catch (Exception e) {
        bungeeEnabled = false;
    }
    if (bungeeEnabled) {
        Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
        Bukkit.getMessenger().registerIncomingPluginChannel(this, "SkinsRestorer", (channel, player, message) -> {
            if (!channel.equals("SkinsRestorer"))
                return;
            Bukkit.getScheduler().runTaskAsynchronously(getInstance(), () -> {
                DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
                try {
                    String subchannel = in.readUTF();
                    if (subchannel.equalsIgnoreCase("SkinUpdate")) {
                        try {
                            factory.applySkin(player, SkinStorage.createProperty(in.readUTF(), in.readUTF(), in.readUTF()));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        factory.updateSkin(player);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        });
        if (Config.UPDATER_ENABLED) {
            updater.checkForUpdate(new UpdateCallback() {

                @Override
                public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) {
                    if (hasDirectDownload) {
                        console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    | SkinsRestorer |");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    |---------------|");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    |  §eBungee Mode§a  |");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                        console.sendMessage("§e[§2SkinsRestorer§e] §b    Current version: §c" + getVersion());
                        console.sendMessage("§e[§2SkinsRestorer§e]     A new version is available! Downloading it now...");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                        if (updater.downloadUpdate()) {
                            console.sendMessage("§e[§2SkinsRestorer§e] Update downloaded successfully, it will be applied on the next restart.");
                        } else {
                            // Update failed
                            console.sendMessage("§e[§2SkinsRestorer§e] §cCould not download the update, reason: " + updater.getFailReason());
                        }
                    }
                }

                @Override
                public void upToDate() {
                    console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    | SkinsRestorer |");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    |---------------|");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    |  §eBungee Mode§a  |");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                    console.sendMessage("§e[§2SkinsRestorer§e] §b    Current version: §a" + getVersion());
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    This is the latest version!");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                }
            });
        }
        return;
    }
    // Initialise MySQL
    if (Config.USE_MYSQL)
        SkinStorage.init(mysql = new MySQL(Config.MYSQL_HOST, Config.MYSQL_PORT, Config.MYSQL_DATABASE, Config.MYSQL_USERNAME, Config.MYSQL_PASSWORD));
    else
        SkinStorage.init(getDataFolder());
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new CooldownStorage(), 0, 20);
    // Commands
    getCommand("skinsrestorer").setExecutor(new SrCommand());
    getCommand("skin").setExecutor(new SkinCommand());
    getCommand("skins").setExecutor(new GUICommand());
    Bukkit.getPluginManager().registerEvents(new SkinsGUI(), this);
    Bukkit.getPluginManager().registerEvents(new Listener() {

        // LoginEvent happens on attemptLogin so its the best place to set the skin
        @EventHandler
        public void onLogin(PlayerJoinEvent e) {
            Bukkit.getScheduler().runTaskAsynchronously(SkinsRestorer.getInstance(), () -> {
                try {
                    if (Config.DISABLE_ONJOIN_SKINS) {
                        // shouldn't it just skip it if it's true?
                        return;
                    }
                    if (Config.DEFAULT_SKINS_ENABLED)
                        if (SkinStorage.getPlayerSkin(e.getPlayer().getName()) == null) {
                            List<String> skins = Config.DEFAULT_SKINS;
                            int randomNum = (int) (Math.random() * skins.size());
                            factory.applySkin(e.getPlayer(), SkinStorage.getOrCreateSkinForPlayer(skins.get(randomNum)));
                            return;
                        }
                    factory.applySkin(e.getPlayer(), SkinStorage.getOrCreateSkinForPlayer(e.getPlayer().getName()));
                } catch (SkinRequestException ex) {
                    ex.printStackTrace();
                }
            });
        }
    }, this);
    Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
        if (Config.UPDATER_ENABLED) {
            updater.checkForUpdate(new UpdateCallback() {

                @Override
                public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) {
                    if (hasDirectDownload) {
                        console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    | SkinsRestorer |");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                        console.sendMessage("§e[§2SkinsRestorer§e] §b    Current version: §c" + getVersion());
                        console.sendMessage("§e[§2SkinsRestorer§e]     A new version is available! Downloading it now...");
                        console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                        if (updater.downloadUpdate()) {
                            console.sendMessage("§e[§2SkinsRestorer§e] Update downloaded successfully, it will be applied on the next restart.");
                        } else {
                            // Update failed
                            console.sendMessage("§e[§2SkinsRestorer§e] §cCould not download the update, reason: " + updater.getFailReason());
                        }
                    }
                }

                @Override
                public void upToDate() {
                    console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    | SkinsRestorer |");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    +===============+");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                    console.sendMessage("§e[§2SkinsRestorer§e] §b    Current version: §a" + getVersion());
                    console.sendMessage("§e[§2SkinsRestorer§e] §a    This is the latest version!");
                    console.sendMessage("§e[§2SkinsRestorer§e] §a----------------------------------------------");
                }
            });
        }
        if (Config.DEFAULT_SKINS_ENABLED)
            for (String skin : Config.DEFAULT_SKINS) try {
                SkinStorage.setSkinData(skin, MojangAPI.getSkinProperty(MojangAPI.getUUID(skin)));
            } catch (SkinRequestException e) {
                if (SkinStorage.getSkinData(skin) == null)
                    console.sendMessage("§e[§2SkinsRestorer§e] §cDefault Skin '" + skin + "' request error: " + e.getReason());
            }
    });
}
Also used : Listener(org.bukkit.event.Listener) EventHandler(org.bukkit.event.EventHandler) SkinRequestException(skinsrestorer.shared.utils.MojangAPI.SkinRequestException) MetricsLite(org.bstats.bukkit.MetricsLite) MySQL(skinsrestorer.shared.utils.MySQL) SrCommand(skinsrestorer.bukkit.commands.SrCommand) GUICommand(skinsrestorer.bukkit.commands.GUICommand) SkinCommand(skinsrestorer.bukkit.commands.SkinCommand) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) SpigetUpdate(org.inventivetalent.update.spiget.SpigetUpdate) SkinRequestException(skinsrestorer.shared.utils.MojangAPI.SkinRequestException) IOException(java.io.IOException) UpdateCallback(org.inventivetalent.update.spiget.UpdateCallback) CooldownStorage(skinsrestorer.shared.storage.CooldownStorage) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) UniversalSkinFactory(skinsrestorer.bukkit.skinfactory.UniversalSkinFactory)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 MetricsLite (org.bstats.bukkit.MetricsLite)1 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)1 EventHandler (org.bukkit.event.EventHandler)1 Listener (org.bukkit.event.Listener)1 PlayerJoinEvent (org.bukkit.event.player.PlayerJoinEvent)1 SpigetUpdate (org.inventivetalent.update.spiget.SpigetUpdate)1 UpdateCallback (org.inventivetalent.update.spiget.UpdateCallback)1 GUICommand (skinsrestorer.bukkit.commands.GUICommand)1 SkinCommand (skinsrestorer.bukkit.commands.SkinCommand)1 SrCommand (skinsrestorer.bukkit.commands.SrCommand)1 UniversalSkinFactory (skinsrestorer.bukkit.skinfactory.UniversalSkinFactory)1 CooldownStorage (skinsrestorer.shared.storage.CooldownStorage)1 SkinRequestException (skinsrestorer.shared.utils.MojangAPI.SkinRequestException)1 MySQL (skinsrestorer.shared.utils.MySQL)1