2323import com .velocitypowered .api .event .proxy .ProxyInitializeEvent ;
2424import com .velocitypowered .api .event .Subscribe ;
2525import com .velocitypowered .api .plugin .Plugin ;
26+ import com .velocitypowered .api .proxy .ProxyServer ;
27+ import lol .hyper .githubreleaseapi .GitHubRelease ;
28+ import lol .hyper .githubreleaseapi .GitHubReleaseAPI ;
2629import lol .hyper .velocityblockversion .tools .ConfigHandler ;
2730import lol .hyper .velocityblockversion .tools .VersionToStrings ;
2831import net .kyori .adventure .text .Component ;
2932import net .kyori .adventure .text .minimessage .MiniMessage ;
3033import org .bstats .velocity .Metrics ;
34+ import org .json .JSONObject ;
3135import 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