public JSONObject returnAPISuccess(Object calledMethod, Object result) { JSONObject r = new JSONObject(); r.put("result", "success"); r.put("source", calledMethod); r.put("success", result); return r; }
public JSONObject returnAPIError(Object calledMethod, String error) { JSONObject r = new JSONObject(); r.put("result", "error"); r.put("source", calledMethod); r.put("error", error); return r; }
public JSONObject returnAPIException(Object calledMethod, Exception e) { JSONObject r = new JSONObject(); r.put("result", "error"); StringWriter pw = new StringWriter(); e.printStackTrace(new PrintWriter(pw)); e.printStackTrace(); r.put("source", calledMethod); r.put( "error", "Caught exception: " + pw.toString().replaceAll("\\n", "\n").replaceAll("\\r", "\r")); return r; }
public static Object handle(Object obj) { if (obj instanceof World.Environment) { World.Environment e = (World.Environment) obj; if (e == World.Environment.NETHER) { return "nether"; } else { return "normal"; } } else if (obj instanceof File) { return ((File) obj).toString(); } else if (obj instanceof Player) { Player p = (Player) obj; JSONObject o = new JSONObject(); o.put("name", p.getName()); o.put("op", p.isOp()); o.put("health", p.getHealth()); o.put("ip", p.getAddress().toString()); o.put("itemInHand", p.getItemInHand()); o.put("location", p.getLocation()); o.put("inventory", p.getInventory()); o.put("sneaking", p.isSneaking()); o.put("inVehicle", p.isInsideVehicle()); o.put("sleeping", p.isSleeping()); o.put("world", p.getServer().getWorlds().indexOf(p.getWorld())); return o; } else if (obj instanceof Server) { Server s = (Server) obj; JSONObject o = new JSONObject(); o.put("maxPlayers", s.getMaxPlayers()); o.put("players", Arrays.asList(s.getOnlinePlayers())); o.put("port", s.getPort()); o.put("name", s.getName()); o.put("serverName", s.getServerName()); o.put("version", s.getVersion()); o.put("worlds", s.getWorlds()); return o; } else if (obj instanceof World) { World w = (World) obj; JSONObject o = new JSONObject(); o.put("environment", w.getEnvironment()); o.put("fullTime", w.getFullTime()); o.put("time", w.getTime()); o.put("name", w.getName()); o.put("isThundering", w.isThundering()); o.put("hasStorm", w.hasStorm()); return o; } else if (obj instanceof Plugin) { Plugin p = (Plugin) obj; PluginDescriptionFile d = p.getDescription(); JSONObject o = new JSONObject(); o.put("name", d.getName()); o.put("description", d.getDescription()); o.put("authors", d.getAuthors()); o.put("version", d.getVersion()); o.put("website", d.getWebsite()); o.put("enabled", JSONAPI.instance.getServer().getPluginManager().isPluginEnabled(p)); return o; } else if (obj instanceof ItemStack) { ItemStack i = (ItemStack) obj; JSONObject o = new JSONObject(); o.put("type", i.getTypeId()); o.put("durability", i.getDurability()); o.put("amount", i.getAmount()); return o; } else if (obj instanceof PlayerInventory) { PlayerInventory p = (PlayerInventory) obj; JSONObject o = new JSONObject(); JSONObject armor = new JSONObject(); armor.put("boots", p.getBoots()); armor.put("chestplate", p.getChestplate()); armor.put("helmet", p.getHelmet()); armor.put("leggings", p.getLeggings()); o.put("armor", armor); o.put("hand", p.getItemInHand()); o.put("inventory", Arrays.asList(p.getContents())); return o; } else if (obj instanceof Location) { Location l = (Location) obj; JSONObject o = new JSONObject(); o.put("x", l.getX()); o.put("y", l.getY()); o.put("z", l.getZ()); o.put("pitch", l.getPitch()); o.put("yaw", l.getYaw()); return o; } else if (obj instanceof Plugin[]) { List<Plugin> l = Arrays.asList((Plugin[]) obj); Collections.sort(l, new PluginSorter()); return l; } else if (obj instanceof Object[]) { int l = ((Object[]) obj).length; JSONArray a = new JSONArray(); for (int i = 0; i < l; i++) { a.add(((Object[]) obj)[i]); } return a; } Logger.getLogger("JSONAPI").warning("Uncaugh object! Value:"); Logger.getLogger("JSONAPI").warning(obj.toString()); Logger.getLogger("JSONAPI").warning("Type:"); Logger.getLogger("JSONAPI").warning(obj.getClass().getName()); return new Object(); }