Skip to content

Commit

Permalink
Added new methods to ItemBuilder and ItemUtils to add item flags and …
Browse files Browse the repository at this point in the history
…persistent data
  • Loading branch information
boxbeam committed May 13, 2020
1 parent 6d7cc1b commit 7d3851b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/redempt/redlib/itemutils/ItemBuilder.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package redempt.redlib.itemutils;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;

/**
* A utility class to easily create items
Expand Down Expand Up @@ -132,4 +135,24 @@ public ItemBuilder addAttribute(Attribute attribute, double amount, Operation op
return new ItemBuilder(ItemUtils.addAttribute(this, attribute, amount, operation, slot));
}

/**
* Add ItemFlags to this ItemBuilder
* @param flags The ItemFlags to add
* @return The ItemBuilder with the flags added
*/
public ItemBuilder addItemFlags(ItemFlag... flags) {
return new ItemBuilder(ItemUtils.addItemFlags(this, flags));
}

/**
* Add persistent tags to this ItemBuilder
* @param key The key to add the data under
* @param type The type of the data
* @param data The data to store
* @return The ItemBuilder with the persistent data added
*/
public <T, Z> ItemBuilder addPersistentTag(NamespacedKey key, PersistentDataType<T, Z> type, Z data) {
return new ItemBuilder(ItemUtils.addPersistentTag(this, key, type, data));
}

}
33 changes: 33 additions & 0 deletions src/redempt/redlib/itemutils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import java.util.UUID;

import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.attribute.AttributeModifier.Operation;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

/**
* A utility class to easily modify items
Expand Down Expand Up @@ -156,6 +159,36 @@ public static ItemStack addAttribute(ItemStack item, Attribute attribute, double
return clone;
}

/**
* Adds ItemFlags to the item
* @param item The item to add ItemFlags to
* @param flags The ItemFlags to add
* @return The modified item
*/
public static ItemStack addItemFlags(ItemStack item, ItemFlag... flags) {
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(flags);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
}

/**
* Adds persistent data to the item
* @param item The item to add persistent data to
* @param key The key to add the data under
* @param type The type of the data
* @param data The data to store
* @return The modified item
*/
public static <T, Z> ItemStack addPersistentTag(ItemStack item, NamespacedKey key, PersistentDataType<T, Z> type, Z data) {
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(key, type, data);
ItemStack clone = item.clone();
clone.setItemMeta(meta);
return clone;
}

/**
* Add an attribute to the item
* @param item The item to have an attribute added
Expand Down

0 comments on commit 7d3851b

Please sign in to comment.