Sort keys of Configuration section by string length

Discussion in 'Plugin Development' started by Minesuchtiiii, Jun 11, 2019.

  1. So I'm struggling to create a method which sorts the keys of a specific configuration section by their string length. It should be sorted from longest string to shortest string. I searched the net but didn't really find anything that helps me.

    Any help would be much appreciated!
     
  2. Offline

    timtower Ninja on the waves Moderator

    @Minesuchtiiii Put them all in a list.
    Go over the list and find the valuewith the longest name.
    Put it in a second list.
    Repeat till first list is empty.
     
  3. And after that I put them back in the file where they were stored? How do I keep the values to those configurationsection-keys?
     
  4. Offline

    timtower Ninja on the waves Moderator

    Why do you need them stored in the config based on length?
     
  5. Offline

    Kars

    You could keep them in a Map.
    Code:
    Map<String, String> values = new HashMap<>();
    values.put("config string", "value");
    You can add all the values to it and later, when you require the values, get them from the map by their key. Easy.
    Code:
    values.get("config string"); // will be "value"
     
  6. I'm filtering the chat based on strings in a file. Let's say there's "no" and "nope" in the file, the message will be replaced to "**pe" if "no" gets added first.. but what I want is that you get "****" if you write "nope" (and already have added "no")

    Let me explain a bit more, I have the method
    Code:
        public void addSwearWord(Player adder, String swearword) {
          
            if(!(swearscfg.contains("Swearwords." + swearword.toLowerCase()))) {
              
                swearscfg.set("Swearwords." + swearword.toLowerCase() + ".GetsReplaced", true);
                swearscfg.set("Swearwords." + swearword.toLowerCase() + ".BlockedCompletely", false);
                saveSwearsFile();
              
                adder.sendMessage(this.prefix + "§aAdded swearword §5" + swearword.toLowerCase() + "§a!");
              
            }
            else {
              
                adder.sendMessage(this.prefix + "§cYou already added §7" + swearword.toLowerCase() + "§c!");
              
            }
          
        }
    And now I'm trying to create a method which sorts the swearword keys in the configuration section based on their string length :/
     
  7. Offline

    timtower Ninja on the waves Moderator

  8. Offline

    Kars

    I get you but this sounds like a workaround not a solution.

    You are replacing the occurences one at a time. When you check for the next one the previous one has been replaced and therein lies your problem.
    You could keep track of the occurences and replace them all at once. That would be alot cleaner also.

    In this case, "nope" will be replaced with "**pe", but afterward the whole thing gets replaced with "****" anyway. Problem solved.

    Look at the link timtower provided if you want to go through with the sorting, though.
     
  9. I'll give it a try tomorrow and get back here then. Thanks :)
     
  10. Offline

    drives_a_ford

    You could also just use regular expressions to make sure it only triggers on full words. Of course, this would not work in all situations (i.e "ass" would be filtered whereas "asshole" wouldn't). However, there are situations where this would be desirable as well depending on what you're trying to filter (i.e replacing "ass" with "***" everywhere would mean "assassin" becomes "******in").

    And if you truly want to filter words of any kind, you'd need to replace them case as case insensitive

    All in all, chat/swear filtering has long been shown to be rather ineffective. It's way too simple to get around (i.e "ass" becomes "a.ss" and so on) and a good moderation team will always be the better way of handling things.
     

Share This Page