Skip to content

Commit

Permalink
Use tebexapi v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed Feb 13, 2023
1 parent 005db01 commit c4f2904
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .poggit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ projects:
path: ""
libs:
- src: muqsit/tebexapi/TebexApi
version: ^0.0.3
version: ^0.1.0
...
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Tebex
main: muqsit\tebex\Loader
version: 0.1.2
version: 0.1.3
author: Muqsit
api: 4.0.0
permissions:
Expand Down
6 changes: 3 additions & 3 deletions src/muqsit/tebex/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ private function init(ConnectionBasedTebexApi $api, TebexInformation $informatio
}
$this->command->setExecutor($executor);

$account = $this->information->getAccount();
$server = $this->information->getServer();
$this->getLogger()->debug("Listening to events of \"{$server->getName()}\"[#{$server->getId()}] server as \"{$account->getName()}\"[#{$account->getId()}] (latency: " . round($this->getApi()->getLatency() * 1000) . "ms)");
$account = $this->information->account;
$server = $this->information->server;
$this->getLogger()->debug("Listening to events of \"{$server->name}\"[#{$server->id}] server as \"{$account->name}\"[#{$account->id}] (latency: " . round($this->getApi()->getLatency() * 1000) . "ms)");
}

public function getApi() : TebexApi{
Expand Down
10 changes: 5 additions & 5 deletions src/muqsit/tebex/handler/TebexApiUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
final class TebexApiUtils{

public static function convertGuiItemToItem(TebexGuiItem $gui_item) : Item{
$item = StringToItemParser::getInstance()->parse($gui_item->getValue());
$item = StringToItemParser::getInstance()->parse($gui_item->value);
if($item === null){
$plugin = Server::getInstance()->getPluginManager()->getPlugin("Tebex");
if($plugin instanceof Loader){
$plugin->getLogger()->warning("Failed to parse GUI item \"{$gui_item->getValue()}\", using PAPER as fallback");
$plugin->getLogger()->warning("Failed to parse GUI item \"{$gui_item->value}\", using PAPER as fallback");
}
return VanillaItems::PAPER();
}
Expand All @@ -34,18 +34,18 @@ public static function onlineFormatCommand(TebexCommand $command, Player $player
return strtr($command->asRawString(), [
"{name}" => $gamertag,
"{player}" => $gamertag,
"{username}" => "\"{$due_player->getName()}\"",
"{username}" => "\"{$due_player->name}\"",
"{id}" => $player->getXuid()
]);
}

public static function offlineFormatCommand(TebexCommand $command, TebexDuePlayer $due_player) : string{
$gamertag = "\"{$due_player->getName()}\"";
$gamertag = "\"{$due_player->name}\"";
return strtr($command->asRawString(), [
"{name}" => $gamertag,
"{player}" => $gamertag,
"{username}" => $gamertag,
"{id}" => $due_player->getUuid() ?? ""
"{id}" => $due_player->uuid ?? ""
]);
}
}
22 changes: 11 additions & 11 deletions src/muqsit/tebex/handler/command/RegisteredTebexCommandExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ function(CommandSender $sender, Command $command, string $label, array $args) :
$this->registerSubCommand(new TebexSubCommand("info", "Fetch Tebex account, server and API info", new ClosureCommandExecutor(
function(CommandSender $sender, Command $command, string $label, array $args) : bool{
$info = $this->plugin->getInformation();
$account = $info->getAccount();
$server = $info->getServer();
$account = $info->account;
$server = $info->server;

$sender->sendMessage(
"" . TextFormat::EOL .
TextFormat::BOLD . TextFormat::WHITE . "Tebex Account" . TextFormat::RESET . TextFormat::EOL .
TextFormat::WHITE . "ID: " . TextFormat::GRAY . $account->getId() . TextFormat::EOL .
TextFormat::WHITE . "Domain: " . TextFormat::GRAY . $account->getDomain() . TextFormat::EOL .
TextFormat::WHITE . "Name: " . TextFormat::GRAY . $account->getName() . TextFormat::EOL .
TextFormat::WHITE . "Currency: " . TextFormat::GRAY . "{$account->getCurrency()->getIso4217()} ({$account->getCurrency()->getSymbol()})" . TextFormat::EOL .
TextFormat::WHITE . "Online Mode: " . TextFormat::GRAY . ($account->isOnlineModeEnabled() ? "Enabled" : "Disabled") . TextFormat::EOL .
TextFormat::WHITE . "Game Type: " . TextFormat::GRAY . $account->getGameType() . TextFormat::EOL .
TextFormat::WHITE . "Event Logging: " . TextFormat::GRAY . ($account->isLogEventsEnabled() ? "Enabled" : "Disabled") . TextFormat::EOL .
TextFormat::WHITE . "ID: " . TextFormat::GRAY . $account->id . TextFormat::EOL .
TextFormat::WHITE . "Domain: " . TextFormat::GRAY . $account->domain . TextFormat::EOL .
TextFormat::WHITE . "Name: " . TextFormat::GRAY . $account->name . TextFormat::EOL .
TextFormat::WHITE . "Currency: " . TextFormat::GRAY . "{$account->currency->iso_4217} ({$account->currency->symbol})" . TextFormat::EOL .
TextFormat::WHITE . "Online Mode: " . TextFormat::GRAY . ($account->online_mode ? "Enabled" : "Disabled") . TextFormat::EOL .
TextFormat::WHITE . "Game Type: " . TextFormat::GRAY . $account->game_type . TextFormat::EOL .
TextFormat::WHITE . "Event Logging: " . TextFormat::GRAY . ($account->log_events ? "Enabled" : "Disabled") . TextFormat::EOL .
"" . TextFormat::EOL .
TextFormat::BOLD . TextFormat::WHITE . "Tebex Server" . TextFormat::RESET . TextFormat::EOL .
TextFormat::WHITE . "ID: " . TextFormat::GRAY . $server->getId() . TextFormat::EOL .
TextFormat::WHITE . "Name: " . TextFormat::GRAY . $server->getName() . TextFormat::EOL .
TextFormat::WHITE . "ID: " . TextFormat::GRAY . $server->id . TextFormat::EOL .
TextFormat::WHITE . "Name: " . TextFormat::GRAY . $server->name . TextFormat::EOL .
"" . TextFormat::EOL .
TextFormat::BOLD . TextFormat::WHITE . "Tebex API" . TextFormat::RESET . TextFormat::EOL .
TextFormat::WHITE . "Latency: " . TextFormat::GRAY . round($this->plugin->getApi()->getLatency() * 1000) . "ms" . TextFormat::EOL .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public static function handleTypeSecret(Loader $loader, CommandSender $sender, s
$config->set("secret", $secret);
$config->save();

$account = $info->getAccount();
$server = $info->getServer();
$sender->sendMessage("Successfully logged in to server (#{$server->getId()}) {$server->getName()} as (#{$account->getId()}) {$account->getName()}!");
$account = $info->account;
$server = $info->server;
$sender->sendMessage("Successfully logged in to server (#{$server->id}) {$server->name} as (#{$account->id}) {$account->name}!");
}
}

Expand Down
26 changes: 13 additions & 13 deletions src/muqsit/tebex/handler/due/TebexDueCommandsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,26 @@ public function __construct(
$this->offline_commands_handler = new TebexDueOfflineCommandsHandler($plugin, $handler);

$api = $plugin->getApi();
$this->list = self::getListFromGameType($plugin->getInformation()->getAccount()->getGameType(), function(Player $player, TebexDuePlayerHolder $holder) use($api, $handler) : void{
$this->list = self::getListFromGameType($plugin->getInformation()->account->game_type, function(Player $player, TebexDuePlayerHolder $holder) use($api, $handler) : void{
$session = $this->list->getOnlinePlayer($player);
assert($session !== null);
$api->getQueuedOnlineCommands($holder->getPlayer()->getId(), TebexResponseHandler::onSuccess(function(TebexQueuedOnlineCommandsInfo $info) use($player, $session, $holder, $handler) : void{
$api->getQueuedOnlineCommands($holder->getPlayer()->id, TebexResponseHandler::onSuccess(function(TebexQueuedOnlineCommandsInfo $info) use($player, $session, $holder, $handler) : void{
if(!$player->isOnline()){
return;
}

$commands = $info->getCommands();
$commands = $info->commands;
$total_commands = count($commands);
$timestamp = microtime(true);
foreach($commands as $tebex_command){
$session->executeOnlineCommand($tebex_command, $holder->getPlayer(), function(bool $success) use($tebex_command, $handler, &$total_commands, $player, $holder, $timestamp) : void{
$command_string = TebexApiUtils::onlineFormatCommand($tebex_command->getCommand(), $player, $holder->getPlayer());
$command_string = TebexApiUtils::onlineFormatCommand($tebex_command->command, $player, $holder->getPlayer());
if(!$success){
$this->logger->warning("Failed to execute online command: {$command_string}");
return;
}

$command_id = $tebex_command->getId();
$command_id = $tebex_command->id;
$handler->queueCommandDeletion($command_id);
if(--$total_commands === 0){
$current_holder = $this->list->getTebexAwaitingPlayer($player);
Expand All @@ -95,7 +95,7 @@ public function __construct(
public function markAllAsExecuted(?Closure $callback = null) : void{
$this->plugin->getApi()->getDuePlayersList(TebexResponseHandler::onSuccess(function(TebexDuePlayersInfo $result) use($callback) : void{
$marked = 0;
$batches = count($result->getPlayers()) + 1;
$batches = count($result->players) + 1;

$cb = static function(int $done) use(&$marked, &$batches, $callback) : void{
$marked += $done;
Expand All @@ -108,11 +108,11 @@ public function markAllAsExecuted(?Closure $callback = null) : void{

$this->offline_commands_handler->markAllAsExecuted($cb);

foreach($result->getPlayers() as $player){
$this->plugin->getApi()->getQueuedOnlineCommands($player->getId(), TebexResponseHandler::onSuccess(function(TebexQueuedOnlineCommandsInfo $info) use($cb) : void{
$commands = $info->getCommands();
foreach($result->players as $player){
$this->plugin->getApi()->getQueuedOnlineCommands($player->id, TebexResponseHandler::onSuccess(function(TebexQueuedOnlineCommandsInfo $info) use($cb) : void{
$commands = $info->commands;
foreach($commands as $command){
$this->handler->queueCommandDeletion($command->getId());
$this->handler->queueCommandDeletion($command->id);
}
$cb(count($commands));
}));
Expand Down Expand Up @@ -163,17 +163,17 @@ public function checkDuePlayers(?Closure $reschedule_condition = null, ?Closure
$this->plugin->getApi()->getDuePlayersList(TebexResponseHandler::onSuccess(function(TebexDuePlayersInfo $result) use($reschedule_condition, $callback) : void{
$this->onFetchDuePlayers($result);
if($callback !== null){
$callback(count($result->getPlayers()));
$callback(count($result->players));
}
if($reschedule_condition !== null && $reschedule_condition()){
$next_check = $result->getMeta()->getNextCheck();
$next_check = $result->meta->next_check;
$this->plugin->getScheduler()->scheduleDelayedTask(new ClosureTask(function() use($reschedule_condition, $callback) : void{ $this->checkDuePlayers($reschedule_condition, $callback); }), $next_check * 20);
}
}));
}

private function onFetchDuePlayers(TebexDuePlayersInfo $result) : void{
$players = $result->getPlayers();
$players = $result->players;
$this->list->update($players);

$players_c = count($players);
Expand Down
18 changes: 9 additions & 9 deletions src/muqsit/tebex/handler/due/TebexDueOfflineCommandsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Loader $plugin, TebexHandler $handler, int $check_pe
public function check(?Closure $callback = null) : void{
$this->plugin->getApi()->getQueuedOfflineCommands(TebexResponseHandler::onSuccess(function(TebexQueuedOfflineCommandsInfo $info) use($callback) : void{
if($callback !== null){
$callback(count($info->getCommands()));
$callback(count($info->commands));
}
$this->onFetchDueOfflineCommands($info);
}));
Expand All @@ -49,9 +49,9 @@ public function check(?Closure $callback = null) : void{
*/
public function markAllAsExecuted(?Closure $callback = null) : void{
$this->plugin->getApi()->getQueuedOfflineCommands(TebexResponseHandler::onSuccess(function(TebexQueuedOfflineCommandsInfo $info) use($callback) : void{
$commands = $info->getCommands();
$commands = $info->commands;
foreach($commands as $command){
$this->handler->queueCommandDeletion($command->getId());
$this->handler->queueCommandDeletion($command->id);
}
if($callback !== null){
$callback(count($commands));
Expand All @@ -60,16 +60,16 @@ public function markAllAsExecuted(?Closure $callback = null) : void{
}

private function onFetchDueOfflineCommands(TebexQueuedOfflineCommandsInfo $info) : void{
$commands = $info->getCommands();
$commands = $info->commands;

$commands_c = count($commands);
$this->logger->debug("Fetched {$commands_c} offline command" . ($commands_c === 1 ? "" : "s"));

foreach($commands as $command){
$this->executeCommand($command, function(bool $success) use($command) : void{
$command_string = TebexApiUtils::offlineFormatCommand($command->getCommand(), $command->getPlayer());
$command_string = TebexApiUtils::offlineFormatCommand($command->command, $command->player);
if($success){
$command_id = $command->getId();
$command_id = $command->id;
$this->handler->queueCommandDeletion($command_id);
$this->logger->info("Executed offline command #{$command_id}: {$command_string}");
}else{
Expand All @@ -84,9 +84,9 @@ private function onFetchDueOfflineCommands(TebexQueuedOfflineCommandsInfo $info)
* @param Closure(bool) : void $callback
*/
private function executeCommand(TebexQueuedOfflineCommand $command, Closure $callback) : void{
$delay = $command->getConditions()->getDelay();
$delay = $command->conditions->delay;
if($delay > 0){
if(!isset($this->delayed[$id = $command->getId()])){
if(!isset($this->delayed[$id = $command->id])){
$this->delayed[$id] = $id;
$this->plugin->getScheduler()->scheduleDelayedTask(new ClosureTask(function() use($id, $command, $callback) : void{
$callback($this->instantlyExecuteCommand($command));
Expand All @@ -99,6 +99,6 @@ private function executeCommand(TebexQueuedOfflineCommand $command, Closure $cal
}

private function instantlyExecuteCommand(TebexQueuedOfflineCommand $command) : bool{
return Server::getInstance()->dispatchCommand(TebexCommandSender::getInstance(), TebexApiUtils::offlineFormatCommand($command->getCommand(), $command->getPlayer()));
return Server::getInstance()->dispatchCommand(TebexCommandSender::getInstance(), TebexApiUtils::offlineFormatCommand($command->command, $command->player));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public function update(array $due_players) : void{
$this->tebex_due_players_by_index = [];
foreach($due_players as $player){
$holder = new TebexDuePlayerHolder($player);
$this->tebex_due_players_by_id[$player->getId()] = $holder;
$this->tebex_due_players_by_index[$index = $this->indexer->fromTebexDuePlayer($player)] = $player->getId();
$this->tebex_due_players_by_id[$player->id] = $holder;
$this->tebex_due_players_by_index[$index = $this->indexer->fromTebexDuePlayer($player)] = $player->id;
if(isset($this->online_players[$index])){
$this->onMatch($this->online_players[$index]->getPlayer(), $holder);
}
Expand All @@ -74,7 +74,7 @@ public function update(array $due_players) : void{

public function remove(TebexDuePlayerHolder $holder) : void{
$player = $holder->getPlayer();
unset($this->tebex_due_players_by_id[$player->getId()], $this->tebex_due_players_by_index[$this->indexer->fromTebexDuePlayer($player)]);
unset($this->tebex_due_players_by_id[$player->id], $this->tebex_due_players_by_index[$this->indexer->fromTebexDuePlayer($player)]);
}

public function getTebexAwaitingPlayer(Player $player) : ?TebexDuePlayerHolder{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function fromPlayer(Player $player) : string{
}

public function fromTebexDuePlayer(TebexDuePlayer $player) : string{
return strtolower($player->getName());
return strtolower($player->name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function fromPlayer(Player $player) : string{
}

public function fromTebexDuePlayer(TebexDuePlayer $player) : string{
$xuid = $player->getUuid(); // Tebex player uuids in Minecraft: Bedrock Edition (Online) mode is xbox's xuid
return $xuid ?? throw new InvalidArgumentException("Cannot retrieve XUID of Tebex due player: {$player->getName()}");
$xuid = $player->uuid; // Tebex player uuids in Minecraft: Bedrock Edition (Online) mode is xbox's xuid
return $xuid ?? throw new InvalidArgumentException("Cannot retrieve XUID of Tebex due player: {$player->name}");
}
}
12 changes: 6 additions & 6 deletions src/muqsit/tebex/handler/due/session/TebexPlayerSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function destroy() : void{
* @param Closure(bool) : void $callback
*/
public function executeOnlineCommand(TebexQueuedOnlineCommand $command, TebexDuePlayer $due_player, Closure $callback) : void{
$conditions = $command->getConditions();
$delay = $conditions->getDelay();
$conditions = $command->conditions;
$delay = $conditions->delay;
if($delay > 0){
$this->scheduleCommandForDelay($command, $due_player, $delay * 20, $callback);
}else{
Expand All @@ -63,7 +63,7 @@ public function executeOnlineCommand(TebexQueuedOnlineCommand $command, TebexDue
* @return bool
*/
private function scheduleCommandForDelay(TebexQueuedOnlineCommand $command, TebexDuePlayer $due_player, int $delay, Closure $callback) : bool{
if(isset($this->delayed_online_command_handlers[$id = $command->getId()])){
if(isset($this->delayed_online_command_handlers[$id = $command->id])){
return false;
}

Expand All @@ -75,8 +75,8 @@ private function scheduleCommandForDelay(TebexQueuedOnlineCommand $command, Tebe
}

private function instantlyExecuteOnlineCommand(TebexQueuedOnlineCommand $command, TebexDuePlayer $due_player) : bool{
$conditions = $command->getConditions();
$slots = $conditions->getInventorySlots();
$conditions = $command->conditions;
$slots = $conditions->slots;
if($slots > 0){
$inventory = $this->player->getInventory();
$free_slots = $inventory->getSize() - count($inventory->getContents());
Expand All @@ -85,6 +85,6 @@ private function instantlyExecuteOnlineCommand(TebexQueuedOnlineCommand $command
}
}

return $this->player->getServer()->dispatchCommand(TebexCommandSender::getInstance(), TebexApiUtils::onlineFormatCommand($command->getCommand(), $this->player, $due_player));
return $this->player->getServer()->dispatchCommand(TebexCommandSender::getInstance(), TebexApiUtils::onlineFormatCommand($command->command, $this->player, $due_player));
}
}

0 comments on commit c4f2904

Please sign in to comment.