21
21
import java .io .InputStreamReader ;
22
22
import java .net .HttpURLConnection ;
23
23
import java .net .URL ;
24
-
25
- import org .bukkit .plugin .Plugin ;
24
+ import java .util .logging .Level ;
26
25
27
26
import com .comphenix .protocol .ProtocolLibrary ;
28
27
import com .comphenix .protocol .error .Report ;
28
+ import com .comphenix .protocol .utility .Closer ;
29
29
import com .google .common .base .Charsets ;
30
30
31
31
/**
32
32
* Adapted version of the Bukkit updater for use with Spigot resources
33
- *
34
33
* @author dmulloy2
35
34
*/
36
35
37
36
public final class SpigotUpdater extends Updater {
38
37
private String remoteVersion ;
39
38
40
- public SpigotUpdater (Plugin plugin , UpdateType type , boolean announce ) {
39
+ public SpigotUpdater (ProtocolLibrary plugin , UpdateType type , boolean announce ) {
41
40
super (plugin , type , announce );
42
41
}
43
42
@@ -70,34 +69,45 @@ public void run() {
70
69
result = UpdateResult .NO_UPDATE ;
71
70
}
72
71
} catch (Throwable ex ) {
73
- ProtocolLibrary .getErrorReporter ().reportDetailed (
74
- SpigotUpdater .this , Report .newBuilder (REPORT_CANNOT_UPDATE_PLUGIN ).error (ex ).callerParam (this ));
75
- } finally {
76
- // Invoke the listeners on the main thread
77
- for (Runnable listener : listeners ) {
78
- plugin .getServer ().getScheduler ().scheduleSyncDelayedTask (plugin , listener );
79
- }
80
- }
72
+ if (ProtocolLibrary .getConfiguration ().isDebug ()) {
73
+ ProtocolLibrary .getErrorReporter ().reportDetailed (
74
+ SpigotUpdater .this , Report .newBuilder (REPORT_CANNOT_UPDATE_PLUGIN ).error (ex ).callerParam (this ));
75
+ } else {
76
+ plugin .getLogger ().log (Level .WARNING , "Failed to check for updates: " + ex );
77
+ }
78
+
79
+ ProtocolLibrary .disableUpdates ();
80
+ } finally {
81
+ // Invoke the listeners on the main thread
82
+ for (Runnable listener : listeners ) {
83
+ plugin .getServer ().getScheduler ().scheduleSyncDelayedTask (plugin , listener );
84
+ }
85
+ }
81
86
}
82
87
}
83
88
84
- private static final String RESOURCE_URL = "https://www.spigotmc.org/resources/protocollib.1997/" ;
85
- private static final String API_URL = "http://www.spigotmc.org/api/general.php" ;
89
+ private static final String PROTOCOL = "https://" ;
90
+ private static final String RESOURCE_URL = PROTOCOL + "www.spigotmc.org/resources/protocollib.1997/" ;
91
+ private static final String API_URL = PROTOCOL + "www.spigotmc.org/api/general.php" ;
86
92
private static final String ACTION = "POST" ;
87
93
88
94
private static final int ID = 1997 ;
89
95
private static final byte [] API_KEY = ("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=" + ID ).getBytes (Charsets .UTF_8 );
90
96
91
- private String getSpigotVersion () throws IOException {
92
- HttpURLConnection con = (HttpURLConnection ) new URL (API_URL ).openConnection ();
93
- con .setDoOutput (true );
94
- con .setRequestMethod (ACTION );
95
- con .getOutputStream ().write (API_KEY );
96
- String version = new BufferedReader (new InputStreamReader (con .getInputStream ())).readLine ();
97
- if (version .length () <= 7 ) {
98
- return version ;
99
- }
97
+ public String getSpigotVersion () throws IOException {
98
+ Closer closer = Closer .create ();
100
99
101
- return null ;
100
+ try {
101
+ HttpURLConnection con = (HttpURLConnection ) new URL (API_URL ).openConnection ();
102
+ con .setDoOutput (true );
103
+ con .setRequestMethod (ACTION );
104
+ con .getOutputStream ().write (API_KEY );
105
+
106
+ InputStreamReader isr = closer .register (new InputStreamReader (con .getInputStream ()));
107
+ BufferedReader br = closer .register (new BufferedReader (isr ));
108
+ return br .readLine ();
109
+ } finally {
110
+ closer .close ();
111
+ }
102
112
}
103
113
}
0 commit comments