23
23
import com .velocitypowered .api .event .proxy .ProxyInitializeEvent ;
24
24
import com .velocitypowered .api .event .Subscribe ;
25
25
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 ;
26
29
import lol .hyper .velocityblockversion .tools .ConfigHandler ;
27
30
import lol .hyper .velocityblockversion .tools .VersionToStrings ;
28
31
import net .kyori .adventure .text .Component ;
29
32
import net .kyori .adventure .text .minimessage .MiniMessage ;
30
33
import org .bstats .velocity .Metrics ;
34
+ import org .json .JSONObject ;
31
35
import org .slf4j .Logger ;
32
36
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
+
33
44
@ Plugin (
34
45
id = "velocityblockversion" ,
35
46
name = "VelocityBlockVersion" ,
36
- version = "1.0" ,
47
+ version = "1.0.2 " ,
37
48
authors = {"hyperdefined" },
38
49
description = "Block certain Minecraft versions from connecting to your network."
39
50
)
@@ -43,11 +54,24 @@ public class VelocityBlockVersion {
43
54
44
55
public final Logger logger ;
45
56
private final Metrics .Factory metricsFactory ;
57
+ ProxyServer server ;
58
+ String currentVersion ;
46
59
47
60
@ Inject
48
- public VelocityBlockVersion (Logger logger , Metrics .Factory metricsFactory ) {
61
+ public VelocityBlockVersion (ProxyServer server , Logger logger , Metrics .Factory metricsFactory ) {
62
+ this .server = server ;
49
63
this .logger = logger ;
50
64
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
+ }
51
75
}
52
76
53
77
@ Subscribe
@@ -56,6 +80,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
56
80
configHandler = new ConfigHandler (this );
57
81
configHandler .loadConfig ();
58
82
Metrics metrics = metricsFactory .make (this , 13308 );
83
+ server .getScheduler ().buildTask (this , this ::checkForUpdates ).schedule ();
59
84
}
60
85
61
86
@ Subscribe (order = PostOrder .FIRST )
@@ -76,4 +101,27 @@ public void onPlayerLogin(PreLoginEvent event) {
76
101
+ VersionToStrings .versionStrings .get (version ) + " which is blocked!" );
77
102
}
78
103
}
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
+ }
79
127
}
0 commit comments