创建“自定义" Bukkit插件YAML文件 [英] Create 'Custom' Bukkit Plugin YAML File

查看:380
本文介绍了创建“自定义" Bukkit插件YAML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人.我最近刚开始为Minecraft编写Bukkit插件.我已经有前两个插件的开发版本在服务器上运行良好,并且根本没有给我带来太多麻烦.我目前正在研究第三个,但遇到了一些麻烦.

我试图弄清楚如何精确地创建一个YAML文件以及从中读取数据/向其中写入数据.只是为了澄清,我不是在引用config.yml文件,因为我对此没有任何麻烦.我知道如何创建默认的config.yml文件并从中读取数据,而这一切都很好.但是,对于第三个插件,我需要使用单独的YAML文件.我到处寻找帮助,但是95%的答案涉及某人告诉我有关getConfig()的一些信息,这不是我想要的,或者至少我有95%的肯定不是我想要的.我在寻找.在寻找明确答案的几周后,我决定在此处发布我的问题.与往常一样,在此先感谢您的帮助!

我想我已经找到了如何创建YAML文件的方法,但是在那之后我陷入了困境.我只举一个例子说明我的情况.

假设我有以下主要课程:

package ...

import ...

//Here is my main class
public class MyClass extends JavaPlugin {

    //I instantiate my File and FileConfiguration here
    //Should I do this? I need them for my other classes.
    public FileConfiguration myFileConfig = null;
    public File myFile;

    //On Enable
    @Override
    public void onEnable() {

        //Get/Create File
        myFile = new File(getDataFolder(), "myfile.yml");
        if (!myFile.exists()) {
            try {
                myFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

            //Load myfily.yml file configuration
        FileConfiguration myFileConfig = YamlConfiguration.loadConfiguration(myFile);

            //Register my command executor class
        getCommand("test").setExecutor(new myCommandExecutor());
    }

    //On Disable
    @Override
    public void onDisable() {

        //Irrelevant stuff here

    }

}

现在说我还有以下CommandExecutor类(星号标记出发生重要事件的位置.我省去了所有嵌套的if函数以节省时间):

package ...

import ...

public class myCommandExecutor implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (cmd.getName().equalsIgnoreCase("test")) {

            if (args.length > 0) {

****************//RIGHT HERE I WOULD ADD ALL THE COMMAND ARGUMENTS
****************//IMAGINE THE FOLLOWING USAGE FOR THE COMMAND
****************//USAGE: /test <add|del> <one|two|three> <name>
****************//IF THE USER EXECUTED THE FOLLOWING, THE CODE BELOW WOULD BE THE FINAL RESULT
****************//EXECUTED: /test add two hello
                YamlClass.addToFile(args[1], args[2]);

            } else {

                sender.sendMessage("Not enough arguments!");

            }

        }

    }

}

在上面的示例中,如果用户键入/test add two hello,我希望将最后两个参数(两个和你好)发送到另一个类(在此示例中,是类YamlClass中的addToFile(String a, String b))其中args [1]和args [2]将用于将字符串放入这样的文件中:

test:
  one:
  two:
    - hello
  three:

,如果用户随后运行/test add three goodbye,则文件将如下所示:

test:
  one:
  two:
    - hello
  three:
    - goodbye

如果用户要执行/test add three test,它将在该部分添加测试",而不会替换之前添加的再见". 任何人都可以给我一些帮助或提示,以进行此操作吗? 谢谢!

我昨晚弄清楚了.实际上,就File和YamlCinfiguration而言,我所做的一切都正确无误,CommandExecutor出了点问题,但我已将其修复.感谢您的答复!

解决方案

通常,zifnab06的答案没有那么错误.我本人正在自己开发Bukkit插件,并习惯于将以下代码用于YAML文件.

 File f = new File("path/to/your/YAML/file.yml");
YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);
 

这将创建一个YamlConfiguration的新实例,您可以使用yamlFile.set("path", new Object());进行访问以写入值,并使用yamlFile.get("path");进行读取值(您不必事先使用createSection(String)). JavaDoc .

如果要保存/创建.yml文件,只需使用以下代码:

 try {
  yamlFile.save(f);
} catch(IOException e) {
  // Handle any IO exception here
  e.printStackTrace();
}
 

