Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.
Syrent edited this page Jun 21, 2023 · 2 revisions

Item Class

The Item class represents a custom item in the Origin plugin. It allows you to define behavior for when the item is used by a player. The class provides flexibility by allowing you to associate additional data with the item, which can be accessed during item use events.

Key Features

  • Register event listeners automatically upon initialization.
  • Set consumer functions to handle item use events.
  • Support for associating additional data with items.
  • Conversion of ItemStack to Item for convenience.

Usage

To use the Item class, follow these steps:

  1. Create an instance of Item by providing the item stack and optional data:
Item<ExampleShopItem> item = new Item<>(itemStack, data);
SimpleItem item = new Item(itemStack);

itemStack: The item stack representing the custom item. data: Additional data associated with the item (optional). 2. Register consumer functions to handle item use events using the onUse method:

item.onUse(itemConsumer -> {
     Bukkit.broadcast(Component.text("The item type is: " + itemStack.getType()));
});
item.onUse((item, itemData) -> {
    // Handle item use with additional data
    Bukkit.broadcast(Component.text("The price of " + itemStack.getType() + " is: " + itemData.data));
});

itemConsumer: A consumer function that accepts the item stack as a parameter. (item, itemData): A bi-consumer function that accepts the item stack and associated data as parameters. 3. Handle the item use events in the consumer functions:

Consumer<ItemStack> itemConsumer = itemStack -> {
     Bukkit.broadcast(Component.text("The item type is: " + itemStack.getType()));
};

BiConsumer<ItemStack, ExampleShopItem> itemDataConsumer = (itemStack, itemData) -> {
    // Handle item use with additional data
    Bukkit.broadcast(Component.text("The price of " + itemStack.getType() + " is: " + itemData.price));
};
  1. Listen for the item use events by registering the Item as a listener:
Origin.registerListener(item);
  1. Optionally, convert an existing ItemStack to an Item using the extension function toOriginItem:
ItemStack itemStack = new ItemStack(Material.DIAMOND);
OriginItem<ExampleShopItem> item = itemStack.toOriginItem();
ItemStack itemStack = new ItemStack(Material.DIAMOND);
SimpleItem item = itemStack.toOriginItem();

By following these steps, you can create and customize Item instances to add custom behavior to your items in the Origin plugin.

Clone this wiki locally