1
1
package fr .lumi ;
2
2
3
+ import com .google .gson .JsonArray ;
4
+ import com .google .gson .JsonElement ;
5
+ import com .google .gson .JsonObject ;
3
6
import fr .lumi .Commandes .CommandRunnerCommand ;
4
7
import fr .lumi .Commandes .CommandRunnerEditor ;
5
8
import fr .lumi .Commandes .CommandRunnerHelp ;
15
18
import org .bukkit .Bukkit ;
16
19
17
20
import java .io .*;
21
+ import java .net .HttpURLConnection ;
22
+ import java .net .URL ;
23
+ import java .io .BufferedReader ;
24
+ import java .io .InputStreamReader ;
25
+ import java .io .IOException ;
26
+ import java .net .HttpURLConnection ;
27
+ import java .net .URL ;
28
+ import java .util .logging .Level ;
29
+ import com .google .gson .JsonArray ;
30
+ import com .google .gson .JsonElement ;
31
+ import com .google .gson .JsonParser ;
32
+ import com .google .gson .JsonObject ;
18
33
import java .util .ArrayList ;
19
34
import java .util .List ;
20
35
import java .util .Objects ;
@@ -24,7 +39,7 @@ public final class Main extends JavaPlugin {
24
39
25
40
private String [] Logo ={
26
41
"&e&9 &6__ __ &e " ,
27
- "&e&9 /\\ &6/ |\\ /|| \\ &e| &9Auto&6Commands &aVersion &e1.5.3" ,
42
+ "&e&9 /\\ &6/ |\\ /|| \\ &e| &9Auto&6Commands &aVersion &e" + this . getDescription (). getVersion () ,
28
43
"&e&9/--\\ &6\\ __| ||__/&e| &8running on bukkit - paper" ,
29
44
"" };
30
45
@@ -97,7 +112,7 @@ public Utilities getUt(){
97
112
return m_ut ;
98
113
}
99
114
100
-
115
+ // command list, core of the plugin holding current commands
101
116
private List <autocommand > commandList = new ArrayList <autocommand >();
102
117
public List <autocommand > getcommandList (){
103
118
return commandList ;
@@ -126,9 +141,69 @@ public void init(){
126
141
}
127
142
128
143
144
+ public String callGithubForTag (){
145
+ StringBuilder response = new StringBuilder ();
146
+ try {
147
+ // Make HTTP GET request
148
+ URL url = new URL ("https://api.github.com/repos/lumi-git/AutoCommands/tags" );
149
+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
150
+ connection .setRequestMethod ("GET" );
151
+ connection .setRequestProperty ("Accept" , "application/json" );
152
+
153
+ // Check response code
154
+ if (connection .getResponseCode () != 200 ) {
155
+ throw new IOException ("Failed to get response from GitHub API" );
156
+ }
157
+
158
+ // Read response
159
+ BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
160
+
161
+ String line ;
162
+ while ((line = reader .readLine ()) != null ) {
163
+ response .append (line );
164
+ }
165
+ reader .close ();
166
+ } catch (IOException e ) {
167
+ getLogger ().log (Level .WARNING , "Failed to check for a new version on spigot." , e );
168
+ }
169
+ return response .toString ();
170
+ }
171
+
172
+ public String VerifyPluginVersion () {
173
+ String spigotResponse = "" ;
174
+ String currentVersion = this .getDescription ().getVersion ();
175
+
176
+ String response = callGithubForTag ();
177
+
178
+ // Parse JSON response
179
+ JsonParser parser = new JsonParser ();
180
+ JsonElement jsonElement = parser .parse (response );
181
+ if (jsonElement .isJsonArray ()) {
182
+ JsonArray jsonArray = jsonElement .getAsJsonArray ();
183
+ if (jsonArray .size () > 0 ) {
184
+ JsonObject latestTag = jsonArray .get (0 ).getAsJsonObject ();
185
+ spigotResponse = latestTag .get ("name" ).getAsString ();
186
+ }
187
+ }
188
+
189
+ if (spigotResponse .equals ("" )) {
190
+ return "&cFailed to check for a new version on spigot." ;
191
+ }
192
+
193
+ if (spigotResponse .equals (currentVersion )) {
194
+ return "&aYou are running the latest version of AutoCommands " + currentVersion +" !" ;
195
+ }
196
+
197
+ return "&eAutoCommands &a&l" + spigotResponse +" &eis available! &chttps://www.spigotmc.org/resources/acmd-%E2%8F%B0-%E2%8F%B3-autocommands-1-13-1-20-4.100090" ;
198
+ }
199
+
129
200
@ Override
130
201
public void onEnable () {
131
202
203
+ // verify if the plugin is up to date and send a message to the admins
204
+ String broadcastMessage = ChatColor .translateAlternateColorCodes ('&' ,getConfig ().getString ("Prefix" )+VerifyPluginVersion ());
205
+ Bukkit .broadcast (broadcastMessage , "bukkit.broadcast.admin" );
206
+
132
207
if (Bukkit .getPluginManager ().getPlugin ("PlaceholderAPI" ) != null ) {
133
208
papiPresent = true ;
134
209
Bukkit .getConsoleSender ().sendMessage (ChatColor .translateAlternateColorCodes ('&' ,config .getString ("ConsolePrefix" )+" &aPlaceholderAPI found" ));
@@ -234,17 +309,8 @@ public boolean loadCommandsTimeTable() {
234
309
for (String i : getCommandsConfig ().getKeys (false )){
235
310
autocommand acmd = new autocommand (this );
236
311
if (acmd .getInConfig (getCommandsConfig (),this ,i )){
237
- /*
238
- int index=0;
239
- while(acmdIdExist(acmd.getID())){
240
- Bukkit.getConsoleSender().sendMessage(acmd.getID());
241
- acmd.setID(acmd.getID() +"_"+index);
242
- index++;
243
- }*/
244
-
245
312
if (getConfig ().getBoolean ("DisplayAcmdInConsole" ))
246
313
acmd .printToConsole ();
247
-
248
314
acmd .addToScheduler ();
249
315
commandList .add (acmd );
250
316
acmd .saveInConfig (commandsConfig ,this );
0 commit comments