Config saving

Discussion in 'Plugin Development' started by KoolzSkillz, Jun 22, 2014.

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

    KoolzSkillz

    Ok
    I'm an trying to make a configurable easy plugin for my first one with a config file BUT
    The config file ends up blank when it is installed


    I'm not quite sure why anyone know
     
  2. Offline

    NoLiver92

    Does it have information when you open it inside your ide?
     
  3. Offline

    KoolzSkillz

    bwfcwalshy NoLiver92
    Coding
    Code:
    package me.BasicCommands.kyle;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.FileConfigurationOptions;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Icommands
      extends JavaPlugin
      implements Listener
    {
      public static Icommands plugin;
      public Permission playerPermission1 = new Permission("BSC.kill");
      public Permission playerPermission2 = new Permission("BSC.ecopen");
      public Permission playerPermission3 = new Permission("BSC.gettime");
      public Permission playerPermission4 = new Permission("BSC.setime");
      public Permission playerPermission5 = new Permission("BSC.heal");
      public Permission playerPermission6 = new Permission("BSC.sucide");
      public Permission playerPermission7 = new Permission("BSC.egg");
      public Permission playerPermission8 = new Permission("BSC.hat");
      public Permission playerPermission9 = new Permission("BSC.modkit");
      public final Logger logger = Logger.getLogger("Minecraft");
      public int number = 3;
     
      public void onDisable()
      {
        PluginDescriptionFile p = getDescription();
        this.logger.info(p.getName() + " V" + p.getVersion() +
          " Has been enabled!");
      }
     
      public void onEnable()
      {
        PluginDescriptionFile p = getDescription();
        this.logger.info(p.getName() + " V" + p.getVersion() +
          " Has been enabled!");
        getConfig().options().copyDefaults(true);
        saveConfig();
        PluginManager pm = getServer().getPluginManager();
      }
     
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
        Player player = (Player)sender;
        World world = player.getWorld();
        if (commandLabel.equalsIgnoreCase("news"))
        {
          player.sendMessage(ChatColor.GREEN + "[News]: " + ChatColor.GOLD +
            getConfig().getString("news"));
        }
        else if (commandLabel.equalsIgnoreCase("server"))
        {
          player.sendMessage(ChatColor.GREEN + "======[Plugin v1.3]======");
          player.sendMessage(ChatColor.GREEN + "[Server]: " + ChatColor.GOLD +
            getConfig().getString("server"));
          player.sendMessage(ChatColor.GREEN + "======[" +
            getConfig().getString("servername") + "]======");
          player.sendMessage(ChatColor.BLUE + "[Mods]: " + ChatColor.GOLD +
            getConfig().getString("mods"));
          player.sendMessage(ChatColor.GREEN + "[Smods]: " + ChatColor.GOLD +
            getConfig().getString("smods"));
          player.sendMessage(ChatColor.DARK_RED + "[Admin]: " +
            ChatColor.GOLD + getConfig().getString("admins"));
          player.sendMessage(ChatColor.DARK_PURPLE + "[CoOwner]: " +
            ChatColor.GOLD + getConfig().getString("coowners"));
          player.sendMessage(ChatColor.DARK_PURPLE + "[Owner]: " +
            ChatColor.GOLD + getConfig().getString("owners"));
        }
        else if (commandLabel.equalsIgnoreCase("donate"))
        {
          player.sendMessage(ChatColor.GREEN + "[Donate]: " + ChatColor.GOLD +
            getConfig().getString("donateinfo"));
          player.sendMessage(ChatColor.GREEN +
            getConfig().getString("donateinfo2"));
          player.sendMessage(ChatColor.GREEN +
            getConfig().getString("donate"));
        }
        else if (commandLabel.equalsIgnoreCase("motd"))
        {
          player.sendMessage(ChatColor.GREEN + "[Msg of the day]: " +
            ChatColor.GOLD + getConfig().getString("MOTD"));
        }
        else if (commandLabel.equalsIgnoreCase("vote"))
        {
          player.sendMessage(ChatColor.GREEN + "[Voting links]: " +
            ChatColor.GOLD + getConfig().getString("servername"));
          player.sendMessage(ChatColor.GREEN + getConfig().getString("link1"));
          player.sendMessage(ChatColor.GREEN + getConfig().getString("link2"));
          player.sendMessage(ChatColor.GREEN + getConfig().getString("link3"));
        }
        else if (commandLabel.equalsIgnoreCase("owner"))
        {
          player.sendMessage(ChatColor.GREEN + "The Owner is " +
            getConfig().getString("owner"));
        }
        else if (commandLabel.equalsIgnoreCase("IP"))
        {
          player.sendMessage(ChatColor.GREEN + "The servers IP is " +
            getConfig().getString("IP"));
        }
        else if (commandLabel.equalsIgnoreCase("website"))
        {
          player.sendMessage(ChatColor.GREEN + "The website is " +
            getConfig().getString("website"));
        }
        else if ((commandLabel.equalsIgnoreCase("egg")) &&
          (player.hasPermission("BSC.egg")))
        {
          ItemStack egg = new ItemStack(Material.EGG, 64);
          PlayerInventory pi = player.getInventory();
          pi.addItem(new ItemStack[] { egg });
        }
        else if ((commandLabel.equalsIgnoreCase("sucide")) &&
          (player.hasPermission("BSC.sucide")))
        {
          player.setHealth(0);
          player.sendMessage(ChatColor.GREEN + "Commited sucide");
          player.chat("I just commited Sucide D: ingame");
        }
        else if ((commandLabel.equalsIgnoreCase("killplayer")) &&
          (player.hasPermission("BSC.kill")))
        {
          if (args.length == 0)
          {
            player.sendMessage(ChatColor.DARK_RED +
              " TOO LITTLE ARGS  IT IS  " + ChatColor.RED +
              "/killplayer name");
          }
          else if (args.length == 1)
          {
            Player targetPlayer = player.getServer().getPlayer(args[0]);
            targetPlayer.setHealth(0);
            targetPlayer.sendMessage(ChatColor.GREEN +
              " You Just got killed");
          }
          else
          {
            player.sendMessage("You Do not have permission");
          }
        }
        else if ((commandLabel.equalsIgnoreCase("healplayer")) &&
          (player.hasPermission("BSC.heal")))
        {
          if (args.length == 0)
          {
            player.sendMessage(ChatColor.DARK_RED +
              " TOO LITTLE ARGS  IT IS  " + ChatColor.RED +
              "/healplayer name");
          }
          else if (args.length == 1)
          {
            Player targetPlayer = player.getServer().getPlayer(args[0]);
            targetPlayer.setHealth(20);
            targetPlayer.sendMessage(ChatColor.GREEN +
              " You Just got Healed");
          }
          else
          {
            player.sendMessage("You Do not have permission");
          }
        }
        else if (commandLabel.equalsIgnoreCase("ilt"))
        {
          player.chat("I like turtles");
        }
        else if ((commandLabel.equalsIgnoreCase("ec")) &&
          (player.hasPermission("BSC.ecopen")) &&
          (args.length == 0))
        {
          if (player.getServer().getPlayer(args[0]) != null)
          {
            player.sendMessage(ChatColor.GREEN + " Enderchest Opened ");
            player.openInventory(player.getEnderChest());
          }
          else if (args.length == 1)
          {
            if (player.getServer().getPlayer(args[0]) != null)
            {
              Player targetPlayer = player.getServer().getPlayer(
                args[0]);
              player.sendMessage(ChatColor.GREEN +
                player.getDisplayName() +
                " Enderchest Opened ");
              player.openInventory(targetPlayer.getEnderChest());
            }
            else
            {
              player.sendMessage(ChatColor.RED +
                "PLAYER IS NOT ONLINE!");
            }
          }
        }
        return false;
      }
    }
    
    Config File
    Code:
    # Made By Gabe And KoolzSkillz
    # NOTE CHANGE THE THINGS AFTER : DONT CHANGE THE THINGS BEFORE IT OR IT WONT WORK!!!
    # and leave a space after :
     
    #/server Command
    server: Put Info Here
    servername: The servers name goes here
     
    #/staff Command
    mods: Mods names here
    smods: Smods names here
    admins: Admins names here
    coowners: Coowners names here
    owners: Owners name here
     
    #/donate Command
    donateinfo: where to donate and stuff goes here
    donateinfo2: Your Top Donation Thing goes here
    donate: what it has in it and/or what you get goes here
     
    #/MOTD Command
    MOTD: The Moto of the day goes here
     
    #/vote Command
    link1: Voting link1 goes here
    link2: Voting link2 goes here
    link3: Voting link3 goes here
     
    #/news Command
    news: Put Server News Here
     
    #/owner Command
    owner: put owner name here
     
    #/IP Command
    IP: Put servers ip here
     
    #/Website Command
    website: put the servers website here
     
    #Join Message
    'joinmsgline1: (Example) Welcome to our server donkey. this is basicly
    'joinmsgline2: a MOTD that auto pops up :D
     
  4. Offline

    NoLiver92

    Point1: Why do you have permissions variables at the top of the file? they are pointless as dont work as you use them
    Point2: paste your plugin.yml file please
    Point3: you dont need to say plugin is enabled, bukkit does it for you
    Point4: try saveDefaultConfig() instead of copy defaults.
    Point5: you may want to think about splitting the class file in to several (aka one for listeners and one for commands) it will make life simpler for debugging if theres a problem
     
  5. Offline

    KoolzSkillz

    NoLiver92
    Point1: Why do you have permissions variables at the top of the file? I like them like that and they still work
    Point2: paste your plugin.yml file please. pasted with this
    Point3: you dont need to say plugin is enabled, bukkit does it for you. ok i will remove that
    Point4: try saveDefaultConfig() instead of copy defaults. i will work on that
    Point5: you may want to think about splitting the class file in to several. i will work on that\

    Code:
    name: ICommands
    main: me.BasicCommands.kyle.Icommands
    version: 1.5
    commands:
      vote:
        description: Sends a msg
      motd:
        description: Sends a msg
      donate:
        description: Sends a msg
      staff:
        description: Sends a msg
      server:
        description: Sends a msg
      news:
        description: Sends a msg
      heal:
        description: Sends a msg
      egg:
        description: Sends a msg
      owner:
        description: Sends a msg
      website:
        description: Sends a msg
      ip:
        description: Sends a msg
      killplayer:
        description: Sends a msg
      healplayer:
        description: Sends a msg
      ilt:
        description: Sends a msg
      ec:
        description: Sends a msg
      sucide:
        description: Sends a msg
     
     
     
     
     
     
     
    
     
  6. Offline

    NoLiver92

    KoolzSkillz
    Code:
    public Permission playerPermission1 = new Permission("BSC.kill");
      public Permission playerPermission2 = new Permission("BSC.ecopen");
      public Permission playerPermission3 = new Permission("BSC.gettime");
      public Permission playerPermission4 = new Permission("BSC.setime");
      public Permission playerPermission5 = new Permission("BSC.heal");
      public Permission playerPermission6 = new Permission("BSC.sucide");
      public Permission playerPermission7 = new Permission("BSC.egg");
      public Permission playerPermission8 = new Permission("BSC.hat");
      public Permission playerPermission9 = new Permission("BSC.modkit");
    You may like these but you dont use them so whats the point? I would understand if you actually used them but you dont, you do this:
    Code:
    if ((commandLabel.equalsIgnoreCase("killplayer")) &&
          (player.hasPermission("BSC.kill")))
    Also you have not declared your permissions in the plugin.yml file, therefore they wont work.

    Have you got the config file working yet?
     
  7. Offline

    KoolzSkillz

    NoLiver92
    No I havnt and I still have no idea what's wrong with it

    And what do you mean declared them in the plugin.yml file?
    This has worked for me putting them in the coding
     
  8. Offline

    NoLiver92

    KoolzSkillz Have you tried what I suggested?

    I do it slightly diffrently and my method works for all .yml files you need to copy, checkFiles:
    Code:java
    1. public class CheckFiles
    2. {
    3.  
    4. private final AdvancedRegions plugin;
    5.  
    6. public CheckFiles(AdvancedRegions plugin)
    7. {
    8. this.plugin = plugin;
    9. }
    10.  
    11. public void checkConfig(String filename) {
    12. if(!checkFile(filename, plugin.getDataFolder())) {
    13. plugin.getLogger().warning("Error creating config!");
    14. }
    15. }
    16. public boolean checkFile(String filename, File directory) {
    17. if(!(new File(directory, filename)).exists()) {
    18. if(!extractFile(filename, directory)) {
    19. return false;
    20. }
    21. }
    22. return true;
    23. }
    24. public boolean extractFile(String filename, File destination) {
    25. File outputFile = new File(destination, filename);
    26. DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    27. Date date = new Date();
    28. String theDate = dateFormat.format(date);
    29. File backup = new File(destination, theDate + filename);
    30. try {
    31. if(outputFile.exists()) {
    32. outputFile.renameTo(backup);
    33. }
    34. destination.mkdir();
    35. if(getClass().getResourceAsStream("/" + filename) == null) {
    36. if(backup.exists()) {
    37. backup.renameTo(outputFile);
    38. }
    39. System.out.println("File not found in jar: " + filename);
    40. return false;
    41. }
    42. outputFile.createNewFile();
    43. InputStream is = getClass().getResourceAsStream("/" + filename);
    44. FileOutputStream fos = new FileOutputStream(outputFile);
    45. byte[] buffer = new byte[1024];
    46. int bytesRead;
    47. while((bytesRead = is.read(buffer)) > 0) {
    48. fos.write(buffer, 0, bytesRead);
    49. }
    50. fos.flush();
    51. fos.close();
    52. is.close();
    53. }
    54. catch (IOException e) {
    55. e.printStackTrace();
    56. System.out.println("Error extracting file: " + filename);
    57. }
    58. return true;
    59. }
    60.  
    61. }


    Thats a sepperate class file to the main one.

    now in onenable() at the top (first thing you do):
    Code:java
    1. //Check For Config File (leave at top)
    2. CheckFiles fileCheck = new CheckFiles(this);
    3. fileCheck.checkConfig("config.yml");


    This copys everything including comments. It is also prooven to work as I use it in all of my plugins. (I did not write the code its on this forum somewhere) Hope this helps
     
    KoolzSkillz likes this.
  9. Offline

    KoolzSkillz

    NoLiver92
    Ok I'll add that and test it asap

    NoLiver92
    IT WORKS THANK YOU SO MUCH :D
    but what do you mean by declaring my permssions in the plugin.yml file

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

    NoLiver92

    KoolzSkillz
    All plugin developers should be familiar with the tutorial, if your not why are you coding? Read It before posting?
    Scroll down until you get to the permissions section, it explains it all there


    Your welcome, that class is able to do other files too just change the name and it copys the new file. (its the one piece of code all my plugins have).

    for plugin.yml check above post

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    KoolzSkillz likes this.
  11. Offline

    KoolzSkillz

    K thx :D

    NoLiver92 please read this

    Configuring your permissions
    If you want more control over your permissions, for example default values or children then you should consider adding them to your plugin.yml. This is completely optional, however it is advised. Below is an example permissions config that would be appended to the end of your existing plugin.yml:

    It says In the middle

    This is completely optional

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

    NoLiver92

    KoolzSkillz Ahh ok I missed that BUT it is advised and considered as something you do when creating plugins, its a good practice to get in to. but the variables are still not needed :p
     
    KoolzSkillz likes this.
  13. Offline

    KoolzSkillz

    NoLiver92 it's ok,
    I'm gonna keep doing it as I am ATM tho
     
  14. Offline

    KoolzSkillz

    NoLiver92
    One more questions if I want it like this


    These are my classes

    CheckFile
    Main
    MOTD

    I can't get MOTD to recognize
    getConfig().getString("MOTD");

    It wants me to add a receiver into Checkfiles. I tested it and it broke it altogether, (the auto generated Receiver)
    Do you have any idea how to do it
     
  15. Can you send a copy of your MOTD class?

    Also, NoLiver92 would your "checkfiles" class replace bukkit's default saveConfig() completely? And what effect would saveConfig() and reloadConfig() have on a config file, even if you used the checkfiles class on the plugin enable?
     
  16. Offline

    KoolzSkillz

    xYourFreindx

    Code:
    package Commands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class MotdCommand implements CommandExecutor{
     
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            Player player = (Player) sender;
                    if (commandLabel.equalsIgnoreCase("motd")) {
                    if (!player.hasPermission("ICommands.Motd")) {
                        player.sendMessage(ChatColor.RED + "No Permission To Do This");
                    } else {
                        player.sendMessage(getConfig().getString("Motd"));
                    }
                    }
                    return true;
        }
     
    }
    
     
  17. Use new version below​
     
  18. Offline

    Necrodoom

  19. Code:java
    1. package Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class MotdCommand implements CommandExecutor{
    10.  
    11. Main main;
    12. public MOTD(Main plugin){
    13. this.main = plugin;
    14. }
    15.  
    16. public boolean onCommand(CommandSender sender, Command cmd,
    17. String commandLabel, String[] args) {
    18. Player player = (Player) sender;
    19. if (commandLabel.equalsIgnoreCase("motd")) {
    20. if (!player.hasPermission("ICommands.Motd")) {
    21. player.sendMessage(ChatColor.RED + "No Permission To Do This");
    22. } else {
    23. player.sendMessage(main.getConfig().getString("Motd"));
    24. }
    25. }
    26. return true;
    27. }
    28.  
    29. }

    So sorry, I had forgotten that last bit.
    Use this ^ newer version instead. ​
     
  20. Offline

    KoolzSkillz

    xYourFreindx
    My class's name is Main3 which mains do I change to that. Their are too many mains XD
     
  21. Lol, just change the ones that start with a capital letter. The rest are fine.
     
  22. Offline

    KoolzSkillz

    xYourFreindx
    public MOTD(Main plugin){
    Has an error
    It says Return type for method is missing


    Code:
    package Commands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import Main.Main3;
     
    public class MotdCommand implements CommandExecutor{
     
    Main3 main;
    public MOTD(Main3 plugin){
    this.main = plugin;
     
    }
     
    public boolean onCommand(CommandSender sender, Command cmd,
    String commandLabel, String[] args) {
    Player player = (Player) sender;
    if (commandLabel.equalsIgnoreCase("motd")) {
    if (!player.hasPermission("ICommands.Motd")) {
    player.sendMessage(ChatColor.RED + "No Permission To Do This");
    } else {
    player.sendMessage(main.getConfig().getString("Motd"));
    }
    }
    return true;
    }
     
    }
    
     
  23. May I see your new MOTD class? Sorry, I just want to check and make sure you changed the right ones.
    Always eliminate the simplest of the possible errors, is what I always say.
     
  24. Offline

    fireblast709

    KoolzSkillz
    - Don't create a static plugin instance field.
    - Don't use the Minecraft logger, use the Logger object retrieved from JavaPlugin#getLogger()
    - Don't cast CommandSender to Player before checking instanceof Player
    - Use cmd.getName() for alias support
    - Check if getPlayer() returns null (in which case the Player is offline)
     
  25. If your main class is named Main3 and your Motd class is named MOTD, then I can't see anythign funky with that setup. It looks exactly how I've been doing it for my classes. Exact for the import of Main.Main3 Why did you do that?
     
    KoolzSkillz likes this.
  26. Offline

    KoolzSkillz

    xYourFreindx
    Thx it was as simple as I had changed the class name XD





    Another error

    It is breaking the
    getCommand("MOTD")setExecutor(new MotdCommand());
     
  27. Offline

    Dragonphase

    KoolzSkillz

    It's unrelated to your current error, but you could use this resource. It makes configuration management much easier.
     
  28. Offline

    NoLiver92


    for the main config no it wouldnt replace it, you could still use the default saveconfig() etc. but this method was written dso that other .yml files could be used in the same way.
     
Thread Status:
Not open for further replies.

Share This Page