Skip to content

Commit 9732c62

Browse files
author
jobukkit
committed
op ram warning, bugfixes
1 parent ab86562 commit 9732c62

File tree

2 files changed

+74
-34
lines changed

2 files changed

+74
-34
lines changed

src/net/jopv/minecraft/bukkit/minecraftpush/MinecraftPush.java

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package net.jopv.minecraft.bukkit.minecraftpush;
22

3+
import net.pushover.client.MessagePriority;
34
import net.pushover.client.PushoverException;
45
import net.pushover.client.PushoverMessage;
56
import net.pushover.client.PushoverRestClient;
67

78
import org.bukkit.Bukkit;
89
import org.bukkit.ChatColor;
10+
import org.bukkit.OfflinePlayer;
911
import org.bukkit.command.Command;
1012
import org.bukkit.command.CommandSender;
1113
import org.bukkit.configuration.file.FileConfiguration;
@@ -23,6 +25,8 @@
2325
import java.io.File;
2426
import java.io.IOException;
2527
import java.util.Map;
28+
import java.util.Timer;
29+
import java.util.TimerTask;
2630
import java.util.UUID;
2731
import java.util.logging.Level;
2832

@@ -40,7 +44,8 @@ public class MinecraftPush extends JavaPlugin implements Listener
4044

4145
public String titleEnd = "";
4246

43-
public void onEnable() {
47+
public void onEnable()
48+
{
4449
getServer().getPluginManager().registerEvents(this, this);
4550

4651
int users = getUserKeysFileConfiguration().getKeys(false).size();
@@ -63,22 +68,43 @@ public void onEnable() {
6368
else
6469
titleEnd = " (" + getServer().getServerName() + ")";
6570

71+
Timer ramUsageTimer = new Timer();
72+
ramUsageTimer.scheduleAtFixedRate(new RamChecker(), 0, 10000*5);
73+
6674
// Plugin Metrics
67-
try {
75+
try
76+
{
6877
MetricsLite m = new MetricsLite(this);
6978
m.start();
70-
}
71-
catch (Exception e) {
79+
} catch (Exception e)
80+
{
7281
// 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);
7397
}
7498
}
7599

76100
int getAmountOfInvalidUsers()
77101
{
78102
int counter = 0;
79103

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+
{
82108
counter++;
83109
}
84110
}
@@ -87,10 +113,11 @@ int getAmountOfInvalidUsers()
87113
return counter;
88114
}
89115

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+
{
91118
if (cmd.getName().equalsIgnoreCase("pushover"))
92119
{
93-
if (! (sender instanceof Player))
120+
if (!(sender instanceof Player))
94121
{
95122
sender.sendMessage("You can only enable or disable notifications as a player.");
96123
return true;
@@ -108,15 +135,13 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
108135

109136
sender.sendMessage("§aPush notifications enabled! §fYou can disable them at any time using /pushover disable.");
110137
return true;
111-
}
112-
else
138+
} else
113139
{
114140
sender.sendMessage("§cYou don't have permission to receive push notifications!");
115141
return true;
116142
}
117143
}
118-
}
119-
else if (getCmdArgsLength(args) == 0)
144+
} else if (getCmdArgsLength(args) == 0)
120145
if (args[0].equalsIgnoreCase("disable"))
121146
{
122147
getUserKeysFileConfiguration().set(((Player) sender).getUniqueId().toString(), null);
@@ -134,10 +159,10 @@ else if (getCmdArgsLength(args) == 0)
134159
sender.sendMessage(new String[]{
135160
"Use the /pushover command to enable or disable push notifications.",
136161
"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"});
141166
return true;
142167
}
143168

@@ -154,8 +179,7 @@ else if (getCmdArgsLength(args) == 0)
154179

