Config StringList help

Discussion in 'Plugin Development' started by eleectricman226, Jan 17, 2015.

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

    eleectricman226

    Currently, I am making a VoidtoSpawn plugin for a friend. I am trying to store the worlds in a StringList of the config, but it won't generate them, any help?

    Code:
    package net.James.VoidtoSpawn.Main;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin{
        public static Plugin plugin;
        public void onEnnable(){
            getServer().getPluginManager().registerEvents(new Events(), this);
            plugin = this;
            getConfig().options().copyDefaults(true);
            getConfig().addDefault("TeleportWhenYIsLessThan", (double)-15);
            List<String> list = getConfig().getStringList("EnabledWorlds");
            getConfig().addDefault("EnabledWorlds", list);
            saveConfig();
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!(sender instanceof Player)){
                sender.sendMessage("You cannot run this.");
                return false;
            }else{
                Player player = (Player)sender;
            if(label.equalsIgnoreCase("vts")){
                if(args.length < 2){
                    player.sendMessage(ChatColor.RED + "Usage: /vts <addworld, removeworld> <worldName>");
                }else{
                    if(args[0].equalsIgnoreCase("addworld")){
                        if(Bukkit.getWorld(args[1]) != null){
                            if(!getConfig().getStringList("EnabledWorlds").contains(args[1])){
                            getConfig().getStringList("EnabledWorlds").add(args[1]);
                            saveConfig();
                            player.sendMessage(ChatColor.GREEN + "Added the world " + ChatColor.GOLD + args[1] + ChatColor.GREEN + ".");
                        }else{
                            player.sendMessage(ChatColor.RED + "World " + ChatColor.GOLD + args[1] + ChatColor.RED + " already exists in the configuration!");
                        }
                        }else{
                            player.sendMessage(ChatColor.RED + "World " + ChatColor.GOLD + args[1] + ChatColor.RED + " does not exist!");
                        }
                    }else if(args[0].equalsIgnoreCase("removeworld")){
                        if(getConfig().getStringList("EnabledWorlds").contains(args[1])){
                            for(int i = 0; i > getConfig().getStringList("EnabledWorlds").size(); i++){
                                if(getConfig().getStringList("EnabledWorlds").get(i).equals(args[1])){
                                    getConfig().getStringList("EnabledWorlds").remove(i);
                                    saveConfig();
                                    player.sendMessage(ChatColor.GREEN + "Removed the world " + ChatColor.GOLD + args[1] + ChatColor.GREEN + " from the configuration.");
                                }
                            }
                        }else{
                            player.sendMessage(ChatColor.RED + "The world " + ChatColor.GOLD + args[1] + ChatColor.RED + " is not present in the configuration!");
                            return false;
                        }
                    }
                }
            }
            }
            return false;
        }
       
       
    }
    
     
  2. Offline

    InkzzzMC

    What's your config.yml? Post it please.
     
  3. Offline

    Zombie_Striker

    Here is some advice for your onEnnable()
    • Check to make sure the config exists (Make an If statement or just do saveConfig() before you reference it)
    • Check to make sure your List contains anything (Just for better practice)
    • Correct your onEnnable() by replacing it with onEnable()
    • (Optional) Make a public string that equals "EnabledWorlds". Doing this helps to make sure there is no spelling mistakes when you reference it so many times.
    And for your onCommand()
    • Since you have an else statement that checks if the sender is a player, you don't need to return false.
    • You don't need the @Override.
    • (Optional)Instead of checking if the args are less then two, check to make sure it is not two. You currently have it set up where if someone adds extra args it is ignored, which is something you might want to change.
    • You are checking in the removeWorlds section if 'i' is greater then the size, which it will never be unless Enabledworlds is empty! replace '>' with '<'.
    • At the else statement at the end, you have a return false. It does nothing considering there next statement says the same thing, so it could be removed.
    • (Optional)You might want to add a way to list all the worlds that are enabled. It is simple and will be helpful for a plugin like this.
    I hope you find some of this helpful.

    (Edit) Can you tell me how you are checking if the Y is less than -15?
     
    Last edited: Jan 17, 2015
Thread Status:
Not open for further replies.

Share This Page