org.bukkit.persistence.PersistentDataContainer Java Examples

The following examples show how to use org.bukkit.persistence.PersistentDataContainer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LugolsIodinePotion.java    From CraftserveRadiation with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: WorldBorderDataTagType.java    From WorldBorderAPI with MIT License 6 votes vote down vote up
@Override
public WorldBorderData fromPrimitive(PersistentDataContainer primitive, PersistentDataAdapterContext context) {

    WorldBorderData worldBorderData = new WorldBorderData();

    get(primitive, sizeKey, PersistentDataType.DOUBLE).ifPresent(worldBorderData::setSize);
    Optional<Double> centerX = get(primitive, xKey, PersistentDataType.DOUBLE);
    Optional<Double> centerZ = get(primitive, zKey, PersistentDataType.DOUBLE);
    if (centerX.isPresent() && centerZ.isPresent()) {
        worldBorderData.setCenter(centerX.get(), centerZ.get());
    }
    get(primitive, damageBufferInBlocksKey, PersistentDataType.DOUBLE).ifPresent(worldBorderData::setDamageBuffer);
    get(primitive, damageAmountKey, PersistentDataType.DOUBLE).ifPresent(worldBorderData::setDamageAmount);
    get(primitive, warningTimeSecondsKey, PersistentDataType.INTEGER).ifPresent(worldBorderData::setWarningTimeSeconds);
    get(primitive, warningDistanceKey, PersistentDataType.INTEGER).ifPresent(worldBorderData::setWarningDistance);

    return worldBorderData;
}
 
Example #3
Source File: WorldBorderDataTagType.java    From WorldBorderAPI with MIT License 6 votes vote down vote up
@Override
public PersistentDataContainer toPrimitive(WorldBorderData complex, PersistentDataAdapterContext context) {
    PersistentDataContainer persistentDataContainer = context.newPersistentDataContainer();

    persistentDataContainer.set(sizeKey, PersistentDataType.DOUBLE, complex.getSize());
    complex.applyCenter((x, z) -> {
        persistentDataContainer.set(xKey, PersistentDataType.DOUBLE, x);
        persistentDataContainer.set(zKey, PersistentDataType.DOUBLE, z);
    });
    persistentDataContainer.set(damageBufferInBlocksKey, PersistentDataType.DOUBLE, complex.getDamageBuffer());
    persistentDataContainer.set(damageAmountKey, PersistentDataType.DOUBLE, complex.getDamageAmount());
    persistentDataContainer.set(warningTimeSecondsKey, PersistentDataType.INTEGER, complex.getWarningTimeSeconds());
    persistentDataContainer.set(warningDistanceKey, PersistentDataType.INTEGER, complex.getWarningDistance());

    return persistentDataContainer;
}
 
Example #4
Source File: APIUtils.java    From BedWars with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @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);
}
 
Example #5
Source File: APIUtils.java    From BedWars with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @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);
}
 
Example #6
Source File: LugolsIodineEffect.java    From CraftserveRadiation with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #7
Source File: LugolsIodinePotion.java    From CraftserveRadiation with Apache License 2.0 6 votes vote down vote up
public PotionMeta convert(PotionMeta potionMeta) {
    Objects.requireNonNull(potionMeta, "potionMeta");

    Duration duration = this.config.duration();
    String formattedDuration = this.formatDuration(this.config.duration());

    if (this.config.color != null) {
        potionMeta.setColor(this.config.color);
    }
    potionMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
    potionMeta.setDisplayName(ChatColor.AQUA + this.config.name());
    potionMeta.setLore(Collections.singletonList(ChatColor.BLUE + MessageFormat.format(this.config.description(), formattedDuration)));

    PersistentDataContainer container = potionMeta.getPersistentDataContainer();
    container.set(this.potionKey, PersistentDataType.BYTE, TRUE);
    container.set(this.durationSecondsKey, PersistentDataType.INTEGER, (int) duration.getSeconds());
    return potionMeta;
}
 
Example #8
Source File: LugolsIodinePotion.java    From CraftserveRadiation with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #9
Source File: SlimefunUtils.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Toggles an {@link ItemStack} to be Soulbound.<br>
 * If true is passed, this will add the {@link #SOULBOUND_LORE} and
 * add a {@link NamespacedKey} to the item so it can be quickly identified
 * by {@link #isSoulbound(ItemStack)}.<br>
 * If false is passed, this property will be removed.
 *
 * @param item
 *            The {@link ItemStack} you want to add/remove Soulbound from.
 * @param makeSoulbound
 *            If they item should be soulbound.
 * 
 * @see #isSoulbound(ItemStack)
 */
public static void setSoulbound(ItemStack item, boolean makeSoulbound) {
    if (item == null || item.getType() == Material.AIR) {
        throw new IllegalArgumentException("A soulbound item cannot be null or air!");
    }

    boolean isSoulbound = isSoulbound(item);
    ItemMeta meta = item.getItemMeta();

    if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
        PersistentDataContainer container = meta.getPersistentDataContainer();

        if (makeSoulbound && !isSoulbound) {
            container.set(SOULBOUND_KEY, PersistentDataType.BYTE, (byte) 1);
        }

        if (!makeSoulbound && isSoulbound) {
            container.remove(SOULBOUND_KEY);
        }
    }

    List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();

    if (makeSoulbound && !isSoulbound) {
        lore.add(SOULBOUND_LORE);
    }

    if (!makeSoulbound && isSoulbound) {
        lore.remove(SOULBOUND_LORE);
    }

    meta.setLore(lore);
    item.setItemMeta(meta);
}
 
Example #10
Source File: PersistenceWrapper.java    From WorldBorderAPI with MIT License 5 votes vote down vote up
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);
}
 
