public static int minPlayers; == 0?

Discussion in 'Plugin Development' started by mickedplay, Mar 29, 2014.

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

    mickedplay

    Hello everybody,

    I have a problem with my config file. Is public static int minPlayers; every time 0? I used the config to set player size.
    First if-statement of /ctf join:

    It won't work.
    Code:java
    1.  
    2. if(args[0].equalsIgnoreCase("join"))
    3. {
    4. if(CaptureTheFlag.status.getStatus() == 1 && CaptureTheFlag.playersInGame.size() < CaptureTheFlag.maxPlayers)
    5. {
    6. if(!CaptureTheFlag.playersInGame.contains(p))
    7. {
    8. ....
    9. }
    10. else
    11. {
    12. p.sendMessage(CaptureTheFlag.prefix + "Already ingame!");
    13. }
    14. }
    15. else if(CaptureTheFlag.status.getStatus() == 1 && CaptureTheFlag.playersInGame.size() >= getConfig().getInt("CaptureTheFlag.Player.maxPlayers"))
    16. {
    17. p.sendMessage(CaptureTheFlag.prefix + "Full.");
    18. p.sendMessage(CaptureTheFlag.prefix + "Size: ยง6" + playersInGame.size());
    19. }
    20. else
    21. {
    22. return;
    23. }
    24. }
    25.  

    The else if-statement is every time called if I perform /ctf join and says:
    "Full", Size: 0

    For config:
    Code:java
    1.  
    2. public static int minPlayers;
    3. public static int maxPlayers;
    4.  
    5. onEnable:
    6. getConfig().addDefault("CaptureTheFlag.Player.minPlayers", "2");
    7. getConfig().addDefault("CaptureTheFlag.Player.maxPlayers", "32");
    8. getConfig().options().copyDefaults(true);
    9. saveConfig();
    10. minPlayers = getConfig().getInt("CaptureTheFlag.Players.minPlayers");
    11. maxPlayers = getConfig().getInt("CaptureTheFlag.Player.maxPlayers");
    12.  

    Hope you understand what I mean :p
    Thanks!
     
  2. Offline

    MOMOTHEREAL

    Try putting reloadConfig() after the saveConfig() in your onEnable()?
     
  3. Offline

    adam753

    Not sure if it's intentional, but you are saving CaptureTheFlag.Player.minPlayers and loading CaptureTheFlag.Players.minPlayers.
     
  4. Offline

    Azubuso

    mickedplay
    Not entirely sure if this changes anything, but change
    Code:java
    1. getConfig().addDefault("CaptureTheFlag.Player.minPlayers", "2");
    2. // To
    3. getConfig().addDefault("CaptureTheFlag.Player.minPlayers", 2); // As an int, not a string
     
  5. Offline

    mickedplay

    MOMOTHEREAL Won't work :( Same problem. Unfortunately there's no error message in the console.
    adam753 Oh, Thank you! Didn't saw that, but won't work too :(
    Azubuso Yeah, but on every reload after edit to test, it creates the default file again.

    Any ideas? Need help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Borlea

    mickedplay
    Your code:
    Code:java
    1.  
    2. getConfig().addDefault("CaptureTheFlag.Player.minPlayers", "2");
    3. getConfig().addDefault("CaptureTheFlag.Player.maxPlayers", "32");
    4. getConfig().options().copyDefaults(true);
    5. saveConfig();
    6. minPlayers = getConfig().getInt("CaptureTheFlag.Players.minPlayers");
    7. maxPlayers = getConfig().getInt("CaptureTheFlag.Player.maxPlayers");
    8.  

    Couple problems with this. You are saving these values as a string. you are saving them each time server starts up. So its impossible to edit them in config. As it gets set back to 2 and 32 each time server starts. and you are searching for Players instead of player.

    What you should do is.

    Code:java
    1.  
    2. saveDefaultConfig();
    3. minPlayers = getConfig().getInt("CaptureTheFlag.Player.minPlayers");
    4. maxPlayers = getConfig().getInt("CaptureTheFlag.Player.maxPlayers");
    5.  


    Then create a config.yml where your plugin.yml is located. With those values, saveDefaultConfig() only creates and sets the values if the config isn't already generated.

    Here is what your config.yml should be
    Code:yaml
    1.  
    2. CaptureTheFlag:
    3. Player:
    4. minPlayers: 2
    5. maxPlayers: 32
    6.  
     
  7. Offline

    mickedplay

    Borlea After edit the /plugin/XYZ/config.yml nothing happens. The Server uses the Default config.yml in the plugin.jar
     
  8. Offline

    foldagerdk

    mickedplay What Borlea said should work. If it doesn't, make sure you have removed the previously created config.yml in your plugin destination before launching the server. saveDefaultConfig will not override a current configuration, meaning if you had an old configuration file from a previous build, it will stay. And try to add all configuration options to the default config.yml so users have a chance to see what is going on before running the plugin.
     
  9. Offline

    mickedplay

    foldagerdk Borlea I got it :p

    Is there any way to write a reloadConfig command? You have to do /rl every time after edit the file.
    Tried this, won't work:
    Code:
    if(args[0].equalsIgnoreCase("reloadConfig"))
    {
        reloadConfig();
        System.out.println("[CaptureTheFlag] Configuration file reloaded!");
    }
     
  10. Offline

    SquidFruit

    Okay here's how i do config files and this is a personal favorite and works best for me.

    So we are going to start this of by creating a config.yml file where your plugin.yml file is.

    then what u want to do is in your onEnable() you will add

    Code:java
    1. public void onEnable() {
    2. saveDefaultConfig();
    3. reloadConfig();
    4.  
    5.  
    6. }


    what this does it saves the defaults into the config.

    now you are probley wondering how do i get that "defaults" into the config.



    so lets say we want to do this

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args) {
    2. Player player = (Player) sender;
    3. if (CommandLabel.equalsIgnoreCase("Tutorial")) {
    4. player.sendMessage(getConfig().String("Tutorial"));
    5. //gets the string "Tutorial"
    6.  
    7. }
    8. return true;
    9. }
    10.  
    11.  



    so thats the string that we are going to use as this example we are using Tutorial so what we do in the config.yml file we made at the start is this.



    Code:
    Tutorial: 'This is the text that will be sent'

    now you are probley wondering whats the difference from my version to yours well my version is alot easyer to do and use but with yours you have to add whats in the config by coding it. But with my way the yml file does all that code for u once u grab the string.

    Hope this helps..
     
  11. Offline

    foldagerdk

    Is that in your main class? Where you have the onEnable etc.?
     
  12. Offline

    mickedplay

    foldagerdk Main class:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    3. {
    4. if(sender instanceof Player)
    5. {
    6. Player p = (Player)sender;
    7. ...
    8. if(cmd.getName().equalsIgnoreCase("ctf"))
    9. {
    10. if(args.length == 1)
    11. {
    12. if(args[0].equalsIgnoreCase("reloadConfig"))
    13. {
    14. reloadConfig();
    15. System.out.println("[CaptureTheFlag] Configuration file reloaded!");
    16. }
    17. ....
    18. }
    19. }
    20. }
    21. return false;
    22. }
     
  13. Offline

    foldagerdk

    Entire class pls
     
Thread Status:
Not open for further replies.

Share This Page