Skip to content

Commit eaab4b3

Browse files
add unit testing on MaterialRepairRecipe
1 parent f346799 commit eaab4b3

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-8
lines changed

src/block/utils/AnvilHelper.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
use pocketmine\crafting\AnvilCraftResult;
2727
use pocketmine\item\Item;
28-
use pocketmine\player\Player;
2928
use pocketmine\Server;
3029

3130
final class AnvilHelper{
@@ -36,7 +35,7 @@ final class AnvilHelper{
3635
*
3736
* Returns null if the operation can't do anything.
3837
*/
39-
public static function calculateResult(Player $player, Item $base, Item $material, ?string $customName = null) : ?AnvilCraftResult{
38+
public static function calculateResult(Item $base, Item $material, ?string $customName, bool $isCreative) : ?AnvilCraftResult{
4039

4140
$recipe = Server::getInstance()->getCraftingManager()->matchAnvilRecipe($base, $material);
4241
if($recipe === null){
@@ -58,7 +57,7 @@ public static function calculateResult(Player $player, Item $base, Item $materia
5857
$result = new AnvilCraftResult($xpCost, $resultItem, $result->getSacrificeResult());
5958
}
6059

61-
if($result === null || $result->getXpCost() <= 0 || ($result->getXpCost() > self::COST_LIMIT && !$player->isCreative())){
60+
if($result === null || $result->getXpCost() <= 0 || ($result->getXpCost() > self::COST_LIMIT && !$isCreative)){
6261
return null;
6362
}
6463

src/crafting/AnvilCraftingManagerDataFiller.php

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

2424
namespace pocketmine\crafting;
2525

26+
use pocketmine\item\Durable;
2627
use pocketmine\item\ToolTier;
2728
use pocketmine\item\VanillaArmorMaterials;
2829
use pocketmine\item\VanillaItems;
30+
use pocketmine\world\format\io\GlobalItemDataHandlers;
2931

3032
final class AnvilCraftingManagerDataFiller{
3133
public static function fillData(CraftingManager $manager) : CraftingManager{
@@ -62,6 +64,16 @@ public static function fillData(CraftingManager $manager) : CraftingManager{
6264
}
6365
}
6466

67+
foreach(VanillaItems::getAll() as $item){
68+
if($item instanceof Durable){
69+
$itemId = GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName();
70+
$manager->registerAnvilRecipe(new ItemCombineRecipe(
71+
new MetaWildcardRecipeIngredient($itemId),
72+
new MetaWildcardRecipeIngredient($itemId)
73+
));
74+
}
75+
}
76+
6577
return $manager;
6678
}
6779
}

src/crafting/MaterialRepairRecipe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getResultFor(Item $input, Item $material) : ?AnvilCraftResult{
5959
return new AnvilCraftResult(
6060
$numberRepair,
6161
(clone $input)->setDamage(max(0, $damage)),
62-
(clone $material)->pop($numberRepair)
62+
(clone $material)->setCount($material->getCount() - $numberRepair)
6363
);
6464
}
6565
}

src/inventory/transaction/AnvilTransaction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646
Player $source,
4747
private readonly AnvilCraftResult $expectedResult,
4848
private readonly ?string $customName
49-
) {
49+
){
5050
parent::__construct($source);
5151
}
5252

@@ -62,8 +62,8 @@ private function validateFiniteResources(int $xpSpent) : void{
6262
}
6363
}
6464

65-
private function validateInputs(Item $base, Item $material, Item $expectedOutput) : ?int {
66-
$calculAttempt = AnvilHelper::calculateResult($this->source, $base, $material, $this->customName);
65+
private function validateInputs(Item $base, Item $material, Item $expectedOutput) : ?int{
66+
$calculAttempt = AnvilHelper::calculateResult($base, $material, $this->customName, $this->source->isCreative());
6767
if($calculAttempt === null){
6868
return null;
6969
}

src/network/mcpe/handler/ItemStackRequestExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ protected function processItemStackRequestAction(ItemStackRequestAction $action)
355355
}elseif($action instanceof CraftRecipeOptionalStackRequestAction){
356356
$window = $this->player->getCurrentWindow();
357357
if($window instanceof AnvilInventory){
358-
$result = AnvilHelper::calculateResult($this->player, $window->getInput(), $window->getMaterial(), $this->request->getFilterStrings()[0] ?? null);
358+
$result = AnvilHelper::calculateResult($window->getInput(), $window->getMaterial(), $this->request->getFilterStrings()[0] ?? null, $this->player->isCreative());
359359
if($result !== null){
360360
$this->specialTransaction = new AnvilTransaction($this->player, $result, $this->request->getFilterStrings()[0] ?? null);
361361
$this->setNextCreatedItem($result->getOutput());
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 Generator;
27+
use PHPUnit\Framework\TestCase;
28+
use pocketmine\item\Item;
29+
use pocketmine\item\VanillaItems;
30+
use function floor;
31+
32+
class AnvilCraftTest extends TestCase{
33+
public static function materialRepairRecipe() : Generator{
34+
yield "No repair available" => [
35+
VanillaItems::DIAMOND_PICKAXE(),
36+
VanillaItems::DIAMOND(),
37+
null
38+
];
39+
40+
yield "Repair one damage" => [
41+
VanillaItems::DIAMOND_PICKAXE()->setDamage(1),
42+
VanillaItems::DIAMOND(),
43+
new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE(), null)
44+
];
45+
46+
yield "Repair one damage with more materials than expected" => [
47+
VanillaItems::DIAMOND_PICKAXE()->setDamage(1),
48+
VanillaItems::DIAMOND()->setCount(2),
49+
new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE(), VanillaItems::DIAMOND())
50+
];
51+
52+
$diamondPickaxeQuarter = (int) floor(VanillaItems::DIAMOND_PICKAXE()->getMaxDurability() / 4);
53+
yield "Repair one quarter" => [
54+
VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter),
55+
VanillaItems::DIAMOND()->setCount(1),
56+
new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE(), null)
57+
];
58+
59+
yield "Repair one quarter plus 1" => [
60+
VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter + 1),
61+
VanillaItems::DIAMOND()->setCount(1),
62+
new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE()->setDamage(1), null)
63+
];
64+
65+
yield "Repair more than one quarter" => [
66+
VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter * 2),
67+
VanillaItems::DIAMOND()->setCount(2),
68+
new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE(), null)
69+
];
70+
71+
yield "Repair more than one quarter with more materials than expected" => [
72+
VanillaItems::DIAMOND_PICKAXE()->setDamage($diamondPickaxeQuarter * 2),
73+
VanillaItems::DIAMOND()->setCount(3),
74+
new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE(), VanillaItems::DIAMOND()->setCount(1))
75+
];
76+
}
77+
78+
/**
79+
* @dataProvider materialRepairRecipe
80+
*/
81+
public function testMaterialRepairRecipe(Item $base, Item $material, ?AnvilCraftResult $expected) : void{
82+
$recipe = new MaterialRepairRecipe(
83+
new ExactRecipeIngredient((clone $base)->setCount(1)),
84+
new ExactRecipeIngredient((clone $material)->setCount(1))
85+
);
86+
$result = $recipe->getResultFor($base, $material);
87+
if($expected === null){
88+
self::assertNull($result, "Recipe did not match expected result");
89+
return;
90+
}else{
91+
self::assertNotNull($result, "Recipe did not match expected result");
92+
}
93+
self::assertEquals($expected->getXpCost(), $result->getXpCost(), "XP cost did not match expected result");
94+
self::assertTrue($expected->getOutput()->equalsExact($result->getOutput()), "Recipe output did not match expected result");
95+
$sacrificeResult = $expected->getSacrificeResult();
96+
if($sacrificeResult !== null){
97+
$resultExpected = $result->getSacrificeResult();
98+
self::assertNotNull($resultExpected, "Recipe sacrifice result did not match expected result");
99+
self::assertTrue($sacrificeResult->equalsExact($resultExpected), "Recipe sacrifice result did not match expected result");
100+
}else{
101+
self::assertNull($result->getSacrificeResult(), "Recipe sacrifice result did not match expected result");
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)