org.bukkit.persistence.PersistentDataAdapterContext Java Examples

The following examples show how to use org.bukkit.persistence.PersistentDataAdapterContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StringArrayItemTagType.java    From MineTinker with GNU General Public License v3.0 6 votes vote down vote up
@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]);
}
 
Example #2
Source File: WorldBorderDataTagType.java    From WorldBorderAPI with MIT License 6 votes vote down vote up
@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;
}
 
Example #3
Source File: WorldBorderDataTagType.java    From WorldBorderAPI with MIT License 6 votes vote down vote up
@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;
}
 
Example #4
Source File: EnumMapTagType.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@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];
}
 
Example #5
Source File: EnumMapTagType.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@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();
}
 
Example #6
Source File: UUIDTagType.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@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();
}
 
Example #7
Source File: UUIDTagType.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: DisplayItemPersistentDataType.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@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 "";
    }
}
 
Example #9
Source File: DisplayItemPersistentDataType.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
@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)));
    }
}
 
Example #10
Source File: UUIDTagType.java    From IF with The Unlicense 5 votes vote down vote up
@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();
}
 
Example #11
Source File: UUIDTagType.java    From IF with The Unlicense 5 votes vote down vote up
@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);
}