下面列出了org.bukkit.persistence.PersistentDataContainer#has ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean test(ItemStack itemStack) {
Objects.requireNonNull(itemStack, "itemStack");
if (!itemStack.hasItemMeta()) {
return false;
}
PersistentDataContainer container = itemStack.getItemMeta().getPersistentDataContainer();
if (container.has(this.potionKey, PersistentDataType.BYTE)) {
Byte value = container.get(this.potionKey, PersistentDataType.BYTE);
return value != null && value == TRUE;
}
return false;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem();
if (!this.test(item)) {
return;
}
PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer();
int durationSeconds = 0;
if (container.has(this.durationSecondsKey, PersistentDataType.INTEGER)) {
durationSeconds = container.getOrDefault(this.durationSecondsKey, PersistentDataType.INTEGER, 0);
} else if (container.has(this.durationKey, PersistentDataType.INTEGER)) {
durationSeconds = (int) TimeUnit.MINUTES.toSeconds(container.getOrDefault(this.durationKey, PersistentDataType.INTEGER, 0)); // legacy
}
if (durationSeconds <= 0) {
return;
}
Player player = event.getPlayer();
this.effect.setEffect(player, durationSeconds);
this.broadcastConsumption(player, durationSeconds);
}
void tick(Player player) {
Objects.requireNonNull(player, "player");
PersistentDataContainer container = player.getPersistentDataContainer();
if (!container.has(secondsLeftKey, PersistentDataType.INTEGER)) {
return;
}
Integer secondsLeft = container.getOrDefault(secondsLeftKey, PersistentDataType.INTEGER, 0);
if (secondsLeft > 0) {
container.set(secondsLeftKey, PersistentDataType.INTEGER, --secondsLeft);
} else {
container.remove(initialSecondsKey);
container.remove(secondsLeftKey);
}
}
/**
* @param stack
* @param hash
*/
public static void hashIntoInvisibleString(ItemStack stack, String hash) {
ItemMeta meta = stack.getItemMeta();
try {
NamespacedKey key = new NamespacedKey((Plugin) BedwarsAPI.getInstance(), BEDWARS_NAMESPACED_KEY);
PersistentDataContainer container = meta.getPersistentDataContainer();
List<String> propertyLines = new ArrayList<>();
if (container.has(key, PersistentDataType.STRING)) {
String oldString = container.get(key, PersistentDataType.STRING);
propertyLines.addAll((List<String>) new Gson().fromJson(oldString, List.class));
}
propertyLines.add(hash);
container.set(key, PersistentDataType.STRING, new Gson().toJson(propertyLines));
} catch (Throwable ignored) {
// Use the Lore API instead
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
lore.add(convertToInvisibleString(hash));
meta.setLore(lore);
}
stack.setItemMeta(meta);
}
/**
* @param stack
* @param hash
*/
public static void hashIntoInvisibleString(ItemStack stack, String hash) {
ItemMeta meta = stack.getItemMeta();
try {
NamespacedKey key = new NamespacedKey((Plugin) BedwarsAPI.getInstance(), BEDWARS_NAMESPACED_KEY);
PersistentDataContainer container = meta.getPersistentDataContainer();
List<String> propertyLines = new ArrayList<>();
if (container.has(key, PersistentDataType.STRING)) {
String oldString = container.get(key, PersistentDataType.STRING);
propertyLines.addAll((List<String>) new Gson().fromJson(oldString, List.class));
}
propertyLines.add(hash);
container.set(key, PersistentDataType.STRING, new Gson().toJson(propertyLines));
} catch (Throwable ignored) {
// Use the Lore API instead
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
lore.add(convertToInvisibleString(hash));
meta.setLore(lore);
}
stack.setItemMeta(meta);
}
public static <T, Z> boolean hasTag(@NotNull ItemStack item, @NotNull String key, PersistentDataType<T, Z> dataType, boolean useMinecraft) {
ItemMeta meta = item.getItemMeta();
if (meta == null) return false;
PersistentDataContainer container = meta.getPersistentDataContainer();
return container.has((useMinecraft ? NamespacedKey.minecraft(key) : new NamespacedKey(MineTinker.getPlugin(), key)), dataType);
}
/**
* This method checks whether the given {@link ItemStack} is considered {@link Soulbound}.
*
* @param item
* The {@link ItemStack} to check for
* @return Whether the given item is soulbound
*/
public static boolean isSoulbound(ItemStack item) {
if (item == null || item.getType() == Material.AIR) {
return false;
}
else {
ItemMeta meta = item.hasItemMeta() ? item.getItemMeta() : null;
if (meta != null && SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
PersistentDataContainer container = meta.getPersistentDataContainer();
if (container.has(SOULBOUND_KEY, PersistentDataType.BYTE)) {
return true;
}
}
if (SlimefunPlugin.getThirdPartySupportService().isEmeraldEnchantsInstalled()) {
// We wanna operate on a copy now
item = item.clone();
for (ItemEnchantment enchantment : EmeraldEnchants.getInstance().getRegistry().getEnchantments(item)) {
EmeraldEnchants.getInstance().getRegistry().applyEnchantment(item, enchantment.getEnchantment(), 0);
}
}
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem instanceof Soulbound) {
return !sfItem.isDisabled();
}
else if (meta != null) {
return meta.hasLore() && meta.getLore().contains(SOULBOUND_LORE);
}
return false;
}
}
@Override
public IWorldBorder getWorldBorder(Player p) {
IWorldBorder worldBorder = worldBorderApi.getWorldBorder(p);
PersistentDataContainer persistentDataContainer = p.getPersistentDataContainer();
if (persistentDataContainer.has(worldBorderDataKey, worldBorderDataTagType)) {
applyWorldDataToWorldBorder(worldBorder, persistentDataContainer.get(worldBorderDataKey, worldBorderDataTagType));
}
return worldBorder;
}
@Override
public void resetWorldBorderToGlobal(Player player) {
worldBorderApi.resetWorldBorderToGlobal(player);
PersistentDataContainer persistentDataContainer = player.getPersistentDataContainer();
if (persistentDataContainer.has(worldBorderDataKey, worldBorderDataTagType)) {
persistentDataContainer.remove(worldBorderDataKey);
}
}
@Override
public WorldBorderData getWorldBorderData(Player p) {
PersistentDataContainer persistentDataContainer = p.getPersistentDataContainer();
if (persistentDataContainer.has(worldBorderDataKey, worldBorderDataTagType)) {
return persistentDataContainer.get(worldBorderDataKey, worldBorderDataTagType);
}
return null;
}
private void modifyAndUpdateWorldData(Player player, Consumer<WorldBorderData> worldBorderDataConsumer) {
PersistentDataContainer persistentDataContainer = player.getPersistentDataContainer();
WorldBorderData worldBorderData = new WorldBorderData();
if (persistentDataContainer.has(worldBorderDataKey, worldBorderDataTagType)) {
worldBorderData = persistentDataContainer.get(worldBorderDataKey, worldBorderDataTagType);
}
worldBorderDataConsumer.accept(worldBorderData);
persistentDataContainer.set(worldBorderDataKey, worldBorderDataTagType, worldBorderData);
}
private <T, Z> Optional<Z> get(PersistentDataContainer persistentDataContainer, NamespacedKey namespacedKey, PersistentDataType<T, Z> type) {
if (persistentDataContainer.has(namespacedKey, type)) {
return Optional.ofNullable(persistentDataContainer.get(namespacedKey, type));
}
return Optional.empty();
}