How Do I Display a List?

Discussion in 'Plugin Development' started by Prasun, Dec 4, 2017.

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

    Prasun

    Hey guys, today I was making a /warps plugin. When the command, "/setwarp <name>" is run, it will get the world location and coordinates and saves it into the config like this:
    Code:
    else if (cmd.getName().equalsIgnoreCase("setwarp") && sender instanceof Player) {
              
                Player player = (Player) sender;
              
                if (player.hasPermission("BasicCommands.movement.warp.create")) {
                  
                    if (args.length == 0) {
                      
                        player.sendMessage(ChatColor.GRAY + "" + ChatColor.BOLD + "BasicCommands" + ChatColor.DARK_GRAY + ChatColor.BOLD + " ➤ " + ChatColor.RED + "Sorry, but you must " + ChatColor.GOLD + "specify" + ChatColor.RED + " a name.");
                      
                        return true;
                    }
                  
                    settings.getData().set("warps." + args[0] + ".world", player.getLocation().getWorld().getName());
                    settings.getData().set("warps." + args[0] + ".x", player.getLocation().getX());
                    settings.getData().set("warps." + args[0] + ".y", player.getLocation().getY());
                    settings.getData().set("warps." + args[0] + ".z", player.getLocation().getZ());
                    settings.saveData();
                  
                    player.sendMessage(ChatColor.GRAY + "" + ChatColor.BOLD + "BasicCommands" + ChatColor.DARK_GRAY + ChatColor.BOLD + " ➤ " + ChatColor.BLUE + "The warp " + ChatColor.GREEN + args[0] + ChatColor.BLUE + " was set!");
                  
                } else {
                  
                    player.sendMessage(ChatColor.GRAY + "" + ChatColor.BOLD + "BasicCommands" + ChatColor.DARK_GRAY + ChatColor.BOLD + " ➤ " + ChatColor.RED + "Sorry, but you don't have " + ChatColor.GOLD + "permission" + ChatColor.RED + " to do that.");
                  
                }
              
                return true;
    Now, I just need to do the /warps or /warplist command. Here's how it is stored in the config.yml:
    Code:
    warps:
      prasun:
        world: main-hub
        x: -990.9862503139791
        y: 22.0
        z: 672.0372004314182
      Prasun:
        world: main-hub
        x: -990.9948765892714
        y: 22.0
        z: 675.6262837520939
      LOL:
        world: main-hub
        x: -1075.4932326273163
        y: 16.0
        z: 674.9608846676906
    The problem is, I don't know how to display this as a list like..

    --- WARPS ---
    warp1
    warp2
    warp3

    Does anyone know of a solution? Anything would be great. Thanks.
     
  2. Offline

    RunsWithShovels

    @Prasun I suggest that you do not allow making warps that are spelt the same. Even with names that capitalization has changed can be confusing.

    As for the list. Just get the config section of warps from the yml. It's a set string when you get it. Then just determine how you want to display it. Best thing to do is to get the basic set string and print it out , then split / replace / or whatever to the set to make it look like you want.
     
  3. Offline

    Prasun

    @RunsWithShovels If it's not asking for a lot, can you show me an example code? This is what I have right now:
    Code:
    List<String> warps = settings.getData().getStringList("warps");
                  
                    player.sendMessage(ChatColor.GRAY + "" + ChatColor.BOLD + "BasicCommands" + ChatColor.DARK_GRAY + ChatColor.BOLD + " ➤ " + ChatColor.GRAY + ChatColor.BOLD + "Warps");
                    player.sendMessage(ChatColor.GOLD + warps.toString().replace("[", "").replace("]", "").replace(", ", " - "));
    Not exactly sure if this is a correct way to do this.
     
  4. Offline

    RunsWithShovels

    pseudo code

    set<string> mylist = file.getconfigsection("warps").getkeys(false)

    sendmessage(mylist);
     
  5. Offline

    Prasun

    @RunsWithShovels I think I have an idea. Is there a way I can save objects into a list? From there, I can grab it and send the player a message with the contents.

    Plus, your method doesn't seem to work.
     
  6. Offline

    RunsWithShovels

    That is pseudo code, it's not made to work, just to give an idea of how to code it... If you use the correct syntax it works, believe me, I use it all the time, more specifically in the WarpControl plugin that I made recently.

    As for using a list, yeah you can save basically what you want in a list, but you still need to get the data from the file to put it there.

    Can you post the code you tried with my method?
     
  7. Offline

    Prasun

    @RunsWithShovels Here it is:
    Code:
    Set<String> warps = settings.getData().getStringList("warps");
                  
                    player.sendMessage(ChatColor.GRAY + "" + ChatColor.BOLD + "BasicCommands" + ChatColor.DARK_GRAY + ChatColor.BOLD + " ➤ " + ChatColor.GRAY + ChatColor.BOLD + "Warps");
                    player.sendMessage(warps);
     
  8. Offline

    RunsWithShovels


    You're still trying to get a list, you have to get the configurationsection not a stringlist. And you have to use getkeys as I did in the pseudo code.

    What exactly was your issue in the first place? Was it not sending anything, or was it just not in the format you wanting?
     
  9. Alternatively, you can create a list of warps at onEnable() that includes every warp included in the config. Anytime a warp is added, also add it to this list.

    A set is not a list. Sets are basically a collection of objects where you don't want two of the same objects, and you don't care about the order of them. You will have to just check if the warp is set before setting a new one; turning a list into a set would be pointless in this case.

    To send every message in a collection of strings (both Set and List are a type of collection), you'll need to get every string in the collection. If you want this to be built into one continuous message, you can do the following (please make sure to check that you don't make the message sent too long, or it will cause the player to crash/be kicked):

    Code:
    Set<String> warps = getConfig().getConfigurationSection("warps").keySet();
    
    int count = 0;
    StringBuilder message = new StringBuilder();
    
    for (String warp : warps) {
    if (count > 20) break; //stop adding more warps to the displayed list
    count++;
    message.append(warp); //efficiently add this warp to the message; more efficient than message += warp
    message.append(", "); //add a comma and space after the warp
    }
    if (message.length() > 0) {
    message.replace(message.length() - 1, message.length() + 1, ""); //remove the final ", " in message, so no comma is after the last warp
    }
    
    Then sending message.toString() to the player will send the displayed warps.
     
  10. Offline

    Edvio

    You can do a for loop for all the keys in the warps section of the config.
     
  11. Offline

    Prasun

    What I've done so far is create a string list so whenever a warp is created, the coords are saved in their respective subclasses, but also in the said list. Now, I just grab it from the list, but it still bothers me that it doesn't work the way I want it to.

    @RunsWithShovels I want it so that it's like this:

    --- WARPS ---
    1. warp1
    2. warp2
    3. warp3

    If there are no warps, then I would send them a message saying that there are no warps set in that world.
     
  12. Please read what I posted above. Doing list.toString() or "" + list will send whatever the list has for toString(), which isn't what you want in most cases, but I guess could work if you modify it. You will have to loop through each string in the list and add it to a string that will be the message sent, and then send that string as the message once done. I suppose you might not like this idea, but it is really helpful to look into the usage of Lists, collections in general, or read the javadoc of it.
     
    RunsWithShovels likes this.
  13. Offline

    AdamDev

    @Prasun

    If you really want to display the warps as what you had asked it's pretty easy and I'm guessing you don't want to display it 50 warps on the screen.

    If you want it normally, I suggest testing this:
    - When listing the warps use it inside of a for loop:
    Code:
    for (String warp : yourConfig().getStringList("path.to.warps").getKeys(false))
    
    - Then you would have something before the for loop starts displaying what they're looking at (such as -=-Warps-=-)
    - After inside of the for loop you would send the player the warp name and other info if necessary

    If you want it as pages, do this:

    - In-game you probably would do "/warps [1]" ([1] page number), right?

    - In your code, add a page variable to your argument 0
    - After that put this in right after:
    Code:
    if (page < 0) page = 1; // Just so they cant put less than 0 (Again which ever page you want to start at you can change the 1)
    
    - Now were going to set a limit of warps per page:
    - For say you want 10 warps per page
    - For say the page is 3
    Code:
    int start = page * 10;
    int end = start + 10;
    
    // This will display 10 warps between 30-40
    
    - As I said in the suggestion before, display what it is, for loop the warps, send it, blah blah blah

    After all of this you should have either display of 100 warps or 10,25,5,20,etc warps per page.

    If this doesn't help at all then I don't know what could.

    ~ AdamDev
     
Thread Status:
Not open for further replies.

Share This Page