Skip to content

Commit

Permalink
add remove role and custom role command
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxyw committed Mar 14, 2022
1 parent 283515a commit deeea68
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
21 changes: 21 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Sunxyw\MinecraftProtocol;

/**
* Class Config.
*/
class Config
{
public string $host;

public int $port;

public string $password;

public array $allowedRoles;

public string $assignRoleCommand;

public string $removeRoleCommand;
}
16 changes: 12 additions & 4 deletions src/Drivers/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sunxyw\MinecraftProtocol\Drivers;

use JetBrains\PhpStorm\ExpectedValues;
use Sunxyw\MinecraftProtocol\Config;

/**
* Interface DriverInterface.
Expand All @@ -12,11 +13,9 @@ interface DriverInterface
/**
* Driver constructor.
*
* @param $host
* @param $port
* @param $password
* @param Config $config
*/
public function __construct($host, $port, $password);
public function __construct(Config $config);

/**
* Dispatch a command to the server.
Expand Down Expand Up @@ -51,6 +50,15 @@ public function broadcast(string $message): void;
*/
public function assignRole(string $player, string $role): void;

/**
* Remove a player from a role.
*
* @param string $player
* @param string $role
* @return void
*/
public function removeRole(string $player, string $role): void;

/**
* Operate player's whitelist.
*
Expand Down
33 changes: 25 additions & 8 deletions src/Drivers/RemoteConsoleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sunxyw\MinecraftProtocol\Drivers;

use JetBrains\PhpStorm\ExpectedValues;
use Sunxyw\MinecraftProtocol\Config;
use Sunxyw\MinecraftProtocol\MinecraftUtils;
use xPaw\SourceQuery\Exception\SourceQueryException;
use xPaw\SourceQuery\SourceQuery;
Expand All @@ -15,13 +16,16 @@ class RemoteConsoleDriver implements DriverInterface
{
private SourceQuery $connection;

private Config $config;

/** {@inheritDoc} */
public function __construct($host, $port, $password)
public function __construct(Config $config)
{
try {
$this->connection = new SourceQuery();
$this->connection->Connect($host, $port);
$this->connection->SetRconPassword($password);
$this->connection->Connect($config->host, $config->port);
$this->connection->SetRconPassword($config->password);
$this->config = $config;
} catch (SourceQueryException $e) {
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -54,7 +58,7 @@ public function getOnlinePlayers(): array
public function broadcast(string $message, string $prefix = null): void
{
if (!empty($prefix)) {
$prefix = MinecraftUtils::buildComponent('Prefix here', 'light_purple');
$prefix = MinecraftUtils::buildComponent($prefix, 'light_purple');
} else {
$prefix = '';
}
Expand All @@ -68,12 +72,25 @@ public function assignRole(string $player, string $role): void
{
MinecraftUtils::validateUsername($player);
$role = strtolower($role);
$allowed_roles = ['primary', 'introduction'];
if (!in_array($role, $allowed_roles)) {
if (!in_array($role, $this->config->allowedRoles, true)) {
throw new \InvalidArgumentException("Role $role is not allowed");
}
$command = $this->config->assignRoleCommand;
$command = str_replace(['{player}', '{role}'], [$player, $role], $command);
$this->dispatchCommand($command);
}

/** {@inheritDoc} */
public function removeRole(string $player, string $role): void
{
MinecraftUtils::validateUsername($player);
$role = strtolower($role);
if (!in_array($role, $this->config->allowedRoles, true)) {
throw new \InvalidArgumentException("Role $role is not allowed");
}
// TODO: support others permission plugin
$this->dispatchCommand("manuadd $player $role");
$command = $this->config->removeRoleCommand;
$command = str_replace(['{player}', '{role}'], [$player, $role], $command);
$this->dispatchCommand($command);
}

/** {@inheritDoc} */
Expand Down

0 comments on commit deeea68

Please sign in to comment.