Skip to content

Commit

Permalink
Merge pull request #4 from Shweit/dvdb/version-checker
Browse files Browse the repository at this point in the history
Added version Checker
  • Loading branch information
Shweit authored Sep 4, 2024
2 parents fe8c1d7 + 2138b0b commit 932e416
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.shweit</groupId>
<artifactId>PollMaster</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<name>PollMaster</name>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/shweit/pollmaster/PollMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.shweit.pollmaster.commands.CreatePollCommand;
import com.shweit.pollmaster.commands.DeletePollCommand;
import com.shweit.pollmaster.commands.VersionCommand;
import com.shweit.pollmaster.commands.VoteCommand;
import com.shweit.pollmaster.commands.pollDetailsCommand.PollDetailGuiListener;
import com.shweit.pollmaster.commands.pollsCommand.PollsCommand;
import com.shweit.pollmaster.commands.pollsCommand.PollsGuiListener;
import com.shweit.pollmaster.utils.CheckForUpdate;
import com.shweit.pollmaster.utils.ConnectionManager;
import com.shweit.pollmaster.utils.Logger;
import org.bukkit.configuration.file.FileConfiguration;
Expand Down Expand Up @@ -34,8 +36,11 @@ public void onEnable() {
getCommand("polls").setExecutor(new PollsCommand());
getCommand("vote").setExecutor(new VoteCommand());
getCommand("endpoll").setExecutor(new DeletePollCommand());
getCommand("pollmaster").setExecutor(new VersionCommand());

getServer().getPluginManager().registerEvents(new PollsGuiListener(), this);
getServer().getPluginManager().registerEvents(new PollDetailGuiListener(), this);
getServer().getPluginManager().registerEvents(new CheckForUpdate(), this);
}

@Override
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/shweit/pollmaster/commands/VersionCommand.java
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 src/main/java/com/shweit/pollmaster/utils/CheckForUpdate.java
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;
}
}
}
8 changes: 6 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PollMaster
version: '1.0'
version: '1.0.1'
description: A versatile Minecraft plugin that allows players to create, manage, and view polls in-game, providing an engaging way for players to participate in server decisions.
main: com.shweit.poll.PollMaster
main: com.shweit.pollmaster.PollMaster
api-version: '1.21'
author: Shweit

Expand All @@ -25,3 +25,7 @@ commands:
description: End a poll and display the results.
usage: /endpoll <poll_id>
permission: pollmaster.end

pollmaster:
description: Show information about the plugin.
usage: /pollmaster

0 comments on commit 932e416

Please sign in to comment.