[Tutorial/Beginner] Making configs

Discussion in 'Resources' started by theguynextdoor, Dec 30, 2011.

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

    Don Redhorse

    damn this java :rolleyes:

    hmm ok... i probably just shut up as I do it differently... with a seperate class and getters and setters..

    totally agree with you after reading my post again... quick and dirty most of the times only means dirty...
     
  2. Offline

    Darkhand81

  3. Offline

    spaceemotion

    First of all this is a very well-structured and easy to understand How to about configurations.

    I don't know if my question can be placed in here but I am somehow not able to even start a configuration file variable.
    I am using netbeans (version 7.2 on mac osx lion) for my plugin creation (I am a newbie in Java development, but already wrote 3 plugins for a server of my friend (and aren't a newbie in general programming)).
    I used Dinnerbones videos to learn plugin programming, thats also the reason why i used netbeans (and not eclipse).
    But it seems to be like the netbeans depencies are too old: Bukkit still has the old Configuration code in it (I cant import the org.bukkit.configuration.* classes). And I don't know how to update the depencies.
    I already tried to use the newest bukkit.jar as a library but failed (The menu option was not there because it's based on a Maven-Project :/ ).

    Was the "Logger log = Logger.getLogger("Minecraft");" code also implemented in the new version? I am still using "System.out" for cmd line prints.

    My question now is: does anybody knows if i made anything wrong in Netbeans (any solution for how to solve my bukkit version error) or just use Eclipse for the future ?

    Any help is appreaciated and thanks in advance!
    - and sorry for my english, its not my first language ;)
     
  4. Offline

    Darkhand81

    I've always had trouble updating Bukkit and Craftbukkit through Maven and eGit in Eclipse as well, I just delete the projects and start over every time there's a major enough shift that I need to update it.
     
  5. Offline

    spaceemotion

    I already tried that in Netbeans but hadnt changed anything (maybe its caching the downloaded files?), seems to be like i have to use eclipse now (I also had trouble with netbeans in the past).

    Thanks for the quick reply!

    Edit: Seems to work now like a charm with eclipse!
     
  6. Offline

    deregudegu

    Do it work in 1.1?
     
  7. Offline

    theguynextdoor

    I see no reason why configs shouldn't work. They have worked for me so far. The only thing that wont work is my example because they are now outdated. But i will change them at a later date
     
  8. Offline

    phondeux

    Code:
            List<String> messages = plugin.getConfig().getList(" motd.list ");
            for (String s : messages) {
                event.getPlayer().sendMessage(s);
            }
    Eclipse hates this ... it suggests that messages be cast to an object. That won't work, though, as sendMessages needs it as a String. Any suggestions?
     
  9. Offline

    theguynextdoor

    Try replacing
    List<String>
    to
    String[]
     
  10. Offline

    phondeux

    Nope; it still thinks List<object> is the answer. :(

    I wish there was somewhere to cast it.
     
  11. Offline

    theguynextdoor

    Right, List<String> is the answer. Can you show me how you defined the string array / list, and also how you've done it in the config.

    Make sure it is the same concept as the /rules example.
     
  12. Offline

    phondeux

    I did it the same as the rules example;
    Code:
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerJoin(final PlayerJoinEvent event) {
            List<String> messages = plugin.getConfig().getList("motd.list");
            for (String s : messages) {
                event.getPlayer().sendMessage(s);
            }
        }
     
  13. Offline

    theguynextdoor

    Code:
        @EventHandler(priority = EventPriority.LOW)
        public void onPlayerJoin(final PlayerJoinEvent event) {
            List<Object> messages = plugin.getConfig().getList("motd.list");
            for (Object s : messages) {
                event.getPlayer().sendMessage((String) s);
            }
        }
    Code:
    String[] messages = { "derp", "herp", "merk" };
    Code:
    config.addDefault("motd.list", Arrays.asList(messages));
     
    phondeux likes this.
  14. Offline

    phondeux

    Thank you, you're a beautiful human being! :)
     
  15. Offline

    spaceemotion

    Is there a way to check if a config file already exists? I dont want to overwrite the current configuration file. Or does Bukkit automatically check this?
     
  16. Offline

    theguynextdoor

    If it exists i'm pretty sure it wont overwrite.
     
  17. Offline

    Dark_Balor

    In case of you can do : getStringList("motd.list");

    You have here all the method that you can use on a ConfigurationSection : http://jd.bukkit.org/doxygen/d9/dfb...1_1configuration_1_1ConfigurationSection.html

    (and YamlConfiguration is implementing that interface)
     
    phondeux likes this.
  18. Offline

    Iron_Crystal

    Hi

    I am pretty sure I have followed everything your tutorial says to do, and it compiles properly, and it also makes the config file, but whenever I try calling values from it, I get this error:
    [​IMG]
     
  19. Offline

    theguynextdoor

    Can you show me your code, and specify which line is line 40? You have a NPE is all. Maybe you misspelt a config paths name, or using the wrong type of value.
     
  20. Offline

    Iron_Crystal

    I ended up rerwiting my code so that I don't need a config file, but line 40 was the code where I was calling the value from the config file. It went like

    player.setHealth(config.getInt("starve"));

    I don't know what the problem is, unless it has to do with the fact that the config.getInt("starve"); in a separate method, but I am not sure.

    Edit: That was probably it, but I don't need it anymore, so thanks!

    Double Edit: I was trying to call the variables from an onCommand method, and I was wondering how you do that
     
  21. Offline

    theguynextdoor

    To get your config from inside a command method depends. If your command is in your main class then you can use
    this.getConfig()
    if its a separate class, you make a constructor in that class pointing to your main class, and use
    plugin.getConfig()
     
  22. Offline

    Iron_Crystal

  23. Offline

    boardinggamer

    I am trying to add an item to the list but keep getting an error no mater what I do. can someone give me a simple answer on how to add to the list in the config because I have to add a players name to the list.
     
  24. Offline

    theguynextdoor

    config.getList("path").add(player.getName());
    saveConfig();

    if that doesnt work, i will take a better look later
     
  25. Offline

    boardinggamer

    My friend helped me with it and it was a little different then that
    Code:
    List<Object> list = getConfig().getList("Members");
    list.add(plr.getName());
    saveConfig();
     
  26. Offline

    Nytewarrior

    Hi, when I try to use plugin.config.getInt, Eclipse gives an error and says "JavaPlugin.config is not visible". I've looked through this thread, but none of the other solutions really help me..
     
  27. maybe it's private? make it public if you want to make it accessable in outside methor or make a getter method.
     
  28. Offline

    theguynextdoor

    Use
    plugin.getConfig().getInt
     
  29. Offline

    colony88

    Hi I have a problem: I use command executors to handle my commands, but I get errors when I try to get an int from the config file. I'll post the error log and the code.
    error log (open)


    2012-02-25 02:45:08 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'sun' in plugin SetSunnyAdvanced v3.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:402)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:784)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:744)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:732)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:100)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:537)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:435)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    Caused by: java.lang.NullPointerException
    at me.cedi.setsunny.SunCommandExecutor.onCommand(SunCommandExecutor.java:91)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 12 more


    main class (open)

    package me.cedi.setsunny;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;

    public class SetSunnyCore extends JavaPlugin {

    private SunCommandExecutor sunExecutor;
    private RainCommandExecutor rainExecutor;
    private StormCommandExecutor stormExecutor;

    public void onDisable() {
    System.out.println(getDescription().getName() + " v" + getDescription().getVersion() + " Disabled!");
    }

    public void onEnable() {
    System.out.println(getDescription().getName() + " v" + getDescription().getVersion() + " Enabled!");

    this.sunExecutor = new SunCommandExecutor();
    this.rainExecutor = new RainCommandExecutor();
    this.stormExecutor = new StormCommandExecutor();

    this.getCommand("sun").setExecutor(sunExecutor);
    this.getCommand("rain").setExecutor(rainExecutor);
    this.getCommand("storm").setExecutor(stormExecutor);

    final FileConfiguration config = this.getConfig();
    config.options().header("Change this value for the default duration of the '/sun', '/rain' or '/storm' commands.");
    config.addDefault("DEFAULT_DURATION", 1000000);
    config.options().copyDefaults(true);
    saveConfig();
    }

    }

    one of the executor classes (open)

    package me.cedi.setsunny;

    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;

    public class SunCommandExecutor implements CommandExecutor{
    SetSunnyCore plugin;
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    //onCommand(start)-------------------------------------------------
    if (sender instanceof ConsoleCommandSender == true){
    //If ConsoleCommandSender(start)-----------------------------------

    //If there are less than 1 arguments after "/sun"
    if(args.length < 1){

    sender.sendMessage("Not enough arguments.");
    sender.sendMessage("Correct usage is: /sun [world] [duration]");

    }

    //-----------------------------------------------------------------

    //If there is 1 argument after "/sun"
    else if(args.length == 1){

    World w = sender.getServer().getWorld(args[0]);

    if (w == null){
    sender.sendMessage("The world '" + args[0] + "' doesn't exist.");
    return true;
    }

    w.setThundering(false);
    w.setStorm(false);
    w.setWeatherDuration(plugin.getConfig().getInt("DEFAULT_DURATION"));
    sender.sendMessage("Weather set to sunny in " + args[0]);

    }

    //-----------------------------------------------------------------

    //If there are 2 arguments after "/sun"
    else if(args.length == 2){

    World w = sender.getServer().getWorld(args[0]);

    if (w == null){
    sender.sendMessage("The world '" + args[0] + "' doesn't exist.");
    return true;
    }

    w.setThundering(false);
    w.setStorm(false);
    w.setWeatherDuration(Integer.parseInt(args[1]));
    sender.sendMessage("Weather set to sunny in " + args[0] + " for " + args[1] + " ticks.");

    }

    //-----------------------------------------------------------------

    //If there are more than 2 arguments after "/sun"
    else if(args.length > 2){

    sender.sendMessage("Too many arguments.");
    sender.sendMessage("Correct usage is: /sun [world] [duration]");

    }

    //If ConsoleCommandSender(end)-------------------------------------
    }

    //If the sender is a player
    else if(sender instanceof Player == true){

    //If Player(start)-------------------------------------------------

    //If there are less than 1 arguments after "/sun"
    if(args.length < 1 && (sender.hasPermission("SetSunny.local") || sender.hasPermission("SetSunny.*") || sender.isOp())){

    Player p = (Player)sender;
    World world = p.getWorld();

    world.setThundering(false);
    world.setStorm(false);
    world.setWeatherDuration(plugin.getConfig().getInt("DEFAULT_DURATION"));
    sender.sendMessage(ChatColor.YELLOW + "Weather set to sunny in " + ChatColor.GREEN + world.getName());

    }

    //-----------------------------------------------------------------

    //If there is 1 argument after "/sun"
    else if(args.length == 1 && (sender.hasPermission("SetSunny.world.sun") || sender.hasPermission("SetSunny.world") || sender.hasPermission("SetSunny.*") || sender.isOp())){

    World world = sender.getServer().getWorld(args[0]);

    if (world == null){

    sender.sendMessage(ChatColor.RED + "The world '" + args[0] + "' doesn't exist.");
    return true;

    }

    world.setThundering(false);
    world.setStorm(false);
    world.setWeatherDuration(plugin.getConfig().getInt("DEFAULT_DURATION"));
    sender.sendMessage(ChatColor.YELLOW + "Weather set to sunny in " + ChatColor.GREEN + args[0]);

    }

    //-----------------------------------------------------------------

    //If there are 2 arguments after "/sun"
    else if(args.length == 2 && (sender.hasPermission("SetSunny.world.sun") || sender.hasPermission("SetSunny.world") || sender.hasPermission("SetSunny.*") || sender.isOp())){

    World world = sender.getServer().getWorld(args[0]);

    if (world == null){
    sender.sendMessage(ChatColor.RED + "The world '" + args[0] + "' doesn't exist.");
    return true;
    }

    world.setThundering(false);
    world.setStorm(false);
    world.setWeatherDuration(Integer.parseInt(args[1]));
    sender.sendMessage(ChatColor.YELLOW + "Weather set to sunny in " + ChatColor.GREEN + args[0] + ChatColor.YELLOW + " for " + ChatColor.GREEN + args[1] + ChatColor.YELLOW + " ticks.");

    }

    //-----------------------------------------------------------------

    //If there are more than 2 arguments after "/sun"
    else if(args.length > 2){

    sender.sendMessage(ChatColor.RED + "Too many arguments.");
    sender.sendMessage(ChatColor.RED + "Correct usage is: /sun [world] [duration]");

    }

    //If Player(end)---------------------------------------------------

    }

    //onCommand(end)---------------------------------------------------

    return true;
    }
    }
     
  30. Offline

    Darkhand81

    From the first post:

    I still can't manage to get any values from my config while in another class. So that I can compare and see what I'm missing, could I see a short example of a technically complete Main class and Listener class that pulls a config value in the listener?
     
Thread Status:
Not open for further replies.

Share This Page