155180
getServer().broadcastMessage("§d[Broadcasting] " + message);
156181
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!");
159183
return true;
160184
}
161185
return false;
@@ -164,18 +188,18 @@ else if (getCmdArgsLength(args) == 0)
164188
/**
165189
* Workaround for some really weird behavior.
166190
*/
167-
public int getCmdArgsLength(String[]args)
191+
public int getCmdArgsLength(String[] args)
168192
{
169193
if (args != null)
170194
return args.length - 1;
171195
else
172196
return -1;
173197
}
174-
175-
@EventHandler (priority = EventPriority.MONITOR)
198+
199+
@EventHandler(priority = EventPriority.MONITOR)
176200
public void onPlayerJoin(PlayerJoinEvent e)
177201
{
178-
push( e.getJoinMessage() );
202+
push(e.getJoinMessage());
179203

180204
// Invalid Pushover user key warning.
181205
if (invalidUsers == 0)
@@ -190,18 +214,23 @@ public void onPlayerJoin(PlayerJoinEvent e)
190214
}
191215
}
192216

193-
@EventHandler (priority = EventPriority.MONITOR)
217+
@EventHandler(priority = EventPriority.MONITOR)
194218
public void onPlayerQuit(PlayerQuitEvent e)
195219
{
196-
push( e.getQuitMessage() );
220+
push(e.getQuitMessage());
221+
}
222+
223+
public void push(String message)
224+
{
225+
push(message, false, false);
197226
}
198227

199228
/**
200229
* Push a message to all offline players with push notifications enabled.
201230
* @param message The message to be sent. Color codes will be automatically stripped out.
202231
*/
203232
@SuppressWarnings("SuspiciousMethodCalls")
204-
public void push(String message)
233+
public void push(String message, boolean opOnly, boolean warning)
205234
{
206235
Map<String, Object> map = getUserKeysFileConfiguration().getValues(false);
207236

@@ -218,28 +247,39 @@ public void push(String message)
218247
{
219248
try
220249
{
221-
if (Bukkit.getOfflinePlayer(playerUuid).isBanned())
250+
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerUuid);
251+
if (offlinePlayer.isBanned())
222252
{
223253
getUserKeysFileConfiguration().set(playerUuidString, map.get(playerUuidString) + "BANNED");
224-
return;
254+
continue;
225255
}
226256
else if ( ((String )map.get(playerUuidString)).endsWith("BANNED") )
227257
{
228258
String s = (String) map.get(playerUuidString);
229259
getUserKeysFileConfiguration().set(playerUuidString, s.substring(0, s.indexOf("BANNED")));
230260
}
261+
262+
if (opOnly && ! offlinePlayer.isOp())
263+
{
264+
continue;
265+
}
266+
}
267+
catch (Exception e) {
268+
e.printStackTrace();
231269
}
232-
catch (Exception ignored) {}
233270

234271
try
235272
{
236-
pushoverRestClient.pushMessage(PushoverMessage.builderWithApiToken("autoU13MYYXYxMaeupaqF7U7mBe9Bj")
273+
PushoverMessage.Builder pm = PushoverMessage.builderWithApiToken("autoU13MYYXYxMaeupaqF7U7mBe9Bj")
237274
.setUserId(playerPushoverKey)
238275
.setSound("gamelan")
239276
.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());
243283
}
244284
catch (PushoverException e)
245285
{
@@ -286,7 +326,7 @@ public Player getOnlinePlayerByUuid(UUID uuid) {
286326
{
287327
player = getServer().getPlayer(uuid);
288328
}
289-
catch (Exception e)
329+
catch (NoSuchMethodError e)
290330
{
291331
for (Player p : getServer().getOnlinePlayers())
292332
if (p.getUniqueId().equals(uuid))

src/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ author: Jop Vernooij
22
description: Get push notifications from your Minecraft server via Pushover.
33
main: net.jopv.minecraft.bukkit.minecraftpush.MinecraftPush
44
name: MinecraftPush
5-
version: 1.0.1
5+
version: 1.1
66
permissions:
77
minecraftpush.receive:
88
description: Can player receive notifications.

0 commit comments

Comments
 (0)