From f8b25e866d26a4918fdba9a3b67e850b74a5a96b Mon Sep 17 00:00:00 2001
From: RONiN <69498033+RealRONiN@users.noreply.github.com>
Date: Wed, 17 Aug 2022 19:52:22 +0530
Subject: [PATCH] feat: item builder
---
.../arcadelabs/labaide/item/ItemBuilder.java | 209 ++++++++++++++++++
1 file changed, 209 insertions(+)
create mode 100644 src/main/java/in/arcadelabs/labaide/item/ItemBuilder.java
diff --git a/src/main/java/in/arcadelabs/labaide/item/ItemBuilder.java b/src/main/java/in/arcadelabs/labaide/item/ItemBuilder.java
new file mode 100644
index 0000000..f1da8a9
--- /dev/null
+++ b/src/main/java/in/arcadelabs/labaide/item/ItemBuilder.java
@@ -0,0 +1,209 @@
+/*
+ * LabAide - Common utility library for our products.
+ * Copyright (C) 2022 ArcadeLabs Production.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+package in.arcadelabs.labaide.item;
+
+import com.destroystokyo.paper.Namespaced;
+import com.google.common.collect.Multimap;
+import net.kyori.adventure.text.Component;
+import org.bukkit.Material;
+import org.bukkit.NamespacedKey;
+import org.bukkit.attribute.Attribute;
+import org.bukkit.attribute.AttributeModifier;
+import org.bukkit.configuration.serialization.ConfigurationSerialization;
+import org.bukkit.enchantments.Enchantment;
+import org.bukkit.inventory.ItemFlag;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.meta.ItemMeta;
+import org.bukkit.persistence.PersistentDataType;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+@SuppressWarnings("unused")
+public class ItemBuilder {
+ private final ItemStack itemStack;
+ private final ItemMeta itemMeta;
+
+ /**
+ * Instantiates a new Item builder.
+ *
+ * @param material the material
+ */
+ public ItemBuilder(final Material material) {
+ this.itemStack = new ItemStack(material);
+ this.itemMeta = this.itemStack.getItemMeta();
+ }
+
+ /**
+ * Instantiates a new Item builder with head texture.
+ *
+ * @param material the material
+ * @param textureMap the texture map
+ */
+ public ItemBuilder(final Material material, final Map textureMap) {
+ this.itemStack = new ItemStack(material);
+ this.itemMeta = (ItemMeta) ConfigurationSerialization.deserializeObject(textureMap);
+ }
+
+ /**
+ * Sets name.
+ *
+ * @param name the name
+ * @return the name
+ */
+ public ItemBuilder setName(final Component name) {
+ this.itemMeta.displayName(name);
+ return this;
+ }
+
+ /**
+ * Sets lore.
+ *
+ * @param loreList the lore list
+ * @return the lore
+ */
+ public ItemBuilder setLore(final List loreList) {
+ this.itemMeta.lore(loreList);
+ return this;
+ }
+
+ /**
+ * Sets model data.
+ *
+ * @param modelData the model data
+ * @return the model data
+ */
+ public ItemBuilder setModelData(final int modelData) {
+ this.itemMeta.setCustomModelData(modelData);
+ return this;
+ }
+
+ /**
+ * Sets unbreakable.
+ *
+ * @param unbreakable the unbreakable
+ * @return the unbreakable
+ */
+ public ItemBuilder setUnbreakable(final boolean unbreakable) {
+ this.itemMeta.setUnbreakable(unbreakable);
+ return this;
+ }
+
+ /**
+ * Sets pdc object.
+ *
+ * @param namespacedKey the namespaced key
+ * @param type the type
+ * @param value the value
+ * @return the pdc object
+ */
+ public ItemBuilder setPDCObject(final NamespacedKey namespacedKey, final PersistentDataType type, final Object value) {
+ this.itemMeta.getPersistentDataContainer().set(namespacedKey, type, value);
+ return this;
+ }
+
+ /**
+ * Add enchantment item builder.
+ *
+ * @param enchantment the enchantment
+ * @param level the level
+ * @param ignoreLevelRestriction the ignore level restriction
+ * @return the item builder
+ */
+ public ItemBuilder addEnchantment(final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
+ this.itemMeta.addEnchant(enchantment, level, ignoreLevelRestriction);
+ return this;
+ }
+
+ /**
+ * Add item flags item builder.
+ *
+ * @param itemFlags the item flags
+ * @return the item builder
+ */
+ public ItemBuilder addItemFlags(final ItemFlag... itemFlags) {
+ this.itemMeta.addItemFlags(itemFlags);
+ return this;
+ }
+
+ /**
+ * Add attribute modifier item builder.
+ *
+ * @param attribute the attribute
+ * @param modifier the modifier
+ * @return the item builder
+ */
+ public ItemBuilder addAttributeModifier(final Attribute attribute, final AttributeModifier modifier) {
+ this.itemMeta.addAttributeModifier(attribute, modifier);
+ return this;
+ }
+
+ /**
+ * Sets attribute modifiers.
+ *
+ * @param attributeModifiers the attribute modifiers
+ * @return the attribute modifiers
+ */
+ public ItemBuilder setAttributeModifiers(final Multimap attributeModifiers) {
+ this.itemMeta.setAttributeModifiers(attributeModifiers);
+ return this;
+ }
+
+ /**
+ * Sets destroyable keys.
+ *
+ * @param canDestroy the can destroy
+ * @return the destroyable keys
+ */
+ public ItemBuilder setDestroyableKeys(final Collection canDestroy) {
+ this.itemMeta.setDestroyableKeys(canDestroy);
+ return this;
+ }
+
+ /**
+ * Sets placeable keys.
+ *
+ * @param canPlaceOn the can place on
+ * @return the placeable keys
+ */
+ public ItemBuilder setPlaceableKeys(final Collection canPlaceOn) {
+ this.itemMeta.setPlaceableKeys(canPlaceOn);
+ return this;
+ }
+
+ /**
+ * Build item builder.
+ *
+ * @return the item builder
+ */
+ public ItemBuilder build() {
+ this.itemStack.setItemMeta(this.itemMeta);
+ return this;
+ }
+
+ /**
+ * Gets built item.
+ *
+ * @return the built item
+ */
+ public ItemStack getBuiltItem() {
+ return this.itemStack;
+ }
+}
\ No newline at end of file