public PlayerWarnStorage(ConnectionSource connection) throws SQLException {
    super(
        connection,
        (DatabaseTableConfig<PlayerWarnData>)
            BanManager.getPlugin().getConfiguration().getLocalDb().getTable("playerWarnings"));

    if (!this.isTableExists()) {
      TableUtils.createTable(connection, tableConfig);
    } else {
      // Attempt to add new columns
      try {
        String update =
            "ALTER TABLE "
                + tableConfig.getTableName()
                + " ADD COLUMN `expires` INT(10) NOT NULL DEFAULT 0,"
                + " ADD KEY `"
                + tableConfig.getTableName()
                + "_expires_idx` (`expires`)";
        executeRawNoArgs(update);
      } catch (SQLException e) {
      }
      try {
        String update =
            "ALTER TABLE "
                + tableConfig.getTableName()
                + " ADD COLUMN `points` INT(10) NOT NULL DEFAULT 1,"
                + " ADD KEY `"
                + tableConfig.getTableName()
                + "_points_idx` (`points`)";
        executeRawNoArgs(update);
      } catch (SQLException e) {

      }
    }
  }
Exemple #2
0
/**
 * This is a static API for BanManager. All methods are thread safe unless stated otherwise.
 *
 * <p>Note: The API does not handle the notifications to players nor permission checks for
 * exemptions.
 *
 * @author James Mortemore
 */
public class BmAPI {

  private static BanManager plugin = BanManager.getPlugin();

  // Bans
  /**
   * Permanently ban a player. You must handle kicking the player if they are online.
   *
   * @param name - Name of player to ban
   * @param bannedBy - Who the ban is by, can be anything
   * @param reason - Why they are banned
   */
  public static void ban(String name, String bannedBy, String reason) {
    plugin.addPlayerBan(name, bannedBy, reason);
  }

  /**
   * Temporarily ban a player You must handle kicking the player if they are online.
   *
   * @param name - Name of player to ban
   * @param bannedBy - Who the ban is by, can be anything
   * @param reason - Why they are banned
   * @param expires - Unix Timestamp stating the time of when the ban ends
   */
  public static void tempBan(String name, String bannedBy, String reason, long expires) {
    plugin.addPlayerBan(name, bannedBy, reason, expires);
  }

  /**
   * Unban a player
   *
   * @param name - Name of player to unban
   * @param by - Who the unban is by, can be anything
   * @param keepLog - Whether to store the ban as a record or not
   */
  public static void unBan(String name, String by, boolean keepLog) {
    plugin.removePlayerBan(name, by, keepLog);
  }

  /**
   * Check if a player is banned
   *
   * @param name - Name of player to check
   * @return true if the player is currently banned, whether they are permanently banned or
   *     temporarily banned
   */
  public static boolean isBanned(String name) {
    return plugin.isPlayerBanned(name);
  }

  /**
   * Get ban information of a player
   *
   * @param name - Name of player
   * @return - BanData object, if the player is not banned, it returns null
   */
  public static BanData getCurrentBan(String name) {
    return plugin.getPlayerBan(name);
  }

  /**
   * Retrieve past bans of a player. This method is not thread safe and should not be called on the
   * main thread.
   *
   * @param name - Name of player
   * @return ArrayList containing data on all bans
   */
  public static ArrayList<BanData> getPastBans(String name) {
    return plugin.getPlayerPastBans(name);
  }

  // Mutes
  /**
   * Permanently mute a player
   *
   * @param name - Name of player to mute
   * @param mutedBy - Who the mute is by, can be anything
   * @param reason - Why they are muted
   */
  public static void mute(String name, String mutedBy, String reason) {
    plugin.addPlayerMute(name, mutedBy, reason);
  }

  /**
   * Temporarily mute a player
   *
   * @param name - Name of player to mute
   * @param mutedBy - Who the mute is by, can be anything
   * @param reason - Why they are muted
   * @param expires - Unix Timestamp stating the time of when the ban ends
   */
  public static void tempMute(String name, String mutedBy, String reason, long expires) {
    plugin.addPlayerMute(name, mutedBy, reason, expires);
  }

