Bukkit Coding (ArrayLists)? [HELP]

Discussion in 'Plugin Development' started by benzimmer123, Feb 2, 2014.

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

    benzimmer123

    Okay , i have a method that adds the players to an Arraylist which is working fine because it displays the messages and i even made a way to check there in the Arraylist. However i have tried to make a command to make them leave the Arraylist but whenever i do it just tells me that i'm not in the Arraylist!
    Thanks in advance!:)

    Method to add me to the arraylist...

    Code:java
    1. if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "[Join]")) {
    2. if (s.getLine(1).equalsIgnoreCase("0-0")) {
    3. if (gunspleef1.contains(p)) {
    4. p.sendMessage(ChatColor.RED + "You are already in an arena.");
    5. } else {
    6. if (plugin.getConfig().getBoolean("Gunspleef." + "1" + ".started")) {
    7. p.sendMessage(ChatColor.RED + "The game has already started!");
    8. return;
    9. }
    10. players1++;
    11. plugin.getConfig().set("Gunspleef." + "1" + ".players", players1);
    12. plugin.saveConfig();
    13. gunspleef1.add(p);
    14. online(p);


    Method to remove me from the list...

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    2. Player p = (Player) sender;
    3. if (cmd.getName().equalsIgnoreCase("leave")) {
    4. int players1 = plugin.getConfig().getInt("Gunspleef." + "1" + ".players");
    5. if (!(gunspleef1.contains(p))) {
    6. p.sendMessage(ChatColor.RED + "You are not in a game.");
    7. return true;
    8. } else if (gunspleef1.contains(p)) {
    9. gunspleef1.remove(p);
    10. plugin.getConfig().set(p.getName() + ".InGame", false);
    11. p.sendMessage(ChatColor.RED + "You have left your current game");
    12. players1--;
    13. plugin.getConfig().set("Gunspleef." + "1" + ".players", players1);
    14. plugin.saveConfig();
    15. plugin.getPlayerSpawn(p);
     
  2. Offline

    nuno1212sss

    1º Make player final
    2º Don't use else if just use else{
     
  3. Offline

    benzimmer123

    nuno1212sss
    Just done both these things still have the same problem. :(
    Thanks for the quick reply though:D
     
  4. Offline

    Niknea

    benzimmer123 Can I see how you created your array list?
     
  5. Offline

    Rocoty

    Shot in the dark. You instantiated that class twice right?
     
  6. Offline

    benzimmer123

    Rocoty Yes i did.
    Niknea You mean this?
    Code:java
    1. ArrayList<Player> gunspleef1 = new ArrayList<Player>();
     
  7. Offline

    Niknea

    benzimmer123 Send a message to the player before and after you add them to the array list, does both of them appear?
     
  8. Offline

    benzimmer123

    Niknea
    The message from when you join or leave?:p

    EDIT: Oh on join, yes it does;)

    There's something else that's really weird, i made another method of where they leave the arraylist (When they touch a liquid). It works fine even though i copied and pasted the code from the command. Now i'm confused!:p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  9. Offline

    Rocoty

    Because two different instances of the class means two different arraylists
     
  10. Offline

    benzimmer123

    Rocoty
    I'm confused, sorry:p
     
  11. Offline

    Niknea

    benzimmer123 See where you added them to the list? Send them a message right before and right after. Does it show up?
     
  12. Offline

    Rocoty

    Let me explain. Two cats with gray fur. Both have gray fur, but it's not the same fur for that reason.
     
  13. Offline

    benzimmer123


    Niknea
    Already answered it:p ^^

    Rocoty
    So your saying there's two different arraylists?:p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  14. Offline

    Rocoty

    Exactly. Now, this may have happened if you have called 'new <WhateverYourClassIsCalled>()' more than once in your code.
     
  15. Offline

    Fuzzybear04

    Never store Player Objects in arraylists, as it can cause Memory leaks
    Instead, only store the Player's name. To do this, remake your Arraylist As
    (public static (if you want) ArrayList<String> namehere = new ArrayList<String>();
     
Thread Status:
Not open for further replies.

Share This Page