Solved Removing an element from the List

Discussion in 'Spigot Plugin Development' started by cayote20, Mar 25, 2020.

Thread Status:
Not open for further replies.
  1. Suppose that we have a List with elements written in the following format: "Name, integer, integer". We have the name of a player that must be removed from the list. How can I realise it? I tried this way, but the plugin creates some strange things in the config and doesn't do what I want him to do:
    Code (Text):
                                    int i = 0;
                                    for(String s : fractionMembers) {
                                        String[] member = s.split(", ");
                                        if(member[0].equals(target.getName())) {
                                            break;
                                        }
                                        i += 1;
                                    }
                                    fractionMembers.remove(i);
    P.S. Sorry for my stupid question, I'm newbie.
     
  2. illegalargumentexception likely has no relevance to this code. however, this code would throw a concurrentmodificationexception, as youre not allowed to modify a list while youre iterating over it. you have to use an iterator for that.
     
  3. I a little bit rewrote the code and now I don't have any exceptions, but I have the new problem that I described in the comment of the code:
    Code (Text):
                                    List<String> fractionMembers = utils.getFractionMembers(utils.getPlayerFractionID(target));
                                    int i = 0;
                                    for(String s : fractionMembers) {
                                        String[] member = s.split(", ");
                                        if(member[0].equals(target.getName())) {
                                            break;
                                        }
                                        i += 1;
                                    }
                                    Bukkit.getConsoleSender().sendMessage("" + i + ", " + utils.getPlayerFractionID(target) + ", " + target.getName()); // utils.getPlayerFractionID(target) returns 2
                                    fractionMembers.remove(i); // After this utils.getPlayerFractionID(target) will return -1 instead of 2
                                    Bukkit.getConsoleSender().sendMessage("" + i + ", " + utils.getPlayerFractionID(target) + ", " + target.getName()); // utils.getPlayerFractionID(target) returns -1
                                    if(fractionMembers.isEmpty()) {
                                        plugin.getConfig().set("Fractions." + utils.getPlayerFractionID(target) + ".FractionMembers", new ArrayList<String>());
                                        plugin.saveConfig();
                                    } else {
                                        plugin.getConfig().set("Fractions." + utils.getPlayerFractionID(target) + ".FractionMembers", fractionMembers);
                                        plugin.saveConfig();
                                    }
     
  4. Strahan

    Benefactor

    Why are you using a List for something that is multiple fields of data? I'd make a data class for this, or at the very least if you insist on string splitting make a Map<UUID, String> to store the "integer,integer" by the player's UUID.
     
    • Agree Agree x 1
  5. I'd rather you use an appropriate data structure for this rather than what you're doing right now, a map of a UUID and a pair of integers would work.
     
  6. Ok, but why the return value changes? I can't understand...
     
  7. Strahan

    Benefactor

    Dunno why you are doing it that weird way, just use an Iterator:
    Code (Text):
    Iterator<String> it = fractionMembers.iterator();
    while (it.hasNext()) {
      String[] member = it.next().split(", ");
      if (!member[0].equalsIgnoreCase(target.getName())) continue;
     
      it.remove();
    }
    ...though you would be better off not doing this hacky crap in the first place.

    PS also I just noticed your split has a space after the comma. I assume that's because whatever code you wrote to create the data has a space there, but if so that's more hackiness. Comma delimited data should not have errant characters.
     
    • Agree Agree x 1
  8. Same problem :(
     
  9. I will remember it, thanks.
     
  10. Strahan

    Benefactor

    What do you mean, same problem? You last said the problem was the wrong numeric return; this has no return at all so how can it be the same problem? Can you elaborate? What is it doing (or not doing)?
     
  11. Wrong numeric return after removing the element from the list
     
  12. Strahan

    Benefactor

    An iterator's remove method is a void; there is no numeric return so how can it be wrong? Where are you getting a numeric return?
     
  13. That's why I don't know why it returns a wrong integer.
    I get the integer by the getPlayerFractionID(Player player) method:
    Code (Text):
        public int getPlayerFractionID(Player player) {
            List<Integer> fractionIDs = new ArrayList<Integer>();
            for(String s : plugin.getConfig().getConfigurationSection("Fractions").getKeys(false)) {
                fractionIDs.add(Integer.parseInt(s));
            }
            if(isPlayerFractionLeader(player)) {
                String name = player.getName();
                for(int i = 0; i < (fractionIDs.get(fractionIDs.size()-1)+1); i++) {
                    if(fractionIDs.contains(i)) {
                        if(plugin.getConfig().getString("Fractions." + i + ".FractionLeader").equals(name)) return i;
                    }
                }
            } else {
                List<String> members = new ArrayList<String>();
                String playerName = player.getName();
                for(int i = 0; i < (fractionIDs.get(fractionIDs.size()-1)+1); i++) {
                    if(fractionIDs.contains(i)) {
                        members = (List<String>) plugin.getConfig().getList("Fractions." + i + ".FractionMembers");
                        for(int j = 0; j < members.size(); j++) {
                            String[] s = members.get(j).split(", ");
                            String memberName = s[0];
                            if(playerName.equals(memberName)) return i;
                        }
                    }
                }
            }
            return -1;
        }
     
  14. I used your advice and started to store data in HashMaps, works fine. Thanks for your help, I appreciate it very much, have a nice day!
     
    #14 cayote20, Mar 26, 2020
    Last edited: Mar 26, 2020
  15. Just a quick hint:
     
  16. Thanks :D
     
Thread Status:
Not open for further replies.