Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
OneBeuHu authored Jan 18, 2023
1 parent 7a56fc9 commit 7982abc
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: ScoreLens
api: [4.0.0]
main: onebeuhu\scorelens\Loader
version: 1.0.0
author: OneBeuHu
website: https://vk.com/itaranovilya
26 changes: 26 additions & 0 deletions src/onebeuhu/scorelens/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace onebeuhu\scorelens;

use onebeuhu\scorelens\command\HideScoreboardCommand;
use onebeuhu\scorelens\listener\EventListener;
use onebeuhu\scorelens\manager\MethodsManager;
use pocketmine\plugin\PluginBase;

class Loader extends PluginBase
{

/**
* @return void
*/
protected function onEnable(): void
{
$server = $this->getServer();

$methods = new MethodsManager($server);
$this->getServer()->getPluginManager()->registerEvents(new EventListener($methods), $this);

$server->getCommandMap()->register('player', new HideScoreboardCommand());
}

}
135 changes: 135 additions & 0 deletions src/onebeuhu/scorelens/board/ScoreBoard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace onebeuhu\scorelens\board;

use onebeuhu\scorelens\manager\MethodsManager;
use pocketmine\network\mcpe\protocol\SetDisplayObjectivePacket;
use pocketmine\network\mcpe\protocol\SetScorePacket;
use pocketmine\network\mcpe\protocol\types\ScorePacketEntry;
use pocketmine\player\Player;

class ScoreBoard
{

/**
* @var string
*/
protected string $scoreboardName = "";

/**
* @var int
*/
protected int $lineCount = 0;

/**
* @var int
*/
protected int $scoreboardId = 0;

/**
* @var Player
*/
protected Player $player;

/**
* @var MethodsManager
*/
protected MethodsManager $methodsManager;

/**
* @var array
*/
protected array $contents = [];

/**
* @param string $scoreboardName
* @param int $lineCount
* @param Player $player
*/
public function __construct(string $scoreboardName, int $lineCount, Player $player)
{

$this->methodsManager = MethodsManager::getInstance();

$this->scoreboardName = $scoreboardName;

if($lineCount > 16)
{
$this->methodsManager->getServer()->getLogger()->critical("You tried to create a scoreboard with more than 16 lines");
}
$this->lineCount = $lineCount;

for ($line = 1; $line <= $lineCount; $line++)
{

$this->setLine($line, str_repeat(" ", $line));
}

$this->player = $player;
}

/**
* Sends a scoreboard to the player if possible
*
* @return void
*/
public function sendToPlayer() : void
{
$name = $this->player->getName();

if(!$this->methodsManager->isHideList($name))
{
$this->methodsManager->removeScoreBoard($this->player, $name);

$pk = new SetDisplayObjectivePacket();
$pk->displaySlot = $pk::DISPLAY_SLOT_SIDEBAR;
$pk->objectiveName = $this->player->getName();
$pk->displayName = $this->scoreboardName;
$pk->criteriaName = "dummy";
$pk->sortOrder = 0;

$this->player->getNetworkSession()->sendDataPacket($pk);

$lines = 1;

foreach ($this->contents as $content)
{

$entryes = new ScorePacketEntry();
$entryes->type = ScorePacketEntry::TYPE_FAKE_PLAYER;
$entryes->objectiveName = $name;
$entryes->customName = $content;
$entryes->score = $lines;
$entryes->scoreboardId = $this->scoreboardId;

$pk = new SetScorePacket();
$pk->type = 0;
$pk->entries[] = $entryes;
$this->player->getNetworkSession()->sendDataPacket($pk);

$this->scoreboardId++;
$lines++;
}
}
}

/**
* Changes the line on the selected line to $content
*
* @param int $lineNumber
* @param string $content
* @return void
*/
public function setLine(int $lineNumber, string $content) : void
{
if($lineNumber > $this->lineCount)
{
$this->methodsManager->getServer()->getLogger()->warning("You are trying to change line number " . $lineNumber . " even though there are only " . $this->lineCount);

return;
}

$this->contents[$lineNumber] = $content;
}

}
32 changes: 32 additions & 0 deletions src/onebeuhu/scorelens/command/HideScoreboardCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace onebeuhu\scorelens\command;

