|
9 | 9 | namespace VectorNetworkProject\TheMix\game\level;
|
10 | 10 |
|
11 | 11 | use pocketmine\Player;
|
| 12 | +use pocketmine\Server; |
| 13 | +use VectorNetworkProject\TheMix\event\game\PlayerXpChangeEvent; |
| 14 | +use VectorNetworkProject\TheMix\provider\JSON; |
12 | 15 |
|
13 | 16 | class XP
|
14 | 17 | {
|
| 18 | + /* @var string */ |
| 19 | + public const XP = 'xp'; |
| 20 | + |
| 21 | + /* @var string */ |
| 22 | + public const MAX = 'max'; |
| 23 | + |
15 | 24 | /**
|
16 | 25 | * @param Player $player
|
17 | 26 | * @param int $xp
|
| 27 | + * |
18 | 28 | * @return void
|
19 | 29 | */
|
20 | 30 | public static function setXP(Player $player, int $xp): void
|
21 | 31 | {
|
22 |
| - //TODO: |
| 32 | + $event = new PlayerXpChangeEvent($player, $xp); |
| 33 | + Server::getInstance()->getPluginManager()->callEvent($event); |
| 34 | + if (!$event->isCancelled()) { |
| 35 | + $db = new JSON($player->getXuid(), Level::FILE_NAME); |
| 36 | + $db->set(self::XP, $xp); |
| 37 | + } |
23 | 38 | }
|
24 | 39 |
|
25 | 40 | /**
|
26 | 41 | * @param Player $player
|
| 42 | + * @param int $min |
| 43 | + * @param int $max |
| 44 | + * |
27 | 45 | * @return void
|
28 | 46 | */
|
29 |
| - public static function addXP(Player $player): void |
| 47 | + public static function addXP(Player $player, int $min = 10, int $max = 15): void |
30 | 48 | {
|
31 |
| - //TODO: |
| 49 | + $xp = mt_rand($min, $max); |
| 50 | + $event = new PlayerXpChangeEvent($player, $xp); |
| 51 | + Server::getInstance()->getPluginManager()->callEvent($event); |
| 52 | + if (!$event->isCancelled()) { |
| 53 | + $db = new JSON($player->getXuid(), Level::FILE_NAME); |
| 54 | + $db->set(self::XP, self::getXP($player) + $xp); |
| 55 | + } |
32 | 56 | }
|
33 | 57 |
|
34 | 58 | /**
|
35 | 59 | * @param Player $player
|
| 60 | + * |
36 | 61 | * @return int
|
37 | 62 | */
|
38 | 63 | public static function getXP(Player $player): int
|
39 | 64 | {
|
40 |
| - //TODO: |
| 65 | + $db = new JSON($player->getXuid(), Level::FILE_NAME); |
| 66 | + return $db->get(self::XP); |
41 | 67 | }
|
42 | 68 | }
|
0 commit comments