Skip to content

Commit

Permalink
Commands about and reload
Browse files Browse the repository at this point in the history
  • Loading branch information
AIPTU committed Jun 25, 2024
1 parent b200dab commit d791c4d
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
9 changes: 9 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ permissions:
smaccer.bypass.cooldown:
description: Allows players to bypass cooldown.
default: op
smaccer.command.about:
description: Allows players to displays information about the plugin (/smaccer about).
default: op
smaccer.command.create.self:
description: Allows players to create their own entities (/smaccer create).
default: op
Expand Down Expand Up @@ -37,6 +40,12 @@ permissions:
smaccer.command.move.others:
description: Allows players to move an entity to another player (/smaccer move).
default: op
smaccer.command.reload.config:
description: Allows players to reload the configuration (/smaccer reload).
default: op
smaccer.command.reload.emotes:
description: Allows players to teleport to reload the emotes (/smaccer reload).
default: op
smaccer.command.teleport.self:
description: Allows players to teleport to an entity (/smaccer teleport).
default: op
Expand Down
7 changes: 7 additions & 0 deletions src/aiptu/smaccer/command/SmaccerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

namespace aiptu\smaccer\command;

use aiptu\smaccer\command\subcommand\AboutSubCommand;
use aiptu\smaccer\command\subcommand\CreateSubCommand;
use aiptu\smaccer\command\subcommand\DeleteSubCommand;
use aiptu\smaccer\command\subcommand\EditSubCommand;
use aiptu\smaccer\command\subcommand\IdSubCommand;
use aiptu\smaccer\command\subcommand\ListSubCommand;
use aiptu\smaccer\command\subcommand\MoveSubCommand;
use aiptu\smaccer\command\subcommand\ReloadSubCommand;
use aiptu\smaccer\command\subcommand\TeleportSubCommand;
use aiptu\smaccer\Smaccer;
use aiptu\smaccer\utils\FormManager;
Expand Down Expand Up @@ -61,6 +63,7 @@ public function prepare() : void {
$this->addConstraint(new InGameRequiredConstraint($this));

$this->setPermissions([
Permissions::COMMAND_ABOUT,
Permissions::COMMAND_CREATE_SELF,
Permissions::COMMAND_CREATE_OTHERS,
Permissions::COMMAND_DELETE_SELF,
Expand All @@ -71,19 +74,23 @@ public function prepare() : void {
Permissions::COMMAND_LIST,
Permissions::COMMAND_MOVE_SELF,
Permissions::COMMAND_MOVE_OTHERS,
Permissions::COMMAND_RELOAD_CONFIG,
Permissions::COMMAND_RELOAD_EMOTES,
Permissions::COMMAND_TELEPORT_SELF,
Permissions::COMMAND_TELEPORT_OTHERS,
]);

$plugin = $this->getOwningPlugin();
assert($plugin instanceof Smaccer);

$this->registerSubCommand(new AboutSubCommand($plugin, 'about', 'Display plugin information', ['version', 'ver']));
$this->registerSubCommand(new CreateSubCommand($plugin, 'create', 'Create an NPC', ['add', 'spawn']));
$this->registerSubCommand(new DeleteSubCommand($plugin, 'delete', 'Delete an NPC', ['remove', 'despawn']));
$this->registerSubCommand(new EditSubCommand($plugin, 'edit', 'Edit an NPC'));
$this->registerSubCommand(new IdSubCommand($plugin, 'id', 'Check an NPC id'));
$this->registerSubCommand(new ListSubCommand($plugin, 'list', 'Get a list of NPCs in the world'));
$this->registerSubCommand(new MoveSubCommand($plugin, 'move', 'Move an NPC to a player', ['mv']));
$this->registerSubCommand(new ReloadSubCommand($plugin, 'reload', 'Reloads the configuration or emotes'));
$this->registerSubCommand(new TeleportSubCommand($plugin, 'teleport', 'Teleport to an NPC', ['tp']));
}
}
39 changes: 39 additions & 0 deletions src/aiptu/smaccer/command/argument/ReloadTypeArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\command\argument;

use CortexPE\Commando\args\StringEnumArgument;
use pocketmine\command\CommandSender;

class ReloadTypeArgument extends StringEnumArgument {
public const CONFIG = 'config';
public const EMOTES = 'emotes';

protected const VALUES = [
'config' => self::CONFIG,
'emotes' => self::EMOTES,
];

public function getTypeName() : string {
return 'reload';
}

public function getEnumName() : string {
return 'reloadType';
}

public function parse(string $argument, CommandSender $sender) : string {
return (string) $this->getValue($argument);
}
}
64 changes: 64 additions & 0 deletions src/aiptu/smaccer/command/subcommand/AboutSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\command\subcommand;

