/**
   * Called to register HSP's commands with Essentials, which then enables Essentials "respectful"
   * command usurp behavior which will let HSP own commands like "/home" and "/spawn".
   */
  private void registerCommands() {
    log.debug("entering registerCommands()");

    essentialsPlugin = plugin.getServer().getPluginManager().getPlugin("Essentials");

    if (essentialsPlugin == null) {
      log.debug("Essentials plugin not found, registerComamnds() doing nothing");
      return;
    }

    try {
      grabInternalAltCommands();
      mapHSPCommands();
    } catch (Exception e) {
      log.error("Caught exception when trying to register commands with Essentials", e);
    }

    log.debug("exiting registerCommands()");
  }
  /**
   * Using Essentials own internal alternate commands map, assign HSP commands into the map. This is
   * a replication of the internal algorithm that Essentials uses in AlternativeCommandsHandler as
   * of Essentials 2.10.1.
   */
  private void mapHSPCommands() {
    Map<String, PluginCommand> hspCommands = bukkitCommandRegister.getLoadedCommands();
    Collection<PluginCommand> commands = hspCommands.values();
    //        final String pluginName = plugin.getDescription().getName().toLowerCase();

    log.debug("commands.size() = {}", commands == null ? null : commands.size());
    for (Command command : commands) {
      final PluginCommand pc = (PluginCommand) command;
      final List<String> labels = new ArrayList<String>(pc.getAliases());
      labels.add(pc.getName());

      log.debug("registering command {}", pc.getName());
      //            PluginCommand reg = plugin.getServer().getPluginCommand(pluginName + ":" +
      // pc.getName().toLowerCase());
      //            if (reg == null)
      //            {
      //                reg = plugin.getServer().getPluginCommand(pc.getName().toLowerCase());
      //            }
      //            if (reg == null || !reg.getPlugin().equals(plugin))
      //            {
      //                continue;
      //            }
      //            log.debug("reg = {}", reg);
      for (String label : labels) {
        log.debug("registering label {}", label);
        List<PluginCommand> plugincommands = altcommands.get(label.toLowerCase());
        if (plugincommands == null) {
          plugincommands = new ArrayList<PluginCommand>();
          altcommands.put(label.toLowerCase(), plugincommands);
        }
        boolean found = false;
        for (PluginCommand pc2 : plugincommands) {
          if (pc2.getPlugin().equals(plugin)) {
            found = true;
          }
        }
        if (!found) {
          plugincommands.add(pc);
        }
      }
    }
  }
Example #3
0
  @Override
  public void load(Chunk chunk) throws IOException {
    File regionDirectory = getRegionDirectory(chunk.world, chunk.x, chunk.z);
    File f = new File(regionDirectory, chunk.x + "_" + chunk.z);

    // if no file exists, just initialize a small-capacity map
    if (!f.exists()) {
      log.debug("load on chunk {}, no file exists, initializing empty dataset", chunk);
      chunk.map = new IntShortOpenHashMap(100);
      return;
    }

    DataInputStream is = null;

    try {
      is = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));

      // first 32 bits represent the size of the map
      int available = is.readInt();

      // we round up by 30% to account for .75 load factor in hash.
      // this initializes the map at less than the load factor with
      // some extra room for growth before needing a clone & grow
      chunk.map = new IntShortOpenHashMap((int) (available * 1.3));

      while (is.available() > 0) {
        int key = is.readInt();
        short value = is.readShort();
        chunk.map.put(key, value);
        if (log.isDebugEnabled()) {
          Formatter format = new Formatter();
          format.format("loaded chunk{%d,%d} owner %d for key %x", chunk.x, chunk.z, value, key);
          log.debug(format.toString());
          format.close();
        }
      }
    } finally {
      if (is != null) is.close();
    }
  }
  /**
   * Method to grab Essentials internal altCommands hash. This is ugly code but Essentials offers no
   * external APIs to make this any cleaner.
   *
   * @throws NoSuchFieldException
   * @throws SecurityException
   * @throws IllegalAccessException
   * @throws IllegalArgumentException
   */
  @SuppressWarnings("unchecked")
  private void grabInternalAltCommands()
      throws SecurityException, NoSuchFieldException, IllegalArgumentException,
          IllegalAccessException {
    Essentials pluginObject = (Essentials) essentialsPlugin;
    Field commandHandler = pluginObject.getClass().getDeclaredField("alternativeCommandsHandler");
    commandHandler.setAccessible(true);
    AlternativeCommandsHandler ach = (AlternativeCommandsHandler) commandHandler.get(pluginObject);
    Field altCommandsField = ach.getClass().getDeclaredField("altcommands");
    altCommandsField.setAccessible(true);
    altcommands = (HashMap<String, List<PluginCommand>>) altCommandsField.get(ach);

    log.debug("altcommands = {}", altcommands);
  }
  @Override
  public int purgePlayer(String playerName) {
    // first delete any homeInvites the player was invited too
    int rowsPurged = util.deleteRows(TABLE, "invited_player", playerName);

    // then delete any HomeInvites the player sent out to others
    Set<HomeInvite> set = findAllOpenInvites(playerName);
    for (HomeInvite hi : set) {
      try {
        deleteHomeInvite(hi);
        rowsPurged++;
      } catch (Exception e) {
        log.error("Caught exception while purging homeInvite", e);
      }
    }
    return rowsPurged;
  }
  @Override
  public int purgeWorldData(final String world) {
    int rowsPurged = 0;

    // delete any invites whose home is on the purged world
    Set<HomeInvite> set = findAllHomeInvites();
    for (HomeInvite hi : set) {
      if (world.equals(hi.getHome().getWorld())) {
        try {
          deleteHomeInvite(hi);
          rowsPurged++;
        } catch (Exception e) {
          log.error("Caught exception while purging homeInvite", e);
        }
      }
    }
    return rowsPurged;
  }