Skip to content

Commit

Permalink
Added autoupdater
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Jul 7, 2019
1 parent 4e26d92 commit b824143
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: PluginManager
author: Lenni0451
version: 1.0
version: 1.1
main: net.Lenni0451.SpigotPluginManager.PluginManager

commands:
Expand Down
15 changes: 15 additions & 0 deletions src/net/Lenni0451/SpigotPluginManager/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import java.util.Arrays;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import net.Lenni0451.SpigotPluginManager.commands.PluginManager_Command;
import net.Lenni0451.SpigotPluginManager.commands.Reload_Command;
import net.Lenni0451.SpigotPluginManager.tabcomplete.PluginManager_TabComplete;
import net.Lenni0451.SpigotPluginManager.utils.DownloadUtils;
import net.Lenni0451.SpigotPluginManager.utils.Logger;
import net.Lenni0451.SpigotPluginManager.utils.PluginUtils;

public class PluginManager extends JavaPlugin {
Expand Down Expand Up @@ -41,6 +44,18 @@ public void onEnable() {
this.getCommand("pluginmanager").setExecutor(new PluginManager_Command());
this.getCommand("pluginmanager").setAliases(Arrays.asList("pm"));
this.getCommand("pluginmanager").setTabCompleter(new PluginManager_TabComplete());

try {
String newestVersion = DownloadUtils.getNewestVersion();
if(!newestVersion.equals(this.getDescription().getVersion())) {
Logger.sendPrefixMessage(Bukkit.getConsoleSender(), "A new update of PluginManager is available §e(" + this.getDescription().getVersion() + " -> " + newestVersion + ").");
Logger.sendPrefixMessage(Bukkit.getConsoleSender(), "You can download it here: §6https://github.com/Lenni0451/SpigotPluginManager/releases/latest/download/PluginManager.jar");
} else {
Logger.sendPrefixMessage(Bukkit.getConsoleSender(), "You are using the latest version of PluginManager.");
}
} catch (Throwable e) {
Logger.sendPrefixMessage(Bukkit.getConsoleSender(), "§cCould not check for updates.");
}
}

}
23 changes: 23 additions & 0 deletions src/net/Lenni0451/SpigotPluginManager/utils/DownloadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,27 @@ public static JsonArray getSpigotMcPluginList(final int count, final int page) t
return jsonParser.parse(responseBuilder.toString()).getAsJsonArray();
}

public static String getNewestVersion() throws IOException {
//https://github.com/Lenni0451/SpigotPluginManager/releases/tag/1.0
URL url = new URL("https://github.com/Lenni0451/SpigotPluginManager/releases/latest");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestProperty("user-agent", PluginManager.getInstance().getConfig().getString("UserAgent"));

BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder responseBuilder = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
responseBuilder.append(line);
}
br.close();

String urlBase = "https://github.com/Lenni0451/SpigotPluginManager/releases/tag/";
String source = responseBuilder.toString();
String version = source.substring(source.indexOf(urlBase) + urlBase.length());
version = version.substring(0, version.indexOf("&"));

return version;
}

}

0 comments on commit b824143

Please sign in to comment.