ArrayList Not working? Please Help Me so confused

Discussion in 'Spigot Plugin Development' started by OhhhZenix, Jun 4, 2018.

  1. Hello there,
    So I have been working on a plugin but my ArrayList isn't working when is in the side of a bukkitrunnable it says that I'm not in the ArrayList when I am and I have made sure I am in the ArrayList.

    Here is the code:

    Code (Text):
        public void onGiveSpeed() {
            new BukkitRunnable() {

                @Override
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        try {
                            if(arraylist.contains(p.getName())) {
                                p.sendMessage("Worrking");
                            } else {
                                p.sendMessage("Not Worrking");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.runTaskTimer(Bukkit.getPluginManager().getPlugin("AdvancedArmorPackage"), 0L, 20L);
        }
     
  2. Can we see where you add the player to the array list?
     
  3. So if i understand, the output of the given code is "Not working" ?
    Show us when you add yourself to the array
    Btw why the fuck do you have a try/catch ?
     
  4. yeah so is outputting "Not working"
     
  5. That's because you are not in the list, show us when you add yourself to the given array please
     
  6. Code (Text):
        @EventHandler
        public void onjoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            if(!arraylist.contains(p.getName())) {
                arraylist.add(p.getName());
            }
        }
    Yes i have added the register the listener to the main class where the javaplugin extends thing is.
     
  7. Have you tried debugging your join event? Is that code being fired?
     
  8. i just tried and made sure that my join event was working and adding me to the ArrayList by sending a message
     
  9. Are you sure you PlayerJoinEvent and onGiveSpeed method use the same array ?
     
  10. yes they are in the same class
     
  11. Based on the code you sent me I can't see any reason for it to say not working. Can you show me the other places you use the array list, if there are any? Also, when do you remove the player from the list?
     
  12. i remove my self from the ArrayList by reloading the server and to readd my self i leave the server and rejoin it. and no i dont have any other place im using it other then that.
     
  13. What's the use of this array? If you want to have an array that stores all the online players, Bukkit.getOnlinePlayers() already does that. Anyway, please show me where you initialize the arraylist.

    EDIT: If possible, upload the entire class or classes that are using the array please.
     
  14. to give anyone potion effect that is in the arraylist and then take the potion effect away when the person is not in the arraylist anymore
     
  15. Assuming you want the effect to be permanent, you could simply make the duration Integer.MAX. As for checking if the player is in an array list, it might be easier, in this case, to give the player a permission like "plugin.potion.speed" (via a command or something of the sort) and check player.hasPermission("plugin.potion.speed) on playerJoin. If so, provide the infinite duration potion effect (this is assuming something clears the effect after they leave, if you give them the infinite length potion effect it will persist after server resets).
     
  16. Then they are lot of unnecessary things in your code
    try something like that
    Code (Text):
    public final class SpigotHelp extends JavaPlugin implements Listener {

        private List<Player> players = new ArrayList<>();

        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            giveSpeed();
        }

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            if (!players.contains(e.getPlayer())) {
                players.add(e.getPlayer());
            }
        }
     
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            players.remove(e.getPlayer());
        }

        public void giveSpeed() {
            new BukkitRunnable() {
                @Override
                public void run() {
                    for (Player p : players) {
                        p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20, 1));
                    }
                }
            }.runTaskTimer(this, 0L, 20L);
        }

    }
    1 - I've created a list of player instead of a list of name, in this list is stored every player who will get the potion effect every second, so you can remove your
    Code (Text):
    for (Player p : Bukkit.getOnlinePlayers());
    and use
    Code (Text):
    for (Player p : players);
    and give the wanted potion effect
    players are add when joining and removed when leaving server but you probably need another way to remove/add them
    PS the for statement can also be simplified like that:
    Code (Text):
    players.forEach(player -> player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20, 1)));
     
  17. ty actually I think I have figure out away thanks to you.
     
  18. Are you trying to specifically give yourself speed when the piece of armor is equipped?
     
  19. yeah, but when the whole armor is and has the same lore with it but the issue I'm having is giving effects to the ArrayList so basically giving effects to the players that are in that ArrayList.
     
  20. ah, so they have to have a full set of a specific of armor, and if a piece is removed, so should the potion effect.
    if this is the case, you don't even need the array.
    Simply listen to armorEquipping events, and check the full player's armor set for the lore (which seems to be working for you). If so and they don't already have the speed, give them the infinite duration. If they already do but don't have the armor set, remove the potion effect.