Skip to content

Commit

Permalink
implement update version check
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifiht committed Nov 24, 2024
1 parent 121d9cb commit 36815da
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'org.evlis'
version = '0.4.2'
version = '1.0.0'

def targetJavaVersion = 21

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/evlis/lunamatic/GlobalVars.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import java.util.Map;

public class GlobalVars {
// Test flag, makes every night a bloodmoon if true
// Test flag
public static Boolean debug = false;
// Check for updates?
public static Boolean checkUpdates = true;
// enabled moons:
public static Boolean fullMoonEnabled = true;
public static Boolean harvestMoonEnabled = true;
Expand Down
67 changes: 63 additions & 4 deletions src/main/java/org/evlis/lunamatic/Lunamatic.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package org.evlis.lunamatic;

import co.aikar.commands.PaperCommandManager;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.evlis.lunamatic.commands.LumaCommand;
import org.evlis.lunamatic.events.*;
import org.evlis.lunamatic.triggers.Scheduler;

import java.io.InputStreamReader;
import java.lang.module.ModuleDescriptor;
import java.net.HttpURLConnection;
import java.net.URL;

public final class Lunamatic extends JavaPlugin {

//private final ConsoleCommandSender consoleLogger = getServer().getConsoleSender();
Expand All @@ -18,12 +27,27 @@ public final class Lunamatic extends JavaPlugin {
public EntitySpawn entitySpawn;
//public final Logger logger = this.getLogger();
//public final File configFile = new File(this.getDataFolder(), "config.yml");
private static final String REQUIRED_VERSION = "1.21";
private static final String API_URL = "https://api.modrinth.com/v2/project/lunamatic/version";

@Override
public void onEnable() {
//consoleLogger.sendMessage(MiniMessage.miniMessage().deserialize(""));
// Plugin startup logic
// Begin Initialization
this.getComponentLogger().debug(Component.text("Loading Lunamatic...", NamedTextColor.GOLD));
// Get versions
String serverVersion = getServer().getMinecraftVersion();
String currentVersion = this.getPluginMeta().getVersion();
// Check server version, log error if not supported.
if (!serverVersion.startsWith(REQUIRED_VERSION)) {
this.getComponentLogger().error(Component.text("Unsupported server version: " + serverVersion, NamedTextColor.RED));
}
// Config Initialization
saveDefaultConfig();
loadGlobalConfig();
// Update check
if (GlobalVars.checkUpdates) {
checkForUpdates(currentVersion);
}
// Class Initialization
Scheduler schedule = new Scheduler();
timeSkip = new TimeSkip();
Expand All @@ -37,14 +61,16 @@ public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(playerSleep, this);
Bukkit.getServer().getPluginManager().registerEvents(entitySpawn, this);
registerCommands();
loadGlobalConfig();

schedule.GetOmens(this);
// Notify of successful plugin start
this.getComponentLogger().debug(Component.text("Successfully enabled Lunamatic v" + currentVersion, NamedTextColor.GOLD));
}

@Override
public void onDisable() {
// Plugin shutdown logic
this.getComponentLogger().debug(Component.text("Lunamatic has been disabled."));
this.getComponentLogger().debug(Component.text("Lunamatic has been disabled.", NamedTextColor.GOLD));
}

public void registerCommands() {
Expand All @@ -55,6 +81,8 @@ public void registerCommands() {
public void loadGlobalConfig() {
try {
reloadConfig();
// plugin vars
GlobalVars.checkUpdates = getConfig().getBoolean("checkForUpdates");
// moons enabled
GlobalVars.disabledWorlds = getConfig().getStringList("disabledWorlds");
GlobalVars.fullMoonEnabled = getConfig().getBoolean("fullMoonEnabled");
Expand All @@ -68,4 +96,35 @@ public void loadGlobalConfig() {
getLogger().severe("Failed to load configuration: " + e.getMessage());
}
}

public void checkForUpdates(String currentVersionString) {
getLogger().info("Checking for updates...");
try {
// Open a connection to the API
HttpURLConnection connection = (HttpURLConnection) new URL(API_URL).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");

// Parse the response
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
JsonArray jsonArray = JsonParser.parseReader(reader).getAsJsonArray();

// Get the first item in the JSON array
JsonElement latestVersionElement = jsonArray.get(0);
String latestVersionString = latestVersionElement.getAsJsonObject().get("version_number").getAsString();

// Compare versions
ModuleDescriptor.Version latestVersion = ModuleDescriptor.Version.parse(latestVersionString);
ModuleDescriptor.Version currentVersion = ModuleDescriptor.Version.parse(currentVersionString);
if (currentVersion.compareTo(latestVersion) < 0) {
this.getComponentLogger().debug(Component.text("New Version " + latestVersionString + " available (you are on v" + currentVersionString + ")! Download here: https://modrinth.com/plugin/lunamatic", NamedTextColor.GOLD));
}

reader.close();
connection.disconnect();
} catch (Exception e) {
this.getComponentLogger().error(Component.text("Unable to check for updates: " + e.getMessage(), NamedTextColor.RED));
}
}
}
18 changes: 10 additions & 8 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
### Lunamatic configuration ###
# Choose which moon effects to enable,
# for blood moons, new moon must be enabled,
# and for harvest moons the full moon must be enabled.
# Choose which moon effects to enable
fullMoonEnabled: true
newMoonEnabled: true
## Ignored unless fullMoonEnabled == true
harvestMoonEnabled: true
## Ignored unless newMoonEnabled == true
bloodMoonEnabled: true
### Chance for Blood and Harvest Moons ###
# Chance for Blood and Harvest Moons ###
# This sets the number of sides in the dice roll,
# e.g. 2 = cointoss (50/50), 6 = normal die (17% chance)
# e.g. 2 = coin-toss (50/50), 6 = normal die (17% chance)
bloodMoonDieSides: 2
harvestMoonDieSides: 2
### Set worlds to disable moon-effects on ###
# by default both nether & the_end cannot have moon effects
# Set worlds to disable moon-effects on ###
# by design both nether & the_end cannot have moon effects
disabledWorlds:
- world
- world_nether
- world_the_end
- world_the_end
# Do you want to check for updates at server start?
checkForUpdates: true

0 comments on commit 36815da

Please sign in to comment.