|
| 1 | +package me.flame.menus.components.nbt; |
| 2 | + |
| 3 | +import me.flame.menus.util.VersionHelper; |
| 4 | +import org.bukkit.inventory.ItemStack; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | + |
| 7 | +/** |
| 8 | + * Ideally this wouldn't need to be a util, but because of the {@link LegacyNbt} it makes it easier. Legacy |
| 9 | + * Will use the PDC wrapper if version is higher than 1.14 |
| 10 | + */ |
| 11 | +public final class ItemNbt { |
| 12 | + |
| 13 | + private static final NbtWrapper nbt = VersionHelper.IS_PDC_VERSION ? new Pdc() : new LegacyNbt(); |
| 14 | + |
| 15 | + /** |
| 16 | + * Sets an NBT tag to the an {@link ItemStack}. |
| 17 | + * |
| 18 | + * @param itemStack The current {@link ItemStack} to be set. |
| 19 | + * @param key The NBT key to use. |
| 20 | + * @param value The tag value to set. |
| 21 | + * @return An {@link ItemStack} that has NBT set. |
| 22 | + */ |
| 23 | + public static ItemStack setString(@NotNull final ItemStack itemStack, @NotNull final String key, @NotNull final String value) { |
| 24 | + return nbt.setString(itemStack, key, value); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets the NBT tag based on a given key. |
| 29 | + * |
| 30 | + * @param itemStack The {@link ItemStack} to get from. |
| 31 | + * @param key The key to look for. |
| 32 | + * @return The tag that was stored in the {@link ItemStack}. |
| 33 | + */ |
| 34 | + public static String getString(@NotNull final ItemStack itemStack, @NotNull final String key) { |
| 35 | + return nbt.getString(itemStack, key); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Sets a boolean to the {@link ItemStack}. |
| 40 | + * Mainly used for setting an item to be unbreakable on older versions. |
| 41 | + * |
| 42 | + * @param itemStack The {@link ItemStack} to set the boolean to. |
| 43 | + * @param key The key to use. |
| 44 | + * @param value The boolean value. |
| 45 | + * @return An {@link ItemStack} with a boolean value set. |
| 46 | + */ |
| 47 | + public static ItemStack setBoolean(@NotNull final ItemStack itemStack, @NotNull final String key, final boolean value) { |
| 48 | + return nbt.setBoolean(itemStack, key, value); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Removes a tag from an {@link ItemStack}. |
| 53 | + * |
| 54 | + * @param itemStack The current {@link ItemStack} to be removed. |
| 55 | + * @param key The NBT key to remove. |
| 56 | + * @return An {@link ItemStack} that has the tag removed. |
| 57 | + */ |
| 58 | + public static ItemStack removeTag(@NotNull final ItemStack itemStack, @NotNull final String key) { |
| 59 | + return nbt.removeTag(itemStack, key); |
| 60 | + } |
| 61 | +} |
0 commit comments