Custom config files

Discussion in 'Plugin Development' started by girardcome, Jun 12, 2019.

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

    girardcome

    Hello everyone,
    I have a problem with a method I did, the method is:

    What I wanted :
    Add strings in a string list without duplicates, for example our string list contains: "Hi, Hello, Good evening, See you tomorrow" and it should not have X times the same string

    What I did:

    PHP:
    String pathKits "Players." p.getName() + ".permissions.kits";
    List<
    StringkitsPerms plugin.getPlayersConfig().getStringList(pathKits);

    private 
    void countRandom(Integer next, List<StringkitsPerms) {
            
    HashSet<StringstringSet = new HashSet<String>();
            
    stringSet.clear();
            
    Random random = new Random();
            
    ArrayList<StringkitsA = new ArrayList<String>();
            for (
    Kit kit Kit.values()) {
                if (!
    kitsA.contains(kit.name())) {
                    
    kitsA.add(kit.name());
                }
            }

            for (
    int i 0nexti++) {
                if(!
    stringSet.contains(kitsA.get(random.nextInt(kitsA.size()))))
                
    stringSet.add(kitsA.get(random.nextInt(kitsA.size())));

            }
            for (
    String list : stringSet) {
                if(!
    kitsPerms.contains(list)) kitsPerms.add(list);
            }
           
            for(
    Player pBukkit.getOnlinePlayers())  {
                
    p.sendMessage("girardcome has " kitsPerms.size() + " kits\nKits are: " kitsPerms);
            }
        }

    What I got:

     
  2. Offline

    KarimAKL

    @girardcome If you don't want duplicates you can just use a Set instead of a List.
     
  3. Offline

    bwfcwalshy Retired Staff

    Yeah I'd store these in a set, you'd never need an order and you don't want dupes plus quicker iteration.

    The code you have right now seems very... inefficient and redundant. You're making a new set every time, cleaning it (just made...), then you also make a new random EACH TIME, then you have FOUR for loops... not sure why you ever use this first for loop. The second one, you make 2 random ints so that check will only ever be true in edge-cases. And again it seems very useless. You also don't need contains, just add. Less efficient doing a contains

    Once again third for loop seems stupidly redundant, you just keep adding to collections and then adding to another with that collection you just used...

    Last for loop, yay something that has a usage.

    (Also posting to Spigot and Bukkit is gonna cause confusion because we don't know what each other suggested and it can cause even more issues for yourself.)
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page