20
20
import org .bukkit .event .player .PlayerQuitEvent ;
21
21
import org .bukkit .plugin .java .JavaPlugin ;
22
22
23
+ import org .bukkit .scheduler .BukkitScheduler ;
23
24
import org .mcstats .MetricsLite ;
24
25
25
26
import java .io .File ;
26
27
import java .io .IOException ;
27
28
import java .util .Map ;
28
- import java .util .Timer ;
29
- import java .util .TimerTask ;
30
29
import java .util .UUID ;
31
30
import java .util .logging .Level ;
32
31
@@ -44,8 +43,17 @@ public class MinecraftPush extends JavaPlugin implements Listener
44
43
45
44
public String titleEnd = "" ;
46
45
46
+ boolean canPush = true ;
47
+
47
48
public void onEnable ()
48
49
{
50
+ if (! getServer ().getOnlineMode ())
51
+ {
52
+ getLogger ().severe ("Server is in offline mode! Can't send notifications." );
53
+ canPush = false ;
54
+ return ;
55
+ }
56
+
49
57
getServer ().getPluginManager ().registerEvents (this , this );
50
58
51
59
int users = getUserKeysFileConfiguration ().getKeys (false ).size ();
@@ -68,32 +76,38 @@ public void onEnable()
68
76
else
69
77
titleEnd = " (" + getServer ().getServerName () + ")" ;
70
78
71
- Timer ramUsageTimer = new Timer ();
72
- ramUsageTimer .scheduleAtFixedRate (new RamChecker (), 0 , 10000 *5 );
79
+ try
80
+ {
81
+ BukkitScheduler scheduler = Bukkit .getServer ().getScheduler ();
82
+ scheduler .scheduleSyncRepeatingTask (this , new Runnable ()
83
+ {
84
+ long mb = 1048576 ;
85
+
86
+ @ Override
87
+ public void run ()
88
+ {
89
+ if (Runtime .getRuntime ().freeMemory () < (20 * mb ))
90
+ push ("Server almost out of memory!" , true , true );
91
+ }
92
+ }, 0L , 3000L );
93
+ }
94
+ catch (Error e ) {
95
+ getLogger ().warning ("Your Bukkit version is too old. Cannot send out of memory warnings to operators." );
96
+ }
73
97
74
98
// Plugin Metrics
75
99
try
76
100
{
77
101
MetricsLite m = new MetricsLite (this );
78
102
m .start ();
79
- } catch (Exception e )
103
+ }
104
+ catch (Exception e )
80
105
{
81
106
// Cannot upload data :(
82
- } catch (NoSuchMethodError e )
83
- {
84
- // Old Minecraft version
85
107
}
86
- }
87
-
88
- class RamChecker extends TimerTask
89
- {
90
- long mb = 1048576 ;
91
-
92
- @ Override
93
- public void run ()
108
+ catch (NoSuchMethodError e )
94
109
{
95
- if (Runtime .getRuntime ().freeMemory () < (mb * 20 ))
96
- push ("Server almost out of memory!" , true , true );
110
+ // Old Minecraft version
97
111
}
98
112
}
99
113
@@ -129,11 +143,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
129
143
{
130
144
if (sender .hasPermission ("minecraftpush.receive" ))
131
145
{
132
- // Unfortunately, Pushover user key validation is not yet possible: https://github.com/sps/pushover4j /issues/3
146
+ // Unfortunately, Pushover user key validation is not yet possible: https://github.com/Jop-V/MinecraftPush /issues/2
133
147
getUserKeysFileConfiguration ().set (((Player ) sender ).getUniqueId ().toString (), args [1 ]);
134
148
saveUserKeysFile ();
135
149
136
- sender .sendMessage ("§aPush notifications enabled! §fYou can disable them at any time using /pushover disable." );
150
+ StringBuilder messageBuilder = new StringBuilder ("§aPush notifications enabled!§f " );
151
+ if (! canPush )
152
+ messageBuilder .append ("§6You won't receive any at the moment though, because the server is in offline mode.§f " );
153
+ messageBuilder .append ("You can disable them at any time using /pushover disable." );
154
+ sender .sendMessage (messageBuilder .toString ());
137
155
return true ;
138
156
} else
139
157
{
@@ -168,23 +186,53 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
168
186
169
187
if (cmd .getName ().equalsIgnoreCase ("broadcast" ))
170
188
{
189
+ if (getCmdArgsLength (args ) < 1 )
190
+ return false ;
191
+
171
192
if (sender .hasPermission ("minecraftpush.broadcast" ))
172
193
{
173
- StringBuilder builder = new StringBuilder ();
174
-
175
- for (String s : args )
176
- builder .append (s ).append (' ' );
177
-
178
- String message = builder .toString ().trim ();
179
-
194
+ if (!canPush ) sender .sendMessage ("§cServer is in offline mode, can't notify offline players!" );
195
+ String message = messageFromCmdAgs (args );
180
196
getServer ().broadcastMessage ("§d[Broadcasting] " + message );
181
197
push (message );
182
198
} else sender .sendMessage ("§cYou don't have permission to broadcast!" );
183
199
return true ;
184
200
}
201
+
202
+ if (cmd .getName ().equalsIgnoreCase ("warnops" ))
203
+ {
204
+ if (getCmdArgsLength (args ) < 1 )
205
+ return false ;
206
+
207
+ if (sender .hasPermission ("minecraftpush.warnops" ))
208
+ {
209
+ if (!canPush ) sender .sendMessage ("§cServer is in offline mode, can't notify offline operators!" );
210
+ String message = messageFromCmdAgs (args );
211
+ for (Player p : getServer ().getOnlinePlayers ())
212
+ if (p .isOp ()) p .sendMessage (message );
213
+ push (sender .getName () + " warned: " + message , true , true );
214
+ }
215
+ else if (sender .isOp ())
216
+ sender .sendMessage ("§cYou are an operator yourself..." );
217
+ else
218
+ sender .sendMessage ("§cYou don't have permission to warn operators!" );
219
+
220
+ return true ;
221
+ }
222
+
185
223
return false ;
186
224
}
187
225
226
+ public String messageFromCmdAgs (String [] args )
227
+ {
228
+ StringBuilder builder = new StringBuilder ();
229
+
230
+ for (String s : args )
231
+ builder .append (s ).append (' ' );
232
+
233
+ return builder .toString ().trim ();
234
+ }
235
+
188
236
/**
189
237
* Workaround for some really weird behavior.
190
238
*/
@@ -228,10 +276,14 @@ public void push(String message)
228
276
/**
229
277
* Push a message to all offline players with push notifications enabled.
230
278
* @param message The message to be sent. Color codes will be automatically stripped out.
279
+ * @param opOnly Whether only operators should receive this message.
280
+ * @param highPriority Whether this message is of high priority.
231
281
*/
232
282
@ SuppressWarnings ("SuspiciousMethodCalls" )
233
- public void push (String message , boolean opOnly , boolean warning )
283
+ public void push (String message , boolean opOnly , boolean highPriority )
234
284
{
285
+ if (!canPush ) return ;
286
+
235
287
Map <String , Object > map = getUserKeysFileConfiguration ().getValues (false );
236
288
237
289
for (String playerUuidString : map .keySet ())
@@ -241,7 +293,7 @@ public void push(String message, boolean opOnly, boolean warning)
241
293
String playerPushoverKey = String .valueOf (map .get (playerUuidString ));
242
294
243
295
if (playerPushoverKey .equals ("INVALID" ) || playerPushoverKey .equals ("BANNED" ))
244
- return ;
296
+ continue ;
245
297
246
298
if (onlinePlayer == null /* Player is offline, so a notification needs to be sent */ )
247
299
{
@@ -253,18 +305,16 @@ public void push(String message, boolean opOnly, boolean warning)
253
305
getUserKeysFileConfiguration ().set (playerUuidString , map .get (playerUuidString ) + "BANNED" );
254
306
continue ;
255
307
}
256
- else if ( ((String ) map .get (playerUuidString )).endsWith ("BANNED" ) )
308
+ else if ( ((String ) map .get (playerUuidString )).endsWith ("BANNED" ) )
257
309
{
258
310
String s = (String ) map .get (playerUuidString );
259
311
getUserKeysFileConfiguration ().set (playerUuidString , s .substring (0 , s .indexOf ("BANNED" )));
260
312
}
261
313
262
314
if (opOnly && ! offlinePlayer .isOp ())
263
- {
264
315
continue ;
265
- }
266
316
}
267
- catch (Exception e ) {
317
+ catch (NoSuchMethodError e ) {
268
318
e .printStackTrace ();
269
319
}
270
320
@@ -276,7 +326,7 @@ else if ( ((String )map.get(playerUuidString)).endsWith("BANNED") )
276
326
.setTitle ("Minecraft" + titleEnd )
277
327
.setMessage (ChatColor .stripColor (message ));
278
328
279
- if (warning )
329
+ if (highPriority )
280
330
pm .setPriority (MessagePriority .HIGH );
281
331
282
332
pushoverRestClient .pushMessage (pm .build ());
0 commit comments