mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-03 08:39:53 +00:00
Added new dynamic StringToEnchantmentParser
this should be used instead of VanillaEnchantments::fromString(), because it allows registering custom aliases.
This commit is contained in:
parent
18f5fb66bb
commit
08420c2556
@ -26,7 +26,7 @@ namespace pocketmine\command\defaults;
|
|||||||
use pocketmine\command\CommandSender;
|
use pocketmine\command\CommandSender;
|
||||||
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
use pocketmine\command\utils\InvalidCommandSyntaxException;
|
||||||
use pocketmine\item\enchantment\EnchantmentInstance;
|
use pocketmine\item\enchantment\EnchantmentInstance;
|
||||||
use pocketmine\item\enchantment\VanillaEnchantments;
|
use pocketmine\item\enchantment\StringToEnchantmentParser;
|
||||||
use pocketmine\lang\KnownTranslationFactory;
|
use pocketmine\lang\KnownTranslationFactory;
|
||||||
use pocketmine\permission\DefaultPermissionNames;
|
use pocketmine\permission\DefaultPermissionNames;
|
||||||
use pocketmine\utils\TextFormat;
|
use pocketmine\utils\TextFormat;
|
||||||
@ -66,9 +66,8 @@ class EnchantCommand extends VanillaCommand{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
try{
|
$enchantment = StringToEnchantmentParser::getInstance()->parse($args[1]);
|
||||||
$enchantment = VanillaEnchantments::fromString($args[1]);
|
if($enchantment === null){
|
||||||
}catch(\InvalidArgumentException $e){
|
|
||||||
$sender->sendMessage(KnownTranslationFactory::commands_enchant_notFound($args[1]));
|
$sender->sendMessage(KnownTranslationFactory::commands_enchant_notFound($args[1]));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
66
src/item/enchantment/StringToEnchantmentParser.php
Normal file
66
src/item/enchantment/StringToEnchantmentParser.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?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\enchantment;
|
||||||
|
|
||||||
|
use pocketmine\utils\SingletonTrait;
|
||||||
|
use pocketmine\utils\StringToTParser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles parsing enchantments from strings. This is used to interpret names in the /enchant command.
|
||||||
|
*
|
||||||
|
* @phpstan-extends StringToTParser<Enchantment>
|
||||||
|
*/
|
||||||
|
final class StringToEnchantmentParser extends StringToTParser{
|
||||||
|
use SingletonTrait;
|
||||||
|
|
||||||
|
private static function make() : self{
|
||||||
|
$result = new self;
|
||||||
|
|
||||||
|
$result->register("blast_protection", fn() => VanillaEnchantments::BLAST_PROTECTION());
|
||||||
|
$result->register("efficiency", fn() => VanillaEnchantments::EFFICIENCY());
|
||||||
|
$result->register("feather_falling", fn() => VanillaEnchantments::FEATHER_FALLING());
|
||||||
|
$result->register("fire_aspect", fn() => VanillaEnchantments::FIRE_ASPECT());
|
||||||
|
$result->register("fire_protection", fn() => VanillaEnchantments::FIRE_PROTECTION());
|
||||||
|
$result->register("flame", fn() => VanillaEnchantments::FLAME());
|
||||||
|
$result->register("infinity", fn() => VanillaEnchantments::INFINITY());
|
||||||
|
$result->register("knockback", fn() => VanillaEnchantments::KNOCKBACK());
|
||||||
|
$result->register("mending", fn() => VanillaEnchantments::MENDING());
|
||||||
|
$result->register("power", fn() => VanillaEnchantments::POWER());
|
||||||
|
$result->register("projectile_protection", fn() => VanillaEnchantments::PROJECTILE_PROTECTION());
|
||||||
|
$result->register("protection", fn() => VanillaEnchantments::PROTECTION());
|
||||||
|
$result->register("punch", fn() => VanillaEnchantments::PUNCH());
|
||||||
|
$result->register("respiration", fn() => VanillaEnchantments::RESPIRATION());
|
||||||
|
$result->register("sharpness", fn() => VanillaEnchantments::SHARPNESS());
|
||||||
|
$result->register("silk_touch", fn() => VanillaEnchantments::SILK_TOUCH());
|
||||||
|
$result->register("thorns", fn() => VanillaEnchantments::THORNS());
|
||||||
|
$result->register("unbreaking", fn() => VanillaEnchantments::UNBREAKING());
|
||||||
|
$result->register("vanishing", fn() => VanillaEnchantments::VANISHING());
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parse(string $input) : ?Enchantment{
|
||||||
|
return parent::parse($input);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user