presentkim / banner-factory
Factory to quickly create special shaped banner items for axolotl-pm/PocketMine-MP
Requires
- php: ^8.4
- pocketmine/pocketmine-mp: ^5.0
Requires (Dev)
- axolotl-pm/pocketmine-mp: ^5.51
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
banner-factory
Create banners with numbers, arrows and custom shapes in one line.
Packagist ยท Changelog ยท Report a bug ยท Request a feature
$banner = BannerFactory::getInstance()->getBannerItem( DefaultPatternIds::PATTERN_NUM_7, new BannerColorSet(DyeColor::RED(), DyeColor::WHITE()) ); $player->getInventory()->addItem($banner);
๐ Why banner-factory?
A banner is a base color plus up to six pattern layers, each with its own color. Building one layer by layer is noisy and easy to get wrong. This library gives shapes a name and lets you recolor them.
- Named shapes : digits
0-9, four arrows, two gradations, a fox, a duck and an empty banner are built in - Recolorable : every shape takes a
BannerColorSet(base color + extra colors) - Your own shapes : register a pattern list under a name with a compact text format (
"ls1"= left stripe, color 1) - Cached : a banner of the same shape and colors is built once, you always get your own copy
- Tested : on PHP 8.4 / 8.5, including a save and load round trip of the item
๐ฆ Installation
composer require presentkim/banner-factory
| Requirement | Version |
|---|---|
| Server | axolotl-pm/PocketMine-MP (fork of PocketMine-MP 5) |
| PHP | 8.4 or newer |
The Poggit virion for the original PocketMine-MP 5.44 and older is kept on the
legacy/pmmp-5.44branch (no longer maintained).
๐ Quick start
use kim\present\factory\banner\BannerFactory; use kim\present\factory\banner\DefaultPatternIds; use kim\present\factory\banner\data\BannerColorSet; use pocketmine\block\utils\DyeColor; $factory = BannerFactory::getInstance(); // Colors: the first one is the banner's base color, the following ones are the colors of layer 1, 2, ... $colors = new BannerColorSet(DyeColor::BLACK(), DyeColor::YELLOW()); $banner = $factory->getBannerItem(DefaultPatternIds::PATTERN_ARROW_U, $colors); // ?Banner if($banner !== null){ $player->getInventory()->addItem($banner); }
getBannerItem() returns null when no banner is registered under that name.
๐ Examples
Register your own banner
A pattern is <bedrock pattern id><color layer>. "ls1" is STRIPE_LEFT colored with the color at index 1 of the
BannerColorSet; layer 0 is the base color. A color layer the set does not have falls back to the base color.
Patterns are drawn in the order given.
use kim\present\factory\banner\data\BannerPattern; $factory->registerBanner("my_cross", [ BannerPattern::fromHash("cr1"), // cross in color 1 BannerPattern::fromHash("bo0"), // border in the base color ]); $banner = $factory->getBannerItem("my_cross", new BannerColorSet(DyeColor::WHITE(), DyeColor::RED()));
Registering a name again replaces it. BannerPattern::fromHash() throws a RuntimeException for a malformed hash or a
pattern id the server does not know. Ids are the ones of BannerPatternTypeIdMap (for example bo border, ts top
stripe, mc circle, cre creeper, flo flower).
All built-in names
| Constant | Banner |
|---|---|
PATTERN_EMPTY |
No pattern, base color |
PATTERN_NUM_0 ... PATTERN_NUM_9 (PATTERN_NUM_LIST) |
Digits |
PATTERN_ARROW_L _R _U _D (PATTERN_ARROW_LIST) |
Arrows |
PATTERN_GRADATION_BRICKS PATTERN_GRADATION_PVP |
Gradations |
PATTERN_FOX PATTERN_DUCK |
Animals |
๐ API reference
| Member | Description |
|---|---|
BannerFactory::getInstance() |
The singleton factory |
getBannerItem(string $name, BannerColorSet $colors) : ?Banner |
A fresh banner item, null for an unknown name |
registerBanner(string $name, BannerPattern[] $patterns) : void |
Add or replace a banner shape |
getBanner(string $name) : ?array |
The registered BannerPattern list |
new BannerColorSet(DyeColor $base, DyeColor ...$extra) |
Base color and the colors of layers 1, 2, ... |
new BannerPattern(BannerPatternType $type, int $layer) |
One layer; fromHash() / toHash() convert from / to text |
๐งช Development
composer install --ignore-platform-req='ext-*' composer test
๐ License
Distributed under the MIT License. See LICENSE for more information.