Bukkit.getOnlinePlayers()).length;

Discussion in 'Plugin Development' started by RealEmpire, Feb 28, 2016.

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

    RealEmpire

    This error won't go away can anybody tell me how to fix it!?!?! Heres my code:

    Code:
    package de.MapSwitch.main;
    
    import java.util.HashMap;
    import me.fussllein.ffa.main.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class Chat
      implements Listener
    {
      @EventHandler
      public void onChat(AsyncPlayerChatEvent e)
      {
        Player p = e.getPlayer();
        if (p.getDisplayName().startsWith("§4Owner"))
        {
          if (Main.names.containsKey(p.getName()))
          {
            e.setCancelled(true);
            Player[] onlinePlayers;
            int length = (onlinePlayers = Bukkit.getOnlinePlayers()).length;
            for (int i = 0; i < length; i++)
            {
              Player all = onlinePlayers[i];
              if (all.hasPermission("whois.use")) {
                all.sendMessage("§e(" + p.getName() + ") §9" + p.getDisplayName().replace("|", "▏") + " §8» §6" + e.getMessage().replace("%", "%%"));
              } else {
                all.sendMessage("§9" + p.getDisplayName().replace("|", "▏") + " §8» §6" + e.getMessage().replace("%", "%%"));
              }
            }
          }
          else
          {
            e.setFormat("§9" + p.getDisplayName().replace("|", "▏") + " §8» §6" + e.getMessage().replace("%", "%%"));
          }
        }
        else if (Main.names.containsKey(p.getName()))
        {
          e.setCancelled(true);
          Player[] onlinePlayers2;
          int length2 = (onlinePlayers2 = Bukkit.getOnlinePlayers()).length;
          for (int j = 0; j < length2; j++)
          {
            Player all = onlinePlayers2[j];
            if (all.hasPermission("whois.use")) {
              all.sendMessage("§e(" + p.getName() + ") §9" + p.getDisplayName().replace("|", "▏") + " §8» §f" + e.getMessage().replace("%", "%%"));
            } else {
              all.sendMessage("§9" + p.getDisplayName().replace("|", "▏") + " §8» §f" + e.getMessage().replace("%", "%%"));
            }
          }
        }
        else
        {
          e.setFormat("§9" + p.getDisplayName().replace("|", "▏") + " §8» §f" + e.getMessage().replace("%", "%%"));
        }
      }
    }
    
     
  2. Offline

    Gerov

    @RealEmpire

    getOnlinePlayers() returns a collection, not an array, so use .size().
     
  3. Offline

    teej107

    It's .length and it is not a method.

    @RealEmpire If you look at what that getOnlinePlayers method returns, you wouldn't even need to make a thread about this.
     
  4. Offline

    RealEmpire

    Tried it, still gives me an error
     
  5. Offline

    Swakiny

    Which one?
     
  6. Offline

    boomboompower

    @RealEmpire
    Please send us the statetrace via pastebin or something.
     
  7. Offline

    mythbusterma


    This line. You need to stop doing stuff like this. It is not cool or clever to condense code into the fewest lines possible, it's just annoying.

    It depends on what version of Bukkit you're using, but again, just thinking and putting in effort will fix this problem. All you need to do is check what this method returns.
     
    Zombie_Striker likes this.
  8. Offline

    RealEmpire

    I'm not trying to be cool or clever, I DONT KNOW HOW TO DO THIS I NEED HELP FOR GODS SAKE
     
    Liam Townsley likes this.
  9. Offline

    HoeMC

    If you looked at the error you'd be able to search it and find that your problem has nothing to do with the Bukkit API, but rather Java. If you don't know how to solve this problem on your own, you really should be looking at basic Java tutorials before diving into Bukkit.

    The line that @mythbusterma pointed out isn't wrong or related to the problem. It's just not good convention when programming; variables should be initialized on unique lines. There's no purpose for you to do that, it doesn't even save any space.
     
  10. Offline

    Zombie_Striker

    Please don't down play convention. You need to know convention if you have any intention on showing other people your code.
     
  11. Offline

    HoeMC

    What? I didn't downplay convention at all. I only told him that's not the reason why his code isn't working.
     
  12. Offline

    Zombie_Striker

    To me, this is downplaying convention.

    BTW: Always remember to give people links to tutorials or resources.

    @RealEmpire
    In case you never learned/ fully understood Java before, please find a tutorial at the following link, learn Java (this should only take a week), and come back to bukkit.
    https://bukkit.org/threads/new-devs-how-to-make-your-plugin-right-make-it-better.317054/
     
  13. Offline

    HoeMC

    Sorry, I meant there's no reason for him to condense his code like that, not follow conventions.

    This is a Bukkit development forum though, not a Java development forum. If he needs help learning Java, a quick google search will provide him all the resources he needs.
     
  14. Online

    timtower Administrator Administrator Moderator

    More offtopic comments will be removed.
    Please stop the convention discussion.
     
  15. Offline

    mythbusterma

    @timtower

    I brought it up because it's likely the reason he's having issues. He should spread that statement out over multiple lines.

    @RealEmpire

    You haven't told us the error. You haven't told us what version of Bukkit you're using. Your code is borderline unreadable. You haven't even told us what you're trying to do.

    Some explanation is necessary before we can help you, but in the mean time, why don't you make it so that we can read your code.
     
Thread Status:
Not open for further replies.

Share This Page