Skip to content

Commit b402ce6

Browse files
Introduce enchanted books
1 parent 072e4ae commit b402ce6

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/crafting/AnvilCraftingManagerDataFiller.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
namespace pocketmine\crafting;
2525

26+
use pocketmine\data\bedrock\item\ItemTypeNames;
2627
use pocketmine\item\Durable;
28+
use pocketmine\item\ItemTypeIds;
2729
use pocketmine\item\ToolTier;
2830
use pocketmine\item\VanillaArmorMaterials;
2931
use pocketmine\item\VanillaItems;
@@ -64,12 +66,20 @@ public static function fillData(CraftingManager $manager) : CraftingManager{
6466
}
6567
}
6668

69+
$manager->registerAnvilRecipe(new ItemSelfCombineRecipe(
70+
new MetaWildcardRecipeIngredient(ItemTypeNames::ENCHANTED_BOOK)
71+
));
72+
6773
foreach(VanillaItems::getAll() as $item){
6874
if($item instanceof Durable){
6975
$itemId = GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName();
7076
$manager->registerAnvilRecipe(new ItemSelfCombineRecipe(
7177
new MetaWildcardRecipeIngredient($itemId)
7278
));
79+
$manager->registerAnvilRecipe(new ItemDifferentCombineRecipe(
80+
new MetaWildcardRecipeIngredient($itemId),
81+
new EnchantedBookRecipeIngredient($item)
82+
));
7383
}
7484
}
7585

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)