bukkit在1.14.1开始增加了几个读取nbt数据相关的类
-
Returns a custom tag container capable of storing tags on the object. Note that the tags stored on this container are all stored under their own custom namespace therefore modifying default tags using this PersistentDataHolder is impossible.
- 返回一个能够在该对象上存储标签的自定义标签容器。请注意,这个容器上的标签都存储在自己的自定义命名空间下,因此使用这个PersistentDataHolder修改默认标签是不可能的。
PersistentDataContainer
-
//Returns the metadata value that is stored on the PersistentDataHolder instance.
-
//返回指定键指定数据类型的值
-
<T,Z> Z get(NamespacedKey key, PersistentDataType<T,Z> type)
-
-
//Returns the adapter context this tag container uses.
-
//返回这个标签容器的适配器(类似模板一样的东西,比如实体与物品就是不一样的模板)
-
PersistentDataAdapterContext getAdapterContext()
-
-
//Get a set of keys present on this PersistentDataContainer instance.
-
//获取所有键
-
java.util.Set<NamespacedKey> getKeys()
-
-
//Returns the metadata value that is stored on the PersistentDataHolder instance.
-
//在get()的基础上指定默认值
-
<T,Z> Z getOrDefault(NamespacedKey key, PersistentDataType<T,Z> type, Z defaultValue)
-
-
//Returns if the persistent metadata provider has metadata registered matching the provided parameters.
-
//检查是否含有指定键值和类型的键
-
<T,Z> boolean has(NamespacedKey key, PersistentDataType<T,Z> type)
-
-
//Returns if the container instance is empty, therefore has no entries inside it.
-
//判空
-
boolean isEmpty()
-
-
//Removes a custom key from the PersistentDataHolder instance.
-
//删除一对键
-
void remove(NamespacedKey key)
-
-
//Stores a metadata value on the PersistentDataHolder instance.
-
//设置指定键值
- <T,Z> void set(NamespacedKey key, PersistentDataType<T,Z> type, Z value)
PersistentDataType<T,Z>
PersistentDataAdapterContext
-
//Creates a new and empty meta container instance.
-
//返回一个空的容器
- PersistentDataContainer newPersistentDataContainer()
实例
添加并读取物品自定义标签
-
//注:getPlugin() 方法返回的是继承JavaPlugin的主类
-
NamespacedKey MY_DATA = new NamespacedKey(getPlugin(), "my_data"); //注册键值,实际使用推荐用static存到类成员去
-
ItemStack item = new ItemStack(Material.DIAMOND_SWORD); //实验物品
-
-
//添加
-
ItemMeta itemMeta = item.getItemMeta(); //获取数据的复制
-
PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer(); //获取数据容器
-
String data = "Hello World!";
-
dataContainer.set(MY_DATA, PersistentDataType.STRING, data);//添加自定义标签
-
item.setItemMeta(itemMeta); //别忘了只是复制而已,需要设置回去
-
-
//读取
-
ItemMeta itemMeta = item.getItemMeta(); //重新获取新的具有自定义标签的meta
- String readData = itemMeta.getPersistentDataContainer().get(MY_DATA, PersistentDataType.STRING); //读取数据 PersistentDataType是什么类型就返回什么类型的数据
一个键值存入多对数据
-
//注:getPlugin() 方法返回的是继承JavaPlugin的主类
-
NamespacedKey MAIN_DATA = new NamespacedKey(getPlugin(), "main_data"); //注册主键值
-
NamespacedKey MY_DATA1 = new NamespacedKey(getPlugin(), "my_data1"); //注册键值2
-
NamespacedKey MY_DATA2 = new NamespacedKey(getPlugin(), "my_data2"); //注册键值3
-
ItemStack item = new ItemStack(Material.DIAMOND_SWORD); //实验物品
-
-
//添加
-
ItemMeta itemMeta = item.getItemMeta(); //获取数据的复制
-
PersistentDataContainer dataContainer = itemMeta.getPersistentDataContainer(); //获取数据容器
-
PersistentDataContainer newDataContainer = dataContainer.getAdapterContext().newPersistentDataContainer(); //生成一个新的容器
-
-
newDataContainer.set(MY_DATA1, PersistentDataType.STRING, "这是子键1"); //添加自定义子标签 String
-
newDataContainer.set(MY_DATA2, PersistentDataType.INTEGER, 666); //添加自定义子标签 Integer
-
-
dataContainer.set(MAIN_DATA, PersistentDataType.TAG_CONTAINER, newDataContainer); //把子键容器添加为主键
-
item.setItemMeta(itemMeta); //保存数据
-
-
//读取
-
//与上面类似
-
//使用 get(MAIN_DATA, PersistentDataType.TAG_CONTAINER); 然后当做一个容器来获取数据即可
- //可以使用PersistentDataContainer 里的getKeys()方法返回键,然后遍历键get数据 ......
暂时就这些,对于基本的使用也就这样了,此类可以一定程度上代替NBTAPI 这类库(很大,很不爽),唯一的缺点是不能修改原版的东西(文档里说不能)
还有有人说实体的修改之后重启服务端就没了(我没试验过),但是ItemStack我自己实验是不会丢失的。
如有错误和建议请不吝指出
