Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Simulator Enhancements (03/13/24)

Author Topic: [Resolved] IntelManager.getIntel(Class c) returns non-iterable lists  (Read 652 times)

Jaghaimo

  • Admiral
  • *****
  • Posts: 661
    • View Profile

Whenever I try to iterate over that list, I get:
Code
19433 [Thread-4] ERROR com.fs.starfarer.combat.CombatMain  - java.util.ConcurrentModificationExceptionjava.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at stellics.StellicsModPlugin.removeStaleIntel(StellicsModPlugin.java:55)
...
Lists should be iterable as they implement Iterable interface. Code to reproduce (called from onGameLoad):
Code
    private void removeDeprecatedIntel(Class<?> c) {
        IntelManagerAPI manager = Global.getSector().getIntelManager();

        for (IntelInfoPlugin i : manager.getIntel(c)) {// line 55
            manager.removeIntel(i);
        }
    }
If I use "low-tech" approach all is fine and well:
Code
    private void removeDeprecatedIntel(Class<?> c) {
        IntelManagerAPI manager = Global.getSector().getIntelManager();
        List<IntelInfoPlugin> intels = manager.getIntel(c);

        for (int i = 0; i < intels.size(); i++) {
            IntelInfoPlugin intel = intels.get(i);
            manager.removeIntel(intel);
        }
    }
« Last Edit: August 14, 2020, 08:16:16 AM by Thaago »
Logged

Jaghaimo

  • Admiral
  • *****
  • Posts: 661
    • View Profile
Re: IntelManager.getIntel(Class c) returns non-iterable lists
« Reply #1 on: August 14, 2020, 03:00:13 AM »

User error. Was modifying list that I was iterating over. Should make a copy first...
Code
    private void removeDeprecatedIntel(Class<?> c) {
        IntelManagerAPI manager = Global.getSector().getIntelManager();
        List<IntelInfoPlugin> intels = new ArrayList<IntelInfoPlugin>(manager.getIntel(c));

        for (IntelInfoPlugin i : intels) {
            manager.removeIntel(i);
        }
    }

Leaving OP for Alex's morning chuckle. And sorry for wrong section, just noticed there's modded one too.
« Last Edit: August 14, 2020, 03:03:47 AM by Jaghaimo »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 23987
    • View Profile
Re: [Resolved] IntelManager.getIntel(Class c) returns non-iterable lists
« Reply #2 on: August 14, 2020, 09:09:59 AM »

No worries, over the years I've done that more times than I can count :)
Logged