  /**
   * Unmute a player
   *
   * @param name - Name of player to unmute
   * @param by - Who the unmute is by, can be anything
   * @param keepLog - Whether to store the mute as a record or not
   */
  public static void unMute(String name, String by, boolean keepLog) {
    plugin.removePlayerMute(name, by, keepLog);
  }

  /**
   * Check if a player is muted.
   *
   * <p>Due to the nature of mutes only being in memory if the player is online this method should
   * not be called within the main thread.
   *
   * @param name - Name of player to check
   * @return true if the player is currently muted, whether they are permanently muted or
   *     temporarily muted
   */
  public static boolean isMuted(String name) {
    return plugin.isPlayerMuted(name);
  }

  /**
   * Get mute information of a player.
   *
   * <p>Due to the nature of mutes only being in memory if the player is online this method should
   * not be called within the main thread.
   *
   * @param name - Name of player
   * @return - MuteData object, if the player is not banned, it returns null
   */
  public static MuteData getCurrentMute(String name) {
    return plugin.getPlayerMute(name);
  }

  /**
   * Retrieve past mutes of a player. This method is not thread safe and should not be called on the
   * main thread.
   *
   * @param name - Name of player
   * @return ArrayList containing data on all mutes
   */
  public static ArrayList<MuteData> getPastMutes(String name) {
    return plugin.getPlayerPastMutes(name);
  }

  // IP Bans
  /**
   * Permanently ban an IP You must handle kicking any players with the ip if they are online.
   *
   * @param ip - IP address to ban
   * @param bannedBy - Who the ban is by, can be anything
   * @param reason - Why they are banned
   */
  public static void banIP(String ip, String bannedBy, String reason) {
    plugin.addIPBan(ip, bannedBy, reason);
  }

  /**
   * Temporarily ban an IP You must handle kicking any players with the ip if they are online.
   *
   * @param ip - IP address to ban
   * @param bannedBy - Who the ban is by, can be anything
   * @param reason - Why they are banned
   * @param expires - Unix Timestamp stating the time of when the ban ends
   */
  public static void tempIPBan(String ip, String bannedBy, String reason, long expires) {
    plugin.addIPBan(ip, bannedBy, reason, expires);
  }

  /**
   * Unban an IP
   *
   * @param name - IP address to unban
   * @param by - Who the unban is by, can be anything
   * @param keepLog - Whether to store the ban as a record or not
   */
  public static void unBanIP(String name, String by, boolean keepLog) {
    plugin.removeIPBan(name, by, keepLog);
  }

  /**
   * Check if a player is banned
   *
   * @param ip - IP address to check
   * @return true if the player is currently banned, whether they are permanently banned or
   *     temporarily banned
   */
  public static boolean isIPBanned(String ip) {
    return plugin.isIPBanned(ip);
  }

  /**
   * Get ban information of an IP ban
   *
   * @param ip - IP address
   * @return - IPBanData object, if the IP is not banned, it returns null
   */
  public static IPBanData getCurrentIPBan(String ip) {
    return plugin.getIPBan(ip);
  }

  /**
   * Retrieve past ip bans of an IP. This method is not thread safe and should not be called on the
   * main thread.
   *
   * @param ip - IP address to check
   * @return ArrayList containing data on all bans
   */
  public static ArrayList<IPBanData> getPastIPBans(String ip) {
    return plugin.getIPPastBans(ip);
  }

  // Warnings
  /**
   * Warn a player. You must handle the notification to the warned player yourself.
   *
   * @param name - Name of player to warn
   * @param bannedBy - Who the warning is by, can be anything
   * @param reason - Why they are warned
   */
  public static void warn(String name, String warnedBy, String reason) {
    plugin.addPlayerWarning(name, warnedBy, reason);
  }

