-
-
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.
Merge pull request #4 from Shweit/dvdb/version-checker
Added version Checker
- Loading branch information
Showing
5 changed files
with
151 additions
and
3 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/shweit/pollmaster/commands/VersionCommand.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,55 @@ | ||
package com.shweit.pollmaster.commands; | ||
|
||
import com.shweit.pollmaster.PollMaster; | ||
import com.shweit.pollmaster.utils.CheckForUpdate; | ||
import com.shweit.pollmaster.utils.Logger; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.TabExecutor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class VersionCommand implements CommandExecutor, TabExecutor { | ||
|
||
@Override | ||
public boolean onCommand(final CommandSender commandSender, final Command command, final String s, final String[] args) { | ||
Logger.debug("VersionCommand.onCommand()"); | ||
switch (args[0]) { | ||
case "version": | ||
commandSender.sendMessage(ChatColor.GREEN + "PollMaster version: " + ChatColor.GOLD + PollMaster.getInstance().getDescription().getVersion()); | ||
commandSender.sendMessage(""); | ||
commandSender.sendMessage(ChatColor.GREEN + "Check for updates..."); | ||
|
||
// Check for updates | ||
CheckForUpdate checkForUpdate = new CheckForUpdate(); | ||
boolean updateAvailable = checkForUpdate.checkForPluginUpdate(); | ||
if (updateAvailable) { | ||
commandSender.sendMessage(ChatColor.GREEN + "Update available! New Version: " | ||
+ ChatColor.GOLD + PollMaster.getInstance().getDescription().getVersion() | ||
+ ChatColor.GREEN + " -> " + ChatColor.GOLD + checkForUpdate.latestVersion | ||
); | ||
} else { | ||
commandSender.sendMessage(ChatColor.GREEN + "No updates available."); | ||
} | ||
|
||
return true; | ||
|
||
default: | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> onTabComplete(final CommandSender commandSender, final Command command, final String s, final String[] args) { | ||
if (args.length == 1) { | ||
List<String> subCommands = new ArrayList<>(); | ||
subCommands.add("version"); | ||
return subCommands; | ||
} | ||
|
||
return new ArrayList<>(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/com/shweit/pollmaster/utils/CheckForUpdate.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,84 @@ | ||
package com.shweit.pollmaster.utils; | ||
|
||
import com.shweit.pollmaster.PollMaster; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public final class CheckForUpdate implements Listener { | ||
|
||
private static final String GITHUB_API_URL = "https://api.github.com/repos/Shweit/PollMaster/releases/latest"; | ||
private static final String USER_AGENT = "Mozilla/5.0"; | ||
public String latestVersion; | ||
|
||
public boolean checkForPluginUpdate() { | ||
latestVersion = fetchLatestVersion(); | ||
|
||
if (latestVersion != null) { | ||
String currentVersion = PollMaster.getInstance().getDescription().getVersion(); | ||
|
||
return !latestVersion.equals(currentVersion); | ||
} else { | ||
Logger.warning("Failed to fetch the latest version from GitHub API."); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerJoin(final PlayerJoinEvent event) { | ||
if (event.getPlayer().hasPermission("pollmaster.version")) { | ||
Logger.debug("Checking for plugin update..."); | ||
if (checkForPluginUpdate()) { | ||
event.getPlayer().sendMessage("§aA new version of the MinecraftServerAPI plugin is available"); | ||
event.getPlayer().sendMessage("§aCurrent version: §f" + PollMaster.getInstance().getDescription().getVersion()); | ||
event.getPlayer().sendMessage("§aLatest version: §f" + latestVersion); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Fetches the latest version from the GitHub API. | ||
* | ||
* @return the latest version as a String, or null if there was an error. | ||
*/ | ||
private String fetchLatestVersion() { | ||
try { | ||
URL url = new URL(GITHUB_API_URL); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
connection.setRequestProperty("User-Agent", USER_AGENT); | ||
|
||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | ||
StringBuilder content = new StringBuilder(); | ||
String inputLine; | ||
while ((inputLine = in.readLine()) != null) { | ||
content.append(inputLine); | ||
} | ||
|
||
in.close(); | ||
connection.disconnect(); | ||
|
||
String jsonResponse = content.toString(); | ||
Pattern pattern = Pattern.compile("\"tag_name\":\"([^\"]+)\""); | ||
Matcher matcher = pattern.matcher(jsonResponse); | ||
|
||
if (matcher.find()) { | ||
return matcher.group(1); | ||
} else { | ||
Logger.warning("Tag name not found in the GitHub API response."); | ||
return null; | ||
} | ||
} catch (Exception e) { | ||
Logger.error("An error occurred while fetching the latest version: " + e.getMessage()); | ||
return null; | ||
} | ||
} | ||
} |
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