PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
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(string $namespace, string $name){
42 parent::__construct(
43 $namespace,
44 $name,
45 KnownTranslationFactory::pocketmine_command_xp_description(),
46 KnownTranslationFactory::pocketmine_command_xp_usage()
47 );
48 $this->setPermissions([
49 DefaultPermissionNames::COMMAND_XP_SELF,
50 DefaultPermissionNames::COMMAND_XP_OTHER
51 ]);
52 }
53
54 public function execute(CommandSender $sender, string $commandLabel, array $args){
55 if(count($args) < 1){
57 }
58
59 $player = $this->fetchPermittedPlayerTarget($commandLabel, $sender, $args[1] ?? null, DefaultPermissionNames::COMMAND_XP_SELF, DefaultPermissionNames::COMMAND_XP_OTHER);
60 if($player === null){
61 return true;
62 }
63
64 $xpManager = $player->getXpManager();
65 if(str_ends_with($args[0], "L")){
66 $xpLevelAttr = $player->getAttributeMap()->get(Attribute::EXPERIENCE_LEVEL) ?? throw new AssumptionFailedError();
67 $maxXpLevel = (int) $xpLevelAttr->getMaxValue();
68 $currentXpLevel = $xpManager->getXpLevel();
69 $xpLevels = $this->getInteger($sender, substr($args[0], 0, -1), -$currentXpLevel, $maxXpLevel - $currentXpLevel);
70 if($xpLevels >= 0){
71 $xpManager->addXpLevels($xpLevels, false);
72 $sender->sendMessage(KnownTranslationFactory::commands_xp_success_levels((string) $xpLevels, $player->getName()));
73 }else{
74 $xpLevels = abs($xpLevels);
75 $xpManager->subtractXpLevels($xpLevels);
76 $sender->sendMessage(KnownTranslationFactory::commands_xp_success_negative_levels((string) $xpLevels, $player->getName()));
77 }
78 }else{
79 $xp = $this->getInteger($sender, $args[0], max: Limits::INT32_MAX);
80 if($xp < 0){
81 $sender->sendMessage(KnownTranslationFactory::commands_xp_failure_widthdrawXp()->prefix(TextFormat::RED));
82 }else{
83 $xpManager->addXp($xp, false);
84 $sender->sendMessage(KnownTranslationFactory::commands_xp_success((string) $xp, $player->getName()));
85 }
86 }
87
88 return true;
89 }
90}
setPermissions(array $permissions)
Definition Command.php:106
execute(CommandSender $sender, string $commandLabel, array $args)
Definition XpCommand.php:54