|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2018 VectorNetworkProject. All rights reserved. MIT license. |
| 4 | + * |
| 5 | + * GitHub: https://github.com/VectorNetworkProject/TheMix |
| 6 | + * Website: https://www.vector-network.tk |
| 7 | + */ |
| 8 | + |
| 9 | +namespace VectorNetworkProject\TheMix\lib\scoreboard; |
| 10 | + |
| 11 | + |
| 12 | +use pocketmine\network\mcpe\protocol\RemoveObjectivePacket; |
| 13 | +use pocketmine\network\mcpe\protocol\SetDisplayObjectivePacket; |
| 14 | +use pocketmine\network\mcpe\protocol\SetScorePacket; |
| 15 | +use pocketmine\network\mcpe\protocol\types\ScorePacketEntry; |
| 16 | +use pocketmine\Player; |
| 17 | + |
| 18 | +class Scoreboard |
| 19 | +{ |
| 20 | + /* @var array $scoreboards */ |
| 21 | + private static $scoreboards = []; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param Player $player |
| 25 | + */ |
| 26 | + public static function addScoreboard(Player $player): void |
| 27 | + { |
| 28 | + $packet = new SetDisplayObjectivePacket(); |
| 29 | + $packet->displayName = "§l§6The §aM§ci§ex§7"; |
| 30 | + $packet->displaySlot = "sidebar"; |
| 31 | + $packet->objectiveName = "objective"; |
| 32 | + $packet->criteriaName = "dummy"; |
| 33 | + $packet->sortOrder = 0; |
| 34 | + $player->sendDataPacket($packet); |
| 35 | + self::$scoreboards[$player->getXuid()] = "objective"; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param Player $player |
| 40 | + * @return bool |
| 41 | + */ |
| 42 | + public static function hasScoreboard(Player $player): bool |
| 43 | + { |
| 44 | + return (isset(self::$scoreboards[$player->getName()])) |
| 45 | + ? true |
| 46 | + : false; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param Player $player |
| 51 | + * @param int $line |
| 52 | + * @param string $text |
| 53 | + */ |
| 54 | + public static function setLine(Player $player, int $line, string $text): void |
| 55 | + { |
| 56 | + if (!isset(self::$scoreboards[$player->getName()])) return; |
| 57 | + if ($line < 1 || $line > 15) return; |
| 58 | + $entry = new ScorePacketEntry(); |
| 59 | + $entry->objectiveName = "objective"; |
| 60 | + $entry->type = ScorePacketEntry::TYPE_FAKE_PLAYER; |
| 61 | + $entry->customName = $text; |
| 62 | + $entry->score = $line; |
| 63 | + $entry->scoreboardId = $line; |
| 64 | + $score = new SetScorePacket(); |
| 65 | + $score->type = SetScorePacket::TYPE_CHANGE; |
| 66 | + $score->entries[] = $entry; |
| 67 | + $player->sendDataPacket($score); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param Player $player |
| 72 | + */ |
| 73 | + public static function deleteScoreboard(Player $player): void |
| 74 | + { |
| 75 | + $packet = new RemoveObjectivePacket(); |
| 76 | + $packet->objectiveName = "objective"; |
| 77 | + $player->sendDataPacket($packet); |
| 78 | + if (isset(self::$scoreboards[($player->getName())])) unset(self::$scoreboards[$player->getName()]); |
| 79 | + } |
| 80 | +} |
0 commit comments