Need some help: config

Discussion in 'Plugin Development' started by NoobDev, Aug 6, 2012.

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

    NoobDev

    Hello guys :)
    I've got a little problem with following:
    I want to make a command : /whatever <name>, and when that command is performed it should save <name> to the config in for example:
    Code:
    1:
    <name>
    ok, and the next time that command is performed it should check if the name is already in the config.
    but im not able to do that, because of 2 problems: idk how to check if the name is already in the config, and idk how to save the name to the config properly, im always trying it with this:
    Code:
    Player player1 = Bukkit.getServer().getPlayer(args[0]);
    this.getConfig().set("1", player1);
    
    but then it saves me this:
    Code:
    1:
      ==: Player
      name: <name>
    
     
  2. Offline

    azyhd

    Try using,
    Player player = getServer().getPlayer(getName());
    getConfig().get("1").equals(player.getName());
     
  3. Offline

    NoobDev

    ok, do u mean following ?
    if(getConfig().get("1").equals(player.getName())){
    }
    and what about the args, :( ?
     
  4. Offline

    davejavu

    NoobDev do you want your config to be like this:
    Code:
    1:
        derpyname:
        derpyname2:
    
     
  5. Offline

    NoobDev

    davejavu
    almost:
    Code:
    1:
        name1
        name2
    (spaces don't matter)
     
  6. Offline

    davejavu

    NoobDev well then you'll want to do a string list; which is this:
    Code:
    1:
    - name1
    - name2
    Code:java
    1.  
    2. if (getConfig().contains("1")) {
    3. if (getConfig().getStringList("1").contains(args[0])) {
    4. // do whatever
    5. return true;
    6. } else {
    7. List<String> ls = getConfig().getStringList("1");
    8. ls.add(args[0]);
    9. getConfig().set("1", ls);
    10. //Do whatever else.
    11. return true;
    12. }
    13. } else {
    14. List<String> s = new ArrayList<String>();
    15. s.add(args[0]);
    16. getConfig().set("1", s);
    17. return true;
    18. }
     
    NoobDev likes this.
  7. Offline

    NoobDev

    davejavu
    W8 a second ill try that
    Edit: List
    Code:
    List<String> ls = getConfig().getStringList("1w");
    turns red:
    error.jpg

    @davejavu can't u help me :( ?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    Courier

    Show us all your imports at the top of your file.
     
  9. Offline

    NoobDev

    Courier
    here it is, its a warn plugin:
    Code:
            if(cmd.getName().equalsIgnoreCase("warn")){
                if (args.length == 1){
                    if(sender.hasPermission("kwarn.warn")){
                        Player targetwarn = Bukkit.getServer().getPlayer(args[0]);
                        if (targetwarn == null) {
                            sender.sendMessage(ChatColor.RED + "This player is not online!");
                            return true;
                        }
                        else {
                            if (getConfig().contains("1w")) {
                                if (getConfig().getStringList("1w").contains(args[0])) {
                                sender.sendMessage(ChatColor.RED + args[0] + "was already warned one time!");
                                return true;
                                } else {
                                List<String> ls = getConfig().getStringList("1w");
                                ls.add(args[0]);
                                getConfig().set("1w", ls);
                                //Do whatever else.
                                return true;
                                }
                                } else {
                                List<String> s = new ArrayList<String>();
                                s.add(args[0]);
                                getConfig().set("1w", s);
                                return true;
                                }
                        this.saveConfig();
                        targetwarn.sendMessage(ChatColor.RED + "You have been warned by an Admin!");
                        return true;
                        }
                    }
                    else {
                        sender.sendMessage(ChatColor.RED + "You don't have permission!");
                        return true;
                    }
                }
                else {
                    sender.sendMessage(ChatColor.RED + "You just can warn one player / must specify a player!");
                    return false;
                }
            }
     
  10. Offline

    Courier

    I mean at the very top of your file (after package ...; )
    Something like this:
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.LinkedHashSet;
    3. import java.util.List;
    4. import java.util.Set;
    5.  
    6. import org.bukkit.block.Block;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
     
  11. Offline

    NoobDev

    Courier
    Code:
    import java.awt.List;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Kwarns extends JavaPlugin {
        public void onEnable(){
            getLogger().info("KWarns enabled!");
            FileConfiguration cfg = this.getConfig();
            cfg.options().copyDefaults(true);
            this.saveConfig();
        }
     
        public void onDisable(){
            getLogger().info("KWarns disabled!");
            this.saveConfig();
        }
     
  12. Offline

    Courier

    That's your problem. Replace it with:
    Code:java
    1. import java.util.List;
     
  13. Offline

    NoobDev

    Courier
    davejavu
    Oh no it was so easy :D, your both in the credits ^^
     
Thread Status:
Not open for further replies.

Share This Page