Skip to content

Custom Blocks

HydroGames-dev edited this page Oct 3, 2024 · 3 revisions

Basics

Registering a custom block can either be done with or without a model using the CustomiesBlockFactory class. Without a model all you need to do is register the block by providing a Closure and the identifier of the block. The closure must be of the type Closure(): Block. This DOES NOT allow you to use a custom ID for the block, and will cause issues if you do not use the provided value.

use customiesdevs\customies\block\CustomiesBlockFactory;
use pocketmine\block\BlockBreakInfo;
use pocketmine\block\BlockIdentifier;

// ...

public function onEnable(): void {
	CustomiesBlockFactory::getInstance()->registerBlock(
		static fn() => new Block(new BlockIdentifier(BlockTypeIds::newId()),
			"Example Block",
			new BlockTypeInfo(new BlockBreakInfo(8.0))
			),
		"customies:example_block",
		new CreativeInventoryInfo(CreativeInventoryInfo::CATEGORY_ALL, CreativeInventoryInfo::NONE)
	);
}

// ...

It is important to note that the provided closure is shared across threads, meaning you cannot use any variables that are not serializable types defined outside the scope of the closure.

Getting a Block

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

$block = CustomiesBlockFactory::getInstance()->get("customies:example_block");

Using Custom Models

If your block contains a different model

More information about materials and the different properties can be found on docs.microsoft.com.

Creative Inventory

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

  • Category: The base category to show the item in, for example CreativeInventoryInfo::CATEGORY_NATURE will put the item in the nature category
  • Group: The group to put the item in inside of the category, for example CreativeInventoryInfo::GROUP_WOOD will put the item at the end of the wood group
use customiesdevs\customies\block\CustomiesBlockFactory;
use customiesdevs\customies\block\Material;
use customiesdevs\customies\block\Model;
use customiesdevs\customies\item\CreativeInventoryInfo;
use pocketmine\block\BlockBreakInfo;
use pocketmine\block\BlockIdentifier;
use pocketmine\math\Vector3;

// ...

public function onEnable(): void {
	$material = new Material(Material::TARGET_ALL, "example", Material::RENDER_METHOD_ALPHA_TEST);
	$model = new Model([$material], "geometry.example", new Vector3(-8, 0, -8), new Vector3(16, 16, 16));
	$creativeInfo = new CreativeInventoryInfo(CreativeInventoryInfo::CATEGORY_NATURE, CreativeInventoryInfo::GROUP_WOOD);
	CustomiesBlockFactory::getInstance()->registerBlock(static fn(int $id) => new Block(new BlockIdentifier($id, 0), "Example Block", new BlockBreakInfo(1)), "customies:example_block", $model, $creativeInfo);
}

// ...

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