Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

jasonw4331/ScoreboardAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ScoreboardAPI

Discord Poggit-Ci Download count

Basic API

Imports

You will want all of these classes imported where you are using ScoreboardAPI.

use jasonwynn10\ScoreboardAPI\Scoreboard;
use jasonwynn10\ScoreboardAPI\ScoreboardAPI;
use jasonwynn10\ScoreboardAPI\ScoreboardEntry;

Creating a scoreboard

Scoreboard instances can be created through the ScoreboardAPI class. This method defaults to using the sidebar with scores in ascending order.

/** @var PluginBase $this */
$api = $this->getServer()->getPluginManager()->getPlugin("ScoreboardAPI");
$scoreboard = $api->createScoreboard("objective", "Scoreboard Title"); // assumes sidebar in ascending order

Adding an entry

Scoreboards aren't just titles. They need something to fill their lines!

Entries can be created and added to scoreboards through the Scoreboard instance.

$line = 1; // line number
$score = 1; // current score
$type = ScoreboardEntry::TYPE_FAKE_PLAYER; // other types are TYPE_PLAYER and TYPE_ENTITY
$identifier = "line 1"; // this is a string for fake players but must be an entity id for other types
/** @var Scoreboard $scoreboard */
$entry = $scoreboard->createEntry($line, $score, $type, $identifier);
$scoreboard->addEntry($entry);

Once an entry as been added to the scoreboard, all scoreboard viewers will automatically be able to see it.

Removing an Entry

Entries can be removed from scoreboards through the Scoreboard instance.

/** @var Scoreboard $scoreboard */
$entry = $scoreboard->createEntry(1, 1, ScoreboardEntry::TYPE_FAKE_PLAYER, "Line 1");
$scoreboard->addEntry($entry); // entry added

$scoreboard->removeEntry($entry); // remove entry

Once an entry as been removed from the scoreboard, all scoreboard viewers will automatically be able to see it.

Updating an entry

So now you have your entries all on the board, but you need to change one.

Entries can be updated through the Scoreboard instance.

/** @var Scoreboard $scoreboard */
$scoreboard->addEntry($entry); // entry added
$entry->score++; // update score
$entry->customName = "Line ".$entry->score; // update custom name
$scoreboard->updateEntry($entry); // update changed entry

Sending a scoreboard

Now that you've prepared your scoreboard, it needs sent to the players!

Scoreboards can be sent through the ScoreboardAPI class.

/** @var PluginBase $this */
$api = $this->getServer()->getPluginManager()->getPlugin("ScoreboardAPI");
$scoreboard = $api->createScoreboard("objective", "Scoreboard Title");

$api->sendScoreboard($scoreboard); // send scoreboard to everyone

Deleting a scoreboard

Let's say you don't want the scoreboard to show to people anymore.

Scoreboards can be removed through the ScoreboardAPI class.

/** @var PluginBase $this */
$api = $this->getServer()->getPluginManager()->getPlugin("ScoreboardAPI");
$scoreboard = $api->createScoreboard("objective", "Scoreboard Title");
$api->sendScoreboard($scoreboard); // scoreboard sent to everyone

$api->removeScoreboard($scoreboard); // remove scoreboard from everyone

Advanced API

Alternate Scoreboard Creation

Scoreboard instances can be created through the ScoreboardAPI class. This method defaults to using the sidebar with scores in ascending order, but can be changed to use either the LIST or BELOWNAME slot in descending order.

/** @var PluginBase $this */
$api = $this->getServer()->getPluginManager()->getPlugin("ScoreboardAPI");
$scoreboard = $api->createScoreboard("objective", "Scoreboard Title", Scoreboard::SLOT_LIST, Scoreboard::SORT_DESCENDING); // scoreboard is in list slot in descending order

Scoreboard Viewers

In ScoreboardAPI::sendScoreboard(), the second parameter can be set for specific scoreboard viewers to be added.

/** @var PluginBase $this */
$api = $this->getServer()->getPluginManager()->getPlugin("ScoreboardAPI");
$scoreboard = $api->createScoreboard("objective", "Scoreboard Title"); //create scoreboard

/** @var Player $player */
$api->sendScoreboard($scoreboard, [$player]); // scoreboard sent to player

Like sending, the second parameter in ScoreboardAPI::removeScoreboard() can be set for specific scoreboard viewers to be removed.

/** @var PluginBase $this */
$api = $this->getServer()->getPluginManager()->getPlugin("ScoreboardAPI");
$scoreboard = $api->createScoreboard("objective", "Scoreboard Title");
/** @var Player $player */
$api->sendScoreboard($scoreboard); // scoreboard sent to everyone

$api->removeScoreboard($scoreboard, [$player]); // scoreboard removed from player

Entry Viewers

Entry viewers can be set when adding the entry to the scoreboard via the second parameter of Scoreboard::addEntry()

$line = 1; // line number
$score = 1; // current score
$type = ScoreboardEntry::TYPE_FAKE_PLAYER; // other types are TYPE_PLAYER and TYPE_ENTITY
$identifier = "line 1"; // this is a string for fake players but must be an entity id for other types
/** @var Scoreboard $scoreboard */
$entry = $scoreboard->createEntry($line, $score, $type, $identifier);
/** @var Player $player */
$scoreboard->addEntry($entry, [$player]); // add entry to scoreboard for player

Players can be specified for removing the entry via the second parameter of Scoreboard::removeEntry()

$line = 1; // line number
$score = 1; // current score
$type = ScoreboardEntry::TYPE_FAKE_PLAYER; // other types are TYPE_PLAYER and TYPE_ENTITY
$identifier = "line 1"; // this is a string for fake players but must be an entity id for other types
/** @var Scoreboard $scoreboard */
$entry = $scoreboard->createEntry($line, $score, $type, $identifier);
/** @var Player $player */
$scoreboard->removeEntry($entry, [$player]); // remove entry only for player

Updating entries works the similar to adding and removing them by use of the second parameter in Scoreboard::updateEntry()

/** @var Scoreboard $scoreboard */
$scoreboard->addEntry($entry); // add entry
$entry->score++; // update score
$entry->customName = "Line ".$entry->score; // update custom name
/** @var Player $player */
$scoreboard->updateEntry($entry, [$player]); // update changed entry for player

Once an entry as been added or removed, all specified viewers will be able to see the changes immediately.

Scoreboard Padding

Padding the scoreboard will offset the text of each entry from the score with the most digits. Padding does not affect entries which use entity ids.

/** @var Scoreboard $scoreboard */
$scoreboard->padEntries();

Entry Padding

Entry padding can only be done with Fake Player types. Padding offsets the text from the score by the score with the most digits in the scoreboard.

/** @var ScoreboardEntry $entry */
$entry->pad();

About

A simple API for creating scoreboards on PocketMine-MP servers

Topics

Resources

License

Stars

Watchers

Forks

Languages