Example #11
Source File: PersistenceWrapper.java    From WorldBorderAPI with MIT License 5 votes vote down vote up
@Override
public WorldBorderData getWorldBorderData(Player p) {
    PersistentDataContainer persistentDataContainer = p.getPersistentDataContainer();
    if (persistentDataContainer.has(worldBorderDataKey, worldBorderDataTagType)) {
        return persistentDataContainer.get(worldBorderDataKey, worldBorderDataTagType);
    }
    return null;
}
 
Example #12
Source File: PersistenceWrapper.java    From WorldBorderAPI with MIT License 5 votes vote down vote up
@Override
public void resetWorldBorderToGlobal(Player player) {
    worldBorderApi.resetWorldBorderToGlobal(player);
    PersistentDataContainer persistentDataContainer = player.getPersistentDataContainer();
    if (persistentDataContainer.has(worldBorderDataKey, worldBorderDataTagType)) {
        persistentDataContainer.remove(worldBorderDataKey);
    }
}
 
Example #13
Source File: PersistenceWrapper.java    From WorldBorderAPI with MIT License 5 votes vote down vote up
@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;
}
 
Example #14
Source File: PersistentDataService.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
default Optional<String> getString(Object obj, NamespacedKey key) {
    if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14) && obj instanceof PersistentDataHolder) {
        PersistentDataContainer container = ((PersistentDataHolder) obj).getPersistentDataContainer();
        return Optional.ofNullable(container.get(key, PersistentDataType.STRING));
    }

    return Optional.empty();
}
 
Example #15
Source File: LugolsIodineEffect.java    From CraftserveRadiation with Apache License 2.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMilkBucketConsume(PlayerItemConsumeEvent event) {
    if (event.getItem().getType().equals(Material.MILK_BUCKET)) {
        PersistentDataContainer container = event.getPlayer().getPersistentDataContainer();
        container.remove(this.initialSecondsKey);
        container.remove(this.secondsLeftKey);
    }
}
 
Example #16
Source File: SlimefunUtils.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #17
Source File: LugolsIodineEffect.java    From CraftserveRadiation with Apache License 2.0 5 votes vote down vote up
public Effect getEffect(Entity entity) {
    Objects.requireNonNull(entity, "entity");

    PersistentDataContainer container = entity.getPersistentDataContainer();
    int initialSeconds = container.getOrDefault(this.initialSecondsKey, PersistentDataType.INTEGER, -1);
    int secondsLeft = container.getOrDefault(this.secondsLeftKey, PersistentDataType.INTEGER, -1);

    if (initialSeconds != -1 && secondsLeft != -1) {
        return new Effect(initialSeconds, secondsLeft);
    }

    return null;
}
 
Example #18
Source File: DataHandler.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
public static void removeTag(@NotNull ItemStack item, @NotNull String key, boolean useMinecraft) {
    ItemMeta meta = item.getItemMeta();
    if (meta == null) return;
    PersistentDataContainer container = meta.getPersistentDataContainer();

    container.remove((useMinecraft ? NamespacedKey.minecraft(key) : new NamespacedKey(MineTinker.getPlugin(), key)));
    item.setItemMeta(meta);
}
 
Example #19
Source File: DataHandler.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
public static <T, Z> @Nullable Z getTag(@NotNull ItemStack item, @NotNull String key, PersistentDataType<T, Z> dataType, boolean useMinecraft) {
    ItemMeta meta = item.getItemMeta();
    if (meta == null) return null;

    PersistentDataContainer container = meta.getPersistentDataContainer();
    return container.get((useMinecraft ? NamespacedKey.minecraft(key) : new NamespacedKey(MineTinker.getPlugin(), key)), dataType);
}
 
Example #20
Source File: DataHandler.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
public static <T, Z> void setTag(@NotNull ItemStack item, @NotNull String key, Z value, PersistentDataType<T, Z> dataType, boolean useMinecraft) {
    ItemMeta meta = item.getItemMeta();
    if (meta == null) return;

    PersistentDataContainer container = meta.getPersistentDataContainer();
    container.set((useMinecraft ? NamespacedKey.minecraft(key) : new NamespacedKey(MineTinker.getPlugin(), key)), dataType, value);
    item.setItemMeta(meta);
}
 
Example #21
Source File: DataHandler.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #22
Source File: LugolsIodineEffect.java    From CraftserveRadiation with Apache License 2.0 5 votes vote down vote up
public void setEffect(Entity entity, Effect effect) {
    Objects.requireNonNull(entity, "entity");
    Objects.requireNonNull(effect, "effect");

    PersistentDataContainer container = entity.getPersistentDataContainer();
    container.set(this.initialSecondsKey, PersistentDataType.INTEGER, effect.initialSeconds);
    container.set(this.secondsLeftKey, PersistentDataType.INTEGER, effect.secondsLeft);
}
 
Example #23
Source File: WorldBorderDataTagType.java    From WorldBorderAPI with MIT License 4 votes vote down vote up
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();
}
 
Example #24
Source File: WorldBorderDataTagType.java    From WorldBorderAPI with MIT License 4 votes vote down vote up
@Override
public Class<PersistentDataContainer> getPrimitiveType() {
    return PersistentDataContainer.class;
}
 
Example #25
Source File: PersistentDataService.java    From Slimefun4 with GNU General Public License v3.0 4 votes vote down vote up
default void setString(Object obj, NamespacedKey key, String value) {
    if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14) && obj instanceof PersistentDataHolder) {
        PersistentDataContainer container = ((PersistentDataHolder) obj).getPersistentDataContainer();
        container.set(key, PersistentDataType.STRING, value);
    }
}