  /**
   * Retrieve past warnings of a player. This method is not thread safe and should not be called on
   * the main thread.
   *
   * @param name - Name of player
   * @return ArrayList containing data on all warnings
   */
  public static ArrayList<WarnData> getWarnings(String name) {
    return plugin.getPlayerWarnings(name);
  }

  // Kicks
  /**
   * Kicks a player and logs it to the database. You must check the player is online first otherwise
   * a NPE will be thrown.
   *
   * @param name - Name of player to kick
   * @param bannedBy - Who the kick is by, can be anything
   * @param reason - Why they are kicked
   */
  public static synchronized void kick(String name, String kickedBy, String reason) {
    String viewReason = Util.viewReason(reason);
    Player target = Bukkit.getPlayer(name);

    String kick =
        getMessage("kickReason")
            .replace("[name]", target.getDisplayName())
            .replace("[reason]", viewReason)
            .replace("[by]", kickedBy);

    target.kickPlayer(kick);

    plugin.dbLogger.logKick(name, kickedBy, reason);
  }

  // Misc
  /**
   * @param message - The message config node
   * @return String
   */
  public static String getMessage(String message) {
    return plugin.getMessage(message);
  }

  /**
   * Retrieve the players IP. This method is not thread safe and should not be called on the main
   * thread.
   *
   * @param name - Name of the player
   * @return - IP of the player, if not found an empty string is returned.
   */
  public static String getPlayerIP(String name) {
    return plugin.getPlayerIP(name);
  }
}
public class WarningActionsConfig {

  private BanManager plugin = BanManager.getPlugin();
  @Getter private boolean isEnabled = false;
  private HashMap<Double, List<ActionCommand>> actions;

  public WarningActionsConfig(ConfigurationSection config) {
    isEnabled = config.getBoolean("enabled", false);
    actions = new HashMap<>();

    if (!isEnabled) return;

    ConfigurationSection actionsConf = config.getConfigurationSection("actions");

    if (actionsConf == null) return;

    for (String amount : actionsConf.getKeys(false)) {
      if (!StringUtils.isNumeric(amount)) {
        plugin.getLogger().warning("Invalid warning action, " + amount + " is not numeric");
        continue;
      }

      // New check
      List<Map<?, ?>> mapList = actionsConf.getMapList(amount);
      if (mapList != null && mapList.size() != 0) {
        List<ActionCommand> actionCommands = new ArrayList<>(mapList.size());

        for (Map<?, ?> map : mapList) {
          if (map.get("cmd") == null) {
            plugin.getLogger().severe("Missing cmd from warningActions " + amount);
            continue;
          }

          long delay = 0;

          if (map.get("delay") != null) {
            try {
              delay = Long.valueOf((Integer) map.get("delay"));
            } catch (NumberFormatException e) {
              plugin.getLogger().severe("Invalid delay for " + map.get("cmd"));
              continue;
            }

            delay = delay * 20L; // Convert from seconds to ticks
          }

          actionCommands.add(new ActionCommand((String) map.get("cmd"), delay));
        }

        this.actions.put(NumberUtils.toDouble(amount), actionCommands);
      } else {
        List<String> actions = actionsConf.getStringList(amount);
        if (actions.size() == 0) continue;

        plugin
            .getLogger()
            .warning(
                "warningActions amount "
                    + amount
                    + " is using a deprecated list, please use new cmd and delay syntax");
        List<ActionCommand> actionCommands = new ArrayList<>(actions.size());

        for (String action : actions) {
          actionCommands.add(new ActionCommand(action, 0));
        }

        this.actions.put(NumberUtils.toDouble(amount), actionCommands);
      }
    }
  }

  public List<ActionCommand> getCommand(double amount) {
    return actions.get(amount);
  }
}
public class PlayerWarnStorage extends BaseDaoImpl<PlayerWarnData, Integer> {

  private BanManager plugin = BanManager.getPlugin();

