Exemplo n.º 1
0
  /**
   * Load and return all warps
   *
   * @return An ArrayList containing all loaded Warp instances.
   */
  public List<Warp> loadWarps() {
    List<Warp> warps = Lists.newArrayList();
    List<DataAccess> daos = Lists.newArrayList();
    boolean needsUpdate = false;

    try {
      Database.get().loadAll(schema, daos, new HashMap<String, Object>());
      for (DataAccess dao : daos) {
        WarpDataAccess data = (WarpDataAccess) dao;
        Group[] groups = makeGroupArray(data.groups);
        String owner = ToolBox.stringToNull(data.owner);
        String name = data.name;
        boolean playerHome = data.isPlayerHome;
        Location loc = null;
        if (((WarpDataAccess) dao).location.equals("N/A")) {
          loc = Location.fromDataAccess((LocationDataAccess) dao);
        } else {
          needsUpdate = true;
          loc = Location.fromString(data.location);
        }
        Warp warp;

        if (owner != null) {
          warp = new Warp(loc, name, owner, playerHome);
        } else if (groups != null && groups.length > 0) {
          warp = new Warp(loc, groups, name);
        } else {
          // assume this is a public warp
          warp = new Warp(loc, name);
        }
        warps.add(warp);
      }
    } catch (DatabaseReadException e) {
      log.error(e.getMessage(), e);
    }

    // Apply pending updates
    if (needsUpdate) {
      Canary.log.debug("Updating data for Warps...");
      for (Warp warp : warps) {
        this.updateWarp(warp);
      }
    }
    return warps;
  }
  /**
   * Registers a new {@link net.canarymod.api.entity.living.humanoid.npc.NPCBehaviorListener}
   *
   * @param listner the {@link net.canarymod.api.entity.living.humanoid.npc.NPCBehaviorListener} to
   *     be added
   * @param npc the {@link net.canarymod.api.entity.living.humanoid.NonPlayableCharacter} associated
   *     with the listener
   * @param force {@code true} to override existing keys if existant; {@code false} to error out on
   *     duplicate keys (recommended)
   */
  public static void registerNPCListener(
      final NPCBehaviorListener listener, final NonPlayableCharacter npc, final boolean force) {
    synchronized (registered) {
      Method[] methods =
          ToolBox.safeArrayMerge(
              listener.getClass().getMethods(),
              listener.getClass().getDeclaredMethods(),
              new Method[1]);
      for (final Method method : methods) {
        // Check if the method is a NPCBehavior handling method
        final NPCBehavior handler = method.getAnnotation(NPCBehavior.class);

        if (handler == null) {
          continue; // Next, not one of our things
        }
        // Check the parameters for number and type and decide if it's one
        // that is really a handler method
        Class<?>[] parameters = method.getParameterTypes();

        if (parameters.length != 1) {
          throw new NpcAiListenerMethodConsistancyException(
              "Amount of parameters for "
                  + method.getName()
                  + " is invalid. Expected 1, was "
                  + parameters.length);
        }
        Class<?> npcAICls = parameters[0];

        if (!NPCAI.class.isAssignableFrom(npcAICls)) {
          throw new NpcAiListenerMethodConsistancyException(
              "NPCAI is not assignable from " + npcAICls.getName());
        }

        // We checked the class above, ignore unchecked warnings.
        registered.put(
            (Class<? extends NPCAI>) npcAICls,
            new NPCBehaviorRegisteredListener(listener, npc, method));
      }
    }
  }