Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* MIT License
*
* Copyright (c) 2021 TriumphTeam
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package dev.triumphteam.gui.builder.item;
import com.google.common.base.Preconditions;
import dev.triumphteam.gui.components.GuiAction;
import dev.triumphteam.gui.components.exception.GuiException;
import dev.triumphteam.gui.components.util.ItemNbt;
import dev.triumphteam.gui.components.util.Legacy;
import dev.triumphteam.gui.components.util.VersionHelper;
import dev.triumphteam.gui.guis.GuiItem;
import net.kyori.adventure.platform.bukkit.MinecraftComponentSerializer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Contains all the common methods for the future ItemBuilders
*
* @param The ItemBuilder type so the methods can cast to the subtype
*/
@SuppressWarnings("unchecked")
public abstract class BaseItemBuilder> {
private static final EnumSet LEATHER_ARMOR = EnumSet.of(
Material.LEATHER_HELMET, Material.LEATHER_CHESTPLATE, Material.LEATHER_LEGGINGS, Material.LEATHER_BOOTS
);
private static final Field DISPLAY_NAME_FIELD;
private static final Field LORE_FIELD;
static {
try {
final Class> metaClass = VersionHelper.craftClass("inventory.CraftMetaItem");
DISPLAY_NAME_FIELD = metaClass.getDeclaredField("displayName");
DISPLAY_NAME_FIELD.setAccessible(true);
LORE_FIELD = metaClass.getDeclaredField("lore");
LORE_FIELD.setAccessible(true);
} catch (NoSuchFieldException | ClassNotFoundException exception) {
exception.printStackTrace();
throw new GuiException("Could not retrieve displayName nor lore field for ItemBuilder.");
}
}
private ItemStack itemStack;
private ItemMeta meta;
protected BaseItemBuilder(@NotNull final ItemStack itemStack) {
Preconditions.checkNotNull(itemStack, "Item can't be null!");
this.itemStack = itemStack;
meta = itemStack.hasItemMeta() ? itemStack.getItemMeta() : Bukkit.getItemFactory().getItemMeta(itemStack.getType());
}
/**
* Serializes the component with the right {@link net.kyori.adventure.text.serializer.ComponentSerializer} for the current MC version
*
* @param component component to serialize
* @return the serialized representation of the component
*/
protected @NotNull Object serializeComponent(@NotNull final Component component) {
if (VersionHelper.IS_ITEM_NAME_COMPONENT) {
//noinspection UnstableApiUsage
return MinecraftComponentSerializer.get().serialize(component);
} else {
return GsonComponentSerializer.gson().serialize(component);
}
}
/**
* Deserializes the object with the right {@link net.kyori.adventure.text.serializer.ComponentSerializer} for the current MC version
*
* @param obj object to deserialize
* @return the component
*/
protected @NotNull Component deserializeComponent(@NotNull final Object obj) {
if (VersionHelper.IS_ITEM_NAME_COMPONENT) {
//noinspection UnstableApiUsage
return MinecraftComponentSerializer.get().deserialize(obj);
} else {
return GsonComponentSerializer.gson().deserialize((String) obj);
}
}
/**
* Sets the display name of the item using {@link Component}
*
* @param name The {@link Component} name
* @return {@link ItemBuilder}
* @since 3.0.0
*/
@NotNull
@Contract("_ -> this")
public B name(@NotNull final Component name) {
if (meta == null) return (B) this;
if (VersionHelper.IS_COMPONENT_LEGACY) {
meta.setDisplayName(Legacy.SERIALIZER.serialize(name));
return (B) this;
}
try {
DISPLAY_NAME_FIELD.set(meta, this.serializeComponent(name));
} catch (IllegalAccessException exception) {
exception.printStackTrace();
}
return (B) this;
}
/**
* Sets the amount of items
*
* @param amount the amount of items
* @return {@link ItemBuilder}
* @since 3.0.0
*/
@NotNull
@Contract("_ -> this")
public B amount(final int amount) {
itemStack.setAmount(amount);
return (B) this;
}
/**
* Set the lore lines of an item
*
* @param lore Lore lines as varargs
* @return {@link ItemBuilder}
* @since 3.0.0
*/
@NotNull
@Contract("_ -> this")
public B lore(@Nullable final Component @NotNull ... lore) {
return lore(Arrays.asList(lore));
}
/**
* Set the lore lines of an item
*
* @param lore A {@link List} with the lore lines
* @return {@link ItemBuilder}
* @since 3.0.0
*/
@NotNull
@Contract("_ -> this")
public B lore(@NotNull final List<@Nullable Component> lore) {
if (meta == null) return (B) this;
if (VersionHelper.IS_COMPONENT_LEGACY) {
meta.setLore(lore.stream().filter(Objects::nonNull).map(Legacy.SERIALIZER::serialize).collect(Collectors.toList()));
return (B) this;
}
final List