9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-30 20:29:28 +00:00

Fix PersistentDataContainer synchronisation, bump to v2.0.2

This commit is contained in:
William
2022-07-19 11:26:56 +01:00
parent 4663842946
commit dafbcad10e
6 changed files with 141 additions and 15 deletions

View File

@@ -0,0 +1,60 @@
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();
}
}