Skip to content

Commit

Permalink
v1.1.0 - Add SimpleUpdateChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
TechnicallyCoded committed Aug 22, 2023
1 parent 901d515 commit 2014f0f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ plugins {
}

group = 'org.example'
version = '1.0-SNAPSHOT'
version = '1.1.0'

repositories {
mavenCentral()
maven {
url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
content {
includeGroup 'org.bukkit'
includeGroup 'org.spigotmc'
}
}
}

java {
Expand All @@ -16,6 +23,8 @@ java {
}

dependencies {
compileOnly 'org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT'

testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/tcoded/updatechecker/SimpleUpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.tcoded.updatechecker;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

public class SimpleUpdateChecker {

public static void checkUpdate(JavaPlugin plugin, String prefix, int resourceId) {
Server server = plugin.getServer();
ConsoleCommandSender consoleSender = server.getConsoleSender();
PluginDescriptionFile pluginDesc = plugin.getDescription();
String pluginVersion = pluginDesc.getVersion();
String pluginName = pluginDesc.getName();

server.getScheduler().runTaskAsynchronously(plugin, () -> {
consoleSender.sendMessage(prefix + "Checking for updates...");

final UpdateResult result = new UpdateChecker(pluginVersion, resourceId).getResult();

int prioLevel = 0;
String prioColor = ChatColor.AQUA.toString();
String prioLevelName = "null";

switch (result.getType()) {
case FAIL_SPIGOT:
consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: Could not contact Spigot to check if an update is available.");
break;
case UPDATE_LOW:
prioLevel = 1;
prioLevelName = "minor";
break;
case UPDATE_MEDIUM:
prioLevel = 2;
prioLevelName = "feature";
prioColor = ChatColor.GOLD.toString();
break;
case UPDATE_HIGH:
prioLevel = 3;
prioLevelName = "MAJOR";
prioColor = ChatColor.RED.toString();
break;
case DEV_BUILD:
consoleSender.sendMessage(prefix + ChatColor.GOLD + "Warning: You are running an experimental/development build! Proceed with caution.");
break;
case NO_UPDATE:
consoleSender.sendMessage(prefix + ChatColor.RESET + "You are running the latest version.");
break;
default:
break;
}

if (prioLevel > 0) {
consoleSender.sendMessage( "\n" + prioColor +
"===============================================================================\n" +
"A " + prioLevelName + " update to " + pluginName + " is available!\n" +
"Download at https://www.spigotmc.org/resources/" + pluginName.toLowerCase() + "." + resourceId + "/\n" +
"(current: " + result.getCurrentVer() + ", latest: " + result.getLatestVer() + ")\n" +
"===============================================================================\n");
}

});
}

}

0 comments on commit 2014f0f

Please sign in to comment.