Player Files

Discussion in 'Plugin Development' started by Meowkittie64, Nov 8, 2012.

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

    Meowkittie64

    I have been trying to look for a while now, but have found nothing. I am trying to start work on a plugin but have been held up by this issue. If it helps this to make anymore sense, the plugin is a quest based plugin. I know there are others out there already but I need one with more specific features so I have been working on it myself.

    How can I make it so that when a player joins, it creates a file named after them. So for instance, I have the player join event and want it to create a file titled "Meowkittie64.txt" in the folder for this plugin. I understand how to make a single config file but can't seem to get player files to work.

    The next thing I need to do is pull information from an individual file. How could I specify which file to pull the information from. An example would be on a player move event, when a player is standing in a certain location, I want something to happen but only if a line in their "name.txt" file is equal to a certain value.

    Heres a really quick example of what I mean.

    Code:
    Player Join Event{
     
    Code to create file in a folder named after player?
     
    }
     
    Player Move Event {
     
    if location is x y z{
    if (code to get an int value from their "name.txt" file?) == 6{
    Something happens such as they are struck by lightning
     
    (code to change the value of an int in the file )?
    }
     
    }
    else do nothing
    }
    So what are the lines I'd need to create, read, and write to player specific files? thanks in advance and sorry if this seems really basic, i just cant figure it out for multiple players
     
  2. Offline

    raGan.

    Can you tell me exactly what are you trying to achieve ? I know I'm not helping but I'm curious.
     
  3. Offline

    Meowkittie64

    Well its kind of a long story but I will keep it short. I use to use the Citizens plugin to run a questing based server. The plugin had an easy quest making interface and allows you to add NPCs. Just recently I decided to put my server back up and create a new questing server again. But Citizens is now upgraded to Citizens 2 and no longer support quests. So I am going to just write a plugin specific to each quest now instead. I will still be using the citizens API so I can use their NPCs but as far as writing the quests I will have to do from scratch now.
     
  4. Offline

    raGan.

    Hmmm, try looking at Quester, it's the one I made, It supports Citizens 2. I will consider every feature request. (it hasn't been updated a long time, but recent dev versions are stable and ready to be used)
     
  5. Offline

    zeeveener

    You sure you want to use txt files? You can create config-like .yml files on the fly and use those.
    Code:java
    1. File file = new File(plugin.getDataFolder(), "player1.yml");
    2. FileConfiguration useFile = YamlConfiguration.loadConfiguration(file);
    3.  
    4. int test = useFile.getInt("Path.To.Integer", 0);
    5. if(test == 6){
    6. smiteThatPlayer(player1);
    7. }


    Easier than using FileReader and in/out streams to parse the file.

    However, if you want to read from text files still, you might want to brush up on file practices...
    http://www.javapractices.com/topic/TopicAction.do?Id=42
     
  6. Offline

    hutattedonmyarm

    First, use YAML files like zeeveener said, or maybe even a database (SQLite or MySQL).
    If you do it with (yaml or text) files:
    On PlayerJoinEvent, do something like this:
    Code:
    File f = new File(plugin.getDataFolder(), p.getName() + ".yml");
    if(f.exists()) {
        //Your player-file exists
    } else {
      //it does not
    }
    Then read the file, and check the value
     
  7. Offline

    Meowkittie64

    I tried that and it didn't seem to work. Here is my exact code that i ended with, let me know what i did wrong. I think I might be defining the plugin incorrectly? I'm not totally sure.

    Code:
    public class FileListener implements Listener{
     
        Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("NpcTesting");
     
        public void onPlayerJoin(PlayerJoinEvent event){
     
            File f = new File(plugin.getDataFolder(), event.getPlayer().getName() + ".yml");
       
        }
     
    }
    Thats the class that creates the file. The other thing I still don't understand fully is how to call information from the file in a class other than the listener class that created it.

    Edit: I added in a simple Welcome message to the event and that isn't sending either. Did I completely mess something up with the event that it isn't registering?

    Another thing, i noticed when i Import "File" that there were two options. Do I want the Java import for File or the bukkit import for File? I tried both but when using the Bukkit import for File I get caught in a loop of problems that require me to change everything
     
  8. Offline

    Meowkittie64

    Okay I played around a bit with what you guys told me and I got it to create the files. Thanks! I'm still having trouble putting and pulling information into and out of the file. This is what I am trying, but it is not working.


    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
     
    if (cmd.getLabel().equalsIgnoreCase("Input")){
                        Player player1 = (Player) sender;
                        File file = new File("plugins"+File.separator+"NpcTesting"+File.separator+"users"+File.separator+player1.getName()+".yml");
                        FileConfiguration config = YamlConfiguration.loadConfiguration(file);
                        config.set("Player.Name", "Changed");
                        try {
                            config.save(file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                   
                    }
                    else if(cmd.getLabel().equalsIgnoreCase("Output")){
                        Player player2 = (Player) sender;
                        FileConfiguration config = null;
                        File file = new File("plugins"+File.separator+"NpcTesting"+File.separator+"users"+File.separator+player2.getName()+".yml");
                        FileConfiguration config1 = YamlConfiguration.loadConfiguration(file);
                        String name = (String) config1.get("Player.Name");
                        player2.sendMessage("Name is:  " + name);
                    }
    That is the exact location I used to create the file successfuly, I just copied and pasted the path to the file to make sure I had it exactly the same. When I run either of these commands to test it, nothing happens. They are registering as commands, but nothing is changed in the file and the message is not sent to me for entering the command. What did I do wrong here?

    Here is the code I used for the event that now successfuly creates the player's file.

    Code:
    public void onPlayerLogin(PlayerLoginEvent event){
            FileConfiguration config = null;
            File file = new File("plugins"+File.separator+"NpcTesting"+File.separator+"users"+File.separator+event.getPlayer().getName()+".yml");
            if(!file.exists()){
                System.out.println("File Created: "+ event.getPlayer().getName() + ".yml");
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    config = YamlConfiguration.loadConfiguration(file);
                    config.set("Player.Name", event.getPlayer());
                   
                    try {
                        config.save(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
     
Thread Status:
Not open for further replies.

Share This Page