ArrayList Problem

Discussion in 'Plugin Development' started by HeadGam3z, Jun 24, 2014.

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

    HeadGam3z

    Earlier I created a getter for my ArrayList and yada yada yada. Which looks like this:
    Code:java
    1. public ArrayList<String> getFillBottle() {
    2. ArrayList<String> playerFillBottleToggle = new ArrayList<String>();
    3. return playerFillBottleToggle;
    4. }

    After that, I used my getter in my command, which looks like this:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender cs, Command c, String s, String[] a) {
    3. if (c.getName().equalsIgnoreCase("fillbottle")) {
    4. if (cs instanceof Player) {
    5. final Player p = (Player) cs;
    6. if (!u.getFillBottle().contains(p.getUniqueId().toString())) {
    7. p.sendMessage("does not have player's uuid");
    8. u.getFillBottle().add(p.getUniqueId().toString());
    9. p.sendMessage("FillBottle enabled.");
    10. return true;
    11. }
    12. p.sendMessage("does have player's uuid");
    13. u.getFillBottle().remove(p.getUniqueId().toString());
    14. p.sendMessage("FillBottle disabled.");
    15. return true;
    16. }
    17. }
    18. return false;
    19. }


    My problem is that the player's UUID never gets removed - ever. What I mean is, when a player uses /fillbottle for the first time, it stores (I guess, not sure if my getter is working) their UUID into an ArrayList and sends them a message <- that part works fine. When they use it again, it should remove them from the ArrayList, but it fails to do so. Instead, it just puts their UUID back into the ArrayList again, and sends them the message.

    Is my ArrayList failing to add them, is my getter not working, is my brain bad? I'm not sure, so I came to you guys. I'm still trying things out to see if I can get this to work, but if one of you know what's wrong, please feel free to tell me. :D
     
  2. Offline

    MCForger

    HeadGam3z
    Each time you call your getter...your creating a new array list that is empty and returning the new list. Make sure your returning the right list in your getter.
     
  3. Offline

    HeadGam3z

    MCForger
    Oh, yeah, my bad... Guess I need some more coffee. :p
    Thanks for your help!
     
  4. Offline

    MCForger

  5. Offline

    HeadGam3z

    MCForger
    Would you happen to know why it wouldn't be able to read the arraylist from a different class...? I have an event that checks if a player's uuid is in that arraylist, and if so, do something. But the player's uuid is never in the list, apparently... When I toggle the /fillbottle command, it works perfectly. But my event for some reason cannot seem to understand that a player's uuid is in the list.

    Reconfiguration my getter - which is in my Util class:
    Code:java
    1. ArrayList<String> playerFillBottleToggle = new ArrayList<String>();
    2.  
    3. public ArrayList<String> getFillBottle() {
    4. return playerFillBottleToggle;
    5. }

    After I toggle my /fillbottle to on and try to use my event, it says "not in list".
    Event:
    Code:java
    1. Util u = new Util();
    2.  
    3. @EventHandler
    4. public void playerRightClick(PlayerInteractEvent e) {
    5. final Player p = e.getPlayer();
    6. ItemStack emptyBottle = new ItemStack(Material.GLASS_BOTTLE);
    7. if (e.getAction() == Action.RIGHT_CLICK_BLOCK
    8. || e.getAction() == Action.RIGHT_CLICK_AIR) {
    9. if (p.getItemInHand() != null) {
    10. if (p.getItemInHand().equals(emptyBottle)) {
    11. if (u.getFillBottle().contains(p.getUniqueId().toString())) {
    12. p.getInventory().getItemInHand()
    13. .setType(Material.POTION);
    14. p.sendMessage("Bottle filled!");
    15. return;
    16. } else {
    17. p.sendMessage("not in list"); // message I get everytime.
    18. }
    19. } else {
    20. p.sendMessage("bottle no bottle");
    21. }
    22. } else {
    23. p.sendMessage("null");
    24. }
    25. } else {
    26. p.sendMessage("uht oh");
    27. }
    28. }


    Please don't feed me code btw, hints are better than code anyways.
     
  6. Offline

    MCForger

    HeadGam3z
    Are you creating a new util class twice instead of just passing the same object through a constuctor or making it static?
     
  7. Offline

    HeadGam3z

    MCForger
    Creating util twice.. I would pass the same object through a constructor, but I do not know how I would do that. Guess I'll do some research, but until then, I'll make it static for now as it does work, but I really don't like to use static.

    Thanks again!
     
Thread Status:
Not open for further replies.

Share This Page