  public PlayerWarnStorage(ConnectionSource connection) throws SQLException {
    super(
        connection,
        (DatabaseTableConfig<PlayerWarnData>)
            BanManager.getPlugin().getConfiguration().getLocalDb().getTable("playerWarnings"));

    if (!this.isTableExists()) {
      TableUtils.createTable(connection, tableConfig);
    } else {
      // Attempt to add new columns
      try {
        String update =
            "ALTER TABLE "
                + tableConfig.getTableName()
                + " ADD COLUMN `expires` INT(10) NOT NULL DEFAULT 0,"
                + " ADD KEY `"
                + tableConfig.getTableName()
                + "_expires_idx` (`expires`)";
        executeRawNoArgs(update);
      } catch (SQLException e) {
      }
      try {
        String update =
            "ALTER TABLE "
                + tableConfig.getTableName()
                + " ADD COLUMN `points` INT(10) NOT NULL DEFAULT 1,"
                + " ADD KEY `"
                + tableConfig.getTableName()
                + "_points_idx` (`points`)";
        executeRawNoArgs(update);
      } catch (SQLException e) {

      }
    }
  }

  public boolean addWarning(PlayerWarnData data, boolean silent) throws SQLException {
    PlayerWarnEvent event = new PlayerWarnEvent(data, silent);
    Bukkit.getServer().getPluginManager().callEvent(event);

    if (event.isCancelled()) {
      return false;
    }

    boolean created = create(data) == 1;

    if (created)
      Bukkit.getServer()
          .getPluginManager()
          .callEvent(new PlayerWarnedEvent(data, event.isSilent()));

    return created;
  }

  public CloseableIterator<PlayerWarnData> getUnreadWarnings(UUID uniqueId) throws SQLException {
    return queryBuilder()
        .where()
        .eq("player_id", UUIDUtils.toBytes(uniqueId))
        .and()
        .eq("read", false)
        .iterator();
  }

  public CloseableIterator<PlayerWarnData> getWarnings(PlayerData player) throws SQLException {
    return queryBuilder().where().eq("player_id", player).iterator();
  }

  public long getCount(PlayerData player) throws SQLException {
    return queryBuilder().where().eq("player_id", player).countOf();
  }

  public long getPointsCount(PlayerData player) throws SQLException {
    return queryRawValue(
        "SELECT SUM(points) FROM "
            + getTableInfo().getTableName()
            + " WHERE player_id = UNHEX('"
            + player.getUUID().toString().replace("-", "")
            + "')");
  }

  public boolean isRecentlyWarned(PlayerData player) throws SQLException {
    if (plugin.getConfiguration().getWarningCooldown() == 0) {
      return false;
    }

    return queryBuilder()
            .where()
            .eq("player_id", player)
            .and()
            .ge(
                "created",
                (System.currentTimeMillis() / 1000L)
                    - plugin.getConfiguration().getWarningCooldown())
            .countOf()
        > 0;
  }

  public int deleteRecent(PlayerData player) throws SQLException {
    // TODO use a raw DELETE query to reduce to one query
    PlayerWarnData warning =
        queryBuilder()
            .limit(1L)
            .orderBy("created", false)
            .where()
            .eq("player_id", player)
            .queryForFirst();

    return delete(warning);
  }

  public void purge(CleanUp cleanup, boolean read) throws SQLException {
    if (cleanup.getDays() == 0) return;

    updateRaw(
        "DELETE FROM "
            + getTableInfo().getTableName()
            + " WHERE created < UNIX_TIMESTAMP(DATE_SUB(NOW(), "
            + "INTERVAL "
            + cleanup.getDays()
            + " DAY)) AND `read` = "
            + (read ? "1" : "0"));
  }

  public CloseableIterator<PlayerWarnData> findWarnings(long fromTime) throws SQLException {
    if (fromTime == 0) {
      return iterator();
    }

    long checkTime = fromTime + DateUtils.getTimeDiff();

    QueryBuilder<PlayerWarnData, Integer> query = queryBuilder();
    Where<PlayerWarnData, Integer> where = query.where();
    where.ge("created", checkTime);

    query.setWhere(where);

    return query.iterator();
  }
}