use onebeuhu\scorelens\manager\MethodsManager;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\player\Player;

class HideScoreboardCommand extends Command
{

public function __construct()
{
parent::__construct("hsc", "Hide/Show Scoreboard", null, ["hscore"]);
}

/**
* @param CommandSender $sender
* @param string $commandLabel
* @param array $args
* @return mixed|void
*/
public function execute(CommandSender $sender, string $commandLabel, array $args)
{
if($sender instanceof Player)
{
MethodsManager::getInstance()->changeScoreboardStatus($sender);
}
}

}
38 changes: 38 additions & 0 deletions src/onebeuhu/scorelens/listener/EventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace onebeuhu\scorelens\listener;

use onebeuhu\scorelens\manager\MethodsManager;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerQuitEvent;

class EventListener implements Listener
{

/**
* @var MethodsManager|null
*/
protected ?MethodsManager $methodsManager = null;

/**
* @param MethodsManager $methodsManager
*/
public function __construct(MethodsManager $methodsManager)
{
$this->methodsManager = $methodsManager;
}

/**
* @param PlayerQuitEvent $event
* @return void
*/
public function playerQuit(PlayerQuitEvent $event) : void
{
$name = $event->getPlayer()->getName();

if($this->methodsManager->isHideList($name))
{
$this->methodsManager->unsetHideList($name);
}
}
}
123 changes: 123 additions & 0 deletions src/onebeuhu/scorelens/manager/MethodsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace onebeuhu\scorelens\manager;

use JetBrains\PhpStorm\Pure;
use pocketmine\network\mcpe\protocol\PlaySoundPacket;
use pocketmine\network\mcpe\protocol\RemoveObjectivePacket;
use pocketmine\player\Player;
use pocketmine\Server;

class MethodsManager
{

/** @VAR $playerList array */
protected array $playerList = [];

/** @VAR $hideList array */
protected array $hideList = [];

/**
* @var Server|null
*/
protected ?Server $server = null;

/**
* @var MethodsManager|null
*/
protected static ?MethodsManager $methodsmanager = null;

/**
* @param Server $server
*/
public function __construct(Server $server)
{
$this->server = $server;

self::$methodsmanager = $this;
}

/**
* @return Server
*/
public function getServer() : Server
{
return $this->server;
}

/**
* @return MethodsManager
*/
public static function getInstance() : MethodsManager
{
return self::$methodsmanager;
}

/**
* Responsible for displaying/hiding the tablet
*
* @param Player $player
* @return void
*/
public function changeScoreboardStatus(Player $player) : void
{
$name = $player->getName();

if($this->isHideList($name))
{
$this->unsetHideList($name);

$player->sendMessage("§cYou returned the scoreboard to hide it, write the same command again.");
$this->sendSoundToPlayer($player, "note.bassattack");
}
else
{
$this->removeScoreBoard($player, $name);

$this->hideList[$name] = $name;

$player->sendMessage("§aYou have hidden the ScoreBoard, to get it back, write the same command again.");
$this->sendSoundToPlayer($player, "random.orb");
}
}

/**
* @param string $name
* @return bool
*/
#[Pure] public function isHideList(string $name) : bool
{
return isset($this->hideList[$name]);
}

/**
* @param Player $player
* @param string $soundName
* @return void
*/
protected function sendSoundToPlayer(Player $player, string $soundName) : void
{
$position = $player->getPosition();

$player->getNetworkSession()->sendDataPacket(PlaySoundPacket::create($soundName, $position->getX(), $position->getY(), $position->getZ(), 1, 1));
}

/**
* @param string $name
* @return void
*/
public function unsetHideList(string $name) : void
{
unset($this->hideList[$name]);
}

/**
* @param Player $player
* @param string $objectiveName
* @return void
*/
public function removeScoreBoard(Player $player, string $objectiveName) : void
{
$player->getNetworkSession()->sendDataPacket(RemoveObjectivePacket::create($objectiveName));
}
}

0 comments on commit 7982abc

Please sign in to comment.