1.8.8 List randomly becoming empty

Discussion in 'Spigot Plugin Development' started by DarkKnights22, Feb 10, 2020.

  1. Code (Java):
    @Getter
    @RequiredArgsConstructor
    public class War {

        private final List<UUID> teamOne;
        private final List<UUID> teamTwo;
        private final WarType warType;
        private final List<UUID> teamOneAlive;
        private final List<UUID> teamTwoAlive;
        @Nullable
        private final Island winnerIsland;
    }
    Code (Java):
    private void tryPrepareForWar(Set<Queue> queueSet) {
            for (Queue inQueue : queueSet) {
                if (inQueue.getUuid().equals(getPlayer().getUniqueId())) continue;
                getTeamMates().add(getPlayer().getUniqueId());
                List<UUID> teamTwo = inQueue.getTeamMates();
                teamTwo.add(inQueue.getUuid());
                ShardWars.getPlayerQueueManagerMap().get(inQueue.getUuid()).removeFromQueue();
                removeFromQueue();
                new WarManager(new War(getTeamMates(), teamTwo, getWarType(), getTeamMates(), teamTwo, null)).startWar();
                break;
            }
        }
    Code (Java):
    WarManager warManager = ShardWars.getPlayerWarManagerMap().get(player.getUniqueId());
            War war = warManager.getWar();
            if (war.getTeamOneAlive().isEmpty()) {
                warManager.endWar(Core.getInstance().skyblockManager.getIsland(war.getTeamTwo().get(0)),
                        Core.getInstance().skyblockManager.getIsland(war.getTeamOne().get(0)));
                return;
            }
            if (war.getTeamTwoAlive().isEmpty()) {
                warManager.endWar(Core.getInstance().skyblockManager.getIsland(war.getTeamOne().get(0)),
                        Core.getInstance().skyblockManager.getIsland(war.getTeamTwo().get(0)));
                return;
            }
    I'm using the Spigot API and I'm creating a player verse player type plugin. I'm using lombok for getters and constructors. War#getTeamOne() and War#getTeamTwo() randomly become empty. I print the lists throughout the code and it contains a value, but then just before I do WarManager#endWar(), the list is empty. Nowhere in my project do I remove from either of those lists. Any idea why that is? If you need to see more code, lmk.
     
  2. Be aware, I didn't look to in deep with your code. Just some notes that may make debugging easier so you can find your issue.
    • Make a team class
      • Have a collection of living players
      • Have a collection of dead players
      • Make methods inside this class to make your life easier, and the code more readable.
    • Add debugging print statements.
      • Like, whenever a player dies say System.out.println("HEY, THIS DUDE IS DEAD")
    • If you still can't find your issue, your should probably send more code.
      • With your debugging statements, you should be able to find where the issue is.
     
  3. Yeh I've done that, and there's not much I can debug.
     
  4. Do you ever replace the list variables with new lists? Are you referencing an incorrect version of your objects, one where the lists have not been initialized?
     
  5. Are you removing any items from the queue while in the loop?
     
  6. Code (Java):
    public void removeFromQueue() {
            switch (getWarType()) {
                case ONEVONE:
                    ShardWars.getOneVerseOneQueues().remove(ShardWars.getPlayerQueueMap().get(getPlayer().getUniqueId()));
                case TWOVTWO:
                    ShardWars.getTwoVerseTwoQueues().remove(ShardWars.getPlayerQueueMap().get(getPlayer().getUniqueId()));
                case FOURVFOUR:
                    ShardWars.getFourVerseFourQueues().remove(ShardWars.getPlayerQueueMap().get(getPlayer().getUniqueId()));
            }
        }
     
  7. Yea, but is this the queueSet you passed as argument in tryPrepareForWar?
     
  8. Uh yes?
     
  9. Dunno if that is the reason, but use an Iterator and call Iterator#remove(). You are modifying the set as you are looping, which can cause some weird behavior...
    Again, not sure if that works
     
  10. Could you explain what you mean? Like this?

    Code (Java):
    for (Iterator<Queue> iterator = queueSet.iterator(); iterator.hasNext(); ) {
                Queue inQueue = iterator.next();
     
    • Creative Creative x 1
  11. Yes. And then use iterator#remove() to remove an element from the queue
     
  12. How would I do that?
     
  13. using iterator.remove(). If you need to, I assume you can pass it as an argument to your method
     
  14. Code (Java):
    ShardWars.getPlayerQueueManagerMap().get(inQueue.getUuid()).removeFromQueue();
                iterator.remove();
    ?
     
  15. In the function removeFrom Queue
     
  16. How would I do that? Show me the actual code s oI understand.
     
  17. This is confusing since you have two calls to remove something from the queue...
    Just replace the applicable (one of these)
    Code (Java):
    ShardWars.getPlayerQueueManagerMap().get(inQueue.getUuid()).removeFromQueue();
    removeFromQueue();
    with
    Code (Java):
    iterator.remove();
     
  18. I get a ConcurrentModificationException
     
  19. Post code. That's why you would use Iterators in the first place...
     
  20. Code (Text):
    private void tryPrepareForWar(Set<Queue> queueSet) {
            for (Iterator<Queue> iterator = queueSet.iterator(); iterator.hasNext(); ) {
                Queue inQueue = iterator.next();
                if (inQueue.getUuid().equals(getPlayer().getUniqueId())) continue;
                List<UUID> teamOne = getTeamMates();
                teamOne.add(getPlayer().getUniqueId());
                List<UUID> teamTwo = inQueue.getTeamMates();
                teamTwo.add(inQueue.getUuid());
                WarType warType = getWarType();
                ShardWars.getPlayerQueueManagerMap().get(inQueue.getUuid()).removeFromQueue();
                iterator.remove();
                new WarManager(new War(teamOne, teamTwo, warType, teamOne, teamTwo, null)).startWar();
                break;
            }
        }