Skip to content

Custom Items

Seb edited this page Jun 26, 2022 · 5 revisions

Registering a custom item is as simple as registering a normal item, but the ID is calculated for you. All you need to do is use the CustomiesItemFactory class to register the item, and fetch it as you would with a vanilla item.

use customiesdevs\customies\item\CustomiesItemFactory;

// ...

public function onEnable(): void {
	CustomiesItemFactory::getInstance()->registerItem(Item::class, "customies:example_item", "Example Item");
}

// ...

Getting An Item

To get a custom item you need to use the CustomiesItemFactory instead of the regular item factory so you can get the item from the custom identifier instead of needing a numeric id.

$item = CustomiesItemFactory::getInstance()->get("customies:example_item");

Item Components

Custom items can also have components which are used to change the behaviour of items client side, such as making it edible or have durability etc. To get started with components, you need to implement the ItemComponents interface, use the ItemComponentsTrait and call the initComponent method in the constructor of your class.

use customiesdevs\customies\item\ItemComponents;
use customiesdevs\customies\item\ItemComponentsTrait;
use pocketmine\item\Item;

class ExampleItem extends Item implements ItemComponents {
	use ItemComponentsTrait;

	public function __construct() {
		$this->initComponent("example_item", 64);
	}
}

Now that you have an item with components, you can add either components or properties using the addComponent and addProperty methods.

// ...

$this->addComponent("minecraft:armor", ["protection" => new IntTag(4)]);
$this->addProperty("allow_off_hand", true);

// ...

More information about all the different item components and properties can be found on docs.microsoft.com.

Creative Inventory

If you want add the item to the creative inventory, you can provide a CreativeInventoryInfo as the 3th argument to initComponent. This contains the following:

  • Category: The base category to show the item in, for example CreativeInventoryInfo::CATEGORY_EQUIPMENT will put the item in the equipment category
  • Group: The group to put the item in inside of the category, for example CreativeInventoryInfo::GROUP_SWORD will put the item at the end of the sword group
use customiesdevs\customies\item\CreativeInventoryInfo;

// ...

$creativeInfo = new CreativeInventoryInfo(CreativeInventoryInfo::CATEGORY_EQUIPMENT, CreativeInventoryInfo::GROUP_SWORD);
$this->initComponent("example_item", 64, $creativeInfo);

// ...

More information about creative categories and groups can be found on docs.microsoft.com.

Clone this wiki locally