mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-25 14:57:08 +00:00
finalize anvil transaction
This commit is contained in:
parent
44c3e03598
commit
54f746fc11
@ -168,8 +168,10 @@ class AnvilHelper{
|
|||||||
$item->clearCustomName();
|
$item->clearCustomName();
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
$resultCost += self::COST_RENAME;
|
if($item->getCustomName() !== $customName){
|
||||||
$item->setCustomName($customName);
|
$resultCost += self::COST_RENAME;
|
||||||
|
$item->setCustomName($customName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $resultCost;
|
return $resultCost;
|
||||||
|
@ -23,19 +23,43 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\inventory\transaction;
|
namespace pocketmine\inventory\transaction;
|
||||||
|
|
||||||
|
use pocketmine\block\utils\AnvilHelper;
|
||||||
use pocketmine\block\utils\AnvilResult;
|
use pocketmine\block\utils\AnvilResult;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
|
use pocketmine\item\VanillaItems;
|
||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
use function count;
|
use function count;
|
||||||
|
|
||||||
class AnvilTransaction extends InventoryTransaction{
|
class AnvilTransaction extends InventoryTransaction{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Player $source,
|
Player $source,
|
||||||
private AnvilResult $anvilResult
|
private readonly AnvilResult $expectedResult,
|
||||||
|
private readonly ?string $customName
|
||||||
) {
|
) {
|
||||||
parent::__construct($source);
|
parent::__construct($source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function validateFiniteResources(int $xpSpent) : void{
|
||||||
|
$expectedXpCost = $this->expectedResult->getRepairCost();
|
||||||
|
if($xpSpent !== $expectedXpCost){
|
||||||
|
throw new TransactionValidationException("Expected the amount of xp spent to be $expectedXpCost, but received $xpSpent");
|
||||||
|
}
|
||||||
|
|
||||||
|
$xpLevel = $this->source->getXpManager()->getXpLevel();
|
||||||
|
if($xpLevel < $expectedXpCost){
|
||||||
|
throw new TransactionValidationException("Player's XP level $xpLevel is less than the required XP level $expectedXpCost");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function validateInputs(Item $base, Item $material, Item $expectedOutput) : ?AnvilResult {
|
||||||
|
$calculAttempt = AnvilHelper::calculateResult($this->source, $base, $material, $this->customName);
|
||||||
|
if($calculAttempt->getResult() === null || !$calculAttempt->getResult()->equalsExact($expectedOutput)){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $calculAttempt;
|
||||||
|
}
|
||||||
|
|
||||||
public function validate() : void{
|
public function validate() : void{
|
||||||
if(count($this->actions) < 1){
|
if(count($this->actions) < 1){
|
||||||
throw new TransactionValidationException("Transaction must have at least one action to be executable");
|
throw new TransactionValidationException("Transaction must have at least one action to be executable");
|
||||||
@ -47,14 +71,37 @@ class AnvilTransaction extends InventoryTransaction{
|
|||||||
$outputs = [];
|
$outputs = [];
|
||||||
$this->matchItems($outputs, $inputs);
|
$this->matchItems($outputs, $inputs);
|
||||||
|
|
||||||
//TODO
|
if(($outputCount = count($outputs)) !== 1){
|
||||||
|
throw new TransactionValidationException("Expected 1 output item, but received $outputCount");
|
||||||
|
}
|
||||||
|
$outputItem = $outputs[0];
|
||||||
|
|
||||||
|
if(($inputCount = count($inputs)) < 1){
|
||||||
|
throw new TransactionValidationException("Expected at least 1 input item, but received $inputCount");
|
||||||
|
}
|
||||||
|
if($inputCount > 2){
|
||||||
|
throw new TransactionValidationException("Expected at most 2 input items, but received $inputCount");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(count($inputs) < 2){
|
||||||
|
$attempt = $this->validateInputs($inputs[0], VanillaItems::AIR(), $outputItem) ??
|
||||||
|
throw new TransactionValidationException("Inputs do not match expected result");
|
||||||
|
}else{
|
||||||
|
$attempt = $this->validateInputs($inputs[0], $inputs[1], $outputItem) ??
|
||||||
|
$this->validateInputs($inputs[1], $inputs[0], $outputItem) ??
|
||||||
|
throw new TransactionValidationException("Inputs do not match expected result");
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->source->hasFiniteResources()){
|
||||||
|
$this->validateFiniteResources($attempt->getRepairCost());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute() : void{
|
public function execute() : void{
|
||||||
parent::execute();
|
parent::execute();
|
||||||
|
|
||||||
if($this->source->hasFiniteResources()){
|
if($this->source->hasFiniteResources()){
|
||||||
$this->source->getXpManager()->subtractXpLevels($this->anvilResult->getRepairCost());
|
$this->source->getXpManager()->subtractXpLevels($this->expectedResult->getRepairCost());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -356,7 +356,7 @@ class ItemStackRequestExecutor{
|
|||||||
if($window instanceof AnvilInventory){
|
if($window instanceof AnvilInventory){
|
||||||
$result = AnvilHelper::calculateResult($this->player, $window->getInput(), $window->getMaterial(), $this->request->getFilterStrings()[0] ?? null);
|
$result = AnvilHelper::calculateResult($this->player, $window->getInput(), $window->getMaterial(), $this->request->getFilterStrings()[0] ?? null);
|
||||||
if($result !== null){
|
if($result !== null){
|
||||||
$this->specialTransaction = new AnvilTransaction($this->player, $result);
|
$this->specialTransaction = new AnvilTransaction($this->player, $result, $this->request->getFilterStrings()[0] ?? null);
|
||||||
$this->setNextCreatedItem($result->getResult());
|
$this->setNextCreatedItem($result->getResult());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user