Skip to content

Commit 1af7990

Browse files
author
jobukkit
committed
added /warnop, bugfixes, offline mode error
1 parent 9732c62 commit 1af7990

File tree

3 files changed

+97
-39
lines changed

3 files changed

+97
-39
lines changed

MinecraftPush.iml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module type="JAVA_MODULE" version="4">
3-
<component name="NewModuleRootManager" inherit-compiler-output="true">
4-
<exclude-output />
3+
<component name="NewModuleRootManager" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
56
<content url="file://$MODULE_DIR$">
67
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<excludeFolder url="file://$MODULE_DIR$/target" />
79
</content>
810
<orderEntry type="inheritedJdk" />
911
<orderEntry type="sourceFolder" forTests="false" />
10-
<orderEntry type="library" name="bukkit-1.6.4-R2.0" level="project" />
11-
<orderEntry type="library" exported="" name="com.github.sps.pushover.net:pushover-client:1.0.0" level="project" />
12+
<orderEntry type="library" name="com.github.sps.pushover.net:pushover-client:1.0.0" level="project" />
13+
<orderEntry type="library" name="bukkit-1.7.9-R0.2" level="project" />
1214
</component>
1315
</module>
1416

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

Lines changed: 84 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020
import org.bukkit.event.player.PlayerQuitEvent;
2121
import org.bukkit.plugin.java.JavaPlugin;
2222

23+
import org.bukkit.scheduler.BukkitScheduler;
2324
import org.mcstats.MetricsLite;
2425

2526
import java.io.File;
2627
import java.io.IOException;
2728
import java.util.Map;
28-
import java.util.Timer;
29-
import java.util.TimerTask;
3029
import java.util.UUID;
3130
import 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());

src/plugin.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ 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.1
5+
version: 1.2
66
permissions:
77
minecraftpush.receive:
88
description: Can player receive notifications.
99
default: true
1010
minecraftpush.broadcast:
1111
description: Can player broadcast.
1212
default: op
13+
minecraftpush.warnops:
14+
description: Can player warn operators.
15+
default: notop
1316
commands:
1417
pushover:
1518
description: Enable or disable notifications for yourself.
@@ -19,6 +22,9 @@ commands:
1922
broadcast:
2023
description: Broadcast to all online players, and to all offline players with notifications enabled.
2124
usage: /broadcast <message>
25+
warnops:
26+
description: Warn operators about something.
27+
usage: /warnops <message>
2228
minecraftpush:
2329
description: Copyright, source code, and donation information of MinecraftPush.
2430
usage: /minecraftpush

0 commit comments

Comments
 (0)