Abstract the base functionality of StringToItemParser

This commit is contained in:
Dylan K. Taylor 2021-11-08 18:37:05 +00:00
parent a6f6b60bed
commit 18f5fb66bb
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 87 additions and 39 deletions

View File

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

View File

@ -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\utils;
use function array_keys;
use function str_replace;
use function strtolower;
use function trim;
/**
* Handles parsing any Minecraft thing from strings. This can be used, for example, to implement a user-friendly item
* parser to be used by 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-template T
*/
abstract class StringToTParser{
/**
* @var \Closure[]
* @phpstan-var array<string, \Closure(string $input) : T>
*/
private array $callbackMap = [];
/** @phpstan-param \Closure(string $input) : T $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) : T $callback */
public function override(string $alias, \Closure $callback) : void{
$this->callbackMap[$this->reprocess($alias)] = $callback;
}
/**
* Tries to parse the specified string into an enchantment.
* @phpstan-return T|null
*/
public function parse(string $input){
$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);
}
}