Added ExtendedPersistentDataContainer

This commit is contained in:
Auxilor
2022-04-22 20:44:59 +01:00
parent 9a3aac9a66
commit ef42e689ae
12 changed files with 331 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package com.willfp.eco.core;
import com.willfp.eco.core.config.updating.ConfigHandler;
import com.willfp.eco.core.config.wrapper.ConfigFactory;
import com.willfp.eco.core.data.ExtendedPersistentDataContainer;
import com.willfp.eco.core.data.ProfileHandler;
import com.willfp.eco.core.data.keys.KeyRegistry;
import com.willfp.eco.core.drops.DropQueueFactory;
@@ -23,6 +24,7 @@ import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -280,6 +282,14 @@ public interface Handler {
* @param <T> The mob type.
* @return The controlled entity.
*/
@NotNull <T extends Mob> EntityController<T> createEntityController(@NotNull T mob);
/**
* Adapt base PDC to extended PDC.
*
* @param container The container.
* @return The extended container.
*/
@NotNull
<T extends Mob> EntityController<T> createEntityController(@NotNull T mob);
ExtendedPersistentDataContainer adaptPdc(@NotNull PersistentDataContainer container);
}

View File

@@ -0,0 +1,83 @@
package com.willfp.eco.core.data;
import com.willfp.eco.core.Eco;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Set;
public interface ExtendedPersistentDataContainer extends PersistentDataContainer {
/**
* Set a key.
*
* @param key The key.
* @param dataType The data type.
* @param value The value.
* @param <T> The type.
* @param <Z> The type.
*/
<T, Z> void set(@NotNull String key, @NotNull PersistentDataType<T, Z> dataType, @NotNull Z value);
/**
* Get if there is a key.
*
* @param key The key.
* @param dataType The data type.
* @param <T> The type.
* @param <Z> The type.
* @return If the key is present.
*/
<T, Z> boolean has(@NotNull String key, @NotNull PersistentDataType<T, Z> dataType);
/**
* Get a value.
*
* @param key The key.
* @param dataType The data type.
* @param <T> The type.
* @param <Z> The type.
* @return The value, or null if not found.
*/
@Nullable <T, Z> Z get(@NotNull String key, @NotNull PersistentDataType<T, Z> dataType);
/**
* Get a value or default if not present.
*
* @param key The key.
* @param dataType The data type.
* @param defaultValue The default value.
* @param <T> The type.
* @param <Z> The type.
* @return The value, or the default if not found.
*/
@NotNull <T, Z> Z getOrDefault(@NotNull String key, @NotNull PersistentDataType<T, Z> dataType, @NotNull Z defaultValue);
/**
* Get all keys, including namespaced keys.
*
* @return The keys.
*/
@NotNull
Set<String> getAllKeys();
/**
* Remove a key.
*
* @param key The key.
*/
void remove(@NotNull String key);
/**
* Convert regular persistent data container to extended persistent data container.
* <p>
* Only use this with FastItemStack, you're likely to create problems otherwise.
*
* @param base The base container.
* @return The extended container.
*/
static ExtendedPersistentDataContainer wrap(@NotNull PersistentDataContainer base) {
return Eco.getHandler().adaptPdc(base);
}
}

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.core.fast;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.data.ExtendedPersistentDataContainer;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
@@ -203,7 +204,7 @@ public interface FastItemStack extends PersistentDataHolder {
*
* @return The base NBT tag.
*/
PersistentDataContainer getBaseTag();
ExtendedPersistentDataContainer getBaseTag();
/**
* Set the base NBT tag (Not PublicBukkitValues, the base) from a PersistentDataContainer.

View File

@@ -2,6 +2,7 @@ package com.willfp.eco.core.items;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.willfp.eco.core.data.ExtendedPersistentDataContainer;
import com.willfp.eco.core.fast.FastItemStack;
import com.willfp.eco.core.items.args.LookupArgParser;
import com.willfp.eco.core.items.provider.ItemProvider;
@@ -530,8 +531,8 @@ public final class Items {
public static ItemStack setDestroySpeedMultiplier(@NotNull final ItemStack itemStack,
final double multiplier) {
FastItemStack fis = FastItemStack.wrap(itemStack);
PersistentDataContainer tag = fis.getBaseTag();
tag.set(NamespacedKeyUtils.createEcoKey("break_speed"), PersistentDataType.DOUBLE, multiplier);
ExtendedPersistentDataContainer tag = fis.getBaseTag();
tag.set("BreakSpeed", PersistentDataType.DOUBLE, multiplier);
fis.setBaseTag(tag);
return fis.unwrap();
}
@@ -544,9 +545,9 @@ public final class Items {
*/
public static double getDestroySpeedMultiplier(@NotNull final ItemStack itemStack) {
FastItemStack fis = FastItemStack.wrap(itemStack);
PersistentDataContainer tag = fis.getBaseTag();
ExtendedPersistentDataContainer tag = fis.getBaseTag();
return Objects.requireNonNullElse(
tag.get(NamespacedKeyUtils.createEcoKey("break_speed"), PersistentDataType.DOUBLE),
tag.get("BreakSpeed", PersistentDataType.DOUBLE),
1.0
);
}