Solved Death Drop modification - ConcurrentModificationException Error

Discussion in 'Plugin Development' started by EgyptianKing, Aug 29, 2014.

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

    EgyptianKing

    So I have this code to go through the players inventory on death and check their items. If the player has soup, nothing happens, but if the player has anything else, then it will remove that item from the death drop. But I'm getting the ConcurrentModificationException error.


    Here's the code:

    Code:java
    1. for(ItemStack i : e.getDrops())
    2. {
    3. ItemStack soup = new ItemStack(Material.MUSHROOM_SOUP);
    4.  
    5. if(!i.equals(soup))
    6. {
    7. e.getDrops().remove(i);
    8. }
    9.  
    10. else return;
    11. }

    Any ideas?
     
  2. Offline

    fireblast709

    EgyptianKing Either:
    • Don't modify the List you are looping over (i.e calling e.getDrops().remove(i) when looping over e.getDrops())
    • Use an Iterator
     
    hintss likes this.
  3. Offline

    BillyGalbreath

    Without Iterator:
    Code:java
    1.  
    2. List<ItemStack> toRemove = new ArrayList<ItemStack>();
    3. for (ItemStack item : e.getDrops()) {
    4. if (!item.getType().equals(Material.MUSHROOM_SOUP) {
    5. toRemove.add(item);
    6. }
    7. }
    8. for (ItemStack remove : toRemove) {
    9. e.getDrops().remove(remove);
    10. }
    11.  


    With Iterator:
    Code:java
    1.  
    2. Iterator<ItemStack> drops = e.getDrops().iterator();
    3. while(drops.hasNext()) {
    4. ItemStack current = drops.next();
    5. if(!current.getType().equals(Material.MUSHROOM_SOUP)) {
    6. drops.remove();
    7. }
    8. }
    9.  
     
    EgyptianKing likes this.
  4. Offline

    Rocoty

    It should be obvious which one of those two to use.

    Anyway, here's my contribution...without iterator. Only works on indexed collections:
    Code:java
    1. List<ItemStack> drops = e.getDrops();
    2. for (int i = 0; i < drops.size(); i++) {
    3. if (drops.get(i).getType() == Material.MUSHROOM_SOUP) {
    4. drops.remove(i--);
    5. }
    6. }
     
  5. Offline

    sparta417


    That's going to throw an ArrayOutOfBoundsException. Don't post useless solutions.

    Anywhosal, OP, use an Iterator. Not hard to use. Any example above may help.
     
  6. Offline

    fireblast709

    sparta417 Where exactly will it do that. Please elaborate.
     
  7. Offline

    sparta417


    If the first ItemStack is MushroomSoup, it will try to access an element in the List with the index -1.

    Not hard to comprehend.

    I'll break it down for you and the OP of the supplied method:

    List contains X amount of ItemStack objects/instances.

    Loop through List.

    First iteration:

    ItemStack object/instance at index I (in this case, 0; 1st in the List) has Material.MUSHROOM_SOUP, remove object/instance at index I subtract 1. Result index is -1. Hence, ArrayOutOfBoundsException.
     
  8. Offline

    fireblast709

    sparta417 Will it now. Please study the usage of postfix decrement.
     
  9. Offline

    sparta417


    i--;

    is

    i = i - 1;

    Please learn Java properly before posting on your profile whining about new users on this forum. :oops:

    It saddens me that you still don't understand.
     
  10. sparta417 Fair warning: You probably should've quit while you were ahead. fireblast709 does know Java and I suspect he's not called fireblast for nothing. Ima take cover.
     
  11. Offline

    sparta417


    I'm not afraid to prove someone wrong when they just ask rhetorical questions whilst ironically attacking newbies at Java in this forum.
     
  12. Offline

    fireblast709

    sparta417 you are as wrong as you can be :p. You seem to mix up prefix and postfix. In the case of prefix decrementing you would be completely right.

    prefix decrementing: First decrement, then use i
    Code:
    int i = 0;
    System.out.println(--i);
    Will print -1, and i will be -1 afterwards.

    postfix decrementing: First do something with i, then decrement
    Code:
    int i = 0;
    System.out.println(i--)
    Will print 0, and i will be -1 afterwards.

    Let's commence with a walkthrough with a List of size 1:
    • Beforehand, i = 0. Not so surprising since we just set it to 0.
    • Next up, we check the guard like any standard for loop would do. The size is 1, and 0 < 1, thus we get in the loop
    • At the moment that we call get(i--), it will use i = 0 like explained above. Hence, get(0) would return the first element, and i will be -1 after the removal has returned
    • Loop has ended, we increment i by one as stated by the step. i is now 0
    • Once more, we check the guard. It seems that the size is 0 due to our last removal, and i is 0. Since 0 < 0 is false, we break out of the for loop and continue with the rest of the code.
    [Addition] Adding a snippet with output
    Code:
    List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("b");
    list.add("a");
    list.add("a");
    for(int i = 0; i < list.size(); i++)
    {
        System.out.println(i+": "+list.get(i));
        if(list.get(i).equalsIgnoreCase("b"))
            list.remove(i--);
    }
    System.out.println(list.size());
    With the output:
    Code:
    0: a
    1: b
    1: b
    1: a
    2: a
    3
     
    Assist, _Filip, Rocoty and 2 others like this.
  13. Offline

    Rocoty

    sparta417 Please...There is a very significant difference between i-- and --i. Look it up. fireblast709 wasn't wrong. But I'm afraid you are.
     
    BillyGalbreath and AdamQpzm like this.
  14. Offline

    BrentHogeling

  15. Offline

    BillyGalbreath

    Wth, guys. I step away for 2 hours and you go to war with each other :S
     
  16. BillyGalbreath Ah the real problem is when people have a slight misconception about how something works, yet strongly believe they're in the right.
     
  17. Offline

    sparta417


    One minimal flaw of learning a programming language years back doesn't mean I get wrecked. Grow up, child.
     
  18. Offline

    BillyGalbreath

    Why must you throw these kinds of comments into your posts? :confused: What are you trying to prove, and to who?
     
    Rocoty and AdamQpzm like this.
  19. Offline

    sparta417

    • Stop the name-calling and go play nice with the other forum members.

    Lol, silly hick. Stop asking rhetorical questions and go feed the cows.
     
  20. Offline

    BrentHogeling

    I'm the child, yet you just used an insult based on his background, I think sir, you are the one that in fact needs to grow up!
     
Thread Status:
Not open for further replies.

Share This Page