Saving/Loading a String from a Commands Args

Discussion in 'Plugin Development' started by CaLxCyMru, Sep 3, 2013.

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

    CaLxCyMru

    Hello there, I would like to know how I would make it so that the plugin gets the args(arg) then saves it to a File, And It can be loaded up IF the arg is already in the list (.yml/.txt)

    I am using NametagAPI, Here is my code -
    Code:java
    1. if(commandLabel.equalsIgnoreCase("tag") || commandLabel.equalsIgnoreCase("clantag") || commandLabel.equalsIgnoreCase("nametag")){
    2. if(player.hasPermission(new Permissions().tag))
    3. if(args.length == 0){
    4. player.sendMessage(prefix + ChatColor.RED + "Syntax Error!\nUsage: /tag < Tag > " +ChatColor.GREEN+"\n* There is a 9 Char limit. * ");
    5. } if(args.length == 1 && args[0].equalsIgnoreCase("off")){
    6. String playerName = player.getName();
    7. player.sendMessage(prefix + ChatColor.GREEN + "Your clan tag has now been turned off!");
    8. NametagAPI.resetNametag(playerName);
    9.  
    10. }
    11. }
    12. if(args.length == 1 && args[0].length() <= 9 && args[0].equalsIgnoreCase("off") == false){
    13. String playerName = player.getName();
    14. player.sendMessage(prefix + ChatColor.GREEN + "You have now got the Clan tag -"+ ChatColor.RED +" " + args[0]);
    15. NametagAPI.setPrefix(playerName, ChatColor.AQUA + "[" +args[0]+ "] " + ChatColor.RESET);
    16.  
    17.  
    18. }else if(args.length >2) {
    19. player.sendMessage(prefix + ChatColor.RED + "Syntax Error!\nUsage: /tag < Tag > " +ChatColor.GREEN+"\n* There is a 9 Char limit. * ");
    20.  
    21. }else if(args[0].length() > 9){
    22. player.sendMessage(prefix+ ChatColor.RED + "Tag length is to BIG!" + ChatColor.GREEN + "\n* There is a 8 Char limit. *");
    23. }

    I want to make it so the plugin get's the .yml/.txt for the player, Then set prefix for the player from the .yml/.txt file OR if there is no file for the player, Make a new one with the player's name, And get the args. (Also it does this for overwriting).

    I would like PER PLAYER .yml/.txt if possible,
    Thanks!

    PS - Can you please paste the WHOLE code for this, Not snippets of code?
     
  2. Offline

    xBlackLightx

    No one is going to paste the entire code for you. Sorry, that's just the way it is. I know at least for myself, it's because I don't have time. I'll try to help you the best I can. What you're looking for is a StringBuilder. You would put something along the lines of this in your if statement where you want it to successfully add the tag.

    Code:java
    1. StringBuilder buffer1 = new StringBuilder();
    2.  
    3. for(int i = 1; i < args.length; i++)
    4. {
    5. buffer1.append(' ').append(args[i]);
    6. }
    7. String string1 = buffer1.toString();[/i]


    Note: where it says:

    Code:java
    1. for(int i = 1; i < args.length; i++)


    You would replace the number 1 with whatever argument you want the string to start after. Remember that the first argument is 0, not 1.

    Then to put it in a file, you would simply do

    Code:java
    1. this.getConfig().set("WhateverYouWantTheKeyToBeCalled", string1);


    To get the string from the config you would do

    Code:java
    1. this.getConfig().getString("WhateverYouCalledYourKey").substring(1));


    The substring of 1 will remove the space at the beginning of your string.

    Hope this helped you. If you need anything more, just ask, and I'll do my best.
    ~Alden


    By the way, if you see [/i] in the code, remove it. It's a formatting thing from the website.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page