Bukkit之yaml动态读取
在使用bukkit框架写插件的时候会经常使用到yml格式的文件来存储配置或者玩家数据,这里来说一下实现yml中数据的动态读写:
先来看一下yml文件中的内容结构
public boolean addBanSyntheseItem(CommandSender sender, Command cmd, String[] args) {
List list = new ArrayList<>();
Player player = (Player) sender;
ItemStack itemStack = player.getItemInHand();
File airdropFile = new File(Pickaxe.CONFIGPATH);
if(airdropFile.exists()) {
YamlConfiguration yc = new YamlConfiguration();
try {
yc.load(airdropFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}
Set set = yc.getConfigurationSection("banItemList").getKeys(false);
int count = set.size();
//如果yml配置文件是空的,创建根节点,并且添加内容
if(count == 0) {
ConfigurationSection listSection = yc.createSection("banItemList");
Map<String, Object> item = new HashMap();
item.put("id",itemStack.getTypeId());
item.put("durability", (int)itemStack.getDurability());
item.put("type", itemStack.getType().toString());
item.put("displayName", itemStack.getItemMeta().getDisplayName());
item.put("lore", itemStack.getItemMeta().getLore());
listSection.createSection(Integer.toString(itemStack.getTypeId()),item);
try {
yc.save(Pickaxe.CONFIGPATH);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}else if(count>0) { //如果yml中有内容,这直接在其后面追加内容
ConfigurationSection section = yc.getConfigurationSection("banItemList");
Map<String, Object> item = new HashMap();
item.put("id",itemStack.getTypeId());
item.put("durability", (int)itemStack.getDurability());
item.put("type", itemStack.getType().toString());
item.put("displayName", itemStack.getItemMeta().getDisplayName());
item.put("lore", itemStack.getItemMeta().getLore());
section.createSection(Integer.toString(itemStack.getTypeId()),item);
try {
yc.save(Pickaxe.CONFIGPATH);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return false; }
Bukkit之yaml动态读取的更多相关文章
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
前言: 最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容.那么从哪入手呢?因为用的是Unity3d的游戏引擎,思来想去就选择了C#读取XML文件这个小功能 ...
- Unity3D移动平台动态读取外部文件全解析
前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖,总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑 ...
- WinForm开发框架--动态读取DLL模式
1\ WinForm开发框架--动态读取DLL模式 http://www.2cto.com/kf/201306/217199.html 2\ 广州爱奇迪 http://www.iqidi. ...
- php动态读取数据清除最右边距
需求效果一行3栏: 场景模拟:同事给了我这么一段静态代码如下: <!DOCTYPE html> <html lang="en"> <head> ...
- (转)Unity3D移动平台动态读取外部文件全解析
Unity3D移动平台动态读取外部文件全解析 c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一直有 ...
- js对象中动态读取属性值 动态属性值 js正则表达式全局替换
$(document).ready(function(){ var exceptionMsg = '${exception.message }'; var exceptionstr = ''; //j ...
- SparkStreaming动态读取配置文件
SparkStreaming动态读取配置文件 标签: SparkStreaming HDFS 配置文件 MySql 需求 要实现SparkStreaming在流处理过程中能动态的获取到配置文件的改变 ...
- 慕容小匹夫 Unity3D移动平台动态读取外部文件全解析
Unity3D移动平台动态读取外部文件全解析 c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一 ...
- 细说Unity3D(一)——移动平台动态读取外部文件全解析
前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑和 ...
随机推荐
- HIVE metastore Duplicate key name 'PCS_STATS_IDX' (state=42000,code=1061)
HDP 版本:2.4.0.0-169. 解决:将hive 所在 节点上的/usr/hdp/2.4.0.0-169/hive/script/metastore/upgrade/msql/hive-sch ...
- 系统编码,文件编码,python编码
系统编码,可以通过locale命令查看(LINUX)https://wiki.archlinux.org/index.php/Locale_(简体中文), centos7 配置文件在/etc/prof ...
- SAP S4HANA1610/Fiori安装过程全记录
经历各种坑,从硬件到文件,终于安装成功. 有需要安装或使用S4HANA(含Fiori)的同学可以参考. 安装文件分享给大家 链接:http://pan.baidu.com/s/1mi7LfIS 密码: ...
- 程序员自己编写的类和JDK类是一种合作关系。
封装类: JAVA为每一个简单数据类型提供了一个封装类,使每个简单数据类型可以被Object来装载. 除了int和char,其余类型首字母大写即成封装类. 转换字符的方式: int I=10; Str ...
- java面试中被问到的问题
技术面 1) Spring 的注解有哪些? 2) 你如何理解 spring IOC 技术,如果不使用spring,你有哪些方式来加载 bean? 3) spring aop 的原理是什么?它是怎样一 ...
- geoserver 图层样式
1.Styled Layer Descriptor 标准描述了稳当的结构合使用规则.一个文档包含了符号定义和绘制规则,那么这个文档就叫做Styled Layer Desciptor(SLD)样式,它是 ...
- 超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)
https://blog.csdn.net/u014732824/article/details/84952848 int i = System.Text.Encoding.Default.GetBy ...
- fidder(介绍)
处处用到网络抓包 “君子生非异也,善假于物也”,要做好测试很多时候光靠自己是远远不够的,还要会利用测试工具协助测试,这样才能事半功倍. 测试工具多种多样,会在本系列中一一介绍,今天着重介绍网络抓包工具 ...
- B - Assignment
Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this com ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...