9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2026-01-04 15:31:37 +00:00

Correct Persistent Data serialization

This commit is contained in:
William
2022-07-21 14:23:27 +01:00
parent 31552f85e4
commit 8847483ff8
7 changed files with 204 additions and 103 deletions

View File

@@ -1,60 +0,0 @@
package net.william278.husksync.data;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
/**
* Represents the type of persistent data tag, implemented by a Bukkit PersistentDataType.
*/
public enum BukkitPersistentDataTagType {
BYTE(PersistentDataType.BYTE),
SHORT(PersistentDataType.SHORT),
INTEGER(PersistentDataType.INTEGER),
LONG(PersistentDataType.LONG),
FLOAT(PersistentDataType.FLOAT),
DOUBLE(PersistentDataType.DOUBLE),
STRING(PersistentDataType.STRING),
BYTE_ARRAY(PersistentDataType.BYTE_ARRAY),
INTEGER_ARRAY(PersistentDataType.INTEGER_ARRAY),
LONG_ARRAY(PersistentDataType.LONG_ARRAY),
TAG_CONTAINER_ARRAY(PersistentDataType.TAG_CONTAINER_ARRAY),
TAG_CONTAINER(PersistentDataType.TAG_CONTAINER);
public final PersistentDataType<?, ?> dataType;
BukkitPersistentDataTagType(PersistentDataType<?, ?> persistentDataType) {
this.dataType = persistentDataType;
}
public static Optional<BukkitPersistentDataTagType> getDataType(@NotNull String typeName) {
for (BukkitPersistentDataTagType type : values()) {
if (type.name().equalsIgnoreCase(typeName)) {
return Optional.of(type);
}
}
return Optional.empty();
}
/**
* Determine the {@link BukkitPersistentDataTagType} of a tag in a {@link PersistentDataContainer}.
*
* @param container The {@link PersistentDataContainer} to check.
* @param key The {@link NamespacedKey} of the tag to check.
* @return The {@link BukkitPersistentDataTagType} of the key, or {@link Optional#empty()} if the key does not exist.
*/
public static Optional<BukkitPersistentDataTagType> getKeyDataType(@NotNull PersistentDataContainer container,
@NotNull NamespacedKey key) {
for (BukkitPersistentDataTagType dataType : values()) {
if (container.has(key, dataType.dataType)) {
return Optional.of(dataType);
}
}
return Optional.empty();
}
}