Implement periodic heartbeat events to api server, and kicking players

This commit is contained in:
Trever Fischer 2012-11-12 14:38:43 -05:00
parent b8c34f9609
commit 26e5b14d6e
7 changed files with 202 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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<Villager> 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<String, Long> times = new HashMap<String, Long>();
for (World w : getServer().getWorlds()) {
times.put(w.getName(), w.getTime());
}
evt.worldTimes = times;
sendEvent(evt);
}
public void onEnable() {
m_vaultmasters = new ArrayList<Villager>();
m_vaultInventories = new HashMap<String, Inventory>();
@ -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) {

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
import org.json.JSONWriter;
import org.json.JSONException;
import java.util.Map;
public class HeartbeatEvent extends ClientEvent {
public Map<String, Long> 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";
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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;

View File

@ -17,6 +17,8 @@ commands:
usage: /<command>
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.