-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement anvil #6418
Open
ShockedPlot7560
wants to merge
12
commits into
minor-next
Choose a base branch
from
feat/anvil
base: minor-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement anvil #6418
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e4f979d
first look at anvil
ShockedPlot7560 44c3e03
fix PHPstan
ShockedPlot7560 54f746f
finalize anvil transaction
ShockedPlot7560 726e2cb
Add anvil event
ShockedPlot7560 804731d
fix PHPstan
ShockedPlot7560 654b444
add sound and anvil damage
ShockedPlot7560 b1a773c
Merge branch 'minor-next' into feat/anvil
ShockedPlot7560 1cc809c
Merge branch 'minor-next' into feat/anvil
dktapps 7cfb6ee
first look at anvil actions
ShockedPlot7560 b9df798
made AnvilAction constructor final
ShockedPlot7560 c77a72f
some work on anvil
ShockedPlot7560 947c8a0
remove phpstan docs
ShockedPlot7560 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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\block\anvil; | ||
|
||
use pocketmine\item\Item; | ||
|
||
abstract class AnvilAction{ | ||
protected int $xpCost = 0; | ||
|
||
final public function __construct( | ||
protected Item $base, | ||
protected Item $material, | ||
protected ?string $customName | ||
){ } | ||
|
||
/** | ||
* Returns the XP cost requested for this action. | ||
* This XP cost will be summed up to the total XP cost of the anvil operation. | ||
*/ | ||
final public function getXpCost() : int{ | ||
return $this->xpCost; | ||
} | ||
|
||
/** | ||
* If only actions marked as free of repair cost is applied, the result item | ||
* will not have any repair cost increase. | ||
*/ | ||
public function isFreeOfRepairCost() : bool { | ||
return false; | ||
} | ||
|
||
/** | ||
* Processing an action means applying the changes to the result item | ||
* and updating the XP cost property of the action. | ||
*/ | ||
abstract public function process(Item $resultItem) : void; | ||
|
||
/** | ||
* Returns whether this action is valid and can be applied. | ||
*/ | ||
abstract public function canBeApplied() : bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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\block\anvil; | ||
|
||
use pocketmine\item\Item; | ||
use pocketmine\utils\SingletonTrait; | ||
use function is_subclass_of; | ||
|
||
final class AnvilActionsFactory{ | ||
use SingletonTrait; | ||
|
||
/** @var array<class-string<AnvilAction>, true> */ | ||
private array $actions = []; | ||
|
||
private function __construct(){ | ||
$this->register(RenameItemAction::class); | ||
$this->register(CombineEnchantmentsAction::class); | ||
$this->register(RepairWithSacrificeAction::class); | ||
$this->register(RepairWithMaterialAction::class); | ||
} | ||
|
||
/** | ||
* @param class-string<AnvilAction> $class | ||
*/ | ||
public function register(string $class) : void{ | ||
if(!is_subclass_of($class, AnvilAction::class, true)){ | ||
throw new \InvalidArgumentException("Class $class is not an AnvilAction"); | ||
} | ||
if(isset($this->actions[$class])){ | ||
throw new \InvalidArgumentException("Class $class is already registered"); | ||
} | ||
$this->actions[$class] = true; | ||
} | ||
|
||
/** | ||
* Return all available actions for the given items. | ||
* | ||
* @return AnvilAction[] | ||
*/ | ||
public function getActions(Item $base, Item $material, ?string $customName) : array{ | ||
$actions = []; | ||
foreach($this->actions as $class => $_){ | ||
$action = new $class($base, $material, $customName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I really don't like this. |
||
if($action->canBeApplied()){ | ||
$actions[] = $action; | ||
} | ||
} | ||
return $actions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\block\anvil; | ||
|
||
use pocketmine\inventory\transaction\TransactionValidationException; | ||
use pocketmine\item\EnchantedBook; | ||
use pocketmine\item\enchantment\AvailableEnchantmentRegistry; | ||
use pocketmine\item\enchantment\EnchantmentInstance; | ||
use pocketmine\item\enchantment\Rarity; | ||
use pocketmine\item\Item; | ||
use function floor; | ||
use function max; | ||
use function min; | ||
|
||
final class CombineEnchantmentsAction extends AnvilAction{ | ||
public function canBeApplied() : bool{ | ||
return $this->material->hasEnchantments(); | ||
} | ||
|
||
public function process(Item $resultItem) : void{ | ||
foreach($this->material->getEnchantments() as $instance){ | ||
$enchantment = $instance->getType(); | ||
$level = $instance->getLevel(); | ||
if(!AvailableEnchantmentRegistry::getInstance()->isAvailableForItem($enchantment, $this->base)){ | ||
continue; | ||
} | ||
if(($targetEnchantment = $this->base->getEnchantment($enchantment)) !== null){ | ||
// Enchant already present on the target item | ||
$targetLevel = $targetEnchantment->getLevel(); | ||
$newLevel = ($targetLevel === $level ? $targetLevel + 1 : max($targetLevel, $level)); | ||
$level = min($newLevel, $enchantment->getMaxLevel()); | ||
$instance = new EnchantmentInstance($enchantment, $level); | ||
}else{ | ||
// Check if the enchantment is compatible with the existing enchantments | ||
foreach($this->base->getEnchantments() as $testedInstance){ | ||
$testedEnchantment = $testedInstance->getType(); | ||
if(!$testedEnchantment->isCompatibleWith($enchantment)){ | ||
$this->xpCost++; | ||
continue 2; | ||
} | ||
} | ||
} | ||
|
||
$costAddition = match($enchantment->getRarity()){ | ||
Rarity::COMMON => 1, | ||
Rarity::UNCOMMON => 2, | ||
Rarity::RARE => 4, | ||
Rarity::MYTHIC => 8, | ||
default => throw new TransactionValidationException("Invalid rarity " . $enchantment->getRarity() . " found") | ||
}; | ||
|
||
if($this->material instanceof EnchantedBook){ | ||
// Enchanted books are half as expensive to combine | ||
$costAddition = max(1, $costAddition / 2); | ||
} | ||
$levelDifference = $instance->getLevel() - $this->base->getEnchantmentLevel($instance->getType()); | ||
$this->xpCost += (int) floor($costAddition * $levelDifference); | ||
$resultItem->addEnchantment($instance); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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\block\anvil; | ||
|
||
use pocketmine\item\Item; | ||
use function strlen; | ||
|
||
final class RenameItemAction extends AnvilAction{ | ||
private const COST = 1; | ||
|
||
public function canBeApplied() : bool{ | ||
return true; | ||
} | ||
|
||
public function process(Item $resultItem) : void{ | ||
if($this->customName === null || strlen($this->customName) === 0){ | ||
if($this->base->hasCustomName()){ | ||
$this->xpCost += self::COST; | ||
$resultItem->clearCustomName(); | ||
} | ||
}else{ | ||
if($this->base->getCustomName() !== $this->customName){ | ||
$this->xpCost += self::COST; | ||
$resultItem->setCustomName($this->customName); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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\block\anvil; | ||
|
||
use pocketmine\item\Durable; | ||
use pocketmine\item\Item; | ||
use function assert; | ||
use function ceil; | ||
use function floor; | ||
use function max; | ||
use function min; | ||
|
||
final class RepairWithMaterialAction extends AnvilAction{ | ||
private const COST = 1; | ||
|
||
public function canBeApplied() : bool{ | ||
return $this->base instanceof Durable && | ||
$this->base->isValidRepairMaterial($this->material) && | ||
$this->base->getDamage() > 0; | ||
} | ||
|
||
public function process(Item $resultItem) : void{ | ||
assert($resultItem instanceof Durable, "Result item must be durable"); | ||
assert($this->base instanceof Durable, "Base item must be durable"); | ||
|
||
$damage = $this->base->getDamage(); | ||
$quarter = min($damage, (int) floor($this->base->getMaxDurability() / 4)); | ||
$numberRepair = min($this->material->getCount(), (int) ceil($damage / $quarter)); | ||
if($numberRepair > 0){ | ||
$this->material->pop($numberRepair); | ||
$damage -= $quarter * $numberRepair; | ||
} | ||
$resultItem->setDamage(max(0, $damage)); | ||
|
||
$this->xpCost = (int) floor($numberRepair * self::COST); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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\block\anvil; | ||
|
||
use pocketmine\item\Durable; | ||
use pocketmine\item\Item; | ||
use function assert; | ||
use function min; | ||
|
||
final class RepairWithSacrificeAction extends AnvilAction{ | ||
private const COST = 2; | ||
|
||
public function canBeApplied() : bool{ | ||
return $this->base instanceof Durable && | ||
$this->material instanceof Durable && | ||
$this->base->getTypeId() === $this->material->getTypeId(); | ||
} | ||
|
||
public function process(Item $resultItem) : void{ | ||
assert($resultItem instanceof Durable, "Result item must be durable"); | ||
assert($this->base instanceof Durable, "Base item must be durable"); | ||
assert($this->material instanceof Durable, "Material item must be durable"); | ||
|
||
if($this->base->getDamage() !== 0){ | ||
$baseMaxDurability = $this->base->getMaxDurability(); | ||
$baseDurability = $baseMaxDurability - $this->base->getDamage(); | ||
$materialDurability = $this->material->getMaxDurability() - $this->material->getDamage(); | ||
$addDurability = (int) ($baseMaxDurability * 12 / 100); | ||
|
||
$newDurability = min($baseMaxDurability, $baseDurability + $materialDurability + $addDurability); | ||
|
||
$resultItem->setDamage($baseMaxDurability - $newDurability); | ||
|
||
$this->xpCost = self::COST; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to be placed in block namespace? Wouldn’t be better to move anvil inventory actions logic into the
inventory\transaction
namespace since it’s more associated with anvil transactions rather than block itself?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inventory\transaction
namespace is intended for handling player's requested ones, but in this use case what we are actually doing is creating fictitious actions for the use of the anvil.