Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim "timmyRS" Speckhals committed Nov 16, 2019
1 parent 95e2a4e commit a624b12
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 99 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
"name": "timmyrs/phpcraft", "name": "timmyrs/phpcraft",
"description": "A PHP library for all things Minecraft: Java Edition.", "description": "A PHP library for all things Minecraft: Java Edition.",
"keywords": [ "keywords": [
"minecraft-api", "minecraft-library", "nbt-library", "minecraft-protocol", "minecraft-java-edition", "minecraft-java", "minecraft", "mcje" "minecraft-api",
"minecraft-library",
"nbt-library",
"minecraft-protocol",
"minecraft-java-edition",
"minecraft-java",
"minecraft",
"mcje"
], ],
"homepage": "https://github.com/Phpcraft/core", "homepage": "https://github.com/Phpcraft/core",
"type": "library", "type": "library",
Expand Down
2 changes: 1 addition & 1 deletion plugins/Chunks.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @var Plugin $this * @var Plugin $this
*/ */
use Phpcraft\ use Phpcraft\
{BlockState, ClientConnection, Connection, Event\ServerChunkBorderEvent, Event\ServerJoinEvent, Event\ServerTickEvent, NBT\CompoundTag, NBT\LongArrayTag, Plugin, PluginManager}; {BlockState, ClientConnection, Connection, Event\ServerChunkBorderEvent, Event\ServerJoinEvent, Event\ServerTickEvent, NBT\CompoundTag, NBT\LongArrayTag, Plugin};
$this->on(function(ServerJoinEvent $event) $this->on(function(ServerJoinEvent $event)
{ {
if($event->cancelled) if($event->cancelled)
Expand Down
2 changes: 1 addition & 1 deletion plugins/GroupManagement.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @var Plugin $this * @var Plugin $this
*/ */
use Phpcraft\ use Phpcraft\
{ClientConfiguration, Command\ServerCommandSender, Plugin, PluginManager}; {ClientConfiguration, Command\ServerCommandSender, Plugin};
$this->registerCommand("group", function(ServerCommandSender &$sender, ClientConfiguration $player, string $group = "") $this->registerCommand("group", function(ServerCommandSender &$sender, ClientConfiguration $player, string $group = "")
{ {
if($group == "") if($group == "")
Expand Down
8 changes: 7 additions & 1 deletion selfcheck.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@
$str .= $nl; $str .= $nl;
if(defined("PHP_WINDOWS_VERSION_MAJOR")) if(defined("PHP_WINDOWS_VERSION_MAJOR"))
{ {
if(in_array($ext, ["curl", "gd", "gmp", "mbstring", "sockets"])) if(in_array($ext, [
"curl",
"gd",
"gmp",
"mbstring",
"sockets"
]))
{ {
$str .= "├ cone get php-$ext".$nl."└ If you don't have Cone, check the extensions section of your php.ini.\n"; $str .= "├ cone get php-$ext".$nl."└ If you don't have Cone, check the extensions section of your php.ini.\n";
} }
Expand Down
32 changes: 16 additions & 16 deletions src/ClientConnection.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -627,22 +627,6 @@ function setGamemode(int $gamemode): ClientConnection
return $this; return $this;
} }


/**
* Sets the client's abilities according to the given gamemode.
*
* @param int $gamemode
* @return ClientConnection $this
* @see ClientConnection::sendAbilities
* @see ClientConnection::setGamemode
*/
function setAbilitiesFromGamemode(int $gamemode): ClientConnection
{
$this->instant_breaking = ($gamemode == Gamemode::CREATIVE);
$this->flying = ($gamemode == Gamemode::SPECTATOR);
$this->invulnerable = $this->can_fly = ($gamemode == Gamemode::CREATIVE || $gamemode == Gamemode::SPECTATOR);
return $this;
}

/** /**
* Sends the client their abilities. * Sends the client their abilities.
* *
Expand All @@ -668,6 +652,22 @@ function sendAbilities(): ClientConnection
return $this; return $this;
} }


/**
* Sets the client's abilities according to the given gamemode.
*
* @param int $gamemode
* @return ClientConnection $this
* @see ClientConnection::sendAbilities
* @see ClientConnection::setGamemode
*/
function setAbilitiesFromGamemode(int $gamemode): ClientConnection
{
$this->instant_breaking = ($gamemode == Gamemode::CREATIVE);
$this->flying = ($gamemode == Gamemode::SPECTATOR);
$this->invulnerable = $this->can_fly = ($gamemode == Gamemode::CREATIVE || $gamemode == Gamemode::SPECTATOR);
return $this;
}

function getName(): string function getName(): string
{ {
return $this->username; return $this->username;
Expand Down
158 changes: 79 additions & 79 deletions src/IntegratedServer.php
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php <?php
namespace Phpcraft; namespace Phpcraft;
use Exception; use Exception;
use hellsh\UUID;
use Phpcraft\ use Phpcraft\
{Command\Command, Enum\Gamemode, Event\ServerChatEvent, Event\ServerChunkBorderEvent, Event\ServerClientMetadataEvent, Event\ServerClientSettingsEvent, Event\ServerFlyingChangeEvent, Event\ServerJoinEvent, Event\ServerLeaveEvent, Event\ServerMovementEvent, Event\ServerOnGroundChangeEvent, Event\ServerPacketEvent, Event\ServerRotationEvent, Exception\IOException, Packet\ClientSettingsPacket, Packet\JoinGamePacket, Packet\PluginMessage\ClientboundBrandPluginMessagePacket, Packet\ServerboundPacketId}; {Command\Command, Enum\Gamemode, Event\ServerChatEvent, Event\ServerChunkBorderEvent, Event\ServerClientMetadataEvent, Event\ServerClientSettingsEvent, Event\ServerFlyingChangeEvent, Event\ServerJoinEvent, Event\ServerLeaveEvent, Event\ServerMovementEvent, Event\ServerOnGroundChangeEvent, Event\ServerPacketEvent, Event\ServerRotationEvent, Exception\IOException, Packet\ClientSettingsPacket, Packet\JoinGamePacket, Packet\PluginMessage\ClientboundBrandPluginMessagePacket, Packet\ServerboundPacketId};
use hellsh\UUID;
use RuntimeException; use RuntimeException;
class IntegratedServer extends Server class IntegratedServer extends Server
{ {
Expand Down Expand Up @@ -388,84 +388,6 @@ function __construct(string $name = "Phpcraft Integrated Server", array $custom_
}; };
} }


/**
* Starts the integrated server using $argv.
*
* @param string $name
* @param array $custom_config_defaults
* @return IntegratedServer
*/
static function cliStart(string $name = "Phpcraft Integrated Server", array $custom_config_defaults = []): IntegratedServer
{
global $argv;
$options = [
"offline" => false,
"plain" => false
];
for($i = 1; $i < count($argv); $i++)
{
$arg = ltrim($argv[$i], "-/");
switch($arg)
{
case "offline":
case "plain":
$options[$arg] = true;
break;
case "?":
case "help":
echo "offline disables online mode and allows cracked players\n";
echo "plain uses the plain user interface e.g. for writing logs to a file\n";
exit;
default:
die("Unknown flag '$arg' -- use 'help' to get a list of supported flags.\n");
}
}
return self::start($name, $options["offline"], $options["plain"], $custom_config_defaults);
}

/**
* Starts the integrated server using the given settings.
*
* @param string $name
* @param bool $offline
* @param bool $plain
* @param array $custom_config_defaults
* @return IntegratedServer
*/
static function start(string $name = "Phpcraft Integrated Server", bool $offline = false, bool $plain = false, array $custom_config_defaults = []): IntegratedServer
{
try
{
$ui = ($plain ? new PlainUserInterface() : new FancyUserInterface($name));
}
catch(RuntimeException $e)
{
echo "Since you're on PHP <7.2.0 and Windows <10.0.10586, the plain user interface is forcefully enabled.\n";
$ui = new PlainUserInterface();
}
if($offline)
{
$private_key = null;
}
else
{
$ui->add("Generating 1024-bit RSA keypair... ")
->render();
$args = [
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_RSA
];
if(Phpcraft::isWindows())
{
$args["config"] = __DIR__."/openssl.cnf";
}
$private_key = openssl_pkey_new($args) or die("Failed to generate private key.\n");
$ui->append("Done.")
->render();
}
return new static($name, $custom_config_defaults, $ui, $private_key);
}

/** /**
* @return void * @return void
*/ */
Expand Down Expand Up @@ -600,4 +522,82 @@ function saveConfig(): void
{ {
file_put_contents("config/".$this->name.".json", json_encode($this->config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); file_put_contents("config/".$this->name.".json", json_encode($this->config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} }

/**
* Starts the integrated server using $argv.
*
* @param string $name
* @param array $custom_config_defaults
* @return IntegratedServer
*/
static function cliStart(string $name = "Phpcraft Integrated Server", array $custom_config_defaults = []): IntegratedServer
{
global $argv;
$options = [
"offline" => false,
"plain" => false
];
for($i = 1; $i < count($argv); $i++)
{
$arg = ltrim($argv[$i], "-/");
switch($arg)
{
case "offline":
case "plain":
$options[$arg] = true;
break;
case "?":
case "help":
echo "offline disables online mode and allows cracked players\n";
echo "plain uses the plain user interface e.g. for writing logs to a file\n";
exit;
default:
die("Unknown flag '$arg' -- use 'help' to get a list of supported flags.\n");
}
}
return self::start($name, $options["offline"], $options["plain"], $custom_config_defaults);
}

/**
* Starts the integrated server using the given settings.
*
* @param string $name
* @param bool $offline
* @param bool $plain
* @param array $custom_config_defaults
* @return IntegratedServer
*/
static function start(string $name = "Phpcraft Integrated Server", bool $offline = false, bool $plain = false, array $custom_config_defaults = []): IntegratedServer
{
try
{
$ui = ($plain ? new PlainUserInterface() : new FancyUserInterface($name));
}
catch(RuntimeException $e)
{
echo "Since you're on PHP <7.2.0 and Windows <10.0.10586, the plain user interface is forcefully enabled.\n";
$ui = new PlainUserInterface();
}
if($offline)
{
$private_key = null;
}
else
{
$ui->add("Generating 1024-bit RSA keypair... ")
->render();
$args = [
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_RSA
];
if(Phpcraft::isWindows())
{
$args["config"] = __DIR__."/openssl.cnf";
}
$private_key = openssl_pkey_new($args) or die("Failed to generate private key.\n");
$ui->append("Done.")
->render();
}
return new static($name, $custom_config_defaults, $ui, $private_key);
}
} }

0 comments on commit a624b12

Please sign in to comment.