下面列出了怎么用org.bukkit.persistence.PersistentDataAdapterContext的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public String @NotNull [] fromPrimitive(@NotNull byte @NotNull [] bytes, @NotNull PersistentDataAdapterContext itemTagAdapterContext) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
ArrayList<String> list = new ArrayList<>();
while (buffer.remaining() > 0) {
if (buffer.remaining() < 4) break;
int stringLength = buffer.getInt();
if (buffer.remaining() < stringLength) break;
byte[] stringBytes = new byte[stringLength];
buffer.get(stringBytes);
list.add(new String(stringBytes, charset));
}
return list.toArray(new String[0]);
}
@Override
public PersistentDataContainer toPrimitive(WorldBorderData complex, PersistentDataAdapterContext context) {
PersistentDataContainer persistentDataContainer = context.newPersistentDataContainer();
persistentDataContainer.set(sizeKey, PersistentDataType.DOUBLE, complex.getSize());
complex.applyCenter((x, z) -> {
persistentDataContainer.set(xKey, PersistentDataType.DOUBLE, x);
persistentDataContainer.set(zKey, PersistentDataType.DOUBLE, z);
});
persistentDataContainer.set(damageBufferInBlocksKey, PersistentDataType.DOUBLE, complex.getDamageBuffer());
persistentDataContainer.set(damageAmountKey, PersistentDataType.DOUBLE, complex.getDamageAmount());
persistentDataContainer.set(warningTimeSecondsKey, PersistentDataType.INTEGER, complex.getWarningTimeSeconds());
persistentDataContainer.set(warningDistanceKey, PersistentDataType.INTEGER, complex.getWarningDistance());
return persistentDataContainer;
}
@Override
public WorldBorderData fromPrimitive(PersistentDataContainer primitive, PersistentDataAdapterContext context) {
WorldBorderData worldBorderData = new WorldBorderData();
get(primitive, sizeKey, PersistentDataType.DOUBLE).ifPresent(worldBorderData::setSize);
Optional<Double> centerX = get(primitive, xKey, PersistentDataType.DOUBLE);
Optional<Double> centerZ = get(primitive, zKey, PersistentDataType.DOUBLE);
if (centerX.isPresent() && centerZ.isPresent()) {
worldBorderData.setCenter(centerX.get(), centerZ.get());
}
get(primitive, damageBufferInBlocksKey, PersistentDataType.DOUBLE).ifPresent(worldBorderData::setDamageBuffer);
get(primitive, damageAmountKey, PersistentDataType.DOUBLE).ifPresent(worldBorderData::setDamageAmount);
get(primitive, warningTimeSecondsKey, PersistentDataType.INTEGER).ifPresent(worldBorderData::setWarningTimeSeconds);
get(primitive, warningDistanceKey, PersistentDataType.INTEGER).ifPresent(worldBorderData::setWarningDistance);
return worldBorderData;
}
@Override
public byte @NotNull [] toPrimitive(@NotNull EnumMap<K, V> map, @NotNull PersistentDataAdapterContext itemTagAdapterContext) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(map);
return byteOut.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return new byte[0];
}
@Override
public @NotNull EnumMap<K, V> fromPrimitive(@NotNull byte @NotNull [] bytes, @NotNull PersistentDataAdapterContext itemTagAdapterContext) {
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
try {
ObjectInputStream in = new ObjectInputStream(byteIn);
return (EnumMap<K, V>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return this.reference.clone();
}
@Override
public byte @NotNull [] toPrimitive(UUID complex, @NotNull PersistentDataAdapterContext context) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(complex.getMostSignificantBits());
bb.putLong(complex.getLeastSignificantBits());
return bb.array();
}
@Override
public @NotNull UUID fromPrimitive(byte @NotNull [] primitive, @NotNull PersistentDataAdapterContext context) {
ByteBuffer bb = ByteBuffer.wrap(primitive);
long firstLong = bb.getLong();
long secondLong = bb.getLong();
return new UUID(firstLong, secondLong);
}
@NotNull
@Override
public String toPrimitive(
@NotNull ShopProtectionFlag complex, @NotNull PersistentDataAdapterContext context) {
try {
return gson.toJson(complex);
} catch (Throwable th) {
new RuntimeException("Cannot to toPrimitive the shop protection flag.").printStackTrace();
return "";
}
}
@NotNull
@Override
public ShopProtectionFlag fromPrimitive(
@NotNull String primitive, @NotNull PersistentDataAdapterContext context) {
try {
return gson.fromJson(primitive, ShopProtectionFlag.class);
} catch (Throwable th) {
new RuntimeException("Cannot to fromPrimitive the shop protection flag.").printStackTrace();
return new ShopProtectionFlag("", Util.serialize(new ItemStack(Material.STONE)));
}
}
@NotNull
@Override
public byte[] toPrimitive(@NotNull UUID complex, @NotNull PersistentDataAdapterContext context) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
buffer.putLong(complex.getMostSignificantBits());
buffer.putLong(complex.getLeastSignificantBits());
return buffer.array();
}
@NotNull
@Override
public UUID fromPrimitive(@NotNull byte[] primitive, @NotNull PersistentDataAdapterContext context) {
ByteBuffer buffer = ByteBuffer.wrap(primitive);
long most = buffer.getLong();
long least = buffer.getLong();
return new UUID(most, least);
}