mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-18 12:04:46 +00:00
Abstract the base functionality of StringToItemParser
This commit is contained in:
@@ -29,6 +29,7 @@ use pocketmine\block\utils\DyeColor;
|
||||
use pocketmine\block\utils\SlabType;
|
||||
use pocketmine\block\VanillaBlocks;
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use pocketmine\utils\StringToTParser;
|
||||
use function array_keys;
|
||||
use function str_replace;
|
||||
use function strtolower;
|
||||
@@ -36,18 +37,12 @@ use function trim;
|
||||
|
||||
/**
|
||||
* Handles parsing items from strings. This is used to interpret names from the /give command (and others).
|
||||
* Custom aliases may be registered.
|
||||
* Note that the aliases should be user-friendly, i.e. easily readable and writable.
|
||||
*
|
||||
* @phpstan-extends StringToTParser<Item>
|
||||
*/
|
||||
final class StringToItemParser{
|
||||
final class StringToItemParser extends StringToTParser{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var \Closure[]
|
||||
* @phpstan-var array<string, \Closure(string $input) : Item>
|
||||
*/
|
||||
private array $callbackMap = [];
|
||||
|
||||
private static function make() : self{
|
||||
$result = new self;
|
||||
|
||||
@@ -1325,41 +1320,12 @@ final class StringToItemParser{
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** @phpstan-param \Closure(string $input) : Item $callback */
|
||||
public function register(string $alias, \Closure $callback) : void{
|
||||
$key = $this->reprocess($alias);
|
||||
if(isset($this->callbackMap[$key])){
|
||||
throw new \InvalidArgumentException("Alias \"$key\" is already registered");
|
||||
}
|
||||
$this->callbackMap[$key] = $callback;
|
||||
}
|
||||
|
||||
/** @phpstan-param \Closure(string $input) : Block $callback */
|
||||
public function registerBlock(string $alias, \Closure $callback) : void{
|
||||
$this->register($alias, fn(string $input) => $callback($input)->asItem());
|
||||
}
|
||||
|
||||
/** @phpstan-param \Closure(string $input) : Item $callback */
|
||||
public function override(string $alias, \Closure $callback) : void{
|
||||
$this->callbackMap[$this->reprocess($alias)] = $callback;
|
||||
}
|
||||
|
||||
/** Tries to parse the specified string into an item. */
|
||||
public function parse(string $input) : ?Item{
|
||||
$key = $this->reprocess($input);
|
||||
if(isset($this->callbackMap[$key])){
|
||||
return ($this->callbackMap[$key])($input);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function reprocess(string $input) : string{
|
||||
return strtolower(str_replace([" ", "minecraft:"], ["_", ""], trim($input)));
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
public function getKnownAliases() : array{
|
||||
return array_keys($this->callbackMap);
|
||||
return parent::parse($input);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user