Skip to content

Commit

Permalink
refactor: unhardcode github repo, reduce static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriousGuy888 committed Jan 30, 2024
1 parent 3fba0f6 commit 1d327c0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class BillzoVillagers extends JavaPlugin {
private MainConfig mainConfig;
private NameListConfig nameListConfig;

private UpdateChecker updateChecker;

@Override
@SuppressWarnings("ResultOfMethodCallIgnored")
public void onEnable() {
Expand All @@ -49,7 +51,6 @@ public void onEnable() {


new TaskNameVillagers().runTaskTimer(plugin, 0L, 200L);
new UpdateChecker(this).checkForUpdates();
registerListeners();
registerCommands();

Expand All @@ -66,6 +67,10 @@ public void onEnable() {
dataManager = new DataManager();
villagerDeathMessagesEnabled = new HashMap<>();
dataManager.loadPlayerData(); // load data of all online players


updateChecker = new UpdateChecker(this, "SeriousGuy888/BillzoVillagers");
updateChecker.checkForUpdates();
}

private void registerListeners() {
Expand All @@ -77,7 +82,7 @@ private void registerListeners() {
pluginManager.registerEvents(new FoodLevelChangeListener(), this);
pluginManager.registerEvents(new JoinLeaveListener(), this);
pluginManager.registerEvents(new LeashingListener(), this);
pluginManager.registerEvents(new PlayerJoinListener(), this);
pluginManager.registerEvents(new PlayerJoinListener(this), this);
}

private void registerCommands() {
Expand Down Expand Up @@ -112,4 +117,8 @@ public MainConfig getMainConfig() {
public NameListConfig getNameListConfig() {
return nameListConfig;
}

public UpdateChecker getUpdateChecker() {
return updateChecker;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@
import org.bukkit.event.player.PlayerJoinEvent;

public class PlayerJoinListener implements Listener {
@EventHandler
private final BillzoVillagers plugin;

public PlayerJoinListener(BillzoVillagers plugin) {
this.plugin = plugin;
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();

if(!player.isOp())
return;
if(!UpdateChecker.isUpdateAvailable())
if(!plugin.getUpdateChecker().isUpdateAvailable())
return;

player.sendMessage("\n" + ChatColor.AQUA + ChatColor.BOLD
+ "A new version of the BillzoVillagers plugin is available." + ChatColor.AQUA
+ "\nCurrently installed version: v" + BillzoVillagers.getPlugin().getDescription().getVersion()
+ "\nLatest available version: " + UpdateChecker.getLatestVersion());
+ "\nLatest available version: " + plugin.getUpdateChecker().getLatestVersion());

TextComponent linkMessage = new TextComponent(ChatColor.BLUE + "" + ChatColor.UNDERLINE
+ "Updated Release Page" + "\n");
linkMessage.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, UpdateChecker.getLatestReleasePageURL()));
TextComponent linkMessage = new TextComponent(
"" + ChatColor.BLUE + ChatColor.UNDERLINE + "Updated Release Page" + "\n");
linkMessage.setClickEvent(
new ClickEvent(ClickEvent.Action.OPEN_URL, plugin.getUpdateChecker().getLatestReleasePageURL()));
player.spigot().sendMessage(linkMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

public class UpdateChecker {
private final BillzoVillagers plugin;
private final String githubRepo;

private static boolean updateAvailable = false;
private static String latestVersion;
private static String latestReleasePageURL;
private boolean updateAvailable = false;
private String latestVersion;
private String latestReleasePageURL;

public UpdateChecker(BillzoVillagers plugin) {
public UpdateChecker(BillzoVillagers plugin, String githubRepo) {
this.plugin = plugin;
this.githubRepo = githubRepo;
}

/**
Expand All @@ -30,7 +32,7 @@ public UpdateChecker(BillzoVillagers plugin) {
*/
public void checkForUpdates() {
Logger logger = plugin.getLogger();
String url = "https://api.github.com/repos/SeriousGuy888/BillzoVillagers/releases/latest";
String url = "https://api.github.com/repos/" + githubRepo + "/releases/latest";

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {

Expand All @@ -53,7 +55,7 @@ public void checkForUpdates() {
// get the latest release version on github and the version currently installed on the server
String installedVersion = BillzoVillagers.getPlugin().getDescription().getVersion();
latestVersion = releaseJsonObject.get("tag_name").getAsString();
latestReleasePageURL = "https://github.com/SeriousGuy888/BillzoVillagers/releases/" + latestVersion;
latestReleasePageURL = "https://github.com/" + githubRepo + "/releases/" + latestVersion;
logger.info("Latest release found on GitHub: " + latestVersion);

// check if the latest found version is newer than the current version
Expand All @@ -71,15 +73,15 @@ public void checkForUpdates() {

}

public static boolean isUpdateAvailable() {
public boolean isUpdateAvailable() {
return updateAvailable;
}

public static String getLatestVersion() {
public String getLatestVersion() {
return latestVersion;
}

public static String getLatestReleasePageURL() {
public String getLatestReleasePageURL() {
return latestReleasePageURL;
}

Expand Down

0 comments on commit 1d327c0

Please sign in to comment.