diff --git a/src/main/java/us/camin/JoinListener.java b/src/main/java/us/camin/JoinListener.java index f1b553c..79b0433 100644 --- a/src/main/java/us/camin/JoinListener.java +++ b/src/main/java/us/camin/JoinListener.java @@ -47,12 +47,7 @@ import java.io.IOException; import java.util.ArrayList; import us.camin.api.ValidationResponse; -import us.camin.api.ClientEvent; -import us.camin.api.BlockEvent; -import us.camin.api.ChatEvent; -import us.camin.api.DeathEvent; -import us.camin.api.MurderEvent; -import us.camin.api.WeatherEvent; +import us.camin.api.events.*; public class JoinListener implements Listener { Logger log = Logger.getLogger("Caminus.Join"); @@ -151,7 +146,7 @@ public class JoinListener implements Listener { public void onPlayerDeath(PlayerDeathEvent event) { Player target = event.getEntity(); Entity source = target.getKiller(); - ClientEvent evt = new DeathEvent(target.getName(), event.getDeathMessage()); + Event evt = new DeathEvent(target.getName(), event.getDeathMessage()); m_plugin.sendEvent(evt); if (source instanceof Player) { Player killer = (Player)source; diff --git a/src/main/java/us/camin/Plugin.java b/src/main/java/us/camin/Plugin.java index 29c5f31..3ac9fa7 100644 --- a/src/main/java/us/camin/Plugin.java +++ b/src/main/java/us/camin/Plugin.java @@ -52,14 +52,7 @@ import java.util.ListIterator; import java.util.concurrent.Callable; import us.camin.api.Server; -import us.camin.api.ServerEvent; -import us.camin.api.BroadcastEvent; -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 us.camin.api.events.*; import org.json.JSONException; public class Plugin extends JavaPlugin { @@ -89,7 +82,7 @@ public class Plugin extends JavaPlugin { } } - public void handleEvent(ServerEvent e) { + public void handleEvent(us.camin.api.events.Event e) { if (e instanceof BroadcastEvent) { final BroadcastEvent evt = (BroadcastEvent)(e); getServer().getScheduler().callSyncMethod(this, new Callable() { @@ -106,8 +99,8 @@ public class Plugin extends JavaPlugin { return null; } }); - } else if (e instanceof VaultModifyEvent) { - final VaultModifyEvent evt = (VaultModifyEvent)(e); + } else if (e instanceof VaultContentsEvent) { + final VaultContentsEvent evt = (VaultContentsEvent)(e); getServer().getScheduler().callSyncMethod(this, new Callable() { public Void call() { Inventory inv; @@ -116,33 +109,35 @@ public class Plugin extends JavaPlugin { } catch (IOException e) { return null; } - for (PlayerVaultSlot s : evt.contents) { + for (VaultSlot s : evt.items) { if (s.quantity == -1) { inv.clear(s.position); } else { - inv.setItem(s.position, new ItemStack(s.item, s.quantity, s.damage, s.data)); + inv.setItem(s.position, new ItemStack(s.material, s.quantity, (short)s.damage, (byte)s.data)); } } return null; } }); - } else if (e instanceof KickEvent) { - final KickEvent evt = (KickEvent)(e); + } else if (e instanceof PlayerKickEvent) { + final PlayerKickEvent evt = (PlayerKickEvent)(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); - } catch (IOException ex) { - log.severe("Could not close out event. Duplicates will happen!!!"); + if (e != null) { + try { + m_api.notifyEventHandled(e); + } catch (IOException ex) { + log.severe("Could not close out event. Duplicates will happen!!!"); + } } } - public void sendEvent(ClientEvent event) { - ClientEvent[] events = new ClientEvent[1]; + public void sendEvent(us.camin.api.events.Event event) { + us.camin.api.events.Event[] events = new us.camin.api.events.Event[1]; events[0] = event; try { api().sendEvents(events); @@ -195,10 +190,10 @@ public class Plugin extends JavaPlugin { } public void sendHeartbeat() { - HeartbeatEvent evt = new HeartbeatEvent(); + ServerHeartbeatEvent evt = new ServerHeartbeatEvent(); evt.port = getServer().getPort(); evt.name = getServer().getName(); - HashMap times = new HashMap(); + HashMap times = new HashMap(); for (World w : getServer().getWorlds()) { times.put(w.getName(), w.getTime()); } @@ -280,14 +275,14 @@ public class Plugin extends JavaPlugin { public void saveVault(Player p, Inventory inv) throws IOException { ListIterator items = inv.iterator(); - PlayerVaultSlot[] vault = new PlayerVaultSlot[inv.getSize()]; + VaultSlot[] vault = new VaultSlot[inv.getSize()]; int i = 0; while(items.hasNext()) { ItemStack item = items.next(); - PlayerVaultSlot slot = new PlayerVaultSlot(); + VaultSlot slot = new VaultSlot(); slot.position = i; if (item != null) { - slot.item = item.getTypeId(); + slot.material = item.getTypeId(); slot.quantity = item.getAmount(); slot.damage = item.getDurability(); slot.data = item.getData().getData(); @@ -313,9 +308,9 @@ public class Plugin extends JavaPlugin { if (!m_vaultInventories.containsKey(p.getName())) { Inventory inv = p.getServer().createInventory(p, InventoryType.CHEST); m_vaultInventories.put(p.getName(), inv); - PlayerVaultSlot[] vault = m_api.loadVault(p.getName()); + VaultSlot[] vault = m_api.loadVault(p.getName()); for(int i = 0;i. - */ - -import org.json.JSONWriter; -import org.json.JSONException; -import org.bukkit.block.Block; -import org.bukkit.Location; - -public class BlockEvent extends ClientEvent { - public String sender; - public Block block; - public Type type; - - public enum Type { - BREAK, PLACE - } - - public BlockEvent(String player, Block block, Type type) { - this.sender = player; - this.block = block; - this.type = type; - } - - public JSONWriter toJSON(JSONWriter writer) throws JSONException { - writer.key("sender").value(sender); - String evtType; - switch(type) { - case BREAK: - evtType = "break"; - break; - case PLACE: - evtType = "place"; - break; - default: - evtType = "unknown"; - } - writer.key("type").value(evtType); - writer.key("location"); - writer.object(); - Location loc = block.getLocation(); - writer.key("x").value(loc.getBlockX()); - writer.key("y").value(loc.getBlockY()); - writer.key("z").value(loc.getBlockZ()); - writer.endObject(); - return writer; - } - - public String jsonName() { - return "block"; - } -} - diff --git a/src/main/java/us/camin/api/HeartbeatEvent.java b/src/main/java/us/camin/api/HeartbeatEvent.java deleted file mode 100644 index b2c40a6..0000000 --- a/src/main/java/us/camin/api/HeartbeatEvent.java +++ /dev/null @@ -1,45 +0,0 @@ -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/MurderEvent.java b/src/main/java/us/camin/api/MurderEvent.java deleted file mode 100644 index 5122942..0000000 --- a/src/main/java/us/camin/api/MurderEvent.java +++ /dev/null @@ -1,44 +0,0 @@ -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; - -public class MurderEvent extends ClientEvent { - public String player; - public String killer; - public String message; - - public MurderEvent(String player, String killer) { - this.player = player; - this.killer = killer; - } - - public JSONWriter toJSON(JSONWriter writer) throws JSONException { - writer.key("player").value(player); - writer.key("killer").value(killer); - return writer; - } - - public String jsonName() { - return "player-murder"; - } -} - - diff --git a/src/main/java/us/camin/api/Server.java b/src/main/java/us/camin/api/Server.java index 926db15..5410557 100644 --- a/src/main/java/us/camin/api/Server.java +++ b/src/main/java/us/camin/api/Server.java @@ -47,6 +47,9 @@ import org.json.JSONStringer; import java.util.Random; import org.apache.commons.codec.binary.Hex; +import us.camin.api.events.VaultSlot; +import us.camin.api.events.Event; + public class Server { Logger log = Logger.getLogger("Caminus.API"); private String m_url; @@ -105,7 +108,18 @@ public class Server { } public JSONObject readJSON(HttpURLConnection conn) throws IOException { - BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); + BufferedInputStream in; + try{ + in = new BufferedInputStream(conn.getInputStream()); + } catch (IOException e) { + String errorStr; + try { + errorStr = new java.util.Scanner(new BufferedInputStream(conn.getErrorStream())).useDelimiter("\\A").next(); + throw new IOException("Got bad data from server: "+errorStr, e); + } catch (java.util.NoSuchElementException errExc) { + throw e; + } + } String jsonStr; try { jsonStr = new java.util.Scanner(in).useDelimiter("\\A").next(); @@ -117,7 +131,7 @@ public class Server { JSONObject jsonObj = new JSONObject(jsonStr); return jsonObj; } catch (JSONException e) { - throw new IOException("JSON parse error", e); + throw new IOException("JSON parse error: "+jsonStr, e); } } @@ -235,20 +249,20 @@ public class Server { get("server/session/"+player+"/close"); } - public void notifyEventHandled(ServerEvent event) throws IOException { - log.info("Closing event "+event.id); + public void notifyEventHandled(Event event) throws IOException { + log.info("Closing event "+event._id); HashMap params = new HashMap(); - params.put("job", Integer.toString(event.id)); + params.put("job", Integer.toString(event._id)); post("server/events", params); } - public void sendEvents(ClientEvent[] event) throws JSONException, IOException { + public void sendEvents(Event[] event) throws JSONException, IOException { log.info("Submitting events"); JSONStringer out = new JSONStringer(); out.object(); out.key("events"); out.array(); - for (ClientEvent evt : event) { + for (Event evt : event) { out.value(evt.toJSON()); } out.endArray(); @@ -258,20 +272,20 @@ public class Server { put("server/events", params); } - public ServerEvent[] pollEventQueue() throws IOException { + public Event[] pollEventQueue() throws IOException { log.info("Polling server for events"); JSONObject jsonObj = get("server/events"); JSONArray eventList; try { eventList = jsonObj.getJSONArray("events"); } catch (JSONException e) { - return new ServerEvent[0]; + return new Event[0]; } - ServerEvent[] events = new ServerEvent[eventList.length()]; + Event[] events = new Event[eventList.length()]; for (int i = 0;i. - */ - -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONException; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class ServerEvent { - public int id; - static Logger log = Logger.getLogger("Caminus.api"); - public static ServerEvent fromJSON(JSONObject obj) { - int id = obj.optInt("id"); - JSONObject event; - try { - event = obj.getJSONObject("event"); - } catch (JSONException e) { - log.log(Level.SEVERE, "Bad JSON", e); - return null; - } - String type = event.optString("type"); - JSONObject payload; - try { - payload = event.getJSONObject("payload"); - } catch (JSONException e) { - log.log(Level.SEVERE, "Bad JSON ", e); - return null; - } - if (type.equals("broadcast")) { - return BroadcastEvent.fromJSON(payload, id); - } else if (type.equals("player-message")) { - 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/java/us/camin/api/VaultModifyEvent.java b/src/main/java/us/camin/api/VaultModifyEvent.java deleted file mode 100644 index 72e84fa..0000000 --- a/src/main/java/us/camin/api/VaultModifyEvent.java +++ /dev/null @@ -1,55 +0,0 @@ -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 VaultModifyEvent extends ServerEvent { - public PlayerVaultSlot[] contents; - public String player; - public static VaultModifyEvent fromJSON(JSONObject obj, int id) { - VaultModifyEvent ret = new VaultModifyEvent(); - ret.player = obj.optString("player"); - ret.id = id; - JSONArray items; - try { - items = obj.getJSONArray("items"); - } catch (JSONException e) { - return null; - } - ret.contents = new PlayerVaultSlot[items.length()]; - for(int i = 0;i. + */ + +import org.json.*; +import java.util.*; +public class BlockBreakEvent extends Event { + public int y; + public String player; + public int material; + public int z; + public int x; + public static BlockBreakEvent fromJSON(JSONObject obj) throws JSONException { + BlockBreakEvent ret = new BlockBreakEvent(); + ret.y = obj.getInt("y"); + ret.player = obj.getString("player"); + ret.material = obj.getInt("material"); + ret.z = obj.getInt("z"); + ret.x = obj.getInt("x"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("y").value(this.y); +writer.key("player").value(this.player); +writer.key("material").value(this.material); +writer.key("z").value(this.z); +writer.key("x").value(this.x); + return writer; + } + public String jsonName() { return "block-break"; } + static { Event.registerHandler("block-break", BlockBreakEvent.class); } +} diff --git a/src/main/java/us/camin/api/events/BlockPlaceEvent.java b/src/main/java/us/camin/api/events/BlockPlaceEvent.java new file mode 100644 index 0000000..ab7e924 --- /dev/null +++ b/src/main/java/us/camin/api/events/BlockPlaceEvent.java @@ -0,0 +1,47 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class BlockPlaceEvent extends Event { + public int y; + public String player; + public int material; + public int z; + public int x; + public static BlockPlaceEvent fromJSON(JSONObject obj) throws JSONException { + BlockPlaceEvent ret = new BlockPlaceEvent(); + ret.y = obj.getInt("y"); + ret.player = obj.getString("player"); + ret.material = obj.getInt("material"); + ret.z = obj.getInt("z"); + ret.x = obj.getInt("x"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("y").value(this.y); +writer.key("player").value(this.player); +writer.key("material").value(this.material); +writer.key("z").value(this.z); +writer.key("x").value(this.x); + return writer; + } + public String jsonName() { return "block-place"; } + static { Event.registerHandler("block-place", BlockPlaceEvent.class); } +} diff --git a/src/main/java/us/camin/api/DeathEvent.java b/src/main/java/us/camin/api/events/BroadcastEvent.java similarity index 54% rename from src/main/java/us/camin/api/DeathEvent.java rename to src/main/java/us/camin/api/events/BroadcastEvent.java index b2c85f2..893db4b 100644 --- a/src/main/java/us/camin/api/DeathEvent.java +++ b/src/main/java/us/camin/api/events/BroadcastEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,26 +17,19 @@ package us.camin.api; along with Caminus. If not, see . */ -import org.json.JSONWriter; -import org.json.JSONException; - -public class DeathEvent extends ClientEvent { - public String player; - public String message; - - public DeathEvent(String player, String message) { - this.player = player; - this.message = message; - } - - public JSONWriter toJSON(JSONWriter writer) throws JSONException { - writer.key("player").value(player); - writer.key("message").value(message); - return writer; - } - - public String jsonName() { - return "player-death"; - } +import org.json.*; +import java.util.*; +public class BroadcastEvent extends Event { + public String message; + public static BroadcastEvent fromJSON(JSONObject obj) throws JSONException { + BroadcastEvent ret = new BroadcastEvent(); + ret.message = obj.getString("message"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("message").value(this.message); + return writer; + } + public String jsonName() { return "broadcast"; } + static { Event.registerHandler("broadcast", BroadcastEvent.class); } } - diff --git a/src/main/java/us/camin/api/KickEvent.java b/src/main/java/us/camin/api/events/ChatEvent.java similarity index 51% rename from src/main/java/us/camin/api/KickEvent.java rename to src/main/java/us/camin/api/events/ChatEvent.java index 238cda0..4db2eee 100644 --- a/src/main/java/us/camin/api/KickEvent.java +++ b/src/main/java/us/camin/api/events/ChatEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,20 +17,22 @@ package us.camin.api; 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; - } +import org.json.*; +import java.util.*; +public class ChatEvent extends Event { + public String message; + public String sender; + public static ChatEvent fromJSON(JSONObject obj) throws JSONException { + ChatEvent ret = new ChatEvent(); + ret.message = obj.getString("message"); + ret.sender = obj.getString("sender"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("message").value(this.message); +writer.key("sender").value(this.sender); + return writer; + } + public String jsonName() { return "chat"; } + static { Event.registerHandler("chat", ChatEvent.class); } } - diff --git a/src/main/java/us/camin/api/events/Event.java b/src/main/java/us/camin/api/events/Event.java new file mode 100644 index 0000000..7c8cca3 --- /dev/null +++ b/src/main/java/us/camin/api/events/Event.java @@ -0,0 +1,55 @@ +package us.camin.api.events; +import java.util.*; +import org.json.*; +import java.util.logging.Logger; +import java.util.logging.Level; + +public abstract class Event { + public int _id; + public static Logger log; + public static Map s_types; + + static { + s_types = new HashMap(); + log = Logger.getLogger("CaminusEvents"); + } + + public static void registerHandler(String name, Class cls) { + log.info("Registering event type "+name); + s_types.put(name, cls); + } + + public static Event fromJSON(JSONObject obj) throws JSONException { + if (!s_types.containsKey(obj.getString("type"))) { + log.log(Level.SEVERE, "Ignoring unknown event type "+obj.getString("type")); + return null; + } + Class prototype = s_types.get(obj.getString("type")); + Event ret = null; + try { + ret = (Event)prototype.getMethod("fromJSON").invoke(null, obj.getJSONObject("properties")); + } catch (Exception e) { + log.log(Level.SEVERE, "Could not instantiate event", e); + return null; + } + ret._id = obj.getInt("id"); + return ret; + } + + public JSONObject toJSON() throws JSONException { + JSONWriter s = new JSONStringer(); + s.object(); + s.key("type"); + s.value(jsonName()); + s.key("payload"); + s.object(); + s = toJSON(s); + s.endObject(); + s.endObject(); + return new JSONObject(((JSONStringer)s).toString()); + } + + public abstract JSONWriter toJSON(JSONWriter writer) throws JSONException; + + public abstract String jsonName(); +} diff --git a/src/main/java/us/camin/api/PlayerMessageEvent.java b/src/main/java/us/camin/api/events/JoinEvent.java similarity index 56% rename from src/main/java/us/camin/api/PlayerMessageEvent.java rename to src/main/java/us/camin/api/events/JoinEvent.java index fd6588a..b45050b 100644 --- a/src/main/java/us/camin/api/PlayerMessageEvent.java +++ b/src/main/java/us/camin/api/events/JoinEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,19 +17,19 @@ package us.camin.api; along with Caminus. If not, see . */ -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONException; - -public class PlayerMessageEvent extends ServerEvent { - public String message; - public String player; - public static PlayerMessageEvent fromJSON(JSONObject obj, int id) { - PlayerMessageEvent ret = new PlayerMessageEvent(); - ret.message = obj.optString("message"); - ret.player = obj.optString("player"); - ret.id = id; - return ret; - } +import org.json.*; +import java.util.*; +public class JoinEvent extends Event { + public String player; + public static JoinEvent fromJSON(JSONObject obj) throws JSONException { + JoinEvent ret = new JoinEvent(); + ret.player = obj.getString("player"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("player").value(this.player); + return writer; + } + public String jsonName() { return "join"; } + static { Event.registerHandler("join", JoinEvent.class); } } - diff --git a/src/main/java/us/camin/api/ChatEvent.java b/src/main/java/us/camin/api/events/MarketOrderEvent.java similarity index 54% rename from src/main/java/us/camin/api/ChatEvent.java rename to src/main/java/us/camin/api/events/MarketOrderEvent.java index 320a783..197d63d 100644 --- a/src/main/java/us/camin/api/ChatEvent.java +++ b/src/main/java/us/camin/api/events/MarketOrderEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,25 +17,19 @@ package us.camin.api; along with Caminus. If not, see . */ -import org.json.JSONWriter; -import org.json.JSONException; - -public class ChatEvent extends ClientEvent { - public String sender; - public String message; - - public ChatEvent(String sender, String message) { - this.sender = sender; - this.message = message; - } - - public JSONWriter toJSON(JSONWriter writer) throws JSONException { - writer.key("sender").value(sender); - writer.key("message").value(message); - return writer; - } - - public String jsonName() { - return "chat"; - } +import org.json.*; +import java.util.*; +public class MarketOrderEvent extends Event { + public int orderID; + public static MarketOrderEvent fromJSON(JSONObject obj) throws JSONException { + MarketOrderEvent ret = new MarketOrderEvent(); + ret.orderID = obj.getInt("orderID"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("orderID").value(this.orderID); + return writer; + } + public String jsonName() { return "market-order"; } + static { Event.registerHandler("market-order", MarketOrderEvent.class); } } diff --git a/src/main/java/us/camin/api/events/PlayerDeathEvent.java b/src/main/java/us/camin/api/events/PlayerDeathEvent.java new file mode 100644 index 0000000..3eadc89 --- /dev/null +++ b/src/main/java/us/camin/api/events/PlayerDeathEvent.java @@ -0,0 +1,38 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class PlayerDeathEvent extends Event { + public String player; + public String message; + public static PlayerDeathEvent fromJSON(JSONObject obj) throws JSONException { + PlayerDeathEvent ret = new PlayerDeathEvent(); + ret.player = obj.getString("player"); + ret.message = obj.getString("message"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("player").value(this.player); +writer.key("message").value(this.message); + return writer; + } + public String jsonName() { return "player-death"; } + static { Event.registerHandler("player-death", PlayerDeathEvent.class); } +} diff --git a/src/main/java/us/camin/api/ClientEvent.java b/src/main/java/us/camin/api/events/PlayerKickEvent.java similarity index 50% rename from src/main/java/us/camin/api/ClientEvent.java rename to src/main/java/us/camin/api/events/PlayerKickEvent.java index 99739f2..3677048 100644 --- a/src/main/java/us/camin/api/ClientEvent.java +++ b/src/main/java/us/camin/api/events/PlayerKickEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,26 +17,22 @@ package us.camin.api; along with Caminus. If not, see . */ -import org.json.JSONWriter; -import org.json.JSONStringer; -import org.json.JSONException; -import org.json.JSONObject; - -public abstract class ClientEvent { - public JSONObject toJSON() throws JSONException { - JSONWriter s = new JSONStringer(); - s.object(); - s.key("type"); - s.value(jsonName()); - s.key("payload"); - s.object(); - s = toJSON(s); - s.endObject(); - s.endObject(); - return new JSONObject(((JSONStringer)s).toString()); - } - - public abstract String jsonName(); - - public abstract JSONWriter toJSON(JSONWriter writer) throws JSONException; +import org.json.*; +import java.util.*; +public class PlayerKickEvent extends Event { + public String player; + public String message; + public static PlayerKickEvent fromJSON(JSONObject obj) throws JSONException { + PlayerKickEvent ret = new PlayerKickEvent(); + ret.player = obj.getString("player"); + ret.message = obj.getString("message"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("player").value(this.player); +writer.key("message").value(this.message); + return writer; + } + public String jsonName() { return "player-kick"; } + static { Event.registerHandler("player-kick", PlayerKickEvent.class); } } diff --git a/src/main/java/us/camin/api/events/PlayerMessageEvent.java b/src/main/java/us/camin/api/events/PlayerMessageEvent.java new file mode 100644 index 0000000..c45521e --- /dev/null +++ b/src/main/java/us/camin/api/events/PlayerMessageEvent.java @@ -0,0 +1,38 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class PlayerMessageEvent extends Event { + public String player; + public String message; + public static PlayerMessageEvent fromJSON(JSONObject obj) throws JSONException { + PlayerMessageEvent ret = new PlayerMessageEvent(); + ret.player = obj.getString("player"); + ret.message = obj.getString("message"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("player").value(this.player); +writer.key("message").value(this.message); + return writer; + } + public String jsonName() { return "player-message"; } + static { Event.registerHandler("player-message", PlayerMessageEvent.class); } +} diff --git a/src/main/java/us/camin/api/events/PlayerMurderEvent.java b/src/main/java/us/camin/api/events/PlayerMurderEvent.java new file mode 100644 index 0000000..8d70adf --- /dev/null +++ b/src/main/java/us/camin/api/events/PlayerMurderEvent.java @@ -0,0 +1,41 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class PlayerMurderEvent extends Event { + public String player; + public String message; + public String killer; + public static PlayerMurderEvent fromJSON(JSONObject obj) throws JSONException { + PlayerMurderEvent ret = new PlayerMurderEvent(); + ret.player = obj.getString("player"); + ret.message = obj.getString("message"); + ret.killer = obj.getString("killer"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("player").value(this.player); +writer.key("message").value(this.message); +writer.key("killer").value(this.killer); + return writer; + } + public String jsonName() { return "player-murder"; } + static { Event.registerHandler("player-murder", PlayerMurderEvent.class); } +} diff --git a/src/main/java/us/camin/api/BroadcastEvent.java b/src/main/java/us/camin/api/events/QuitEvent.java similarity index 56% rename from src/main/java/us/camin/api/BroadcastEvent.java rename to src/main/java/us/camin/api/events/QuitEvent.java index 8bf20d4..5f4e355 100644 --- a/src/main/java/us/camin/api/BroadcastEvent.java +++ b/src/main/java/us/camin/api/events/QuitEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,16 +17,19 @@ package us.camin.api; along with Caminus. If not, see . */ -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONException; - -public class BroadcastEvent extends ServerEvent { - public String message; - public static BroadcastEvent fromJSON(JSONObject obj, int id) { - BroadcastEvent ret = new BroadcastEvent(); - ret.message = obj.optString("message"); - ret.id = id; - return ret; - } +import org.json.*; +import java.util.*; +public class QuitEvent extends Event { + public String player; + public static QuitEvent fromJSON(JSONObject obj) throws JSONException { + QuitEvent ret = new QuitEvent(); + ret.player = obj.getString("player"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("player").value(this.player); + return writer; + } + public String jsonName() { return "quit"; } + static { Event.registerHandler("quit", QuitEvent.class); } } diff --git a/src/main/java/us/camin/api/events/ServerHeartbeatEvent.java b/src/main/java/us/camin/api/events/ServerHeartbeatEvent.java new file mode 100644 index 0000000..61e9bbd --- /dev/null +++ b/src/main/java/us/camin/api/events/ServerHeartbeatEvent.java @@ -0,0 +1,46 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class ServerHeartbeatEvent extends Event { + public Map worldTimes; + public int port; + public String name; + public static ServerHeartbeatEvent fromJSON(JSONObject obj) throws JSONException { + ServerHeartbeatEvent ret = new ServerHeartbeatEvent(); + + JSONObject worldTimes_obj = obj.getJSONObject("worldTimes"); + for(String name : JSONObject.getNames(worldTimes_obj)) { + ret.worldTimes.put(name, worldTimes_obj.get(name)); + } + + ret.port = obj.getInt("port"); + ret.name = obj.getString("name"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("worldTimes").value(this.worldTimes); +writer.key("port").value(this.port); +writer.key("name").value(this.name); + return writer; + } + public String jsonName() { return "server-heartbeat"; } + static { Event.registerHandler("server-heartbeat", ServerHeartbeatEvent.class); } +} diff --git a/src/main/java/us/camin/api/events/VaultContentsEvent.java b/src/main/java/us/camin/api/events/VaultContentsEvent.java new file mode 100644 index 0000000..89e832d --- /dev/null +++ b/src/main/java/us/camin/api/events/VaultContentsEvent.java @@ -0,0 +1,43 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class VaultContentsEvent extends Event { + public String player; + public List items; + public static VaultContentsEvent fromJSON(JSONObject obj) throws JSONException { + VaultContentsEvent ret = new VaultContentsEvent(); + ret.player = obj.getString("player"); + + JSONArray items_list = obj.getJSONArray("items"); + ret.items = new ArrayList(); + for(int i = 0;i. + */ + +import org.json.*; +import java.util.*; +public class VaultSlot { + public String name; + public int durability; + public int position; + public int material; + public int data; + public int damage; + public int quantity; + public static VaultSlot fromJSON(JSONObject obj) throws JSONException { + VaultSlot ret = new VaultSlot(); + try { + ret.name = obj.getString("name"); + } catch (JSONException e) {} + try { + ret.durability = obj.getInt("durability"); + } catch (JSONException e) {} + ret.position = obj.getInt("position"); + ret.material = obj.getInt("material"); + ret.data = obj.getInt("data"); + ret.damage = obj.getInt("damage"); + ret.quantity = obj.getInt("quantity"); + return ret; + } +} diff --git a/src/main/java/us/camin/api/WeatherEvent.java b/src/main/java/us/camin/api/events/WeatherEvent.java similarity index 50% rename from src/main/java/us/camin/api/WeatherEvent.java rename to src/main/java/us/camin/api/events/WeatherEvent.java index 995d03f..38f7b76 100644 --- a/src/main/java/us/camin/api/WeatherEvent.java +++ b/src/main/java/us/camin/api/events/WeatherEvent.java @@ -1,4 +1,4 @@ -package us.camin.api; +package us.camin.api.events; /* This file is part of Caminus @@ -17,27 +17,22 @@ package us.camin.api; along with Caminus. If not, see . */ -import org.json.JSONWriter; -import org.json.JSONException; - -public class WeatherEvent extends ClientEvent { - public String world; - public boolean isRaining; - - public WeatherEvent(String world, boolean rain) { - this.world = world; - this.isRaining = rain; - } - - public JSONWriter toJSON(JSONWriter writer) throws JSONException { - writer.key("world").value(world); - writer.key("isRaining").value(isRaining); - return writer; - } - - public String jsonName() { - return "weather"; - } +import org.json.*; +import java.util.*; +public class WeatherEvent extends Event { + public String world; + public boolean isRaining; + public static WeatherEvent fromJSON(JSONObject obj) throws JSONException { + WeatherEvent ret = new WeatherEvent(); + ret.world = obj.getString("world"); + ret.isRaining = obj.getBoolean("isRaining"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("world").value(this.world); +writer.key("isRaining").value(this.isRaining); + return writer; + } + public String jsonName() { return "weather"; } + static { Event.registerHandler("weather", WeatherEvent.class); } } - - diff --git a/src/main/java/us/camin/api/events/WebHeartbeatEvent.java b/src/main/java/us/camin/api/events/WebHeartbeatEvent.java new file mode 100644 index 0000000..0814ceb --- /dev/null +++ b/src/main/java/us/camin/api/events/WebHeartbeatEvent.java @@ -0,0 +1,49 @@ +package us.camin.api.events; + +/* + 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.*; +import java.util.*; +public class WebHeartbeatEvent extends Event { + public Map heartbeat; + public int server; + public String dayPeriod; + public int time; + public static WebHeartbeatEvent fromJSON(JSONObject obj) throws JSONException { + WebHeartbeatEvent ret = new WebHeartbeatEvent(); + + JSONObject heartbeat_obj = obj.getJSONObject("heartbeat"); + for(String name : JSONObject.getNames(heartbeat_obj)) { + ret.heartbeat.put(name, heartbeat_obj.get(name)); + } + + ret.server = obj.getInt("server"); + ret.dayPeriod = obj.getString("dayPeriod"); + ret.time = obj.getInt("time"); + return ret; + } + public JSONWriter toJSON(JSONWriter writer) throws JSONException { +writer.key("heartbeat").value(this.heartbeat); +writer.key("server").value(this.server); +writer.key("day-period").value(this.dayPeriod); +writer.key("time").value(this.time); + return writer; + } + public String jsonName() { return "web-heartbeat"; } + static { Event.registerHandler("web-heartbeat", WebHeartbeatEvent.class); } +}