com.betafase.mcmanager.api.SpigotUpdateChecker.java Source code

Java tutorial

Introduction

Here is the source code for com.betafase.mcmanager.api.SpigotUpdateChecker.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.betafase.mcmanager.api;

import com.betafase.mcmanager.utils.spiget.ServerRequest;
import com.betafase.mcmanager.utils.spiget.SpigetPlugin;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

/**
 *
 * @author dbrosch
 */
public class SpigotUpdateChecker implements UpdateChecker {

    private final int spigot_id;
    private final Plugin plugin;

    public SpigotUpdateChecker(Plugin plugin, int spigot_id) {
        this.spigot_id = spigot_id;
        this.plugin = plugin;
    }

    public int getSpigotID() {
        return spigot_id;
    }

    @Override
    public Plugin getPlugin() {
        return plugin;
    }

    @Override
    public UpdateInfo checkUpdate() {
        ServerRequest r = new ServerRequest("resources/" + spigot_id + "/versions?sort=-name");
        JsonElement e = r.getAsJsonElement();
        if (e.isJsonArray()) {
            JsonObject current = e.getAsJsonArray().get(0).getAsJsonObject();
            String version = current.get("name").getAsString();
            if (!version.equalsIgnoreCase(plugin.getDescription().getVersion())) {
                UpdateInfo info = new UpdateInfo(plugin, null, version, "8Update found via SpigotUpdater",
                        current.has("url") ? current.get("url").getAsString() : null) {
                    @Override
                    public void performUpdate(Player notifier) {
                        SpigetPlugin d = new SpigetPlugin(spigot_id);
                        d.loadInformation();
                        if (d.hasDirectDownload()) {
                            notifier.sendMessage(
                                    "aCached Download from spiget.org found. Downloading directly...");
                            d.downloadDirectly(notifier, "plugins/" + getJarName());
                        } else if (d.isExternalDownload()) {
                            notifier.sendMessage(
                                    "clError elThis Plugin has an external download. You must download it manually: "
                                            + d.getDownloadURL());
                        } else {
                            notifier.sendMessage("aDownload started. Please wait...");
                            notifier.performCommand("wget plugins " + d.getDownloadURL());
                        }
                    }

                };
                return info;
            }
        } else {
            System.out.println("Could not check update for " + plugin.getName() + " : " + r.getAsString());
        }
        return null;
    }

}