Saving a custom file

Discussion in 'Plugin Development' started by TheDirtyDan, Sep 21, 2012.

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

    TheDirtyDan

    I have tried making this and looking at tutorials, but I can't find a good one on either File/YAML writing that doesn't deal with making a single config (which I already know how to do). I want the ability to use /g create Test, and a file named Test.yml is created and I can store values and stuff there. Thanks in advance.
     
  2. Offline

    kyle1320

    TheDirtyDan
    Code:
    public YamlConfiguration getConfig(String name) {
        File folder = new File(getDataFolder());
        File file = new File(getDataFolder() + name +".yml");
        if(!folder.exists()) {
            folder.mkdir();
        }
       
        try {
            file.createNewFile();
            YamlConfiguration.loadConfiguration(file).save(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return YamlConfiguration.loadConfiguration(file);
    }
     
  3. Offline

    TheDirtyDan

    Code:
    public YamlConfiguration getGuild(CommandSender sender, String name){
            File folder = new File(plugin.getDataFolder(), "guilds");
            File file = new File(plugin.getDataFolder(), name + ".yml");
            if(!folder.exists())
                folder.mkdir();
            if(!file.exists()){
                try{
                    file.createNewFile();
                    YamlConfiguration.loadConfiguration(file).save(file);
                    sender.sendMessage(ChatColor.GREEN+"You have created the guild " + name);
                }catch(IOException e){
                    e.printStackTrace();
                }
            }else{ sender.sendMessage(ChatColor.RED+"Guild already exists!"); }
            return YamlConfiguration.loadConfiguration(file);
        }
    Code:
    if(label.equalsIgnoreCase("guild")){
                    if(args.length == 1){
                        util.getGuild(sender, args[0]);
                    }
                }
    kyle1320 It's giving me unhandled exception
     
  4. Offline

    kyle1320

    TheDirtyDan
    You're trying to put them in a folder named "guilds" from what I can tell? If that's the case it needs to be something like:
    Code:
    File folder = new File(plugin.getDataFolder() + File.separator + "guilds");
    File file = new File(plugin.getDataFolder() + File.separator + "guilds" + File.separator + name + ".yml");
     
  5. Offline

    TheDirtyDan

    kyle1320
    Code:
    public YamlConfiguration getGuild(CommandSender sender, String name){
            File folder = new File(plugin.getDataFolder() + File.separator + "guilds");
            File file = new File(plugin.getDataFolder() + File.separator + "guilds" + File.separator + name + ".yml");
            if(!folder.exists())
                folder.mkdir();
            if(!file.exists()){
                try{
                    file.createNewFile();
                    YamlConfiguration.loadConfiguration(file).save(file);
                    sender.sendMessage(ChatColor.GREEN+"You have created the guild " + name);
                }catch(IOException e){
                    e.printStackTrace();
                    sender.sendMessage(ChatColor.RED+"IOException : I dunno fix it!");
                }
            }else{ sender.sendMessage(ChatColor.RED+"Guild already exists!"); }
            return YamlConfiguration.loadConfiguration(file);
        }
    Still throwing Unhanded exception error
     
  6. Offline

    kyle1320

    TheDirtyDan
    Post the error and the line the error points to
     
  7. Offline

    TheDirtyDan

    Code:
    17:28:26 [INFO] TheDirtyDan issued server command: /guild CollKidClub
    17:28:26 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'guil
    d' in plugin Guilds v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:16
    8)
            at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:4
    98)
            at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.
    java:880)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:826)
     
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:808)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
            at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
            at net.minecraft.server.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:577)
            at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
            at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:473)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:405)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
            at me.thedirtydan.guilds.Main.onCommand(Main.java:40)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
            ... 15 more
    kyle1320
     
  8. Offline

    kyle1320

  9. Offline

    TheDirtyDan

    I'm using this command method to make the guild ( the command being /guild [text] )
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(sender instanceof Player){
                if(label.equalsIgnoreCase("guild")){
                    if(args.length == 1){
                        util.getGuild(sender, args[0]);
                    }
                }
            }
           
            return true;
        }
     
  10. Offline

    kyle1320

    So which line is line 40?
     
  11. Offline

    TheDirtyDan

    lol
    Code:
    util.getGuild(sender, args[0]);
     
  12. Offline

    TheDirtyDan

  13. Offline

    andf54

    If the files aren’t supposed to be edited by users, then use gson.

    Code:
    Gson gson = new Gson();
     
    // Write:
    String str = gson.toJson(myClass);
    writeString(str);
    // Read
    String str = readString();
    MyClass myClass = gson.fromJson(str, MyClass.class);
    
    MyClass = Guild in your case, I think.

    You can use my WriterReader https://github.com/andfRa/Saga/blob/master/src/org/saga/saveload/WriterReader.java
    You only need a custom Directory enum and delete/change some lines.
     
Thread Status:
Not open for further replies.

Share This Page