|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * |
| 5 | + * ____ _ _ __ __ _ __ __ ____ |
| 6 | + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ |
| 7 | + * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | |
| 8 | + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ |
| 9 | + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Lesser General Public License as published by |
| 13 | + * the Free Software Foundation, either version 3 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * @author PocketMine Team |
| 17 | + * @link http://www.pocketmine.net/ |
| 18 | + * |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace pocketmine\crafting; |
| 25 | + |
| 26 | +use pocketmine\item\enchantment\AvailableEnchantmentRegistry; |
| 27 | +use pocketmine\item\Item; |
| 28 | +use pocketmine\item\ItemTypeIds; |
| 29 | + |
| 30 | +/** |
| 31 | + * Recipe ingredient that matches enchanted books that can apply at least one enchantment to the target item. |
| 32 | + */ |
| 33 | +final class EnchantedBookRecipeIngredient implements RecipeIngredient{ |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private Item $compareItem |
| 37 | + ){} |
| 38 | + |
| 39 | + public function getCompareItem() : Item{ return $this->compareItem; } |
| 40 | + |
| 41 | + public function accepts(Item $item) : bool{ |
| 42 | + if($item->getCount() < 1){ |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + if($item->getTypeId() !== ItemTypeIds::ENCHANTED_BOOK){ |
| 47 | + // We only accept enchanted books in this ingredient |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + $enchantmentRegistry = AvailableEnchantmentRegistry::getInstance(); |
| 52 | + foreach($item->getEnchantments() as $compareEnchantment){ |
| 53 | + if($enchantmentRegistry->isAvailableForItem($compareEnchantment->getType(), $this->compareItem)){ |
| 54 | + // As long as one enchantment in the book is applicable to the target item |
| 55 | + // the combination is possible, so we accept this item |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return false; |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + public function __toString() : string{ |
| 65 | + return "EnchantedBookRecipeIngredient($this->compareItem)"; |
| 66 | + } |
| 67 | +} |
0 commit comments