Solved List in config

Discussion in 'Plugin Development' started by KarimAKL, Apr 1, 2018.

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

    KarimAKL

    What i want to create: A list in config where you can type player names like this:
    Names:
    - Randomguy1
    - Randomguy2
    - Randomguy3
    - etc

    I don't know how to do that in the first place and i don't want to do this:
    Code:
    if (array[1].equalsIgnoreCase("Randomguy1")) {
    
    For every person i want to add, so what would i do in that case?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    KarimAKL

    @timtower I don't know how i should use it. :/
     
  4. Offline

    The_Spaceman

    ArrayList<String> array = (ArrayList<String>) config.getConfig().getStringList("Names");

    for (String person : array) {
    if (person.equalsIgnoreCase("RandomGuy1")) {
    //do something
    }
    }
     
  5. Offline

    KarimAKL

    @The_Spaceman Wouldn't that mean i should type the name like you did there everytime? "if (person.equalsIgnoreCase("RandomGuy1")) {"
     
  6. Offline

    The_Spaceman

    I dont really know what you want to do but, what timtower said is that you can test is a String is available in your list using: array.contains(yourStringHere);

    can you please tell me more about the functionality?
     
  7. Offline

    KarimAKL

    @The_Spaceman I have a class where i have a PlayerCommandPreprocessEvent and then i want a config where i can just add people like this:
    Names:
    - RandomGuy1
    - RandomGuy2
    - etc
    Instead of doing “if (array[1].equalsIgnoreCase(“RandomGuy1”)) {“ everytime
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    KarimAKL

    @timtower I should probably be more specific, what i want is the player in arg 1 of the command like /ban (player), that would just be "array.contains(args[0])", right? Well if that's right then how do i make the list in config? I'm a little confused, could you explain how it works(like the steps of what i need to do)?
    EDIT: I just looked it up and i think the name for what i want is StringList or something like that.
     
    Last edited by a moderator: Apr 3, 2018
  10. Offline

    KarimAKL

    bump, i still need this. :/
     
  11. @KarimAKL
    To make a list in a config you do
    ArrayList <String> playerList = new ArrayList <String>();
    getConfig ().set ("Names", playerList);
    saveConfig ();
    Then to get the list and check if a name is in the list

    ArrayList <String> list = getConfig ().getStringList ("Names");

    If (list.contains (whatever){
    //do stuff
    }
     
  12. Offline

    KarimAKL

    @Blackwing_Forged Okay but i'm confused why do i need the "if (list.contains(whatever)) {"? Wouldn't i just be able to use something like "if (list.contains(Names)) {" instead?
     
  13. @KarimAKL
    What exactly are you trying to do? are you trying to see if a name is in that list, if so thats why you do if list.contains("your string")
    if you want to loop through the contents do

    for(int i = 0; i < list.size(); i++)
    {
    String item = list.get(i);

    }
     
  14. Offline

    KarimAKL

  15. Offline

    KarimAKL

    @Blackwing_Forged Okay i just tried the list.contains thing but it didn't work, either i did it wrong or i just can't use it in my case so i tried this instead:
    Code:
    if (!(p.getName().equalsIgnoreCase(plugin.getConfig().getStringList("My-String-Here")))) {
    
    but it comes with an error at the "equalsIgnoreCase" part, here is the error:
    "The method equalsIgnoreCase(String) in the type String is not applicable for the arguments (List<String>)"
    So i wanna ask how i would be able to make this:
    Code:
    if (!(p.getName().equalsIgnoreCase("in-game-name") || p.getName().equalsIgnoreCase("another-in-game-name"))) {
    
    and this:
    Code:
    if (array[1].equalsIgnoreCase("in-game-name") || array[1].equalsIgnoreCase("another-in-game-name")) {
    
    so that i can just add them to a list in config and it would count here? As i said i already tried something like this:
    Code:
    if (!(p.getName().equalsIgnoreCase(plugin.getConfig().getStringList("My-String-Here")))) {
    
    at the first one.
     
  16. Offline

    timtower Administrator Administrator Moderator

    @KarimAKL listinconfig.contains(p.getName())
     
  17. Offline

    KarimAKL

    @timtower Like this?:
    Code:
    if (!(plugin.getConfig().getStringList("whateverstring".contains(p.getName())))) {
    
    It comes with an error at the "getStringList" part so i think that's wrong but if not i don't quite get it. :/
    Here is the error: "The method getStringList(String) in the type MemorySection is not applicable for the arguments (boolean)"
     
  18. Offline

    timtower Administrator Administrator Moderator

  19. Offline

    KarimAKL

    @timtower Oh, it was right then? I just accidentally put the .contains on the other side of the string instead of on the other side of the bracket then? It doesn't come with an error now so i guess it's right then? Thanks alot. :) Question: Does it matter if i use caps in the config or not? If it does matter then how would i make it so that it doesn't matter?
    EDIT: I forgot to ask: What should i type in config if i don't want to have anyone under "names" by default? Also i don't know if this has anything to do with this but when i type a command from the PlayerCommandPreprocessEvent it comes with an error.
    Here is the error:
    Code:
    [15:36:39 INFO]: PlayerName issued server command: /command
    [15:36:39 ERROR]: Could not pass event PlayerCommandPreprocessEvent to PluginName VersionNumber
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1154) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at me.karim.plugin.events.EventOnCommand.useCommand(EventOnCommand.java:71) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_161]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 16 more
    
    If you need my class you can just ask.
     
    Last edited by a moderator: Apr 16, 2018
  20. Offline

    KarimAKL

Thread Status:
Not open for further replies.

Share This Page