Skip to content

Commit 48bd639

Browse files
committed
update checker + correct version bump
1 parent 7c50972 commit 48bd639

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

Diff for: pom.xml

+19-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<groupId>lol.hyper</groupId>
2525
<artifactId>velocityblockversion</artifactId>
26-
<version>1.0.1</version>
26+
<version>1.0.2</version>
2727
<packaging>jar</packaging>
2828

2929
<name>VelocityBlockVersion</name>
@@ -41,6 +41,19 @@
4141
</resource>
4242
</resources>
4343
<plugins>
44+
<plugin>
45+
<artifactId>maven-clean-plugin</artifactId>
46+
<version>3.1.0</version>
47+
<executions>
48+
<execution>
49+
<id>auto-clean</id>
50+
<phase>initialize</phase>
51+
<goals>
52+
<goal>clean</goal>
53+
</goals>
54+
</execution>
55+
</executions>
56+
</plugin>
4457
<plugin>
4558
<groupId>org.apache.maven.plugins</groupId>
4659
<artifactId>maven-compiler-plugin</artifactId>
@@ -143,5 +156,10 @@
143156
<version>3.0.0</version>
144157
<scope>compile</scope>
145158
</dependency>
159+
<dependency>
160+
<groupId>lol.hyper</groupId>
161+
<artifactId>github-release-api</artifactId>
162+
<version>1.0.1</version>
163+
</dependency>
146164
</dependencies>
147165
</project>

Diff for: src/main/java/lol/hyper/velocityblockversion/VelocityBlockVersion.java

+50-2
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,28 @@
2323
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
2424
import com.velocitypowered.api.event.Subscribe;
2525
import com.velocitypowered.api.plugin.Plugin;
26+
import com.velocitypowered.api.proxy.ProxyServer;
27+
import lol.hyper.githubreleaseapi.GitHubRelease;
28+
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
2629
import lol.hyper.velocityblockversion.tools.ConfigHandler;
2730
import lol.hyper.velocityblockversion.tools.VersionToStrings;
2831
import net.kyori.adventure.text.Component;
2932
import net.kyori.adventure.text.minimessage.MiniMessage;
3033
import org.bstats.velocity.Metrics;
34+
import org.json.JSONObject;
3135
import org.slf4j.Logger;
3236

37+
import java.io.BufferedReader;
38+
import java.io.IOException;
39+
import java.io.InputStream;
40+
import java.io.InputStreamReader;
41+
import java.nio.charset.StandardCharsets;
42+
import java.util.stream.Collectors;
43+
3344
@Plugin(
3445
id = "velocityblockversion",
3546
name = "VelocityBlockVersion",
36-
version = "1.0",
47+
version = "1.0.2",
3748
authors = {"hyperdefined"},
3849
description = "Block certain Minecraft versions from connecting to your network."
3950
)
@@ -43,11 +54,24 @@ public class VelocityBlockVersion {
4354

4455
public final Logger logger;
4556
private final Metrics.Factory metricsFactory;
57+
ProxyServer server;
58+
String currentVersion;
4659

4760
@Inject
48-
public VelocityBlockVersion(Logger logger, Metrics.Factory metricsFactory) {
61+
public VelocityBlockVersion(ProxyServer server, Logger logger, Metrics.Factory metricsFactory) {
62+
this.server = server;
4963
this.logger = logger;
5064
this.metricsFactory = metricsFactory;
65+
// this kinda sucks but whatever
66+
InputStream json = VelocityBlockVersion.class.getResourceAsStream("/velocity-plugin.json");
67+
if (json != null) {
68+
String text = new BufferedReader(
69+
new InputStreamReader(json, StandardCharsets.UTF_8))
70+
.lines()
71+
.collect(Collectors.joining("\n"));
72+
JSONObject version = new JSONObject(text);
73+
currentVersion = version.getString("version");
74+
}
5175
}
5276

5377
@Subscribe
@@ -56,6 +80,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
5680
configHandler = new ConfigHandler(this);
5781
configHandler.loadConfig();
5882
Metrics metrics = metricsFactory.make(this, 13308);
83+
server.getScheduler().buildTask(this, this::checkForUpdates).schedule();
5984
}
6085

6186
@Subscribe(order = PostOrder.FIRST)
@@ -76,4 +101,27 @@ public void onPlayerLogin(PreLoginEvent event) {
76101
+ VersionToStrings.versionStrings.get(version) + " which is blocked!");
77102
}
78103
}
104+
105+
public void checkForUpdates() {
106+
GitHubReleaseAPI api;
107+
try {
108+
api = new GitHubReleaseAPI("velocityblockversion", "hyperdefined");
109+
} catch (IOException e) {
110+
logger.warn("Unable to check updates!");
111+
e.printStackTrace();
112+
return;
113+
}
114+
GitHubRelease current = api.getReleaseByTag(currentVersion);
115+
GitHubRelease latest = api.getLatestVersion();
116+
if (current == null) {
117+
logger.warn("You are running a version that does not exist on GitHub. If you are in a dev environment, you can ignore this. Otherwise, this is a bug!");
118+
return;
119+
}
120+
int buildsBehind = api.getBuildsBehind(current);
121+
if (buildsBehind == 0) {
122+
logger.info("You are running the latest version.");
123+
} else {
124+
logger.warn("A new version is available (" + latest.getTagVersion() + ")! You are running version " + current.getTagVersion() + ". You are " + buildsBehind + " version(s) behind.");
125+
}
126+
}
79127
}

0 commit comments

Comments
 (0)