PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
XpCommand.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
34use function abs;
35use function count;
36use function str_ends_with;
37use function substr;
38
40
41 public function __construct(){
42 parent::__construct(
43 "xp",
44 KnownTranslationFactory::pocketmine_command_xp_description(),
45 KnownTranslationFactory::pocketmine_command_xp_usage()
46 );
47 $this->setPermissions([
48 DefaultPermissionNames::COMMAND_XP_SELF,
49 DefaultPermissionNames::COMMAND_XP_OTHER
50 ]);
51 }
52
53 public function execute(CommandSender $sender, string $commandLabel, array $args){
54 if(count($args) < 1){
56 }
57
58 $player = $this->fetchPermittedPlayerTarget($sender, $args[1] ?? null, DefaultPermissionNames::COMMAND_XP_SELF, DefaultPermissionNames::COMMAND_XP_OTHER);
59 if($player === null){
60 return true;
61 }
62
63 $xpManager = $player->getXpManager();
64 if(str_ends_with($args[0], "L")){
65 $xpLevelAttr = $player->getAttributeMap()->get(Attribute::EXPERIENCE_LEVEL) ?? throw new AssumptionFailedError();
66 $maxXpLevel = (int) $xpLevelAttr->getMaxValue();
67 $currentXpLevel = $xpManager->getXpLevel();
68 $xpLevels = $this->getInteger($sender, substr($args[0], 0, -1), -$currentXpLevel, $maxXpLevel - $currentXpLevel);
69 if($xpLevels >= 0){
70 $xpManager->addXpLevels($xpLevels, false);
71 $sender->sendMessage(KnownTranslationFactory::commands_xp_success_levels((string) $xpLevels, $player->getName()));
72 }else{
73 $xpLevels = abs($xpLevels);
74 $xpManager->subtractXpLevels($xpLevels);
75 $sender->sendMessage(KnownTranslationFactory::commands_xp_success_negative_levels((string) $xpLevels, $player->getName()));
76 }
77 }else{
78 $xp = $this->getInteger($sender, $args[0], max: Limits::INT32_MAX);
79 if($xp < 0){
80 $sender->sendMessage(KnownTranslationFactory::commands_xp_failure_widthdrawXp()->prefix(TextFormat::RED));
81 }else{
82 $xpManager->addXp($xp, false);
83 $sender->sendMessage(KnownTranslationFactory::commands_xp_success((string) $xp, $player->getName()));
84 }
85 }
86
87 return true;
88 }
89}
setPermissions(array $permissions)
Definition Command.php:103
execute(CommandSender $sender, string $commandLabel, array $args)
Definition XpCommand.php:53