Skip to content

Commit

Permalink
added /warnop, bugfixes, offline mode error
Browse files Browse the repository at this point in the history
  • Loading branch information
jobukkit committed Jun 5, 2014
1 parent 9732c62 commit 1af7990
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 39 deletions.
10 changes: 6 additions & 4 deletions MinecraftPush.iml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="bukkit-1.6.4-R2.0" level="project" />
<orderEntry type="library" exported="" name="com.github.sps.pushover.net:pushover-client:1.0.0" level="project" />
<orderEntry type="library" name="com.github.sps.pushover.net:pushover-client:1.0.0" level="project" />
<orderEntry type="library" name="bukkit-1.7.9-R0.2" level="project" />
</component>
</module>

118 changes: 84 additions & 34 deletions src/net/jopv/minecraft/bukkit/minecraftpush/MinecraftPush.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import org.bukkit.scheduler.BukkitScheduler;
import org.mcstats.MetricsLite;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.logging.Level;

Expand All @@ -44,8 +43,17 @@ public class MinecraftPush extends JavaPlugin implements Listener

public String titleEnd = "";

boolean canPush = true;

public void onEnable()
{
if (! getServer().getOnlineMode())
{
getLogger().severe("Server is in offline mode! Can't send notifications.");
canPush = false;
return;
}

getServer().getPluginManager().registerEvents(this, this);

int users = getUserKeysFileConfiguration().getKeys(false).size();
Expand All @@ -68,32 +76,38 @@ public void onEnable()
else
titleEnd = " (" + getServer().getServerName() + ")";

Timer ramUsageTimer = new Timer();
ramUsageTimer.scheduleAtFixedRate(new RamChecker(), 0, 10000*5);
try
{
BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
scheduler.scheduleSyncRepeatingTask(this, new Runnable()
{
long mb = 1048576;

@Override
public void run()
{
if (Runtime.getRuntime().freeMemory() < (20 * mb))
push("Server almost out of memory!", true, true);
}
}, 0L, 3000L);
}
catch (Error e) {
getLogger().warning("Your Bukkit version is too old. Cannot send out of memory warnings to operators.");
}

// Plugin Metrics
try
{
MetricsLite m = new MetricsLite(this);
m.start();
} catch (Exception e)
}
catch (Exception e)
{
// Cannot upload data :(
} catch (NoSuchMethodError e)
{
// Old Minecraft version
}
}

class RamChecker extends TimerTask
{
long mb = 1048576;

@Override
public void run()
catch (NoSuchMethodError e)
{
if (Runtime.getRuntime().freeMemory() < (mb * 20))
push("Server almost out of memory!", true, true);
// Old Minecraft version
}
}

