Skip to content

Commit 7227d9b

Browse files
added version checker + fixed redis connection and added useSSL
1 parent 4079e33 commit 7227d9b

File tree

11 files changed

+268
-18
lines changed

11 files changed

+268
-18
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "io.lightstudios.core"
10-
version = "0.4.5"
10+
version = "0.4.6"
1111

1212
repositories {
1313
mavenCentral()

src/main/java/io/lightstudios/core/LightCore.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.lightstudios.core.database.model.ConnectionProperties;
1313
import io.lightstudios.core.database.model.DatabaseCredentials;
1414
import io.lightstudios.core.economy.EconomyManager;
15+
import io.lightstudios.core.github.VersionChecker;
1516
import io.lightstudios.core.inventory.events.MenuEvent;
1617
import io.lightstudios.core.items.LightItem;
1718
import io.lightstudios.core.items.events.UpdateLightItem;
@@ -33,7 +34,6 @@
3334
import io.lightstudios.core.util.files.configs.CoreSettings;
3435
import io.lightstudios.core.util.interfaces.LightCommand;
3536
import io.lightstudios.core.world.WorldManager;
36-
import io.lightstudios.core.world.events.BlockPlacedByPlayer;
3737
import lombok.Getter;
3838
import org.bstats.bukkit.Metrics;
3939
import org.bukkit.Bukkit;
@@ -65,6 +65,7 @@ public class LightCore extends JavaPlugin {
6565
private HookManager hookManager;
6666
private EconomyManager economyManager;
6767
private WorldManager worldManager;
68+
private VersionChecker versionChecker;
6869

6970
private FileManager coreFile;
7071
private FileManager messageFile;
@@ -129,6 +130,12 @@ public void onEnable() {
129130
// automatically checks for LightCoins as economy plugin or use the default vault economy as provider
130131
// used in all of my plugins
131132
this.economyManager = new EconomyManager();
133+
// GitHub version checker for all my “Light” plugin series
134+
135+
if(this.settings.checkForUpdates()) {
136+
this.versionChecker = new VersionChecker();
137+
}
138+
132139

133140
// on success loading the core module
134141
this.lightCoreEnabled = true;
@@ -319,6 +326,18 @@ public void registerCommands() {
319326
public void reloadCore() {
320327
generateCoreFiles();
321328
this.consolePrinter.printInfo("Reloaded the core files.");
329+
330+
if (!this.settings.checkForUpdates()) {
331+
if (this.versionChecker != null) {
332+
this.versionChecker.getScheduler().shutdown();
333+
}
334+
this.versionChecker = null;
335+
} else {
336+
if(this.versionChecker == null) {
337+
this.consolePrinter.printInfo("Enable version checker after reload.");
338+
this.versionChecker = new VersionChecker();
339+
}
340+
}
322341
}
323342

324343
private void enableRedisConnection() {
@@ -328,7 +347,8 @@ private void enableRedisConnection() {
328347
this.redisManager = new RedisManager(
329348
settings.redisHost(),
330349
settings.redisPort(),
331-
settings.redisPassword()
350+
settings.redisPassword(),
351+
settings.redisUseSSL()
332352
);
333353
this.isRedis = true;
334354
return;
@@ -342,7 +362,7 @@ public void readCoreItems() {
342362

343363
List<File> files = itemFiles.getYamlFiles();
344364
if(files.isEmpty()) {
345-
getConsolePrinter().printError(List.of(
365+
getConsolePrinter().printWarning(List.of(
346366
"No item files found in the items folder.",
347367
"Skipping this part ..."));
348368
return;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.lightstudios.core.github;
2+
3+
import io.lightstudios.core.LightCore;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.json.JSONObject;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.URI;
12+
import java.net.URL;
13+
import java.util.concurrent.CompletableFuture;
14+
15+
public class GithubVersionManager {
16+
17+
private final String GITHUB_API_URL;
18+
private final String currentVersion;
19+
20+
/**
21+
* Create a new GithubVersionManager instance with the plugin name and the current version.
22+
* @param pluginName the name of the plugin (Case sensitive)
23+
* @param currentVersion the current version of the plugin
24+
*/
25+
public GithubVersionManager(String pluginName, String currentVersion) {
26+
this.currentVersion = currentVersion;
27+
this.GITHUB_API_URL = "https://api.github.com/repos/lightPlugins/" + pluginName + "/releases/latest";
28+
}
29+
30+
public CompletableFuture<String> getLatestVersionAsync() {
31+
return CompletableFuture.supplyAsync(() -> {
32+
try {
33+
URI uri = new URI(GITHUB_API_URL);
34+
URL url = uri.toURL();
35+
JSONObject jsonResponse = getJsonObject(url);
36+
return jsonResponse.getString("tag_name");
37+
} catch (Exception e) {
38+
LightCore.instance.getConsolePrinter().printError("Could not check for plugin updates: " + e.getMessage());
39+
return null;
40+
}
41+
});
42+
}
43+
44+
private @NotNull JSONObject getJsonObject(URL url) throws IOException {
45+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
46+
connection.setRequestMethod("GET");
47+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
48+
StringBuilder response = new StringBuilder();
49+
String inputLine;
50+
while ((inputLine = in.readLine()) != null) {
51+
response.append(inputLine);
52+
}
53+
in.close();
54+
return new JSONObject(response.toString());
55+
}
56+
57+
public CompletableFuture<UpdateCheckResult> isUpdateAvailableAsync() {
58+
return getLatestVersionAsync().thenApply(latestVersion -> {
59+
boolean updateAvailable = latestVersion != null && !latestVersion.equals(currentVersion);
60+
return new UpdateCheckResult(updateAvailable, latestVersion);
61+
});
62+
}
63+
64+
public record UpdateCheckResult(boolean updateAvailable, String latestVersion) {
65+
66+
}
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package io.lightstudios.core.github;
2+
3+
import io.lightstudios.core.LightCore;
4+
import io.lightstudios.core.github.checks.CheckLightCoins;
5+
import io.lightstudios.core.github.checks.CheckLightCore;
6+
import lombok.Getter;
7+
8+
import java.util.List;
9+
import java.util.concurrent.CompletableFuture;
10+
import java.util.concurrent.Executors;
11+
import java.util.concurrent.ScheduledExecutorService;
12+
import java.util.concurrent.TimeUnit;
13+
14+
@Getter
15+
public class VersionChecker {
16+
17+
private final CheckLightCore checkLightCore;
18+
private final CheckLightCoins checkLightCoins;
19+
private final ScheduledExecutorService scheduler;
20+
21+
public VersionChecker() {
22+
this.checkLightCore = new CheckLightCore();
23+
this.checkLightCoins = new CheckLightCoins();
24+
this.scheduler = Executors.newScheduledThreadPool(1);
25+
scheduleUpdateChecks();
26+
}
27+
28+
private void scheduleUpdateChecks() {
29+
scheduler.scheduleAtFixedRate(this::checkForUpdates, 0, 2, TimeUnit.HOURS);
30+
}
31+
32+
private void checkForUpdates() {
33+
LightCore.instance.getLogger().info("Checking for plugin updates...");
34+
35+
CompletableFuture<GithubVersionManager.UpdateCheckResult> lightCore =
36+
this.checkLightCore.getVersionManager().isUpdateAvailableAsync();
37+
CompletableFuture<GithubVersionManager.UpdateCheckResult> lightCoins =
38+
this.checkLightCoins.getVersionManager().isUpdateAvailableAsync();
39+
40+
// check for lightCore updates
41+
lightCore.thenAccept(result -> {
42+
if (result.updateAvailable()) {
43+
LightCore.instance.getConsolePrinter().printInfo(List.of(
44+
"An update is available for " + this.checkLightCore.getPluginName() + "!",
45+
"Latest version: " + result.latestVersion(),
46+
"Your version: " + this.checkLightCore.getCurrentVersion(),
47+
"Download it here: https://github.com/lightPlugins/lightCore/releases"
48+
));
49+
} else {
50+
LightCore.instance.getConsolePrinter().printInfo(this.checkLightCore.getPluginName() + " is up to date!");
51+
}
52+
}).exceptionally(ex -> {
53+
LightCore.instance.getConsolePrinter().printError(List.of(
54+
"Failed to check for updates for " + this.checkLightCore.getPluginName() + "!",
55+
"Error: " + ex.getMessage()
56+
));
57+
return null;
58+
});
59+
60+
// check for lightCoins updates
61+
lightCoins.thenAccept(result -> {
62+
if (result.updateAvailable()) {
63+
LightCore.instance.getConsolePrinter().printInfo(List.of(
64+
"An update is available for " + this.checkLightCoins.getPluginName() + "!",
65+
"Latest version: " + result.latestVersion(),
66+
"Your version: " + this.checkLightCoins.getCurrentVersion(),
67+
"Download it here: https://www.spigotmc.org/resources/83862"
68+
));
69+
} else {
70+
LightCore.instance.getConsolePrinter().printInfo(this.checkLightCoins.getPluginName() + " is up to date!");
71+
}
72+
}).exceptionally(ex -> {
73+
LightCore.instance.getConsolePrinter().printError(List.of(
74+
"Failed to check for updates for " + this.checkLightCoins.getPluginName() + "!",
75+
"Error: " + ex.getMessage()
76+
));
77+
return null;
78+
});
79+
}
80+
81+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.lightstudios.core.github.checks;
2+
3+
import io.lightstudios.coins.LightCoins;
4+
import io.lightstudios.core.LightCore;
5+
import io.lightstudios.core.github.GithubVersionManager;
6+
import lombok.Getter;
7+
8+
@Getter
9+
public class CheckLightCoins {
10+
11+
private final String currentVersion;
12+
private final String pluginName = "lightCoins";
13+
private final GithubVersionManager versionManager;
14+
15+
public CheckLightCoins() {
16+
if (LightCore.instance.getHookManager().isExistLightCoins()) {
17+
LightCoins lightCoins = LightCoins.instance;
18+
this.currentVersion = lightCoins.getDescription().getVersion();
19+
this.versionManager = new GithubVersionManager(pluginName, currentVersion);
20+
} else {
21+
this.currentVersion = null;
22+
this.versionManager = null;
23+
}
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.lightstudios.core.github.checks;
2+
import io.lightstudios.core.LightCore;
3+
import io.lightstudios.core.github.GithubVersionManager;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class CheckLightCore {
8+
9+
private final String currentVersion;
10+
private final GithubVersionManager versionManager;
11+
private final String pluginName = "lightCore";
12+
13+
public CheckLightCore() {
14+
this.currentVersion = LightCore.instance.getDescription().getVersion();
15+
this.versionManager = new GithubVersionManager(pluginName, currentVersion);
16+
}
17+
}

src/main/java/io/lightstudios/core/redis/RedisManager.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,49 @@
44
import lombok.Getter;
55
import redis.clients.jedis.Jedis;
66
import redis.clients.jedis.JedisPool;
7-
import redis.clients.jedis.JedisPooled;
7+
import redis.clients.jedis.JedisPoolConfig;
88

9-
import java.net.URI;
10-
import java.net.URISyntaxException;
9+
import java.util.List;
1110

1211
@Getter
1312
public class RedisManager {
1413

15-
private final JedisPool jedisPool;
14+
private JedisPool jedisPool = null;
1615

1716
/**
1817
* Create a new RedisManager instance with redis credentials
1918
* from the core config.
2019
* @param host the host of the Redis server
2120
* @param port the port of the Redis server
2221
* @param password the password of the Redis server
22+
* @param useSSL if the Redis server uses SSL
2323
*/
24-
public RedisManager(String host, int port, String password) {
24+
public RedisManager(String host, int port, String password, boolean useSSL) {
2525
try {
26-
this.jedisPool = new JedisPool("localhost", 6379);
26+
JedisPoolConfig poolConfig = new JedisPoolConfig();
27+
if (password == null || password.isEmpty()) {
28+
this.jedisPool = new JedisPool(poolConfig, host, port, 0, null, useSSL);
29+
} else {
30+
this.jedisPool = new JedisPool(poolConfig, host, port, 0, password, useSSL);
31+
}
2732
} catch (Exception e) {
28-
throw new RuntimeException("Invalid Redis URI", e);
33+
LightCore.instance.getConsolePrinter().printError(List.of(
34+
"Could not connect Redis server on " + host + ":" + port + ". ",
35+
"Please check your credentials in the core.yml from LightCore.",
36+
"Error: " + e.getMessage()
37+
));
38+
return;
2939
}
3040

3141
// test the connection to the Redis server
3242
if (testConnection()) {
33-
LightCore.instance.getConsolePrinter().printInfo("Connected successfully to the provided Redis server.");
43+
LightCore.instance.getConsolePrinter().printInfo("Connection to Redis server on " + host + ":" + port + " was successful!");
3444
} else {
35-
throw new RuntimeException("Could not connect to the provided Redis server with params: "
36-
+ host + ":" + port + " and password: " + password);
45+
LightCore.instance.getConsolePrinter().printError(List.of(
46+
"Could not connect Redis server on " + host + ":" + port + ". ",
47+
"Please check your credentials in the core.yml from LightCore.",
48+
"Error: &4Test connection failed!"
49+
));
3750
}
3851
}
3952

@@ -46,7 +59,13 @@ private boolean testConnection() {
4659
String response = jedis.ping();
4760
return "PONG".equals(response);
4861
} catch (Exception e) {
49-
throw new RuntimeException("Could not connect to the provided Redis server", e);
62+
LightCore.instance.getConsolePrinter().printError(List.of(
63+
"Could not receive a PONG result from your Redis server!",
64+
"Please check your credentials in the core.yml from LightCore",
65+
"or check if your Redis server is running correctly.",
66+
"Error: " + e.getMessage()
67+
));
68+
return false;
5069
}
5170
}
5271

@@ -56,6 +75,7 @@ private boolean testConnection() {
5675
public void close() {
5776
if (jedisPool != null) {
5877
jedisPool.close();
78+
LightCore.instance.getConsolePrinter().printInfo("Redis connection closed!");
5979
}
6080
}
6181
}

src/main/java/io/lightstudios/core/util/ConsolePrinter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ public void printInfo(List<String> messages) {
2626
sendEmptyLine();
2727
}
2828

29+
public void printWarning(String message) {
30+
Bukkit.getConsoleSender().sendMessage(prefix + "§8[§6WARNING§8] §e" + message);
31+
}
32+
33+
public void printWarning(List<String> messages) {
34+
sendEmptyLine();
35+
for (String message : messages) {
36+
Bukkit.getConsoleSender().sendMessage(prefix + "§8[§6WARNING§8] §e" + message);
37+
}
38+
sendEmptyLine();
39+
}
40+
2941
public void printError(String message) {
3042
Bukkit.getConsoleSender().sendMessage(prefix + "§8[§4ERROR§8] §c" + message);
3143
}

src/main/java/io/lightstudios/core/util/files/configs/CoreSettings.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public String syncType() {
4444
public String redisHost() { return config.getString("server-synchronisation.redis.host"); }
4545
public int redisPort() { return config.getInt("server-synchronisation.redis.port"); }
4646
public String redisPassword() { return config.getString("server-synchronisation.redis.password"); }
47+
public boolean redisUseSSL() { return config.getBoolean("server-synchronisation.redis.useSSL"); }
4748
public String serverName() { return config.getString("server-synchronisation.server-name"); }
4849

50+
public boolean checkForUpdates() { return config.getBoolean("checkForUpdates"); }
51+
4952
//protections
5053
public boolean protectionCommandCooldownEnable() { return config.getBoolean("protections.commandCooldown.enable"); }
5154
public long protectionCommandCooldownTime() { return config.getLong("protections.commandCooldown.cooldown"); }

0 commit comments

Comments
 (0)