Skip to content

Commit

Permalink
Initial Unified Commands API (#6), still missing permissions tho
Browse files Browse the repository at this point in the history
  • Loading branch information
timmyRS committed Aug 20, 2019
1 parent 830cb28 commit ca9d9b7
Show file tree
Hide file tree
Showing 25 changed files with 630 additions and 202 deletions.
6 changes: 3 additions & 3 deletions client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
require "vendor/autoload.php";
use Phpcraft\
{Account, AssetsManager, Event\ClientConsoleEvent, Event\ClientJoinEvent, Event\ClientPacketEvent, FancyUserInterface, Packet\ClientboundPacket, Packet\KeepAliveRequestPacket, Packet\ServerboundBrandPluginMessagePacket, Phpcraft, PluginManager, Position, ServerConnection, UserInterface, Versions};
{Account, AssetsManager, Event\ClientConsoleEvent, Event\ClientJoinEvent, Event\ClientPacketEvent, FancyUserInterface, Packet\ClientboundPacket, Packet\KeepAliveRequestPacket, Packet\ServerboundBrandPluginMessagePacket, Phpcraft, Plugin\PluginManager, Position, ServerConnection, UserInterface, Versions};
$options = [];
for($i = 1; $i < count($argv); $i++)
{
Expand Down Expand Up @@ -538,7 +538,7 @@ function handleConsoleMessage(string $msg)
$message = $con->readString();
if($con->readByte() != 2)
{
$ui->add(Phpcraft::chatToText(json_decode($message, true), 1, $translations));
$ui->add(Phpcraft::chatToText(json_decode($message, true), Phpcraft::FORMAT_ANSI, $translations));
}
}
else if($packet_name == "player_info")
Expand Down Expand Up @@ -842,7 +842,7 @@ function handleConsoleMessage(string $msg)
}
else if($packet_name == "disconnect")
{
$ui->add("Server closed connection: ".Phpcraft::chatToText($con->readString(), 1))
$ui->add("Server closed connection: ".Phpcraft::chatToText($con->readString(), Phpcraft::FORMAT_ANSI))
->render();
$reconnect = !isset($options["noreconnect"]);
$next_tick = microtime(true) + 10;
Expand Down
1 change: 1 addition & 0 deletions nbt.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{
$con->read_buffer = file_get_contents($argv[1]);
}
/** @noinspection PhpUnhandledExceptionInspection */
$tag = $con->readNBT();
if($con->read_buffer !== "")
{
Expand Down
94 changes: 53 additions & 41 deletions plugins/BasicCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,59 @@
* @var Plugin $this
*/
use Phpcraft\
{ClientConnection, Event\ServerChatEvent, Plugin};
$this->on(function(ServerChatEvent $event)
{ClientConnection, Command\CommandSender, Plugin\Plugin, Plugin\PluginManager};
$this->registerCommand("help", function(CommandSender &$sender)
{
if($event->cancelled || substr($event->message, 0, 1) != "/")
$commands = [];
foreach(PluginManager::$registered_commands as $command)
{
return;
array_push($commands, $command->getSyntax());
}
$con = $event->client;
assert($con instanceof ClientConnection);
if(substr($event->message, 0, 11) == "/abilities ")
{
$con->startPacket("clientbound_abilities");
$con->writeByte(hexdec(substr($event->message, 11, 1)));
$con->writeFloat(0.05);
$con->writeFloat(0.1);
$con->send();
}
else if(substr($event->message, 0, 10) == "/gamemode ")
{
$gamemode = intval(substr($event->message, 10));
$con->setGamemode($gamemode);
$con->startPacket("player_info");
$con->writeVarInt(1);
$con->writeVarInt(1);
$con->writeUUID($con->uuid);
$con->writeVarInt($gamemode);
$con->send();
}
else if(substr($event->message, 0, 10) == "/metadata ")
{
$con->startPacket("entity_metadata");
$con->writeVarInt($con->eid);
$con->writeByte(0);
$con->writeVarInt(0);
$con->writeByte(hexdec(substr($event->message, 10, 2)));
$con->writeByte(0xFF);
$con->send();
}
else
{
$con->sendMessage("That's not any command I know. Try /abilities <0-F>, /gamemode <0-3>, or /metadata <00-FF>.");
}
$event->cancelled = true;
});
$sender->sendMessage(["text" => "I know the following commands:\n".join("\n", $commands)]);
})
->registerCommand([
"gamemode",
"gm"
], function(CommandSender &$client, int $gamemode)
{
if(!$client instanceof ClientConnection)
{
$client->sendMessage("This command is only for players.");
return;
}
$client->setGamemode($gamemode);
$client->startPacket("player_info");
$client->writeVarInt(1);
$client->writeVarInt(1);
$client->writeUUID($client->uuid);
$client->writeVarInt($gamemode);
$client->send();
})
->registerCommand("abilities", function(CommandSender &$client, $abilities)
{
if(!$client instanceof ClientConnection)
{
$client->sendMessage("This command is only for players.");
return;
}
$client->startPacket("clientbound_abilities");
$client->writeByte(hexdec($abilities));
$client->writeFloat(0.05);
$client->writeFloat(0.1);
$client->send();
})
->registerCommand("metadata", function(CommandSender &$client, $metadata)
{
if(!$client instanceof ClientConnection)
{
$client->sendMessage("This command is only for players.");
return;
}
$client->startPacket("entity_metadata");
$client->writeVarInt($client->eid);
$client->writeByte(0);
$client->writeVarInt(0);
$client->writeByte(hexdec($metadata));
$client->writeByte(0xFF);
$client->send();
});
4 changes: 2 additions & 2 deletions plugins/BossBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*
* @var Plugin $this
*/
use Phpcraft\
{Event\Event, Event\ServerJoinEvent, Event\ServerTickEvent, Packet\AddBossBarPacket, Packet\UpdateBossBarHealthPacket, Packet\UpdateBossBarTitlePacket, Plugin};
use hellsh\UUID;
use Phpcraft\
{Event\Event, Event\ServerJoinEvent, Event\ServerTickEvent, Packet\AddBossBarPacket, Packet\UpdateBossBarHealthPacket, Packet\UpdateBossBarTitlePacket, Plugin\Plugin};
global $bossbar_i;
$bossbar_i = 0;
$this->on(function(ServerJoinEvent $event)
Expand Down
2 changes: 1 addition & 1 deletion plugins/CrashClients.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @var Plugin $this
*/
use Phpcraft\
{ClientConnection, Event\ServerChatEvent, Packet\JoinGamePacket, Plugin};
{ClientConnection, Event\ServerChatEvent, Packet\JoinGamePacket, Plugin\Plugin};
$this->on(function(ServerChatEvent $event)
{
if(!$event->cancelled && $event->message == "crash me")
Expand Down
2 changes: 1 addition & 1 deletion plugins/DoubleJump.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @var Plugin $this
*/
use Phpcraft\
{Enum\Gamemode, Event\ServerFlyingChangeEvent, Event\ServerOnGroundChangeEvent, Plugin};
{Enum\Gamemode, Event\ServerFlyingChangeEvent, Event\ServerOnGroundChangeEvent, Plugin\Plugin};
$this->on(function(ServerOnGroundChangeEvent $event)
{
if($event->client->on_ground && !$event->client->can_fly)
Expand Down
Loading

0 comments on commit ca9d9b7

Please sign in to comment.