Skip to content

Commit

Permalink
Update checker & anonymous metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasDeBruijn committed Jun 6, 2021
1 parent f202a44 commit 4750444
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 25 deletions.
20 changes: 3 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,14 @@ allprojects {
}

dependencies {
//HTTP client
compile 'com.github.TheDutchMC:HttpLib:1.1'

//Logging framework
compileOnly 'org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT'
compile 'org.slf4j:slf4j-log4j12:1.7.29'

//JSON
compile 'com.google.code.gson:gson:2.8.6'

//JDA
compile('net.dv8tion:JDA:4.2.0_168') {
exclude module: 'opus-java'
}

//Spigot API
compileOnly 'org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT'

compile 'dev.array21:classvalidator:1.0.0'

compile 'dev.array21:pluginstatlib:1.0.5'
}

shadowJar() {
Expand All @@ -63,13 +52,11 @@ shadowJar() {
relocate 'com.google.code.gson', 'nl.thedutchmc.skinfixer.libs.com.google.code.gson'
relocate 'nl.thedutchmc.httplib', 'nl.thedutchmc.skinfixer.libs.nl.thedutchmc.httplib'
relocate 'org.slf4j', 'nl.thedutchmc.skinfixer.libs.org.slf4j'
relocate 'dev.array21.classvalidator', 'nl.thedutchmc.skinfixer.libs.dev.array21.classvalidator'
relocate 'dev.array21', 'nl.thedutchmc.skinfixer.libs.dev.array21'
}

task testJar(type: ShadowJar) {

relocate 'net.dv8tion.jda', 'nl.thedutchmc.libs.jda'


description 'Build a test Jar'
archiveClassifier = 'DEV'
Expand All @@ -79,7 +66,6 @@ task testJar(type: ShadowJar) {
}

task releaseJar(type: ShadowJar) {

relocate 'net.dv8tion.jda', 'nl.thedutchmc.libs.jda'

description 'Build a release Jar'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pluginVersion = 1.4.0
pluginVersion = 1.4.1
pluginGroup = nl.thedutchmc
pluginName = SkinFixer
pluginAuthor = TheDutchMC
Expand Down
32 changes: 25 additions & 7 deletions src/main/java/nl/thedutchmc/SkinFixer/SkinFixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import dev.array21.pluginstatlib.PluginStat;
import dev.array21.pluginstatlib.PluginStat.PluginStatBuilder;
import nl.thedutchmc.SkinFixer.commandexecutors.GetCodeCommandExecutor;
import nl.thedutchmc.SkinFixer.commandexecutors.SetSkinCommandExecutor;
import nl.thedutchmc.SkinFixer.commandexecutors.SkinFixerCommandExecutor;
import nl.thedutchmc.SkinFixer.fileHandlers.ConfigurationHandler;
import nl.thedutchmc.SkinFixer.fileHandlers.StorageHandler;
import nl.thedutchmc.SkinFixer.language.LangHandler;
import nl.thedutchmc.SkinFixer.minecraftevents.PlayerJoinEventListener;
import nl.thedutchmc.SkinFixer.updatechecker.UpdateChecker;

public class SkinFixer extends JavaPlugin {

public static SkinFixer INSTANCE;
public static StorageHandler STORAGE;

public static final String NMS_VERSION = Bukkit.getServer().getClass().getPackage().getName().substring(23);
public static String PLUGIN_VERSION;

public static final Logger LOGGER = LogManager.getLogger(SkinFixer.class);
Expand All @@ -30,7 +31,14 @@ public void onEnable() {
PLUGIN_VERSION = Bukkit.getPluginManager().getPlugin("SkinFixer").getDescription().getVersion();

SkinFixer.logInfo("Welcome to SkinFixer version " + PLUGIN_VERSION + " by TheDutchMC!");
SkinFixer.logInfo("Using NMS Version " + SkinFixer.NMS_VERSION);

new Thread(new Runnable() {
@Override
public void run() {
new UpdateChecker(SkinFixer.this).checkUpdate();

}
}, "SkinFixer UpdateChecker Thread").start();

//Read the configuration
ConfigurationHandler configHandler = new ConfigurationHandler();
Expand All @@ -44,6 +52,16 @@ public void onEnable() {
langHandler.loadLang("en");
}

if(!ConfigurationHandler.disableStat) {
PluginStat stat = PluginStatBuilder.createDefault()
.setLogErrFn(SkinFixer::logWarn)
.setSetUuidFn(configHandler::setUuid)
.setUuid(ConfigurationHandler.statUuid)
.build();

stat.start();
}

//Storage of skins and pending keys
STORAGE = new StorageHandler();
STORAGE.loadConfig();
Expand Down Expand Up @@ -74,11 +92,11 @@ public void onDisable() {
logInfo("Thank you for using SkinFixer by TheDutchMC");
}

public static void logInfo(String log) {
Bukkit.getLogger().info("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log);
public static void logInfo(Object log) {
Bukkit.getLogger().info("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log.toString());
}

public static void logWarn(String log) {
Bukkit.getLogger().warning("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log);
public static void logWarn(Object log) {
Bukkit.getLogger().warning("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public class ConfigurationHandler {
*/
public static String language;

/**
* @since 1.4.1
*/
public static String statUuid;

/**
* @since 1.4.1
*/
public static boolean disableStat;

private File file;
private FileConfiguration config;

Expand Down Expand Up @@ -60,5 +70,21 @@ public void readConfig() {
}

language = this.getConfig().getString("language");

String uuid = this.getConfig().getString("statUuid");
statUuid = uuid != null ? uuid : "";
disableStat = this.getConfig().getBoolean("disableStat");

}

public void setUuid(String uuid) {
ConfigurationHandler.statUuid = uuid;

this.getConfig().set("statUuid", uuid);
try {
this.config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.thedutchmc.SkinFixer.updatechecker;

import com.google.gson.annotations.SerializedName;

public class GithubResponse {
@SerializedName("html_url")
private String htmlUrl;

@SerializedName("tag_name")
private String tagName;

/**
* @return The URL of the new GitHub Release
*/
public String getUrl() {
return htmlUrl;
}
/**
* @return The name of the Release
*/
public String getTagName() {
return tagName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package nl.thedutchmc.SkinFixer.updatechecker;

import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Pattern;

import com.google.gson.Gson;

import nl.thedutchmc.SkinFixer.SkinFixer;
import nl.thedutchmc.httplib.Http;
import nl.thedutchmc.httplib.Http.RequestMethod;
import nl.thedutchmc.httplib.Http.ResponseObject;

public class UpdateChecker {

private SkinFixer plugin;

public UpdateChecker(SkinFixer plugin) {
this.plugin = plugin;
}

public void checkUpdate() {
String[] currentVersion = this.plugin.getDescription().getVersion().split(Pattern.quote("."));
int currentMajorVersion = Integer.parseInt(currentVersion[0]);
int currentMinorVersion = Integer.parseInt(currentVersion[1]);
int currentBuild = Integer.parseInt((currentVersion.length > 2) ? currentVersion[2] : "0");

HashMap<String, String> headers = new HashMap<>();
headers.put("User-Agent", "SkinFixer Plugin v" + this.plugin.getDescription().getVersion());

ResponseObject response;
try {
response = new Http().makeRequest(RequestMethod.GET, "https://api.github.com/repos/thedutchmc/skinfixer/releases/latest", null, null, null, headers);
} catch(IOException e) {
SkinFixer.logWarn(String.format("An issue occurred while checking what the latest version of SkinFixer is: IOException (%s)", e.getMessage()));
return;
}

if(response.getResponseCode() != 200) {
SkinFixer.logWarn(String.format("Got a non-200 status code while checking what the latest version of SkinFixer is: HTTP-%d (%s)", response.getResponseCode(), response.getConnectionMessage()));
return;
}

final Gson gson = new Gson();
GithubResponse responseDeserialized = gson.fromJson(response.getMessage(), GithubResponse.class);

String[] latestVersion = responseDeserialized.getTagName().split(Pattern.quote("."));
int latestMajorVersion = Integer.parseInt(latestVersion[0]);
int latestMinorVersion = Integer.parseInt(latestVersion[1]);
int latestBuild = Integer.parseInt((latestVersion.length > 2) ? latestVersion[2] : "0");

if(latestMajorVersion > currentMajorVersion) {
updateAvailable(responseDeserialized.getUrl(), responseDeserialized.getTagName());
return;
}

if(latestMinorVersion > currentMinorVersion) {
updateAvailable(responseDeserialized.getUrl(), responseDeserialized.getTagName());
return;
}

if(latestBuild > currentBuild) {
updateAvailable(responseDeserialized.getUrl(), responseDeserialized.getTagName());
return;
}

SkinFixer.logInfo("You are running the latest version of SkinFixer. Nice work! :D");
}

private void updateAvailable(String url, String latestVersion) {
SkinFixer.logWarn(String.format("A SkinFixer update is available. You are running version %s, the latest version is %s. You can download it here: %s", this.plugin.getDescription().getVersion(), latestVersion, url));
}
}

0 comments on commit 4750444

Please sign in to comment.