Expand Down Expand Up @@ -129,11 +143,15 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
{
if (sender.hasPermission("minecraftpush.receive"))
{
// Unfortunately, Pushover user key validation is not yet possible: https://github.com/sps/pushover4j/issues/3
// Unfortunately, Pushover user key validation is not yet possible: https://github.com/Jop-V/MinecraftPush/issues/2
getUserKeysFileConfiguration().set(((Player) sender).getUniqueId().toString(), args[1]);
saveUserKeysFile();

sender.sendMessage("§aPush notifications enabled! §fYou can disable them at any time using /pushover disable.");
StringBuilder messageBuilder = new StringBuilder("§aPush notifications enabled!§f ");
if (! canPush)
messageBuilder.append("§6You won't receive any at the moment though, because the server is in offline mode.§f ");
messageBuilder.append("You can disable them at any time using /pushover disable.");
sender.sendMessage(messageBuilder.toString());
return true;
} else
{
Expand Down Expand Up @@ -168,23 +186,53 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String

if (cmd.getName().equalsIgnoreCase("broadcast"))
{
if (getCmdArgsLength(args) < 1)
return false;

if (sender.hasPermission("minecraftpush.broadcast"))
{
StringBuilder builder = new StringBuilder();

for (String s : args)
builder.append(s).append(' ');

String message = builder.toString().trim();

if (!canPush) sender.sendMessage("§cServer is in offline mode, can't notify offline players!");
String message = messageFromCmdAgs(args);
getServer().broadcastMessage("§d[Broadcasting] " + message);
push(message);
} else sender.sendMessage("§cYou don't have permission to broadcast!");
return true;
}

if (cmd.getName().equalsIgnoreCase("warnops"))
{
if (getCmdArgsLength(args) < 1)
return false;

if (sender.hasPermission("minecraftpush.warnops"))
{
if (!canPush) sender.sendMessage("§cServer is in offline mode, can't notify offline operators!");
String message = messageFromCmdAgs(args);
for (Player p : getServer().getOnlinePlayers())
if (p.isOp()) p.sendMessage(message);
push(sender.getName() + " warned: " + message, true, true);
}
else if (sender.isOp())
sender.sendMessage("§cYou are an operator yourself...");
else
sender.sendMessage("§cYou don't have permission to warn operators!");

return true;
}

return false;
}

public String messageFromCmdAgs(String[] args)
{
StringBuilder builder = new StringBuilder();

for (String s : args)
builder.append(s).append(' ');

return builder.toString().trim();
}

/**
* Workaround for some really weird behavior.
*/
Expand Down Expand Up @@ -228,10 +276,14 @@ public void push(String message)
/**
* Push a message to all offline players with push notifications enabled.
* @param message The message to be sent. Color codes will be automatically stripped out.
* @param opOnly Whether only operators should receive this message.
* @param highPriority Whether this message is of high priority.
*/
@SuppressWarnings("SuspiciousMethodCalls")
public void push(String message, boolean opOnly, boolean warning)
public void push(String message, boolean opOnly, boolean highPriority)
{
if (!canPush) return;

Map<String, Object> map = getUserKeysFileConfiguration().getValues(false);

for (String playerUuidString : map.keySet())
Expand All @@ -241,7 +293,7 @@ public void push(String message, boolean opOnly, boolean warning)
String playerPushoverKey = String.valueOf(map.get(playerUuidString));

if (playerPushoverKey.equals("INVALID") || playerPushoverKey.equals("BANNED"))
return;
continue;

if (onlinePlayer == null /* Player is offline, so a notification needs to be sent */)
{
Expand All @@ -253,18 +305,16 @@ public void push(String message, boolean opOnly, boolean warning)
getUserKeysFileConfiguration().set(playerUuidString, map.get(playerUuidString) + "BANNED");
continue;
}
else if ( ((String )map.get(playerUuidString)).endsWith("BANNED") )
else if ( ((String) map.get(playerUuidString)).endsWith("BANNED") )
{
String s = (String) map.get(playerUuidString);
getUserKeysFileConfiguration().set(playerUuidString, s.substring(0, s.indexOf("BANNED")));
}

if (opOnly && ! offlinePlayer.isOp())
{
continue;
}
}
catch (Exception e) {
catch (NoSuchMethodError e) {
e.printStackTrace();
}

Expand All @@ -276,7 +326,7 @@ else if ( ((String )map.get(playerUuidString)).endsWith("BANNED") )
.setTitle("Minecraft" + titleEnd)
.setMessage(ChatColor.stripColor(message));

if (warning)
if (highPriority)
pm.setPriority(MessagePriority.HIGH);

pushoverRestClient.pushMessage(pm.build());
Expand Down
8 changes: 7 additions & 1 deletion src/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ author: Jop Vernooij
description: Get push notifications from your Minecraft server via Pushover.
main: net.jopv.minecraft.bukkit.minecraftpush.MinecraftPush
name: MinecraftPush
version: 1.1
version: 1.2
permissions:
minecraftpush.receive:
description: Can player receive notifications.
default: true
minecraftpush.broadcast:
description: Can player broadcast.
default: op
minecraftpush.warnops:
description: Can player warn operators.
default: notop
commands:
pushover:
description: Enable or disable notifications for yourself.
Expand All @@ -19,6 +22,9 @@ commands:
broadcast:
description: Broadcast to all online players, and to all offline players with notifications enabled.
usage: /broadcast <message>
warnops:
description: Warn operators about something.
usage: /warnops <message>
minecraftpush:
description: Copyright, source code, and donation information of MinecraftPush.
usage: /minecraftpush

0 comments on commit 1af7990

Please sign in to comment.