use aiptu\smaccer\Smaccer;
use aiptu\smaccer\utils\Permissions;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;
use function implode;
use function sprintf;

class AboutSubCommand extends BaseSubCommand {
/** @param list<string> $aliases */
public function __construct(
PluginBase $plugin,
string $name,
string $description = '',
array $aliases = []
) {
parent::__construct($plugin, $name, $description, $aliases);
}

public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void {
/** @var Smaccer $plugin */
$plugin = $this->plugin;

$info = [
'Name' => $plugin->getFullName(),
'Plugin API Version(s)' => implode(', ', $plugin->getDescription()->getCompatibleApis()),
'Author(s)' => implode(', ', $plugin->getDescription()->getAuthors()),
];

$sender->sendMessage(TextFormat::GREEN . 'Plugin Information:');
foreach ($info as $label => $value) {
$this->sendFormattedMessage($sender, $label, $value);
}
}

private function sendFormattedMessage(CommandSender $sender, string $label, string $value) : void {
$sender->sendMessage(sprintf(
'%s| %s | %s |',
TextFormat::GREEN,
TextFormat::WHITE . $label,
TextFormat::GREEN . $value . TextFormat::RESET
));
}

public function prepare() : void {
$this->setPermission(Permissions::COMMAND_ABOUT);
}
}
66 changes: 66 additions & 0 deletions src/aiptu/smaccer/command/subcommand/ReloadSubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\command\subcommand;

use aiptu\smaccer\command\argument\ReloadTypeArgument;
use aiptu\smaccer\Smaccer;
use aiptu\smaccer\tasks\LoadEmotesTask;
use aiptu\smaccer\utils\EmoteUtils;
use aiptu\smaccer\utils\Permissions;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;

class ReloadSubCommand extends BaseSubCommand {
/** @param list<string> $aliases */
public function __construct(
PluginBase $plugin,
string $name,
string $description = '',
array $aliases = []
) {
parent::__construct($plugin, $name, $description, $aliases);
}

public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void {
/** @var Smaccer $plugin */
$plugin = $this->plugin;

$reloadType = $args['reloadType'];

switch ($reloadType) {
case ReloadTypeArgument::CONFIG:
$plugin->reloadConfig();
$sender->sendMessage(TextFormat::GREEN . 'Configuration reloaded successfully.');
break;
case ReloadTypeArgument::EMOTES:
$plugin->getServer()->getAsyncPool()->submitTask(new LoadEmotesTask(EmoteUtils::getEmoteCachePath()));
$sender->sendMessage(TextFormat::GREEN . 'Emotes reloaded successfully.');
break;
default:
$sender->sendMessage(TextFormat::RED . 'Invalid reload type specified.');
break;
}
}

public function prepare() : void {
$this->setPermissions([
Permissions::COMMAND_RELOAD_CONFIG,
Permissions::COMMAND_RELOAD_EMOTES,
]);

$this->registerArgument(0, new ReloadTypeArgument('reloadType'));
}
}
3 changes: 3 additions & 0 deletions src/aiptu/smaccer/utils/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class Permissions {
public const BYPASS_COOLDOWN = 'smaccer.bypass.cooldown';
public const COMMAND_ABOUT = 'smaccer.command.about';
public const COMMAND_CREATE_SELF = 'smaccer.command.create.self';
public const COMMAND_CREATE_OTHERS = 'smaccer.command.create.others';
public const COMMAND_DELETE_SELF = 'smaccer.command.delete.self';
Expand All @@ -25,6 +26,8 @@ class Permissions {
public const COMMAND_LIST = 'smaccer.command.list';
public const COMMAND_MOVE_SELF = 'smaccer.command.move.self';
public const COMMAND_MOVE_OTHERS = 'smaccer.command.move.others';
public const COMMAND_RELOAD_CONFIG = 'smaccer.command.reload.config';
public const COMMAND_RELOAD_EMOTES = 'smaccer.command.reload.emotes';
public const COMMAND_TELEPORT_SELF = 'smaccer.command.teleport.self';
public const COMMAND_TELEPORT_OTHERS = 'smaccer.command.teleport.others';
}

0 comments on commit d791c4d

Please sign in to comment.