diff --git a/src/main/java/us/camin/HeartbeatCommand.java b/src/main/java/us/camin/HeartbeatCommand.java new file mode 100644 index 0000000..00e22f9 --- /dev/null +++ b/src/main/java/us/camin/HeartbeatCommand.java @@ -0,0 +1,37 @@ +package us.camin; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class HeartbeatCommand implements CommandExecutor { + private Plugin m_plugin; + + public HeartbeatCommand(Plugin p) { + m_plugin = p; + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { + if (sender.hasPermission("caminus.heartbeat")) { + m_plugin.sendHeartbeat(); + } + return true; + } +} diff --git a/src/main/java/us/camin/Plugin.java b/src/main/java/us/camin/Plugin.java index 0bbac48..094d73b 100644 --- a/src/main/java/us/camin/Plugin.java +++ b/src/main/java/us/camin/Plugin.java @@ -58,6 +58,8 @@ import us.camin.api.PlayerMessageEvent; import us.camin.api.ClientEvent; import us.camin.api.VaultModifyEvent; import us.camin.api.PlayerVaultSlot; +import us.camin.api.HeartbeatEvent; +import us.camin.api.KickEvent; import org.json.JSONException; public class Plugin extends JavaPlugin { @@ -69,6 +71,7 @@ public class Plugin extends JavaPlugin { private VomitCommand m_vomitCommand; private VaultCommand m_vaultCommand; private ServerEventPoller m_eventPoll; + private ServerHeartbeat m_heartbeat; private ArrayList m_vaultmasters; public Server api() { @@ -78,6 +81,7 @@ public class Plugin extends JavaPlugin { public void onDisable() { log.info("[Caminus] Plugin disabled"); m_eventPoll.stop(); + m_heartbeat.stop(); m_api = null; saveVaultmasters(); for (Villager v : m_vaultmasters) { @@ -122,6 +126,13 @@ public class Plugin extends JavaPlugin { return null; } }); + } else if (e instanceof KickEvent) { + final KickEvent evt = (KickEvent)(e); + log.info("Got kick event for "+evt.player+": "+evt.message); + Player p = getServer().getPlayer(evt.player); + if (p != null) { + p.kickPlayer(evt.message); + } } try { m_api.notifyEventHandled(e); @@ -183,6 +194,18 @@ public class Plugin extends JavaPlugin { saveConfig(); } + public void sendHeartbeat() { + HeartbeatEvent evt = new HeartbeatEvent(); + evt.port = getServer().getPort(); + evt.name = getServer().getName(); + HashMap times = new HashMap(); + for (World w : getServer().getWorlds()) { + times.put(w.getName(), w.getTime()); + } + evt.worldTimes = times; + sendEvent(evt); + } + public void onEnable() { m_vaultmasters = new ArrayList(); m_vaultInventories = new HashMap(); @@ -204,6 +227,7 @@ public class Plugin extends JavaPlugin { pm.registerEvents(m_listener, this); m_eventPoll = new ServerEventPoller(this); + m_heartbeat = new ServerHeartbeat(this); m_motdCommand = new MOTDCommand(this); getCommand("motd").setExecutor(m_motdCommand); @@ -213,6 +237,8 @@ public class Plugin extends JavaPlugin { m_vaultCommand = new VaultCommand(this); getCommand("vaultmaster").setExecutor(m_vaultCommand); + getCommand("heartbeat").setExecutor(new HeartbeatCommand(this)); + CommandExecutor economyCommand = new EconomyCommand(this); getCommand("balance").setExecutor(economyCommand); @@ -225,6 +251,7 @@ public class Plugin extends JavaPlugin { log.info("[Caminus] Plugin enabled"); getServer().getScheduler().scheduleAsyncDelayedTask(this, m_eventPoll); + getServer().getScheduler().scheduleAsyncDelayedTask(this, m_heartbeat); } public void checkFreeHalfDoorDay(Player sender) { diff --git a/src/main/java/us/camin/ServerHeartbeat.java b/src/main/java/us/camin/ServerHeartbeat.java new file mode 100644 index 0000000..a9bf083 --- /dev/null +++ b/src/main/java/us/camin/ServerHeartbeat.java @@ -0,0 +1,49 @@ +package us.camin; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import java.lang.Runnable; +import java.io.IOException; +import java.util.logging.Logger; +import org.bukkit.scheduler.BukkitScheduler; + +public class ServerHeartbeat implements Runnable { + private Plugin m_plugin; + private boolean m_running; + private int m_taskid; + Logger log = Logger.getLogger("CaminusHeartbeat"); + + public ServerHeartbeat(Plugin plugin) { + m_plugin = plugin; + m_running = true; + } + + public void run() { + if (m_running) { + m_plugin.sendHeartbeat(); + final BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + m_taskid = scheduler.scheduleAsyncDelayedTask(m_plugin, this, 100); + } + } + + public void stop() { + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + scheduler.cancelTask(m_taskid); + m_running = false; + } +} diff --git a/src/main/java/us/camin/api/HeartbeatEvent.java b/src/main/java/us/camin/api/HeartbeatEvent.java new file mode 100644 index 0000000..b2c40a6 --- /dev/null +++ b/src/main/java/us/camin/api/HeartbeatEvent.java @@ -0,0 +1,45 @@ +package us.camin.api; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import org.json.JSONWriter; +import org.json.JSONException; +import java.util.Map; + +public class HeartbeatEvent extends ClientEvent { + public Map worldTimes; + public int port; + public String name; + + public JSONWriter toJSON(JSONWriter writer) throws JSONException { + writer.key("port").value(port); + writer.key("name").value(name); + writer.key("worlds").object(); + for (String world : worldTimes.keySet()) { + writer.key(world).object(); + writer.key("time").value(worldTimes.get(world)); + writer.endObject(); + } + writer.endObject(); + return writer; + } + + public String jsonName() { + return "heartbeat"; + } +} diff --git a/src/main/java/us/camin/api/KickEvent.java b/src/main/java/us/camin/api/KickEvent.java new file mode 100644 index 0000000..238cda0 --- /dev/null +++ b/src/main/java/us/camin/api/KickEvent.java @@ -0,0 +1,36 @@ +package us.camin.api; + +/* + This file is part of Caminus + + Caminus is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Caminus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Caminus. If not, see . + */ + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; + +public class KickEvent extends ServerEvent { + public String message; + public String player; + + public static KickEvent fromJSON(JSONObject obj, int id) { + KickEvent ret = new KickEvent (); + ret.player = obj.optString("player"); + ret.message = obj.optString("message"); + ret.id = id; + return ret; + } +} + diff --git a/src/main/java/us/camin/api/ServerEvent.java b/src/main/java/us/camin/api/ServerEvent.java index c5a5a21..f35651c 100644 --- a/src/main/java/us/camin/api/ServerEvent.java +++ b/src/main/java/us/camin/api/ServerEvent.java @@ -49,6 +49,8 @@ public class ServerEvent { return PlayerMessageEvent.fromJSON(payload, id); } else if (type.equals("vault-contents")) { return VaultModifyEvent.fromJSON(payload, id); + } else if (type.equals("player-kick")) { + return KickEvent.fromJSON(payload, id); } else { log.log(Level.SEVERE, "Unhandled event type: "+type); return null; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 01851b4..27773c4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -17,6 +17,8 @@ commands: usage: / vaultmaster: description: Create a vaultmaster NPC at your current location. + heartbeat: + description: Send a heartbeat to the caminus api server permissions: caminus.*: default: op @@ -26,6 +28,7 @@ permissions: caminus.freedoorday: true caminus.vomit: true caminus.vaultmaster: true + caminus.heartbeat: true caminus.freedoorday: default: false description: Enables free half door day @@ -38,3 +41,6 @@ permissions: caminus.vaultmaster: default: op description: Allows creation of vaultmasters, interfaces to the vault system. + caminus.heartbeat: + default: op + description: Allows use of the /heartbeat command.