ERROR - Need asap ***

Discussion in 'Spigot Plugin Development' started by Achieving, Apr 26, 2017.

  1. I keep getting this error, im not sure how to fix it. Paperspigot & API as well as custom spigot are added as dependencies. - the error mainly occurs when there is a .length on a line of code but i dont want to have to change them
  2. Choco


    You're likely using decompiled code (which I don't recommend). You're calling Bukkit#getOnlinePlayers(), but the decompiler thinks its returning the OLD method's returned value, Player[], rather than the now updated method that returns Collection<Player>. These are some errors you should be able to fix by yourself with some basic knowledge.

    (It also really helps to actually see the code as well as the error)
  3. Example of error code is as followed @2008Choco

    public ScoreboardHandler(Core plugin)
    this.playerBoards = new HashMap();
    this.plugin = plugin;
    this.timerSidebarProvider = new TimerSidebarProvider(plugin);
    Bukkit.getPluginManager().registerEvents(this, plugin);
    for (Player players : Bukkit.getOnlinePlayers())
    PlayerBoard playerBoard;
    setPlayerBoard(players.getUniqueId(), playerBoard = new PlayerBoard(plugin, players));


    public void onRecord(PlayerInteractEvent e)
    Player p = e.getPlayer();
    if ((modMode.contains(p.getName())) && (p.getItemInHand().getType() == Material.EYE_OF_ENDER) && (e.getAction().toString().contains("RIGHT")))
    Player random = Bukkit.getOnlinePlayers()[new java.util.Random().nextInt(Bukkit.getOnlinePlayers().length)];
    if (Bukkit.getOnlinePlayers().length == 1)

    The code is directly from the src and the dependence are new, ive got no clue
  4. Yep getOnlinePlayers.length refers to an array.
    You need to change that to size()
    And remove the arrays.asList bit as onlinePlayers is a collection that extends player.

    You'll likely have to update addUpdates to support collection rather than array.

    That or make an array or list called online that is populated from getOnlinePlayers

    Sent from my iPhone using Tapatalk
    • Agree Agree x 1
  5. Code (Text):

    if (p.hasPermission("modmode.randomtp"))
        ArrayList<Player> list = new ArrayList<Player>();

        if (list.size() == 0)
           p.sendMessage("There is currently noone to teleport to.");
           int randomIndex = new Random().nextInt(list.size());
           Player randomPlayer = list.get(randomIndex);
           p.sendMessage("Teleporting to " + randomPlayer.getName());
           return true;
    This is code I use to randomly teleport a player around.
  6. Why would you add them to a List when it returns a Collection, which you can Iterate and do all the stuff you want with.
  7. Because lists are indexed. There's a little trick though which would be a little faster where you go up to a certain iteration with the iterator. Not everyone thinks of this however.
  8. Could just call Collection#toArray(). Could also use lambda stream. And yeh, iretate up to a certain point as well.
  9. I was bored, cleaned this up a bit.
    Code (Java):
    private final SplittableRandom random = new SplittableRandom();

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
            return false;
        final Player[] players = Bukkit.getOnlinePlayers().stream().filter(p -> p != sender).toArray(Player[]::new);
        if (players.length == 0) {
            sender.sendMessage("There's currently noone to teleport to!");
            return false;
        final Player target;
        ((Player) sender).teleport(target = players[random.nextInt(players.length)]);
        sender.sendMessage("Teleporting to " + target.getName());
        return true;
    Yes, could be prettier and more efficient (I'm iterating over the whole list just to exclude the sender, as an example), I agree, but this should be a bit better - what bugged me the most was the 'new Random()' anyways.
    Also, don't initialize variables the way I initialized 'random' here, I find it messy.

    EDIT: Before anyone complains about the '!=' I used, yes, in general you shouldn't do that, but I believe the 'sender' instance should be the same as the one in the List.
    • Agree Agree x 1