NextDevv's Docs Help

KItemStack

class KItemStack(private var itemStack: ItemStack = ItemStack(Material.AIR)) { fun builder(): Builder { return Builder(itemStack) } class Builder(private var itemStack: ItemStack = ItemStack(Material.AIR)) { private var name: String = "" private var lore: MutableList<String> = mutableListOf() private var material: Material = Material.BARRIER private var amount: Int = 1 private val enchants: MutableList<Enchantment> = mutableListOf() private val itemFlags: MutableList<ItemFlag> = mutableListOf() fun getListOfRandom(amount: Int): List<ItemStack> { val list = mutableListOf<ItemStack>() for(i in 0 until amount) { list.add(setMaterial(randomMaterial()).build()) } return list } fun addItemFlag(itemFlag: ItemFlag): Builder { itemFlags.add(itemFlag) return this } fun addItemFlags(itemFlags: MutableList<ItemFlag>): Builder { this.itemFlags.addAll(itemFlags) return this } fun removeItemFlag(itemFlag: ItemFlag): Builder { itemFlags.remove(itemFlag) return this } fun removeItemFlags(itemFlags: MutableList<ItemFlag>): Builder { this.itemFlags.removeAll(itemFlags) return this } fun setName(name: String): Builder { this.name = name return this } fun setLore(lore: MutableList<String>): Builder { this.lore = lore return this } fun setLore(vararg lore: String): Builder { this.lore = lore.toMutableList() return this } fun setMaterial(material: Material): Builder { this.material = material return this } fun setAmount(amount: Int): Builder { this.amount = amount return this } fun addEnchantment(enchantment: Enchantment): Builder { enchants.add(enchantment) return this } fun addEnchantments(enchantments: MutableList<Enchantment>): Builder { enchants.addAll(enchantments) return this } fun removeEnchantment(enchantment: Enchantment): Builder { enchants.remove(enchantment) return this } fun removeEnchantments(enchantments: MutableList<Enchantment>): Builder { enchants.removeAll(enchantments) return this } fun build(): ItemStack { itemStack.type = material itemStack.amount = amount val itemMeta = itemStack.itemMeta itemMeta?.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)) itemMeta?.lore = lore.map { ChatColor.translateAlternateColorCodes('&', it) } enchants.forEach { enchantment -> itemMeta?.addEnchant(enchantment, 1, true) } itemFlags.forEach { itemFlag -> itemMeta?.addItemFlags(itemFlag) } itemStack.itemMeta = itemMeta return itemStack } } }

Description & Properties

The KItemStack class is a utility class for creating ItemStack objects with custom properties such as name, lore, material, amount, enchantments, and item flags. It provides a fluent builder pattern for constructing ItemStack objects with the desired properties.

The class has the following properties and methods:

  • itemStack: The ItemStack object that is being built.

  • builder(): Returns a new Builder object to start building a new ItemStack.

  • Builder: A nested class that provides methods for setting the properties of the ItemStack and building the final ItemStack object.

  • name: The display name of the ItemStack.

  • lore: The lore of the ItemStack.

  • material: The material type of the ItemStack.

  • amount: The amount of the ItemStack.

  • enchants: A list of enchantments to apply to the ItemStack.

  • itemFlags: A list of item flags to apply to the ItemStack.

  • getListOfRandom(amount: Int): Generates a list of random ItemStack objects with random materials.

  • addItemFlag(itemFlag: ItemFlag): Adds an item flag to the ItemStack builder.

  • addItemFlags(itemFlags: MutableList<ItemFlag>): Adds multiple item flags to the ItemStack builder.

  • removeItemFlag(itemFlag: ItemFlag): Removes an item flag from the ItemStack builder.

  • removeItemFlags(itemFlags: MutableList<ItemFlag>): Removes multiple item flags from the ItemStack builder.

  • setName(name: String): Sets the display name of the ItemStack.

  • setLore(lore: MutableList<String>): Sets the lore of the ItemStack.

  • setLore(vararg lore: String): Sets the lore of the ItemStack using varargs.

  • setMaterial(material: Material): Sets the material type of the ItemStack.

  • setAmount(amount: Int): Sets the amount of the ItemStack.

  • addEnchantment(enchantment: Enchantment): Adds an enchantment to the ItemStack.

  • addEnchantments(enchantments: MutableList<Enchantment>): Adds multiple enchantments to the ItemStack.

  • removeEnchantment(enchantment: Enchantment): Removes an enchantment from the ItemStack.

  • removeEnchantments(enchantments: MutableList<Enchantment>): Removes multiple enchantments from the ItemStack.

  • build(): Builds the final ItemStack object with the specified properties.

Example

You can use the KItemStack class to create custom ItemStack objects with specific properties and apply them to GUI elements, items, or other game mechanics. The fluent builder pattern allows you to easily set the desired properties of the ItemStack and create it with a single method call.

Here is an example of how to create a custom ItemStack using the KItemStack class in Kotlin:

val itemStack = KItemStack().builder() .setName("&bCustom Sword") .setLore("&7This is a custom sword.") .setMaterial(Material.DIAMOND_SWORD) .addEnchantment(Enchantment.DAMAGE_ALL) .addItemFlag(ItemFlag.HIDE_ENCHANTS) .setAmount(1) .build()

In this example, we create a custom ItemStack object representing a diamond sword with a custom name, lore, enchantment, item flag, and amount. The KItemStack class provides a convenient way to create and customize ItemStack objects in a concise and readable manner.

Summary

The KItemStack class simplifies the process of creating custom ItemStack objects in Bukkit/Spigot plugins by providing a fluent builder pattern for setting the properties of the ItemStack. By using the KItemStack class, you can easily create and customize ItemStack objects with specific attributes and apply them to various aspects of your plugin, such as GUIs, items, or game mechanics.

Last modified: 03 July 2024