2020import org .bukkit .event .player .PlayerQuitEvent ;
2121import org .bukkit .plugin .java .JavaPlugin ;
2222
23+ import org .bukkit .scheduler .BukkitScheduler ;
2324import org .mcstats .MetricsLite ;
2425
2526import java .io .File ;
2627import java .io .IOException ;
2728import java .util .Map ;
28- import java .util .Timer ;
29- import java .util .TimerTask ;
3029import java .util .UUID ;
3130import java .util .logging .Level ;
3231
@@ -44,8 +43,17 @@ public class MinecraftPush extends JavaPlugin implements Listener
4443
4544 public String titleEnd = "" ;
4645
46+ boolean canPush = true ;
47+
4748 public void onEnable ()
4849 {
50+ if (! getServer ().getOnlineMode ())
51+ {
52+ getLogger ().severe ("Server is in offline mode! Can't send notifications." );
53+ canPush = false ;
54+ return ;
55+ }
56+
4957 getServer ().getPluginManager ().registerEvents (this , this );
5058
5159 int users = getUserKeysFileConfiguration ().getKeys (false ).size ();
@@ -68,32 +76,38 @@ public void onEnable()
6876 else
6977 titleEnd = " (" + getServer ().getServerName () + ")" ;
7078
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+ }
7397
7498 // Plugin Metrics
7599 try
76100 {
77101 MetricsLite m = new MetricsLite (this );
78102 m .start ();
79- } catch (Exception e )
103+ }
104+ catch (Exception e )
80105 {
81106 // Cannot upload data :(
82- } catch (NoSuchMethodError e )
83- {
84- // Old Minecraft version
85107 }
86- }
87-
88- class RamChecker extends TimerTask
89- {
90- long mb = 1048576 ;
91-
92- @ Override
93- public void run ()
108+ catch (NoSuchMethodError e )
94109 {
95- if (Runtime .getRuntime ().freeMemory () < (mb * 20 ))
96- push ("Server almost out of memory!" , true , true );
110+ // Old Minecraft version
97111 }
98112 }
99113
@@ -129,11 +143,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
129143 {
130144 if (sender .hasPermission ("minecraftpush.receive" ))
131145 {
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
133147 getUserKeysFileConfiguration ().set (((Player ) sender ).getUniqueId ().toString (), args [1 ]);
134148 saveUserKeysFile ();
135149
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 ());
137155 return true ;
138156 } else
139157 {
@@ -168,23 +186,53 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
168186
169187 if (cmd .getName ().equalsIgnoreCase ("broadcast" ))
170188 {
189+ if (getCmdArgsLength (args ) < 1 )
190+ return false ;
191+
171192 if (sender .hasPermission ("minecraftpush.broadcast" ))
172193 {
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 );
180196 getServer ().broadcastMessage ("§d[Broadcasting] " + message );
181197 push (message );
182198 } else sender .sendMessage ("§cYou don't have permission to broadcast!" );
183199 return true ;
184200 }
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+
185223 return false ;
186224 }
187225
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+
188236 /**
189237 * Workaround for some really weird behavior.
190238 */
@@ -228,10 +276,14 @@ public void push(String message)
228276 /**
229277 * Push a message to all offline players with push notifications enabled.
230278 * @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.
231281 */
232282 @ SuppressWarnings ("SuspiciousMethodCalls" )
233- public void push (String message , boolean opOnly , boolean warning )
283+ public void push (String message , boolean opOnly , boolean highPriority )
234284 {
285+ if (!canPush ) return ;
286+
235287 Map <String , Object > map = getUserKeysFileConfiguration ().getValues (false );
236288
237289 for (String playerUuidString : map .keySet ())
@@ -241,7 +293,7 @@ public void push(String message, boolean opOnly, boolean warning)
241293 String playerPushoverKey = String .valueOf (map .get (playerUuidString ));
242294
243295 if (playerPushoverKey .equals ("INVALID" ) || playerPushoverKey .equals ("BANNED" ))
244- return ;
296+ continue ;
245297
246298 if (onlinePlayer == null /* Player is offline, so a notification needs to be sent */ )
247299 {
@@ -253,18 +305,16 @@ public void push(String message, boolean opOnly, boolean warning)
253305 getUserKeysFileConfiguration ().set (playerUuidString , map .get (playerUuidString ) + "BANNED" );
254306 continue ;
255307 }
256- else if ( ((String ) map .get (playerUuidString )).endsWith ("BANNED" ) )
308+ else if ( ((String ) map .get (playerUuidString )).endsWith ("BANNED" ) )
257309 {
258310 String s = (String ) map .get (playerUuidString );
259311 getUserKeysFileConfiguration ().set (playerUuidString , s .substring (0 , s .indexOf ("BANNED" )));
260312 }
261313
262314 if (opOnly && ! offlinePlayer .isOp ())
263- {
264315 continue ;
265- }
266316 }
267- catch (Exception e ) {
317+ catch (NoSuchMethodError e ) {
268318 e .printStackTrace ();
269319 }
270320
@@ -276,7 +326,7 @@ else if ( ((String )map.get(playerUuidString)).endsWith("BANNED") )
276326 .setTitle ("Minecraft" + titleEnd )
277327 .setMessage (ChatColor .stripColor (message ));
278328
279- if (warning )
329+ if (highPriority )
280330 pm .setPriority (MessagePriority .HIGH );
281331
282332 pushoverRestClient .pushMessage (pm .build ());
0 commit comments