mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-07 12:18:46 +00:00
more work on ItemCombine
This commit is contained in:
parent
eaab4b35d2
commit
084feaef53
@ -67,8 +67,7 @@ final class AnvilCraftingManagerDataFiller{
|
|||||||
foreach(VanillaItems::getAll() as $item){
|
foreach(VanillaItems::getAll() as $item){
|
||||||
if($item instanceof Durable){
|
if($item instanceof Durable){
|
||||||
$itemId = GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName();
|
$itemId = GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName();
|
||||||
$manager->registerAnvilRecipe(new ItemCombineRecipe(
|
$manager->registerAnvilRecipe(new ItemSelfCombineRecipe(
|
||||||
new MetaWildcardRecipeIngredient($itemId),
|
|
||||||
new MetaWildcardRecipeIngredient($itemId)
|
new MetaWildcardRecipeIngredient($itemId)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -34,38 +34,15 @@ use function floor;
|
|||||||
use function max;
|
use function max;
|
||||||
use function min;
|
use function min;
|
||||||
|
|
||||||
/**
|
abstract class ItemCombineRecipe implements AnvilRecipe{
|
||||||
* Represent a recipe that repair an item with a material in an anvil.
|
abstract protected function validate(Item $input, Item $material) : bool;
|
||||||
*/
|
|
||||||
class ItemCombineRecipe implements AnvilRecipe{
|
|
||||||
public function __construct(
|
|
||||||
private RecipeIngredient $input,
|
|
||||||
private RecipeIngredient $material
|
|
||||||
){ }
|
|
||||||
|
|
||||||
public function getInput() : RecipeIngredient{
|
|
||||||
return $this->input;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMaterial() : RecipeIngredient{
|
|
||||||
return $this->material;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getResultFor(Item $input, Item $material) : ?AnvilCraftResult{
|
public function getResultFor(Item $input, Item $material) : ?AnvilCraftResult{
|
||||||
if($this->input->accepts($input) && $this->material->accepts($material)){
|
if($this->validate($input, $material)){
|
||||||
$result = (clone $input);
|
$result = (clone $input);
|
||||||
$xpCost = 0;
|
$xpCost = 0;
|
||||||
if($result instanceof Durable && $material instanceof Durable){
|
if($result instanceof Durable && $material instanceof Durable && $this->repair($result, $material)){
|
||||||
$damage = $result->getDamage();
|
// The two items are compatible for repair
|
||||||
if($damage !== 0){
|
|
||||||
$baseMaxDurability = $result->getMaxDurability();
|
|
||||||
$baseDurability = $baseMaxDurability - $damage;
|
|
||||||
$materialDurability = $material->getMaxDurability() - $material->getDamage();
|
|
||||||
$addDurability = (int) ($baseMaxDurability * 12 / 100);
|
|
||||||
|
|
||||||
$result->setDamage($baseMaxDurability - min($baseMaxDurability, $baseDurability + $materialDurability + $addDurability));
|
|
||||||
}
|
|
||||||
|
|
||||||
$xpCost = 2;
|
$xpCost = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,9 +94,30 @@ class ItemCombineRecipe implements AnvilRecipe{
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($xpCost !== 0){
|
||||||
return new AnvilCraftResult($xpCost, $result, null);
|
return new AnvilCraftResult($xpCost, $result, null);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function repair(Durable $result, Durable $material) : bool{
|
||||||
|
$damage = $result->getDamage();
|
||||||
|
if($damage === 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$baseMaxDurability = $result->getMaxDurability();
|
||||||
|
$baseDurability = $baseMaxDurability - $damage;
|
||||||
|
$materialDurability = $material->getMaxDurability() - $material->getDamage();
|
||||||
|
$addDurability = (int) ($baseMaxDurability * 12 / 100);
|
||||||
|
|
||||||
|
$result->setDamage($baseMaxDurability - min(
|
||||||
|
$baseMaxDurability,
|
||||||
|
$baseDurability + $materialDurability + $addDurability
|
||||||
|
));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
41
src/crafting/ItemDifferentCombineRecipe.php
Normal file
41
src/crafting/ItemDifferentCombineRecipe.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\crafting;
|
||||||
|
|
||||||
|
use pocketmine\item\Item;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent a recipe that repair an item with a material in an anvil.
|
||||||
|
*/
|
||||||
|
class ItemDifferentCombineRecipe extends ItemCombineRecipe{
|
||||||
|
public function __construct(
|
||||||
|
private RecipeIngredient $base,
|
||||||
|
private RecipeIngredient $material
|
||||||
|
){
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function validate(Item $input, Item $material) : bool{
|
||||||
|
return $this->base->accepts($input) && $this->material->accepts($material);
|
||||||
|
}
|
||||||
|
}
|
44
src/crafting/ItemSelfCombineRecipe.php
Normal file
44
src/crafting/ItemSelfCombineRecipe.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\crafting;
|
||||||
|
|
||||||
|
use pocketmine\item\Item;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent a recipe that repair an item with a material in an anvil.
|
||||||
|
*/
|
||||||
|
class ItemSelfCombineRecipe extends ItemCombineRecipe{
|
||||||
|
/**
|
||||||
|
* @param RecipeIngredient $target The item that will be concerned by the combinaison.
|
||||||
|
* The input and material have to be accepted by this ingredient to be able to combine them.
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
private RecipeIngredient $target
|
||||||
|
){
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function validate(Item $input, Item $material) : bool{
|
||||||
|
return $this->target->accepts($input) && $this->target->accepts($material);
|
||||||
|
}
|
||||||
|
}
|
@ -25,12 +25,14 @@ namespace pocketmine\crafting;
|
|||||||
|
|
||||||
use Generator;
|
use Generator;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use pocketmine\item\enchantment\EnchantmentInstance;
|
||||||
|
use pocketmine\item\enchantment\VanillaEnchantments;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\item\VanillaItems;
|
use pocketmine\item\VanillaItems;
|
||||||
use function floor;
|
use function floor;
|
||||||
|
|
||||||
class AnvilCraftTest extends TestCase{
|
class AnvilCraftTest extends TestCase{
|
||||||
public static function materialRepairRecipe() : Generator{
|
public static function materialRepairRecipeProvider() : Generator{
|
||||||
yield "No repair available" => [
|
yield "No repair available" => [
|
||||||
VanillaItems::DIAMOND_PICKAXE(),
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
VanillaItems::DIAMOND(),
|
VanillaItems::DIAMOND(),
|
||||||
@ -76,29 +78,117 @@ class AnvilCraftTest extends TestCase{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider materialRepairRecipe
|
* @dataProvider materialRepairRecipeProvider
|
||||||
*/
|
*/
|
||||||
public function testMaterialRepairRecipe(Item $base, Item $material, ?AnvilCraftResult $expected) : void{
|
public function testMaterialRepairRecipe(Item $base, Item $material, ?AnvilCraftResult $expected) : void{
|
||||||
$recipe = new MaterialRepairRecipe(
|
$recipe = new MaterialRepairRecipe(
|
||||||
new ExactRecipeIngredient((clone $base)->setCount(1)),
|
new WildcardRecipeIngredient(),
|
||||||
new ExactRecipeIngredient((clone $material)->setCount(1))
|
new WildcardRecipeIngredient()
|
||||||
);
|
);
|
||||||
$result = $recipe->getResultFor($base, $material);
|
self::assertAnvilCraftResultEquals($expected, $recipe->getResultFor($base, $material));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function itemSelfCombineRecipeProvider() : Generator{
|
||||||
|
yield "Combine two identical items without damage and enchants" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
|
null
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Enchant on base item and no enchants on material with no damage" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
|
null
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "No enchant on base item and one enchant on material" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)), null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with damage" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->setDamage(10),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
|
new AnvilCraftResult(1, VanillaItems::DIAMOND_PICKAXE()->setDamage(0), null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with damage for material" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE(),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->setDamage(10),
|
||||||
|
null
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with different enchantments" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::EFFICIENCY(), 2)),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE()
|
||||||
|
->addEnchantment(new EnchantmentInstance(VanillaEnchantments::EFFICIENCY(), 2))
|
||||||
|
->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with different enchantments with damage" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::EFFICIENCY(), 2))->setDamage(10),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
new AnvilCraftResult(4, VanillaItems::DIAMOND_PICKAXE()
|
||||||
|
->addEnchantment(new EnchantmentInstance(VanillaEnchantments::EFFICIENCY(), 2))
|
||||||
|
->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with different enchantments with damage for material" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::EFFICIENCY(), 2)),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1))->setDamage(10),
|
||||||
|
new AnvilCraftResult(2, VanillaItems::DIAMOND_PICKAXE()
|
||||||
|
->addEnchantment(new EnchantmentInstance(VanillaEnchantments::EFFICIENCY(), 2))
|
||||||
|
->addEnchantment(new EnchantmentInstance(VanillaEnchantments::UNBREAKING(), 1)),
|
||||||
|
null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with same enchantment level" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 1)),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 1)),
|
||||||
|
new AnvilCraftResult(8, VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 2)), null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with same enchantment level and damage" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 1))->setDamage(10),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 1)),
|
||||||
|
new AnvilCraftResult(10, VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 2))->setDamage(0), null)
|
||||||
|
];
|
||||||
|
|
||||||
|
yield "Combine two identical items with same enchantment level and damage for material" => [
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 1)),
|
||||||
|
VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 1))->setDamage(10),
|
||||||
|
new AnvilCraftResult(8, VanillaItems::DIAMOND_PICKAXE()->addEnchantment(new EnchantmentInstance(VanillaEnchantments::FORTUNE(), 2)), null)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider itemSelfCombineRecipeProvider
|
||||||
|
*/
|
||||||
|
public function testItemSelfCombineRecipe(Item $base, Item $combined, ?AnvilCraftResult $expected) : void{
|
||||||
|
$recipe = new ItemSelfCombineRecipe(new WildcardRecipeIngredient());
|
||||||
|
self::assertAnvilCraftResultEquals($expected, $recipe->getResultFor($base, $combined));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function assertAnvilCraftResultEquals(?AnvilCraftResult $expected, ?AnvilCraftResult $actual) : void{
|
||||||
if($expected === null){
|
if($expected === null){
|
||||||
self::assertNull($result, "Recipe did not match expected result");
|
self::assertNull($actual, "Recipe did not match expected result");
|
||||||
return;
|
return;
|
||||||
}else{
|
}else{
|
||||||
self::assertNotNull($result, "Recipe did not match expected result");
|
self::assertNotNull($actual, "Recipe did not match expected result");
|
||||||
}
|
}
|
||||||
self::assertEquals($expected->getXpCost(), $result->getXpCost(), "XP cost did not match expected result");
|
self::assertEquals($expected->getXpCost(), $actual->getXpCost(), "XP cost did not match expected result");
|
||||||
self::assertTrue($expected->getOutput()->equalsExact($result->getOutput()), "Recipe output did not match expected result");
|
self::assertTrue($expected->getOutput()->equalsExact($actual->getOutput()), "Recipe output did not match expected result");
|
||||||
$sacrificeResult = $expected->getSacrificeResult();
|
$sacrificeResult = $expected->getSacrificeResult();
|
||||||
if($sacrificeResult !== null){
|
if($sacrificeResult !== null){
|
||||||
$resultExpected = $result->getSacrificeResult();
|
$resultExpected = $actual->getSacrificeResult();
|
||||||
self::assertNotNull($resultExpected, "Recipe sacrifice result did not match expected result");
|
self::assertNotNull($resultExpected, "Recipe sacrifice result did not match expected result");
|
||||||
self::assertTrue($sacrificeResult->equalsExact($resultExpected), "Recipe sacrifice result did not match expected result");
|
self::assertTrue($sacrificeResult->equalsExact($resultExpected), "Recipe sacrifice result did not match expected result");
|
||||||
}else{
|
}else{
|
||||||
self::assertNull($result->getSacrificeResult(), "Recipe sacrifice result did not match expected result");
|
self::assertNull($actual->getSacrificeResult(), "Recipe sacrifice result did not match expected result");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
37
tests/phpunit/crafting/WildcardRecipeIngredient.php
Normal file
37
tests/phpunit/crafting/WildcardRecipeIngredient.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\crafting;
|
||||||
|
|
||||||
|
use pocketmine\item\Item;
|
||||||
|
|
||||||
|
final class WildcardRecipeIngredient implements RecipeIngredient{
|
||||||
|
|
||||||
|
public function __toString() : string{
|
||||||
|
return "WildcardRecipeIngredient()";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accepts(Item $item) : bool{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user