Added auto updater for the plugin

This commit is contained in:
OmerBenGera 2020-09-18 17:02:49 +03:00
parent c4973486c9
commit 4885ddc4f0
3 changed files with 97 additions and 22 deletions

View File

@ -0,0 +1,71 @@
package com.bgsoftware.wildloaders;
import javax.net.ssl.HttpsURLConnection;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
public final class Updater {
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private static String latestVersion, versionDescription;
static{
setLatestVersion();
}
//Just so no one would be able to call the constructor
private Updater(){}
public static boolean isOutdated(){
return !plugin.getDescription().getVersion().equals(latestVersion);
}
public static String getLatestVersion(){
return latestVersion;
}
static String getVersionDescription(){
return versionDescription;
}
private static void setLatestVersion(){
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://bg-software.com/versions.json").openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
connection.setDoInput(true);
try(InputStream reader = connection.getInputStream()){
Class<?> jsonObjectClass, gsonClass;
try{
jsonObjectClass = Class.forName("net.minecraft.util.com.google.gson.JsonObject");
gsonClass = Class.forName("net.minecraft.util.com.google.gson.Gson");
}catch(ClassNotFoundException ex){
jsonObjectClass = Class.forName("com.google.gson.JsonObject");
gsonClass = Class.forName("com.google.gson.Gson");
}
Object jsonObject = gsonClass.getMethod("fromJson", Reader.class, Class.class)
.invoke(gsonClass.newInstance(), new InputStreamReader(reader), jsonObjectClass);
Object jsonElement = jsonObjectClass.getMethod("get", String.class).invoke(jsonObject, "wildloaders");
Object plugin = jsonElement.getClass().getMethod("getAsJsonObject").invoke(jsonElement);
Object versionElement = plugin.getClass().getMethod("get", String.class).invoke(plugin, "version");
Object descriptionElement = plugin.getClass().getMethod("get", String.class).invoke(plugin, "description");
latestVersion = (String) versionElement.getClass().getMethod("getAsString").invoke(versionElement);
versionDescription = (String) descriptionElement.getClass().getMethod("getAsString").invoke(descriptionElement);
}
} catch(Exception ex){
//Something went wrong...
latestVersion = plugin.getDescription().getVersion();
}
}
}

View File

@ -9,6 +9,7 @@ import com.bgsoftware.wildloaders.handlers.NPCHandler;
import com.bgsoftware.wildloaders.handlers.SettingsHandler;
import com.bgsoftware.wildloaders.listeners.BlocksListener;
import com.bgsoftware.wildloaders.listeners.ChunksListener;
import com.bgsoftware.wildloaders.listeners.PlayersListener;
import com.bgsoftware.wildloaders.metrics.Metrics;
import com.bgsoftware.wildloaders.nms.NMSAdapter;
import com.bgsoftware.wildloaders.utils.database.Database;
@ -58,6 +59,7 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
getServer().getPluginManager().registerEvents(new BlocksListener(this), this);
getServer().getPluginManager().registerEvents(new ChunksListener(this), this);
getServer().getPluginManager().registerEvents(new PlayersListener(this), this);
CommandsHandler commandsHandler = new CommandsHandler(this);
getCommand("loader").setExecutor(commandsHandler);
@ -65,12 +67,12 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders {
Locale.reload();
// if(Updater.isOutdated()) {
// log("");
// log("A new version is available (v" + Updater.getLatestVersion() + ")!");
// log("Version's description: \"" + Updater.getVersionDescription() + "\"");
// log("");
// }
if(Updater.isOutdated()) {
log("");
log("A new version is available (v" + Updater.getLatestVersion() + ")!");
log("Version's description: \"" + Updater.getVersionDescription() + "\"");
log("");
}
log("******** ENABLE DONE ********");
}

View File

@ -1,34 +1,36 @@
package com.bgsoftware.wildloaders.listeners;
import com.bgsoftware.wildloaders.Updater;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import com.bgsoftware.wildloaders.utils.threads.Executor;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerJoinEvent;
@SuppressWarnings("unused")
public final class PlayersListener implements Listener {
private WildLoadersPlugin plugin;
private final WildLoadersPlugin plugin;
public PlayersListener(WildLoadersPlugin plugin){
this.plugin = plugin;
}
/**
* In some versions, the loaders can die even if they are in creative.
* This should fix the issue.
*/
/*
Just notifies me if the server is using WildBuster
*/
@EventHandler(priority = EventPriority.LOWEST)
public void onLoaderDamage(EntityDamageEvent e){
if(!(e.getEntity() instanceof LivingEntity))
return;
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e){
if(e.getPlayer().getUniqueId().toString().equals("45713654-41bf-45a1-aa6f-00fe6598703b")){
Executor.sync(() -> e.getPlayer().sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.WHITE + "WildSeries" + ChatColor.DARK_GRAY + "] " +
ChatColor.GRAY + "This server is using WildLoaders v" + plugin.getDescription().getVersion()), 5L);
}
if(plugin.getNPCs().isNPC((LivingEntity) e.getEntity())) {
e.setCancelled(true);
e.setDamage(0D);
if(e.getPlayer().isOp() && Updater.isOutdated()){
Executor.sync(() -> e.getPlayer().sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "WildLoaders" +
ChatColor.GRAY + " A new version is available (v" + Updater.getLatestVersion() + ")!"), 20L);
}
}