1
1
package net .jopv .minecraft .bukkit .minecraftpush ;
2
2
3
+ import net .pushover .client .MessagePriority ;
3
4
import net .pushover .client .PushoverException ;
4
5
import net .pushover .client .PushoverMessage ;
5
6
import net .pushover .client .PushoverRestClient ;
6
7
7
8
import org .bukkit .Bukkit ;
8
9
import org .bukkit .ChatColor ;
10
+ import org .bukkit .OfflinePlayer ;
9
11
import org .bukkit .command .Command ;
10
12
import org .bukkit .command .CommandSender ;
11
13
import org .bukkit .configuration .file .FileConfiguration ;
23
25
import java .io .File ;
24
26
import java .io .IOException ;
25
27
import java .util .Map ;
28
+ import java .util .Timer ;
29
+ import java .util .TimerTask ;
26
30
import java .util .UUID ;
27
31
import java .util .logging .Level ;
28
32
@@ -40,7 +44,8 @@ public class MinecraftPush extends JavaPlugin implements Listener
40
44
41
45
public String titleEnd = "" ;
42
46
43
- public void onEnable () {
47
+ public void onEnable ()
48
+ {
44
49
getServer ().getPluginManager ().registerEvents (this , this );
45
50
46
51
int users = getUserKeysFileConfiguration ().getKeys (false ).size ();
@@ -63,22 +68,43 @@ public void onEnable() {
63
68
else
64
69
titleEnd = " (" + getServer ().getServerName () + ")" ;
65
70
71
+ Timer ramUsageTimer = new Timer ();
72
+ ramUsageTimer .scheduleAtFixedRate (new RamChecker (), 0 , 10000 *5 );
73
+
66
74
// Plugin Metrics
67
- try {
75
+ try
76
+ {
68
77
MetricsLite m = new MetricsLite (this );
69
78
m .start ();
70
- }
71
- catch ( Exception e ) {
79
+ } catch ( Exception e )
80
+ {
72
81
// Cannot upload data :(
82
+ } catch (NoSuchMethodError e )
83
+ {
84
+ // Old Minecraft version
85
+ }
86
+ }
87
+
88
+ class RamChecker extends TimerTask
89
+ {
90
+ long mb = 1048576 ;
91
+
92
+ @ Override
93
+ public void run ()
94
+ {
95
+ if (Runtime .getRuntime ().freeMemory () < (mb * 20 ))
96
+ push ("Server almost out of memory!" , true , true );
73
97
}
74
98
}
75
99
76
100
int getAmountOfInvalidUsers ()
77
101
{
78
102
int counter = 0 ;
79
103
80
- for (Map .Entry entry : getUserKeysFileConfiguration ().getValues (false ).entrySet ()) {
81
- if (entry .getValue ().equals ("INVALID" )) {
104
+ for (Map .Entry entry : getUserKeysFileConfiguration ().getValues (false ).entrySet ())
105
+ {
106
+ if (entry .getValue ().equals ("INVALID" ))
107
+ {
82
108
counter ++;
83
109
}
84
110
}
@@ -87,10 +113,11 @@ int getAmountOfInvalidUsers()
87
113
return counter ;
88
114
}
89
115
90
- public boolean onCommand (CommandSender sender , Command cmd , String label , String [] args ) {
116
+ public boolean onCommand (CommandSender sender , Command cmd , String label , String [] args )
117
+ {
91
118
if (cmd .getName ().equalsIgnoreCase ("pushover" ))
92
119
{
93
- if (! (sender instanceof Player ))
120
+ if (!(sender instanceof Player ))
94
121
{
95
122
sender .sendMessage ("You can only enable or disable notifications as a player." );
96
123
return true ;
@@ -108,15 +135,13 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
108
135
109
136
sender .sendMessage ("§aPush notifications enabled! §fYou can disable them at any time using /pushover disable." );
110
137
return true ;
111
- }
112
- else
138
+ } else
113
139
{
114
140
sender .sendMessage ("§cYou don't have permission to receive push notifications!" );
115
141
return true ;
116
142
}
117
143
}
118
- }
119
- else if (getCmdArgsLength (args ) == 0 )
144
+ } else if (getCmdArgsLength (args ) == 0 )
120
145
if (args [0 ].equalsIgnoreCase ("disable" ))
121
146
{
122
147
getUserKeysFileConfiguration ().set (((Player ) sender ).getUniqueId ().toString (), null );
@@ -134,10 +159,10 @@ else if (getCmdArgsLength(args) == 0)
134
159
sender .sendMessage (new String []{
135
160
"Use the /pushover command to enable or disable push notifications." ,
136
161
"MinecraftPush, Copyright (C) Jop Vernooij, 2014. http://www.jopv.net/ " +
137
- "This is NOT an official Pushover app. Pushover is a trademark and product of Superblock, LLC. " +
138
- "This plugin is powered by pushover4j: https://github.com/sps/pushover4j " +
139
- "Source available at https://github.com/Jop-V/MinecraftPush/. " +
140
- "If this plugin is an important part of your Minecraft, please donate a little bitcoin: :) 1LkJKBJuadQxdZN46yuyWzn2kncSLm1tvU" });
162
+ "This is NOT an official Pushover app. Pushover is a trademark and product of Superblock, LLC. " +
163
+ "This plugin is powered by pushover4j: https://github.com/sps/pushover4j " +
164
+ "Source available at https://github.com/Jop-V/MinecraftPush/. " +
165
+ "If this plugin is an important part of your Minecraft, please donate a little bitcoin: :) 1LkJKBJuadQxdZN46yuyWzn2kncSLm1tvU" });
141
166
return true ;
142
167
}
143
168
@@ -154,8 +179,7 @@ else if (getCmdArgsLength(args) == 0)
154
179
155
180
getServer ().broadcastMessage ("§d[Broadcasting] " + message );
156
181
push (message );
157
- }
158
- else sender .sendMessage ("§cYou don't have permission to broadcast!" );
182
+ } else sender .sendMessage ("§cYou don't have permission to broadcast!" );
159
183
return true ;
160
184
}
161
185
return false ;
@@ -164,18 +188,18 @@ else if (getCmdArgsLength(args) == 0)
164
188
/**
165
189
* Workaround for some really weird behavior.
166
190
*/
167
- public int getCmdArgsLength (String []args )
191
+ public int getCmdArgsLength (String [] args )
168
192
{
169
193
if (args != null )
170
194
return args .length - 1 ;
171
195
else
172
196
return -1 ;
173
197
}
174
-
175
- @ EventHandler (priority = EventPriority .MONITOR )
198
+
199
+ @ EventHandler (priority = EventPriority .MONITOR )
176
200
public void onPlayerJoin (PlayerJoinEvent e )
177
201
{
178
- push ( e .getJoinMessage () );
202
+ push (e .getJoinMessage ());
179
203
180
204
// Invalid Pushover user key warning.
181
205
if (invalidUsers == 0 )
@@ -190,18 +214,23 @@ public void onPlayerJoin(PlayerJoinEvent e)
190
214
}
191
215
}
192
216
193
- @ EventHandler (priority = EventPriority .MONITOR )
217
+ @ EventHandler (priority = EventPriority .MONITOR )
194
218
public void onPlayerQuit (PlayerQuitEvent e )
195
219
{
196
- push ( e .getQuitMessage () );
220
+ push (e .getQuitMessage ());
221
+ }
222
+
223
+ public void push (String message )
224
+ {
225
+ push (message , false , false );
197
226
}
198
227
199
228
/**
200
229
* Push a message to all offline players with push notifications enabled.
201
230
* @param message The message to be sent. Color codes will be automatically stripped out.
202
231
*/
203
232
@ SuppressWarnings ("SuspiciousMethodCalls" )
204
- public void push (String message )
233
+ public void push (String message , boolean opOnly , boolean warning )
205
234
{
206
235
Map <String , Object > map = getUserKeysFileConfiguration ().getValues (false );
207
236
@@ -218,28 +247,39 @@ public void push(String message)
218
247
{
219
248
try
220
249
{
221
- if (Bukkit .getOfflinePlayer (playerUuid ).isBanned ())
250
+ OfflinePlayer offlinePlayer = Bukkit .getOfflinePlayer (playerUuid );
251
+ if (offlinePlayer .isBanned ())
222
252
{
223
253
getUserKeysFileConfiguration ().set (playerUuidString , map .get (playerUuidString ) + "BANNED" );
224
- return ;
254
+ continue ;
225
255
}
226
256
else if ( ((String )map .get (playerUuidString )).endsWith ("BANNED" ) )
227
257
{
228
258
String s = (String ) map .get (playerUuidString );
229
259
getUserKeysFileConfiguration ().set (playerUuidString , s .substring (0 , s .indexOf ("BANNED" )));
230
260
}
261
+
262
+ if (opOnly && ! offlinePlayer .isOp ())
263
+ {
264
+ continue ;
265
+ }
266
+ }
267
+ catch (Exception e ) {
268
+ e .printStackTrace ();
231
269
}
232
- catch (Exception ignored ) {}
233
270
234
271
try
235
272
{
236
- pushoverRestClient . pushMessage ( PushoverMessage .builderWithApiToken ("autoU13MYYXYxMaeupaqF7U7mBe9Bj" )
273
+ PushoverMessage . Builder pm = PushoverMessage .builderWithApiToken ("autoU13MYYXYxMaeupaqF7U7mBe9Bj" )
237
274
.setUserId (playerPushoverKey )
238
275
.setSound ("gamelan" )
239
276
.setTitle ("Minecraft" + titleEnd )
240
- .setMessage (ChatColor .stripColor (message ))
241
- .build ()
242
- );
277
+ .setMessage (ChatColor .stripColor (message ));
278
+
279
+ if (warning )
280
+ pm .setPriority (MessagePriority .HIGH );
281
+
282
+ pushoverRestClient .pushMessage (pm .build ());
243
283
}
244
284
catch (PushoverException e )
245
285
{
@@ -286,7 +326,7 @@ public Player getOnlinePlayerByUuid(UUID uuid) {
286
326
{
287
327
player = getServer ().getPlayer (uuid );
288
328
}
289
- catch (Exception e )
329
+ catch (NoSuchMethodError e )
290
330
{
291
331
for (Player p : getServer ().getOnlinePlayers ())
292
332
if (p .getUniqueId ().equals (uuid ))
0 commit comments