Bukkit编程之动态向yml文件中添加属性

 1 yaml = new Yaml();        
 2             String goods = args[0];
 3             String goodsNum = args[1];
 4             
 5             YamlConfiguration yc = new YamlConfiguration();
 6             try {
 7                 yc.load(new File(System.getProperty("user.dir")+"/plugins/PbMainPlugin/configDef.yml"));
 8             } catch (FileNotFoundException e1) {
 9                 
10                 e1.printStackTrace();
11             } catch (IOException e1) {
12                 
13                 e1.printStackTrace();
14             } catch (InvalidConfigurationException e1) {
15                 
16                 e1.printStackTrace();
17             }
18             
19             Set set = yc.getConfigurationSection("drop").getKeys(false);
20             
21             lastDropNum = set.size();  //查看yml文件中是否有内容,没有内容返回0,否则返回内容长度
22             
23             player.sendMessage("大小是:"+set.size()+"");
24             player.sendMessage("内容是:"+set+"");
25             YamlConfiguration yamlConfiguration = new YamlConfiguration();
26             if(lastDropNum==0) { //如果原来yml文件中没有内容
27 
28                 ConfigurationSection drop = yamlConfiguration.createSection("drop");
29                 
30                 Map<String, Object> item = new HashMap();
31                 item.put("item-Id", Material.valueOf(goods.trim()).getId());
32                 item.put("item-Name", Material.valueOf(goods.trim()).name());
33                 drop.createSection("1",item);    
34                 try {
35                     
36                     yamlConfiguration.save(System.getProperty("user.dir")+"/plugins/PbMainPlugin/configDef.yml");
37                 } catch (IOException e) {
38                     // TODO Auto-generated catch block
39                     e.printStackTrace();
40                 }
41                 
42             }
43             
44             if(lastDropNum!=0) { //如果yml中有内容,则向yml文件中追加新的内容
45                 yamlConfiguration = YamlConfiguration.loadConfiguration(new File(System.getProperty("user.dir")+"/plugins/PbMainPlugin/configDef.yml"));
46                 ConfigurationSection drop = yamlConfiguration.getConfigurationSection("drop");
47                 Map<String, Object> item = new HashMap();
48                 item.put("item-Id", Material.valueOf(goods.trim()).getId());
49                 item.put("item-Name", Material.valueOf(goods.trim()).name());
50                 drop.set((lastDropNum+1)+"", item);
51                 try {
52                     yamlConfiguration.save(System.getProperty("user.dir")+"/plugins/PbMainPlugin/configDef.yml");
53                 } catch (IOException e) {
54                     
55                     e.printStackTrace();
56                 }
57 
58             }
59 
60             //向玩家背包中添加物品
61             PlayerInventory inventory = player.getInventory();
62             ItemStack itemStack = new ItemStack(Material.valueOf(goods.trim()),Integer.parseInt(goodsNum));
63             inventory.addItem(itemStack);
64             return true;

yml文件中的结构为:

原文地址:https://www.cnblogs.com/lihuibin/p/9156002.html