-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Took 1 hour 17 minutes
- Loading branch information
Showing
6 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/cc/synkdev/synkLibs/bukkit/UpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/cc/synkdev/synkLibs/components/PluginUpdate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters