Skip to content

Commit

Permalink
Add Update Util
Browse files Browse the repository at this point in the history
  • Loading branch information
CubitsDev committed Jul 11, 2021
1 parent 5c0c080 commit 0717d66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/network/palace/show/ShowPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import network.palace.show.npc.SoftNPCManager;
import network.palace.show.utils.BuildUtil;
import network.palace.show.utils.FileUtil;
import network.palace.show.utils.UpdateUtil;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -98,6 +99,11 @@ public void onEnable() {

Metrics metrics = new Metrics(this, pluginId);

new UpdateUtil(this, 94141).getVersion(v -> {
if (!this.getDescription().getVersion().equalsIgnoreCase(v)) {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "A New update is available for Show! It is always recommended that you upgrade! Link: https://www.spigotmc.org/resources/show-make-huge-spectaculars-in-minecraft.94141/");
}
});

Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Show is now enabled!");
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Huge shoutout to Legobuilder0813 for making this work for The Palace Network. Time to let your awesome code shine");
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/network/palace/show/utils/UpdateUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package network.palace.show.utils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Consumer;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class UpdateUtil {

private JavaPlugin plugin;
private int resourceId;

public UpdateUtil(JavaPlugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}

public void getVersion(final Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException exception) {
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage());
}
});
}
}

0 comments on commit 0717d66

Please sign in to comment.