我希望这能回答您的问题;如果需要使用一些代码,则可以使用 BitBucket 上可用的插件的源代码. /p>

everyone. I have just recently gotten into writing Bukkit plugins for Minecraft. I've already got dev versions for my first two plugins running fine on my server, and they haven't given me much trouble at all. I'm currently working on a third, and I've run into some trouble.

I am trying to figure out how exactly to create a YAML file and read/write data from/to it. Just to clarify, I am NOT referring to a config.yml file, as I am not having any trouble with that. I know how to create a default config.yml file and read data from it, and all that is just fine and dandy. However, with my third plugin, I need to use a separate YAML file. I've looked around for help, but 95% of the answers I get involve someone telling me something about getConfig(), which is NOT what I'm looking for, or at least I'm 95% sure that's not what I'm looking for. After a few weeks of searching for a clear answer, I've decided to post my question here. As always, thanks in advance for any help!

I think I've figured out how to create a YAML file, but I'm stuck after that. I'll just give an example of my situation.

Let's say I had the following main class:

package ...

import ...

//Here is my main class
public class MyClass extends JavaPlugin {

    //I instantiate my File and FileConfiguration here
    //Should I do this? I need them for my other classes.
    public FileConfiguration myFileConfig = null;
    public File myFile;

    //On Enable
    @Override
    public void onEnable() {

        //Get/Create File
        myFile = new File(getDataFolder(), "myfile.yml");
        if (!myFile.exists()) {
            try {
                myFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

            //Load myfily.yml file configuration
        FileConfiguration myFileConfig = YamlConfiguration.loadConfiguration(myFile);

            //Register my command executor class
        getCommand("test").setExecutor(new myCommandExecutor());
    }

    //On Disable
    @Override
    public void onDisable() {

        //Irrelevant stuff here

    }

}

Now say I also had the following CommandExecutor class (the asterisks mark where important stuff happens. I left out all the nested if functions to save your time):

package ...

import ...

public class myCommandExecutor implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (cmd.getName().equalsIgnoreCase("test")) {

            if (args.length > 0) {

****************//RIGHT HERE I WOULD ADD ALL THE COMMAND ARGUMENTS
****************//IMAGINE THE FOLLOWING USAGE FOR THE COMMAND
****************//USAGE: /test <add|del> <one|two|three> <name>
****************//IF THE USER EXECUTED THE FOLLOWING, THE CODE BELOW WOULD BE THE FINAL RESULT
****************//EXECUTED: /test add two hello
                YamlClass.addToFile(args[1], args[2]);

            } else {

                sender.sendMessage("Not enough arguments!");

            }

        }

    }

}

In the example above, if a user typed /test add two hello, I would want the last two arguments (two and hello) sent to a method in another class (in this example, addToFile(String a, String b) in the class YamlClass) in which args[1] and args[2] would be used to put a string into a file like this:

test:
  one:
  two:
    - hello
  three:

and if the user then ran /test add three goodbye the file would look like this:

test:
  one:
  two:
    - hello
  three:
    - goodbye

If a user were to then do /test add three test it would add 'test' to the section without replacing the 'goodbye' that was previously added. Could anyone give me some help or tips on how to go about doing this? Thanks!

[EDIT] I figured it out last night. I was actually doing everything correctly as far as the File and YamlCinfiguration go, there was something wrong with my CommandExecutor, but I fixed it. Thanks for the responses!

解决方案

Generally, zifnab06's answer isn't that wrong. I myself am working on a Bukkit plugin myself and got used to using the following piece of code for YAML files.

File f = new File("path/to/your/YAML/file.yml");
YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);

This creates a new instance of a YamlConfiguration which you can access with yamlFile.set("path", new Object()); for writing values and yamlFile.get("path"); for reading values (you don't necessarily need to use createSection(String) beforehand). All methods which can be used are contained in the JavaDoc.

If you want to save/create your .yml file, you just have to use this code:

try {
  yamlFile.save(f);
} catch(IOException e) {
  // Handle any IO exception here
  e.printStackTrace();
}

I hope this answered your question; if you need some code to work with, you can use my plugin's source code available on BitBucket.

这篇关于创建“自定义" Bukkit插件YAML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