Solved help getOnlinePlayers

Discussion in 'Plugin Development' started by SearchingNick, May 8, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    SearchingNick

    code :
    Code:
    @EventHandler
      public void onPlayerPreLogin(AsyncPlayerPreLoginEvent e)
      {
        String ip = e.getAddress().toString().replace("/", "");
        Player[] player_on = Bukkit.getServer().getOnlinePlayers();
        String name = e.getName();
      
        int counter = 0;
        Player[] arrayOfPlayer1;
        int j = (arrayOfPlayer1 = player_on).length;
        for (int i = 0; i < j; i++)
        {
          Player singlePlayer = arrayOfPlayer1[i];
          if (singlePlayer.getAddress().toString().replace("/", "").equals(ip)) {
            counter++;
          }
        }
        if (counter >= this.plugin.getConfig().getInt("AntiAlt.Settings.OneTime")) {
          e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, this.plugin.getConfig().getString("AntiAlt.Settings.KickMessageOneTime"));
        }
        try
        {
          Statement statement = AntiAlt.c.createStatement();
         
          ResultSet res = statement.executeQuery("SELECT COUNT(*) AS countedIP2 FROM `AntiAlt2` WHERE `ip` = '" + ip + "';");
          res.next();
          if (res.getInt("countedIP2") >= this.plugin.getConfig().getInt("AntiAlt.Settings.MaxGeneral")) {
            e.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, this.plugin.getConfig().getString("AntiAlt.Settings.KickMessageMaxGeneral"));
          }
          res.close();
         
          ResultSet res2 = statement.executeQuery("SELECT COUNT(id) AS namecount FROM AntiAlt2 WHERE `player` = '" + name + "';");
          res2.next();
          if (res2.getInt("namecount") >= 1) {
            statement.executeUpdate("UPDATE `AntiAlt2` SET `ip` = '" + ip + "' WHERE `player` = '" + name + "';");
          } else {
            statement.executeUpdate("INSERT INTO `AntiAlt2` (`player`, `ip`) VALUES ('" + name + "', '" + ip + "');");
          }
          res2.close();
        }
        catch (SQLException e1)
        {
          e1.printStackTrace();
        }
      }
     
  2. Online

    timtower Ninja on the waves Moderator

  3. Offline

    SearchingNick

    Player[] player_on = Bukkit.getServer().getOnlinePlayers();
    in eclipse give me a error
     
  4. Online

    timtower Ninja on the waves Moderator

  5. Offline

    SearchingNick

    how can i resolve that problem then ?
     
  6. Online

    timtower Ninja on the waves Moderator

    You change the type of player_on to the return type of getOnlinePlayers()
    You can even remove the Player[] part, then hover your mouse over player_on and select the quick fix.
     
  7. Offline

    SearchingNick

    i change to this Collection<? extends Player> player_on = Bukkit.getServer().getOnlinePlayers();

    now error on int j = (arrayOfPlayer1 = player_on).length;
     
  8. Online

    timtower Ninja on the waves Moderator

    Split those assignments. No reason to have them on the same line.
    And a collection doesn't have a length value. You need to find a replacement for it.
     
  9. Offline

    SearchingNick

    int counter = 0;
    Collection<? extends Player> arrayOfPlayer1;
    int j = (arrayOfPlayer1 = player_on);
     
  10. Online

    timtower Ninja on the waves Moderator

  11. Offline

    SearchingNick

  12. Online

    timtower Ninja on the waves Moderator

    int j = (arrayOfPlayer1 = player_on);
    There.
    There should only be 1 = on that line.
     
  13. Offline

    SearchingNick

    can you give me your resolution ?
     
  14. Online

    timtower Ninja on the waves Moderator

    Code:
    int counter = 0;
    Player[] arrayOfPlayer1;
    int j = (arrayOfPlayer1 = player_on).length;
    for (int i = 0; i < j; i++)
    {
    Player singlePlayer = arrayOfPlayer1[i];
    if (singlePlayer.getAddress().toString().replace("/", "").equals(ip)) {
    counter++;
    }
    }
    Replace that entire block with an enhanced for loop
     
  15. Offline

    SearchingNick

    its the same error on player_on
     
  16. Online

    timtower Ninja on the waves Moderator

    What code did you use then?
     
  17. Offline

    SearchingNick

  18. Online

    timtower Ninja on the waves Moderator

  19. Offline

    SearchingNick

  20. Online

    timtower Ninja on the waves Moderator

    Google "enchanced for loop java" and look at the differences.
    You don't need a counter, you certainly don't need two lists.
     
  21. Offline

    SearchingNick

    i cant find my error
     
  22. Online

    timtower Ninja on the waves Moderator

    You are using a counter loop, not an enhanced for loop.
    The fact that you have so many variables that are from a very outdated tutorial is causing your issues.
     
  23. Offline

    SearchingNick

    can you fix it for me?
     
  24. Online

    timtower Ninja on the waves Moderator

    Code:
    for (Player singlePlayer : Bukkit.getOnlinePlayers())
    {
    if (singlePlayer.getAddress().toString().replace("/", "").equals(ip)) {
    counter++;
    }
    }
    This is an enhanced for loop.
    No more need for weird variables and double storage.
     
  25. Offline

    SearchingNick

    ty xD, i find my error so ty anyway xD
     
Thread Status:
Not open for further replies.

Share This Page