#857: Add boolean PersistentDataType

By: Jishuna <joshl5324@gmail.com>
This commit is contained in:
Bukkit/Spigot 2023-05-27 10:45:49 +10:00
parent 3ccdb11aab
commit 4771f99d1d

View File

@ -55,6 +55,16 @@ public interface PersistentDataType<T, Z> {
PersistentDataType<Float, Float> FLOAT = new PrimitivePersistentDataType<>(Float.class);
PersistentDataType<Double, Double> DOUBLE = new PrimitivePersistentDataType<>(Double.class);
/*
Boolean.
*/
/**
* A convenience implementation to convert between Byte and Boolean as there is
* no native implementation for booleans. <br>
* Any byte value > 0 is considered to be true.
*/
PersistentDataType<Byte, Boolean> BOOLEAN = new BooleanPersistentDataType();
/*
String.
*/
@ -155,4 +165,36 @@ public interface PersistentDataType<T, Z> {
return primitive;
}
}
/**
* A convenience implementation to convert between Byte and Boolean as there is
* no native implementation for booleans. <br>
* Any byte value > 0 is considered to be true.
*/
class BooleanPersistentDataType implements PersistentDataType<Byte, Boolean> {
@NotNull
@Override
public Class<Byte> getPrimitiveType() {
return byte.class;
}
@NotNull
@Override
public Class<Boolean> getComplexType() {
return boolean.class;
}
@NotNull
@Override
public Byte toPrimitive(@NotNull Boolean complex, @NotNull PersistentDataAdapterContext context) {
return (byte) (complex ? 1 : 0);
}
@NotNull
@Override
public Boolean fromPrimitive(@NotNull Byte primitive, @NotNull PersistentDataAdapterContext context) {
return primitive != 0;
}
}
}