mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-19 23:37:45 +00:00
Fix StringToItemParser::override() not fully overriding aliases (#6835)
This commit is contained in:
@@ -38,6 +38,7 @@ use pocketmine\item\VanillaItems as Items;
|
|||||||
use pocketmine\utils\SingletonTrait;
|
use pocketmine\utils\SingletonTrait;
|
||||||
use pocketmine\utils\StringToTParser;
|
use pocketmine\utils\StringToTParser;
|
||||||
use function array_keys;
|
use function array_keys;
|
||||||
|
use function count;
|
||||||
use function strtolower;
|
use function strtolower;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1597,6 +1598,20 @@ final class StringToItemParser extends StringToTParser{
|
|||||||
$this->reverseMap[$item->getStateId()][$alias] = true;
|
$this->reverseMap[$item->getStateId()][$alias] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function override(string $alias, \Closure $callback) : void{
|
||||||
|
$oldItem = $this->parse($alias);
|
||||||
|
if($oldItem !== null){
|
||||||
|
$oldStateId = $oldItem->getStateId();
|
||||||
|
unset($this->reverseMap[$oldStateId][$alias]);
|
||||||
|
if(count($this->reverseMap[$oldStateId]) === 0){
|
||||||
|
unset($this->reverseMap[$oldStateId]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parent::override($alias, $callback);
|
||||||
|
$newItem = $callback($alias);
|
||||||
|
$this->reverseMap[$newItem->getStateId()][$alias] = true;
|
||||||
|
}
|
||||||
|
|
||||||
/** @phpstan-param \Closure(string $input) : Block $callback */
|
/** @phpstan-param \Closure(string $input) : Block $callback */
|
||||||
public function registerBlock(string $alias, \Closure $callback) : void{
|
public function registerBlock(string $alias, \Closure $callback) : void{
|
||||||
$this->register($alias, fn(string $input) => $callback($input)->asItem());
|
$this->register($alias, fn(string $input) => $callback($input)->asItem());
|
||||||
|
80
tests/phpunit/item/StringToItemParserTest.php
Normal file
80
tests/phpunit/item/StringToItemParserTest.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?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\item;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class StringToItemParserTest extends TestCase{
|
||||||
|
|
||||||
|
public function testOverrideRemovesOldAlias() : void{
|
||||||
|
$parser = new StringToItemParser();
|
||||||
|
|
||||||
|
$item1 = VanillaItems::DIAMOND();
|
||||||
|
$item2 = VanillaItems::EMERALD();
|
||||||
|
|
||||||
|
$parser->register("test_alias", fn() => $item1);
|
||||||
|
|
||||||
|
self::assertContains("test_alias", $parser->lookupAliases($item1));
|
||||||
|
self::assertNotContains("test_alias", $parser->lookupAliases($item2));
|
||||||
|
|
||||||
|
$parser->override("test_alias", fn() => $item2);
|
||||||
|
|
||||||
|
self::assertNotContains("test_alias", $parser->lookupAliases($item1));
|
||||||
|
self::assertContains("test_alias", $parser->lookupAliases($item2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOverrideWithNewAlias() : void{
|
||||||
|
$parser = new StringToItemParser();
|
||||||
|
|
||||||
|
$item = VanillaItems::DIAMOND();
|
||||||
|
|
||||||
|
$parser->override("new_alias", fn() => $item);
|
||||||
|
|
||||||
|
self::assertContains("new_alias", $parser->lookupAliases($item));
|
||||||
|
self::assertSame($item, $parser->parse("new_alias"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testOverrideMultipleAliases() : void{
|
||||||
|
$parser = new StringToItemParser();
|
||||||
|
|
||||||
|
$item1 = VanillaItems::DIAMOND();
|
||||||
|
$item2 = VanillaItems::EMERALD();
|
||||||
|
|
||||||
|
$parser->register("alias1", fn() => $item1);
|
||||||
|
$parser->register("alias2", fn() => $item1);
|
||||||
|
$parser->register("alias3", fn() => $item1);
|
||||||
|
|
||||||
|
self::assertCount(3, $parser->lookupAliases($item1));
|
||||||
|
|
||||||
|
$parser->override("alias2", fn() => $item2);
|
||||||
|
|
||||||
|
self::assertCount(2, $parser->lookupAliases($item1));
|
||||||
|
self::assertContains("alias1", $parser->lookupAliases($item1));
|
||||||
|
self::assertContains("alias3", $parser->lookupAliases($item1));
|
||||||
|
self::assertNotContains("alias2", $parser->lookupAliases($item1));
|
||||||
|
|
||||||
|
self::assertCount(1, $parser->lookupAliases($item2));
|
||||||
|
self::assertContains("alias2", $parser->lookupAliases($item2));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user