more work on ItemCombine

This commit is contained in:
ShockedPlot7560 2025-03-23 17:14:20 +01:00
parent eaab4b35d2
commit 084feaef53
No known key found for this signature in database
GPG Key ID: D7539B420F1FA86E
6 changed files with 252 additions and 43 deletions

View File

@ -67,8 +67,7 @@ final class AnvilCraftingManagerDataFiller{
foreach(VanillaItems::getAll() as $item){
if($item instanceof Durable){
$itemId = GlobalItemDataHandlers::getSerializer()->serializeType($item)->getName();
$manager->registerAnvilRecipe(new ItemCombineRecipe(
new MetaWildcardRecipeIngredient($itemId),
$manager->registerAnvilRecipe(new ItemSelfCombineRecipe(
new MetaWildcardRecipeIngredient($itemId)
));
}

View File

@ -34,38 +34,15 @@ use function floor;
use function max;
use function min;
/**
* Represent a recipe that repair an item with a material in an anvil.
*/
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;
}
abstract class ItemCombineRecipe implements AnvilRecipe{
abstract protected function validate(Item $input, Item $material) : bool;
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);
$xpCost = 0;
if($result instanceof Durable && $material instanceof Durable){
$damage = $result->getDamage();
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));
}
if($result instanceof Durable && $material instanceof Durable && $this->repair($result, $material)){
// The two items are compatible for repair
$xpCost = 2;
}
@ -94,7 +71,7 @@ class ItemCombineRecipe implements AnvilRecipe{
}
}
$costAddition = match($enchantment->getRarity()){
$costAddition = match ($enchantment->getRarity()) {
Rarity::COMMON => 1,
Rarity::UNCOMMON => 2,
Rarity::RARE => 4,
@ -117,9 +94,30 @@ class ItemCombineRecipe implements AnvilRecipe{
);
}
return new AnvilCraftResult($xpCost, $result, null);
if($xpCost !== 0){
return new AnvilCraftResult($xpCost, $result, 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;
}
}

View 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);
}
}

View 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);
}
}

View File

@ -25,12 +25,14 @@ namespace pocketmine\crafting;
use Generator;
use PHPUnit\Framework\TestCase;
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\VanillaEnchantments;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
use function floor;
class AnvilCraftTest extends TestCase{
public static function materialRepairRecipe() : Generator{
public static function materialRepairRecipeProvider() : Generator{
yield "No repair available" => [
VanillaItems::DIAMOND_PICKAXE(),
VanillaItems::DIAMOND(),
@ -76,29 +78,117 @@ class AnvilCraftTest extends TestCase{
}
/**
* @dataProvider materialRepairRecipe
* @dataProvider materialRepairRecipeProvider
*/
public function testMaterialRepairRecipe(Item $base, Item $material, ?AnvilCraftResult $expected) : void{
$recipe = new MaterialRepairRecipe(
new ExactRecipeIngredient((clone $base)->setCount(1)),
new ExactRecipeIngredient((clone $material)->setCount(1))
new WildcardRecipeIngredient(),
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){
self::assertNull($result, "Recipe did not match expected result");
self::assertNull($actual, "Recipe did not match expected result");
return;
}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::assertTrue($expected->getOutput()->equalsExact($result->getOutput()), "Recipe output did not match expected result");
self::assertEquals($expected->getXpCost(), $actual->getXpCost(), "XP cost did not match expected result");
self::assertTrue($expected->getOutput()->equalsExact($actual->getOutput()), "Recipe output did not match expected result");
$sacrificeResult = $expected->getSacrificeResult();
if($sacrificeResult !== null){
$resultExpected = $result->getSacrificeResult();
$resultExpected = $actual->getSacrificeResult();
self::assertNotNull($resultExpected, "Recipe sacrifice result did not match expected result");
self::assertTrue($sacrificeResult->equalsExact($resultExpected), "Recipe sacrifice result did not match expected result");
}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");
}
}
}

View 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;
}
}