Skip to content

Commit

Permalink
Added an auto-update system
Browse files Browse the repository at this point in the history
Took 1 hour 17 minutes
  • Loading branch information
SynkMC committed Oct 14, 2024
1 parent 5f6e19e commit d7e7e0a
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 16 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cc.synkdev</groupId>
<artifactId>SynkLibs</artifactId>
<version>1.6</version>
<version>1.7</version>
<packaging>jar</packaging>

<name>SynkLibs</name>
Expand Down Expand Up @@ -115,8 +115,9 @@

<dependency>
<groupId>co.aikar</groupId>
<artifactId>acf-paper</artifactId>
<artifactId>acf-bukkit</artifactId>
<version>0.5.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

<dependency>
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/cc/synkdev/synkLibs/bukkit/SynkLibs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import cc.synkdev.synkLibs.bukkit.commands.ReportCmd;
import cc.synkdev.synkLibs.bukkit.commands.SlCmd;
import cc.synkdev.synkLibs.components.GlobalErrorHandler;
import cc.synkdev.synkLibs.components.PluginUpdate;
import cc.synkdev.synkLibs.components.SynkPlugin;
import co.aikar.commands.PaperCommandManager;
import co.aikar.commands.BukkitCommandManager;
import lombok.Getter;
import lombok.Setter;
import org.bstats.bukkit.Metrics;
Expand All @@ -16,29 +17,40 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class SynkLibs extends JavaPlugin implements SynkPlugin {
@Getter private static SynkLibs instance;
@Setter String prefix = ChatColor.translateAlternateColorCodes('&', "&8[&6SynkLibs&8] » &r");
@Setter @Getter static SynkPlugin spl = null;
@Getter
private static SynkLibs instance;
@Setter
String prefix = ChatColor.translateAlternateColorCodes('&', "&8[&6SynkLibs&8] » &r");
@Setter
@Getter
static SynkPlugin spl = null;
public static Map<SynkPlugin, String> availableUpdates = new HashMap<>();
private final File configFile = new File(getDataFolder(), "config.yml");
public FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
public static String lang = "en";
private PaperCommandManager pcm;
@Getter @Setter private static Boolean loopReport = false;
private BukkitCommandManager pcm;
@Getter
@Setter
private static Boolean loopReport = false;
public static Map<String, String> langMap = new HashMap<>();
public List<PluginUpdate> outdated = new ArrayList<>();

@Override
public void onEnable() {
instance = this;

loadConfig();

langMap.clear();
langMap.putAll(Lang.init(this, new File(getDataFolder(), "lang.json")));

pcm = new PaperCommandManager(this);
pcm = new BukkitCommandManager(this);
Thread.setDefaultUncaughtExceptionHandler(new GlobalErrorHandler("https://discord.com/api/webhooks/1294577862359257129/W7BssLiR8LpvfA7KeiAsBerXMHGvxB-1o0lKL70ly5RviPKwM4omvnXibqsKHkhsYAHW"));
pcm.setDefaultExceptionHandler(new GlobalErrorHandler("https://discord.com/api/webhooks/1294577862359257129/W7BssLiR8LpvfA7KeiAsBerXMHGvxB-1o0lKL70ly5RviPKwM4omvnXibqsKHkhsYAHW"));

Expand All @@ -47,11 +59,12 @@ public void onEnable() {
Bukkit.getPluginManager().registerEvents(new Utils(this), this);
Utils.checkUpdate(this);

pcm.enableUnstableAPI("help");
pcm.registerCommand(new ReportCmd(this));
pcm.registerCommand(new SlCmd(this));

loadConfig();
outdated.clear();
outdated.addAll(UpdateChecker.checkOutated());
if (!outdated.isEmpty()) UpdateChecker.update(outdated);
}

public void loadConfig() {
Expand Down Expand Up @@ -82,7 +95,7 @@ public String name() {

@Override
public String ver() {
return "1.6";
return "1.7";
}

@Override
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/cc/synkdev/synkLibs/bukkit/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cc.synkdev.synkLibs.bukkit;

import cc.synkdev.synkLibs.components.PluginUpdate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class UpdateChecker {
public static JSONObject readData() {
try {
URL url = new URL("https://synkdev.cc/storage/versions.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder content = new StringBuilder();
String ln;
while ((ln = in.readLine()) != null) {
content.append(ln);
}
in.close();
conn.disconnect();
return new JSONObject(content.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static List<PluginUpdate> checkOutated() {
SynkLibs.setSpl(SynkLibs.getInstance());
List<Plugin> synkPlugs = new ArrayList<>();
for (Plugin pl : Bukkit.getPluginManager().getPlugins()) {
if (pl.getDescription().getAuthors().contains("Synk")) {
synkPlugs.add(pl);
}
}

List<PluginUpdate> list = new ArrayList<>();
JSONObject obj = readData();
for (Plugin pl : synkPlugs) {
if (obj.has(pl.getName())) {
JSONObject plObj = obj.getJSONObject(pl.getName());
String ver = plObj.getString("version");
if (!pl.getDescription().getVersion().equals(ver)) {
String link = plObj.getString("link");
File file = null;
for (File lF : pl.getDataFolder().getParentFile().listFiles()) {
if (file == null) {
if (lF.getName().contains(pl.getName()) && lF.getName().contains(".jar")) {
file = lF;
}
}
}
if (file != null) list.add(new PluginUpdate(ver, pl.getName(), link, file));
}
}
}
return list;
}
public static void update(List<PluginUpdate> list) {
for (PluginUpdate pl : list) {
try {
pl.getCurrent().delete();
URL url = new URL(pl.getDl());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream in = conn.getInputStream();
OutputStream out = Files.newOutputStream(new File(pl.getCurrent().getParentFile(), pl.getPlugin() + ".jar").toPath())) {

byte[] buffer = new byte[4096];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}

}
conn.disconnect();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Utils.log(SynkLibs.getInstance().prefix+ ChatColor.GOLD+"New plugin versions have been downloaded! Restart your server for the changes to apply.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ private void sendWH(Throwable e) {

@Override
public boolean execute(BaseCommand command, RegisteredCommand registeredCommand, CommandIssuer sender, List<String> args, Throwable t) {
uncaughtException(null, t);
log = false;
return true;
if (!SynkLibs.getLoopReport()) sendWH(t);
if (log) logError(t);
log = true;
return false;
}
}
19 changes: 19 additions & 0 deletions src/main/java/cc/synkdev/synkLibs/components/PluginUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cc.synkdev.synkLibs.components;

import lombok.Getter;

import java.io.File;

@Getter
public class PluginUpdate {
private String num;
private String plugin;
private String dl;
private File current;
public PluginUpdate(String num, String plugin, String dl, File current) {
this.num = num;
this.plugin = plugin;
this.dl = dl;
this.current = current;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ name: SynkLibs
version: '${project.version}'
main: cc.synkdev.synkLibs.bukkit.SynkLibs
api-version: '1.13'
authors: [ Synk, KingsMC ]
authors: [ Synk ]

0 comments on commit d7e7e0a

Please sign in to comment.