Saving Data in Flatfile

Discussion in 'Plugin Development' started by SuperEvan200, Mar 1, 2013.

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

    SuperEvan200

    I want to save my new plugin's data in a preferred yml file or any other type of flatfile storage. (Not MySQL). Please don't give me the link to the wiki page. I don't understand a word of their essay. Here is my main code.

    Code:
    package me.superevan200.kingdoms;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Kingdoms extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Kingdoms plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        }
       
        String kingdom1 = ChatColor.DARK_BLUE + "[Delolia] " + ChatColor.RESET;
        String kingdom2 = ChatColor.DARK_AQUA + "[Syviel] " + ChatColor.RESET;
        String kingdom3 = ChatColor.DARK_GREEN + "[Natura] " + ChatColor.RESET;
        String kingdom4 = ChatColor.DARK_RED + "[Valkyrie] " + ChatColor.RESET;
     
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
            if(args[0].equalsIgnoreCase("help"))
                player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("delolia"))
                player.setDisplayName(kingdom1 + player.getDisplayName());       
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("syviel"))
                player.setDisplayName(kingdom2 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("natura"))
                player.setDisplayName(kingdom3 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("valkyrie"))
                player.setDisplayName(kingdom4 + player.getDisplayName());   
            }
            return true;
        }
    }
    
    I started developing this plugin a few days ago, and this is my first plugin. I'm very new to java and Bukkit plugin development and I need a clear explanation on how to do this.

    Thanks. :)
     
  2. Offline

    Lolmewn

    But the wiki page is great! FileConfiguration is a great utility, and you probably should learn how to use it.
     
    microgeek likes this.
  3. Offline

    bleachisback

    The wiki is honestly the best resource for this kind of stuff. You should look into trying to spend some time to read it to solve your problems and learn other things, but for now, use these:
    Code:java
    1. getConfig().set("path",property);//sets "path" in config as property
    2. getConfig().get("path");//returns whatever "path" is in your config
    3. saveConfig();//saves the config, and should be used after every time you change it
    4. reloadConfig();//reloads the config from the disc; should only be used if you think the user has changed something
    5.  
     
  4. Offline

    SuperEvan200

    Where do I get "FileConfiguration". And I don't get a word of the wiki page. They usually use top level developer vocabulary and hide the actual information with a bunch of things I most likely won't need and don't understand.

    Thanks. So how do I edit the path? Do I simply put /plugins? Plus, where do I insert this code. And does this save my player data such as this (I wanted players to be able to keep their prefix after they logout and when the server restarts):
    Code:
    public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
            if(args[0].equalsIgnoreCase("help"))
                player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    bleachisback

    "path" in Configuration.set() is the path within the file. By using the default command getConfig() it is using the config in plugins/PluginName. So if I used this code:
    Code:java
    1.  
    2. getConfig().set("prefixes."+player.getName(),"prefix");
    3. saveConfig();


    It would create a new config in plugins/PluginName/config.yml (if it doesn't already exist) that looks like this:
    Code:
    prefixes:
        player1: prefix
        player2: prefix
    
    and using this code later:
    Code:java
    1. getConfig().getString("prefixes."+player.getName(),"default");


    Will return the player's stored prefix, or "default" if it doesn't exist.
     
    SuperEvan200 likes this.
  6. Offline

    SuperEvan200

    Thanks, this cleared it up very well. :) One more thing though, where do I place these codes. Do I place this code:
    Code:
    getConfig().set("prefixes."+player.getName(),"prefix");
    saveConfig();[/syntax]
    After an if statement of a player executing a command to receive a prefix, just anywhere, or below or above the if statements, or somewhere else?

    Where do I also place this code:
    Code:
    getConfig().getString("prefixes."+player.getName(),"default");
    Should I make a new if statement for when a player joins the game. If so, how would I write the "if" statement? (The public Boolean and the actual "if) For example here I have a public boolean for a command and an if for a player executing a specific command.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
            if(args[0].equalsIgnoreCase("help"))
     
  7. Offline

    bleachisback

    SuperEvan200 Where you write that code is up to you; it depends on what you want to do with it.
     
  8. Offline

    SuperEvan200

    Ok, I guess I'll try the way I described, but I have a question.
    How would I write the "if" statement? (The public Boolean and the actual "if) For example here I have a public boolean for a command and an if for a player executing a specific command.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
            if(args[0].equalsIgnoreCase("help"))
    Bump.

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

    Burnett1

    Please explain further.
     
  10. Offline

    SuperEvan200

    Burnett1: You know how this is an if statement for when the player executes the command /kingdoms
    Code:
    if(commandLabel.equalsIgnoreCase("kingdom")){
    What is the code for the if statement when any players joins the server?
    What do I also put for the Boolean when a player joins the ame? For example the Boolean here is about the commands:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
    Bump.

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

    Burnett1

    Ok joining a game isn't an if statement, it is an event. Below your onDisable put this.
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e){
          //code
        }
    Need more help ask.

    I would advise watching this series of bukkit videos all the way through.

    http://www.youtube.com/playlist?list=PL4A0C6138F3F5AA41
     
  12. Offline

    SuperEvan200

    Here's my updated code. For some reason when I join a kingdom, it sends me the message for all the kingdoms and gives me all the kingdom's prefixes. It also doesn't save after I logout. Someone please help! :(

    Code:
    package me.superevan200.kingdoms;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Kingdoms extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Kingdoms plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        }
       
        @EventHandler
      public void onJoin(PlayerJoinEvent e) {
            Player player = event.getPlayer();
            getConfig().getString("prefixes."+player.getName(),"default");
      }
       
        String kingdom1 = ChatColor.DARK_BLUE + "[Delolia] " + ChatColor.RESET;
        String kingdom2 = ChatColor.DARK_AQUA + "[Syviel] " + ChatColor.RESET;
        String kingdom3 = ChatColor.DARK_GREEN + "[Natura] " + ChatColor.RESET;
        String kingdom4 = ChatColor.DARK_RED + "[Valkyrie] " + ChatColor.RESET;
     
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
            if(args[0].equalsIgnoreCase("help"))
                player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("delolia"))
                player.setDisplayName(player.getName());
                player.setDisplayName(kingdom1 + player.getDisplayName());
                player.sendMessage(ChatColor.DARK_BLUE + "You have joined Delolia!");
                getConfig().set("prefixes."+player.getName(),"prefix");
                saveConfig();
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("syviel"))
                player.setDisplayName(player.getName());
                player.setDisplayName(kingdom2 + player.getDisplayName());   
                player.sendMessage(ChatColor.DARK_AQUA + "You have joined <insert kingdom>!");
                getConfig().set("prefixes."+player.getName(),"prefix");
                saveConfig();
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("natura"))
                player.setDisplayName(player.getName());
                player.setDisplayName(kingdom3 + player.getDisplayName());   
                player.sendMessage(ChatColor.DARK_GREEN + "You have joined <insert kingdom>!");
                getConfig().set("prefixes."+player.getName(),"prefix");
                saveConfig();
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("valkyrie"))
                player.setDisplayName(player.getName());
                player.setDisplayName(kingdom4 + player.getDisplayName());   
                player.sendMessage(ChatColor.DARK_RED + "You have joined <insert kingdom>!");
                getConfig().set("prefixes."+player.getName(),"prefix");
                saveConfig();
            }
            return true;
        }
    }
     
  13. Offline

    CookCreeperz

    Player player = event.getPlayer();
     
    SuperEvan200 likes this.
  14. Offline

    SuperEvan200

    CookCreeperz Eclipse marks "event" in "event.getPlayer();" as an error and states event cannot be resolved.

    [EDIT]: Data also doesn't save for some reason, but the yml file does generate (See post #16)
    [EDIT]: In fact whenever I say /k anything anything or /k hrrh wderfgvwqgyhj or /k thryhetw wawuykuik (Literally anything), it sends me 4 messages saying I joined each kingdom, and gives me all prefixes.
     
  15. Try e.getPlayer() instead of event.getPlayer() or rename your parameter
    Code:
    PlayerJoinEvent e
    to
    Code:
    PlayerJoinEvent event
     
    CookCreeperz and SuperEvan200 like this.
  16. Offline

    SuperEvan200

    Thanks, that solved the error. ;)

    However, Data also doesn't save for some reason, but the yml file does generate and whenever I say /k anything anythinghere or /k wetfdsrtre wderfgvwqgyhj or /k wegsuhjgfwhj wawuykuik (Literally anything), it sends me 4 messages saying I joined each kingdom, and gives me all prefixes.

    If you want to see my full code refer to post #16, it contains an updated version of my code.
     
  17. Well, your method runs through to the end and you lost some { } on the way down. The {} are optional if, and only if you use only one command that should be in the block of code. So if you have something like:

    Code:
    if (true != false) {
      doSomeCrazyStuffHere();
    }
    
    you can shorten it by using
    Code:
    if (true !=  false)
      doSomeCrazyStuffHere();
    
    but as your code should learn you now, using this can get you in trouble if you don't have it in mind, as

    Code:
    if (true ==  false)
      wouldNeverBeExecuted();
      willBeExecutedAlthoughItIsIndented();
    
    will executed the second method, regardless of what is the result in the if expression. Thats what you probably want:

    Code:
    if (true ==  false) {
      wouldNeverBeExecuted();
      willNotBeExecutedAsWell().asItIsInTheBlockOfCode().thatBelongsToTheIf();
    }
    
    In your case:
    Code:
        if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("natura"))
                player.setDisplayName(player.getName());
                player.setDisplayName(kingdom2 + player.getDisplayName());  
    goes to
    Code:
       if(args[0].equalsIgnoreCase("join"))
       {
            if(args[1].equalsIgnoreCase("natura")) {
                player.setDisplayName(player.getName());
            }
        }
        player.setDisplayName(kingdom2 + player.getDisplayName()); 
    
     
  18. Offline

    microgeek

    If you can't undersand the vocabulary on the Wiki page then programming is way over your head, I'd suggest learning basic computer science terminology.
     
  19. Offline

    SuperEvan200

    microgeek Have you read what I even said? I know basic science computer terminology.

    This isn't what I'm trying to get.

    Code:
      if(args[0].equalsIgnoreCase("join"))
      {
            if(args[1].equalsIgnoreCase("natura")) {
                player.setDisplayName(player.getName());
            }
        }
        player.setDisplayName(kingdom2 + player.getDisplayName()); 
    Only on /join natura I wan't the player to get the prefix natura, not just /join.

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

    minoneer

    I'm sorry, but someone who is asking about the difference between an if statement and an event, or doesn't know how to propperly write an if statement does NOT know even the most basic things about computer science.
     
  21. Offline

    SuperEvan200

    minoneer I'm sorry, but do you know the difference between basic computer science and programming? You know, not everyone who knows basic computer signs knows Java well. I wasn't asking the difference between a statement and an event, I was asking how to indicate when a player joins a game, assuming it would be an if statement. Stop posting here, its a help topic, not a criticism topic.

    Bump. I still have the same problem, and data is also not saving. Post #16 refers to the most updated version of my code.

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

    SuperEvan200

  23. Offline

    SuperEvan200

    Someone help!
     
  24. Offline

    SuperEvan200

  25. Offline

    SuperEvan200

  26. Offline

    sionzee

    I don't understand this.
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e) {
    3.  
    4. // Get player.
    5. Player player = event.getPlayer();
    6.  
    7. // What is it ? I don't understand this code... What do you mean ?
    8. getConfig().getString("prefixes."+player.getName(),"default");
    9. }
     
    SuperEvan200 likes this.
  27. Offline

    SuperEvan200

    sionzee
    I want to make the plugin load the player's prefix once he joins the game.
     
  28. Offline

    microgeek

    [​IMG]
    In all honesty, read the Wiki.
     
  29. Offline

    Exoaria

    If you don't understand it, read the Wiki. I started learning Java about 4 days ago and I've already finished my first plugin because I read the Wiki, watched videos, and didn't expect people to write my code for me. These people on this forum are intelligent and very kind, violently bumping the thread when you obviously aren't getting any credibility doesn't really support it.

    Here's some documentation on flatfiles:
    http://forums.bukkit.org/threads/bukkits-yaml-configuration-tutorial.42770/

    Here's the wiki documentation on flatfiles:
    http://wiki.bukkit.org/Configuration_API_Reference

    Inbox me your current issue and I will see if I can help. I'm new as well, but I can try.
    I will explain things but you really need to do the reading. If I didn't read I wouldn't of learned as much as I did.
     
    zack6849 and microgeek like this.
  30. Offline

    sionzee

    Exoaria
    This is not full CODE!
    Code:java
    1. getConfig().getString("prefixes."+player.getName(),"default");

    There it is unnecessary. Why you get prefix and not use it, You only load.

    What I think with ( FULL CODE ) ?
    Example: p.setDisplayName(getConfig().getString("prefixes."+player.getName(),"default"));
     
Thread Status:
Not open for further replies.

Share This Page