예제 #1
0
  public boolean addEnchantmentsToPlayerInventorySlot(
      String playerName, int slot, List<Object> enchantments) {
    try {
      Player p = getPlayerExact(playerName);
      PlayerInventory inv = p.getInventory();
      ItemStack it;

      if (slot == inv.getHeldItemSlot()) it = inv.getHelmet();
      else if (slot == 102) it = inv.getChestplate();
      else if (slot == 101) it = inv.getLeggings();
      else if (slot == 100) it = inv.getBoots();
      else it = inv.getItem(slot);

      for (int i = 0; i < enchantments.size(); i++) {
        JSONObject o = (JSONObject) enchantments.get(i);
        it.addEnchantment(
            Enchantment.getById(Integer.valueOf(o.get("enchantment").toString())),
            Integer.valueOf(o.get("level").toString()));
      }

      p.saveData();

      return true;
    } catch (NullPointerException e) {
      return false;
    }
  }
예제 #2
0
파일: JSONServer.java 프로젝트: Xon/jsonapi
 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;
 }
예제 #3
0
파일: JSONServer.java 프로젝트: Xon/jsonapi
 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;
 }
예제 #4
0
파일: JSONServer.java 프로젝트: Xon/jsonapi
 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;
 }
예제 #5
0
  public boolean setPlayerInventorySlotWithDataDamageAndEnchantments(
      String playerName,
      int slot,
      int blockID,
      final int data,
      int damage,
      int quantity,
      Object[] enchantments) {
    try {
      if (blockID == 0) {
        return clearPlayerInventorySlot(playerName, slot);
      }

      Player p = getPlayerExact(playerName);
      PlayerInventory inv = p.getInventory();
      ItemStack it = (new MaterialData(blockID, (byte) data)).toItemStack(quantity);
      it.setDurability((short) data);

      for (int i = 0; i < enchantments.length; i++) {
        JSONObject o = (JSONObject) enchantments[i];
        it.addEnchantment(
            Enchantment.getById(Integer.valueOf(o.get("enchantment").toString())),
            Integer.valueOf(o.get("level").toString()));
      }

      if (slot == 103) inv.setHelmet(it);
      else if (slot == 102) inv.setChestplate(it);
      else if (slot == 101) inv.setLeggings(it);
      else if (slot == 100) inv.setBoots(it);
      else inv.setItem(slot, it);

      p.saveData();

      return true;
    } catch (NullPointerException e) {
      return false;
    }
  }
예제 #6
0
  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();
  }