Skip to content

Commit

Permalink
Add Update_Checker v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hank9999 committed May 2, 2020
1 parent 851c783 commit 896f66f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
39 changes: 20 additions & 19 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public void onEnable() {

getLogger().info(ChatColor.BLUE + "Keywoord Plugin Enable");
getLogger().info(ChatColor.GOLD + "Version v" + getDescription().getVersion());

new Update_Checker(this, 78091).getVersion(version -> {
if (this.getDescription().getVersion().equalsIgnoreCase(version)) {
getLogger().info(ChatColor.AQUA + "There is not a new update available.");
} else {
getLogger().info(ChatColor.AQUA + "There is a new update " + version + " available.");
getLogger().info(ChatColor.AQUA + "See it in https://www.spigotmc.org/resources/keywordblock.78091/");
}
});
}

@Override
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/github/hank9999/keywordblock/Update_Checker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.hank9999.keywordblock;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Consumer;

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

public class Update_Checker {

private Plugin plugin;
private int resourceId;

public Update_Checker(Plugin 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(ChatColor.AQUA + "Cannot look for updates: " + exception.getMessage());
}
});
}
}

0 comments on commit 896f66f

Please sign in to comment.