Skip to content

[v5] Addons (OUTDATED)

Tayyab R edited this page Mar 19, 2021 · 1 revision

NOTICE

Addons have been removed on version 6.0+
Use tag handling instead.

Here are the general instructing for developers who want to add support of their plugin to ScoreHud via an addon.
The whole process is extremely simple.

Addon Making

Step 1

You start by making a new PHP file.

For this tutorial I would be using a file called EconomyApiAddon.php. And adding a namespace. It could be any of your choice. The {} at the end of the namespace are necessary.

namespace JackMD\ScoreHud\Addons{}

Step 2

Now define the name and main file location of the addon like so.

/**
 * @name EconomyApiAddon
 * @version 1.0.0
 * @main JackMD\ScoreHud\Addons\EconomyApiAddon
 * @depend EconomyAPI
 */
namespace JackMD\ScoreHud\Addons{}
  • @name will be the name of your addon and @main will be the namespace plus @name of the addon.
  • @version is important and is used to check the addon version. Its also used to tell the user if an addon update is available.
  • @depend tag is optional. If set it will make sure that the addon won't get loaded until the required plugin is loaded and if the plugin doesn't exist it will then disable the addon.
    You can add multiple plugins in @depend just separate them with a comma. Example: @depend MyPlot,BlockSniper,Core
  • Additionally you can also use @api and mention the api your addon is compatible with. The api is compared with PocketMine-MP api. Multiple api's can also be added separated by a comma (,). Example: @api 3.0.0,4.0.0

Step 3

Now we will make a class with the same name as in @name which will extend AddonBase.

/**
 * @name EconomyApiAddon
 * @version 1.0.0
 * @main JackMD\ScoreHud\Addons\EconomyApiAddon
 * @depend EconomyAPI
 */
namespace JackMD\ScoreHud\Addons
{
	use JackMD\ScoreHud\addon\AddonBase;
	use onebone\economyapi\EconomyAPI;
	use pocketmine\Player;

	class EconomyApiAddon extends AddonBase{}
}

The extending is necessary. Doing so you will be able to access the methods mentioned and explained here.

Step 4

I now make use onEnable() method. It is only called once when the addon enables.
I then get hold of the plugin needed which in this case is EconomyAPI like so.

/**
 * @name EconomyApiAddon
 * @version 1.0.0
 * @main JackMD\ScoreHud\Addons\EconomyApiAddon
 * @depend EconomyAPI
 */
namespace JackMD\ScoreHud\Addons
{
	use JackMD\ScoreHud\addon\AddonBase;
	use onebone\economyapi\EconomyAPI;
	use pocketmine\Player;

	class EconomyApiAddon extends AddonBase{

		/** @var EconomyAPI */
		private $economyAPI;

		public function onEnable(): void{
			$this->economyAPI = $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
		}
	}
}

Its worth mentioning that we do not need to check if whether $this->economyAPI is an instance of EconomyAPI since we added @depend tag. If EconomyAPI was not found then this addon will simply not be enabled.

Step 5

Till now we haven't done anything to make ScoreHud aware of what tag we would be using and what value should appear on the scoreboard.

To accomplish this we use getProcessedTags(Player $player): array method and return the array containing the tags this addon would provide.

/**
 * @name EconomyApiAddon
 * @version 1.0.0
 * @main JackMD\ScoreHud\Addons\EconomyApiAddon
 * @depend EconomyAPI
 */
namespace JackMD\ScoreHud\Addons
{
	use JackMD\ScoreHud\addon\AddonBase;
	use onebone\economyapi\EconomyAPI;
	use pocketmine\Player;

	class EconomyApiAddon extends AddonBase{

		/** @var EconomyAPI */
		private $economyAPI;

		public function onEnable(): void{
			$this->economyAPI = $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
		}

		/**
		 * @param Player $player
		 * @return array
		 */
		public function getProcessedTags(Player $player): array{
			return [
				"{money}" => $this->economyAPI->myMoney($player)
			];
		}
	}
}

We simply return the array containing our required tag as key and its data as value.
More tags can also be added.

Step 6

Your addon is now complete. Simple wasn't it?
To test it follow the tutorial here.

Clone this wiki locally