日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Bukkit之yaml动态读取

發布時間:2023/12/31 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 Bukkit之yaml动态读取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在使用bukkit框架寫插件的時候會經常使用到yml格式的文件來存儲配置或者玩家數據,這里來說一下實現yml中數據的動態讀寫:

先來看一下yml文件中的內容結構

 1 public boolean addBanSyntheseItem(CommandSender sender, Command cmd, String[] args) {
 2         List list = new ArrayList<>();
 3         Player player = (Player) sender;
 4         ItemStack itemStack = player.getItemInHand();
 5         File airdropFile = new File(Pickaxe.CONFIGPATH);
 6         if(airdropFile.exists()) {
 7             YamlConfiguration yc = new YamlConfiguration();
 8             try {
 9                 yc.load(airdropFile);
10             } catch (Exception e) {
11                 e.printStackTrace();
12                 return false;
13             }
14             Set set = yc.getConfigurationSection("banItemList").getKeys(false);
15             int count = set.size();
16             //如果yml配置文件是空的,創建根節點,并且添加內容
17             if(count == 0) {
18                 ConfigurationSection listSection = yc.createSection("banItemList");
19                  Map<String, Object> item = new HashMap();
20                  item.put("id",itemStack.getTypeId());
21                  item.put("durability", (int)itemStack.getDurability());
22                  item.put("type", itemStack.getType().toString());
23                  item.put("displayName", itemStack.getItemMeta().getDisplayName());
24                  item.put("lore", itemStack.getItemMeta().getLore());
25                  listSection.createSection(Integer.toString(itemStack.getTypeId()),item); 
26                  try {
27                     yc.save(Pickaxe.CONFIGPATH);
28                     return true;
29                 } catch (IOException e) {
30                     e.printStackTrace();
31                     return false;
32                 }
33             }else if(count>0) {    //如果yml中有內容,這直接在其后面追加內容
34                 ConfigurationSection section = yc.getConfigurationSection("banItemList");
35                  Map<String, Object> item = new HashMap();
36                 item.put("id",itemStack.getTypeId());
37                 item.put("durability", (int)itemStack.getDurability());
38                 item.put("type", itemStack.getType().toString());
39                 item.put("displayName", itemStack.getItemMeta().getDisplayName());
40                 item.put("lore", itemStack.getItemMeta().getLore());
41                 section.createSection(Integer.toString(itemStack.getTypeId()),item);
42                 try {
43                     yc.save(Pickaxe.CONFIGPATH);
44                     return true;
45                 } catch (IOException e) {
46                     e.printStackTrace();
47                     return false;
48                 }
49             }
50         }
51         return false;
52         
53     }

總結

以上是生活随笔為你收集整理的Bukkit之yaml动态读取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。