Solved help getOnlinePlayers

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

Thread Status:
Not open for further replies.
  1. 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. Offline

    timtower Administrator Administrator Moderator

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

    timtower Administrator Administrator Moderator

  5. how can i resolve that problem then ?
     
  6. Offline

    timtower Administrator Administrator 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. i change to this Collection<? extends Player> player_on = Bukkit.getServer().getOnlinePlayers();

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

    timtower Administrator Administrator 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. int counter = 0;
    Collection<? extends Player> arrayOfPlayer1;
    int j = (arrayOfPlayer1 = player_on);
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    timtower Administrator Administrator Moderator

    int j = (arrayOfPlayer1 = player_on);
    There.
    There should only be 1 = on that line.
     
  12. can you give me your resolution ?
     
  13. Offline

    timtower Administrator Administrator 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
     
  14. its the same error on player_on
     
  15. Offline

    timtower Administrator Administrator Moderator

    What code did you use then?
     
  16. Offline

    timtower Administrator Administrator Moderator

  17. Offline

    timtower Administrator Administrator Moderator

    Google "enchanced for loop java" and look at the differences.
    You don't need a counter, you certainly don't need two lists.
     
  18. i cant find my error
     
  19. Offline

    timtower Administrator Administrator 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.
     
  20. can you fix it for me?
     
  21. Offline

    timtower Administrator Administrator 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.
     
  22. ty xD, i find my error so ty anyway xD
     
Thread Status:
Not open for further replies.

Share This Page