Class AbstractCustomItem

java.lang.Object
net.mathias2246.buildmc.api.item.AbstractCustomItem
All Implemented Interfaces:
net.kyori.adventure.key.Keyed, org.bukkit.Keyed
Direct Known Subclasses:
AbstractTool

public abstract class AbstractCustomItem extends Object implements org.bukkit.Keyed
An abstract-class that can be used to define custom-item types for your plugin. Custom items are defined by some data that is stored inside the ItemStack PDC.

This doesn't actually define a new item type or Material.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final @NotNull org.bukkit.NamespacedKey
    The NamespacedKey used to store the id of the custom item inside the ItemStack PDC
    static final @NotNull DeferredRegistry<AbstractCustomItem>
    Shared registry instance for AbstractCustomItem instances
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractCustomItem(@NotNull org.bukkit.plugin.Plugin plugin, @NotNull org.bukkit.NamespacedKey key)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract @NotNull org.bukkit.inventory.ItemStack
    Creates the default ItemStack for this custom-item type.
    abstract boolean
    canUse(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.event.player.PlayerInteractEvent event)
    Checks if the player can interact in any way with this custom item.
    static @Nullable org.bukkit.NamespacedKey
    getCustomItemKey(@Nullable org.bukkit.inventory.ItemStack item)
    Tries to read the custom item type from an ItemStack.
    @NotNull org.bukkit.NamespacedKey
     
    @NotNull org.bukkit.plugin.Plugin
     
    void
    giveToPlayer(@NotNull org.bukkit.entity.Player player)
    Gives a single item of this type to a player.
    void
    giveToPlayer(@NotNull org.bukkit.entity.Player player, int amount)
    Gives a certain amount of this custom item to a player.
    void
    giveToPlayer(@NotNull org.bukkit.entity.Player player, @NotNull ItemMetaModifier modifier)
    Gives a single item of this type to a player and modifies the ItemMeta with a custom modifier.
    boolean
    isThis(@Nullable org.bukkit.inventory.ItemStack item)
    Checks if the given ItemStack is of this custom item type.
    protected abstract void
    onInteract(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.event.player.PlayerInteractEvent event)
    Executed when a player left- or right-clicks with this custom-item type in his hand.
    void
    onInteractEvent(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.event.player.PlayerInteractEvent event)
    Called when a PlayerInteractEvent from the CustomItemListener was done with this custom item type
    protected abstract void
    onLeftClick(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.Location at, @NotNull org.bukkit.event.player.PlayerInteractEvent event)
    Executed when a player left-clicks with this custom-item type in his hand.
    protected abstract void
    onRightClick(@NotNull org.bukkit.inventory.ItemStack item, @NotNull org.bukkit.Location at, @NotNull org.bukkit.event.player.PlayerInteractEvent event)
    Executed when a player right-clicks with this custom-item type in his hand.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.bukkit.Keyed

    key
  • Field Details

    • customItemsRegistry

      @NotNull public static final @NotNull DeferredRegistry<AbstractCustomItem> customItemsRegistry
      Shared registry instance for AbstractCustomItem instances
    • CUSTOM_ITEM_PDC_KEY

      @NotNull public static final @NotNull org.bukkit.NamespacedKey CUSTOM_ITEM_PDC_KEY
      The NamespacedKey used to store the id of the custom item inside the ItemStack PDC
  • Constructor Details

    • AbstractCustomItem

      public AbstractCustomItem(@NotNull @NotNull org.bukkit.plugin.Plugin plugin, @NotNull @NotNull org.bukkit.NamespacedKey key)
  • Method Details

    • getPlugin

      @NotNull public @NotNull org.bukkit.plugin.Plugin getPlugin()
      Returns:
      The plugin that owns this custom type
    • getKey

      @Contract(pure=true) @NotNull public @NotNull org.bukkit.NamespacedKey getKey()
      Specified by:
      getKey in interface org.bukkit.Keyed
    • getCustomItemKey

      @Nullable public static @Nullable org.bukkit.NamespacedKey getCustomItemKey(@Nullable @Nullable org.bukkit.inventory.ItemStack item)
      Tries to read the custom item type from an ItemStack.
      Returns:
      The NamespacedKey for the custom item type or null if not found or invalid
    • giveToPlayer

      public void giveToPlayer(@NotNull @NotNull org.bukkit.entity.Player player, int amount)
      Gives a certain amount of this custom item to a player.
    • giveToPlayer

      public void giveToPlayer(@NotNull @NotNull org.bukkit.entity.Player player)
      Gives a single item of this type to a player.
    • giveToPlayer

      public void giveToPlayer(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull ItemMetaModifier modifier)
      Gives a single item of this type to a player and modifies the ItemMeta with a custom modifier.
      Parameters:
      modifier - An implementation of a ItemMetaModifier to apply e.g. platform dependent modifications to the ItemMeta.
    • isThis

      @Contract(pure=true) public boolean isThis(@Nullable @Nullable org.bukkit.inventory.ItemStack item)
      Checks if the given ItemStack is of this custom item type.
      Returns:
      True if the given ItemStack is of this type.
    • canUse

      public abstract boolean canUse(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.event.player.PlayerInteractEvent event)
      Checks if the player can interact in any way with this custom item.

      You should implement logic like, for example, cooldowns or location checks that should be applied.

    • onInteractEvent

      @Internal public void onInteractEvent(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.event.player.PlayerInteractEvent event)
      Called when a PlayerInteractEvent from the CustomItemListener was done with this custom item type
    • onInteract

      protected abstract void onInteract(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.event.player.PlayerInteractEvent event)
      Executed when a player left- or right-clicks with this custom-item type in his hand.
    • onLeftClick

      protected abstract void onLeftClick(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.Location at, @NotNull @NotNull org.bukkit.event.player.PlayerInteractEvent event)
      Executed when a player left-clicks with this custom-item type in his hand.
    • onRightClick

      protected abstract void onRightClick(@NotNull @NotNull org.bukkit.inventory.ItemStack item, @NotNull @NotNull org.bukkit.Location at, @NotNull @NotNull org.bukkit.event.player.PlayerInteractEvent event)
      Executed when a player right-clicks with this custom-item type in his hand.
    • buildDefaultItemStack

      @NotNull protected abstract @NotNull org.bukkit.inventory.ItemStack buildDefaultItemStack()
      Creates the default ItemStack for this custom-item type.

      You should implement logic that changes e.g. ItemMeta like tools or consumables here. Is called inside the constructor to create the default ItemStack.