Game servers
How a module reads servers, gets their status from ServerQuery, sends commands and receives events through GameLink, finds a player's game account and connects its own hosting panel.
By the end, your module gives a player an item on a server whose GameLink plugin knows the action, learns how the delivery ended, and receives events the plugin sends. The page also covers showing a server's online count and finding a player's SteamID.
Minimal example
The Example module gives an example.crate on a chosen server. The action is declared in boot(), the delivery goes through GameActions::send(), and the plugin's answer arrives as an event.
<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Services;
use Flute\Core\Database\Entities\CommandDelivery;
use Flute\Core\Database\Entities\Server;
use Flute\Core\Database\Entities\User;
use Flute\Core\Servers\Exceptions\GameActionUnavailable;
use Flute\Core\Servers\GameActions;
final class CrateDelivery
{
public const ACTION = 'example.crate';
public const SOURCE = 'example';
public function __construct(
private readonly GameActions $actions,
) {
}
/** @throws GameActionUnavailable */
public function give(User $user, Server $server, int $grantId): CommandDelivery
{
$player = $this->actions->identityFor($user, $server)
?? throw new GameActionUnavailable(GameActionUnavailable::NO_IDENTITY);
return $this->actions->send(
$server,
self::ACTION,
['crate' => $grantId],
self::SOURCE,
$player,
dedupeKey: 'crate:' . $grantId,
);
}
}<?php
declare(strict_types = 1);
namespace Flute\Modules\Example\Providers;
use DI\Container;
use Flute\Core\Api\GameLink\Events\GameLinkEventReceived;
use Flute\Core\Servers\Events\CommandDeliveryFinished;
use Flute\Core\Servers\GameActions;
use Flute\Core\Servers\Values\ModuleAction;
use Flute\Core\Support\ModuleServiceProvider;
use Flute\Modules\Example\Services\CrateDelivery;
use Flute\Modules\Example\Services\CrateGrants;
final class ExampleProvider extends ModuleServiceProvider
{
public function register(Container $container): void
{
}
public function boot(Container $container): void
{
$this->bootstrapModule();
$container->get(GameActions::class)->register(
new ModuleAction(CrateDelivery::ACTION, ['crate'], needsPlayer: true, ttl: 86400, requireOnline: true),
);
events()->addListener(CommandDeliveryFinished::NAME, static function (CommandDeliveryFinished $e): void {
if ($e->source === CrateDelivery::SOURCE) {
app(CrateGrants::class)->finished($e->dedupeKey, $e->status, $e->reason);
}
});
events()->addListener(GameLinkEventReceived::NAME, static function (GameLinkEventReceived $e): void {
if ($e->type === 'example.crate_opened' && $e->playerId !== null) {
app(CrateGrants::class)->opened($e->server, $e->playerId, $e->data);
}
});
}
}To make the delivery readable in the delivery list, add a delivery.crate key to Resources/lang/en/example.php. Without it the action shows as example.crate — crate: 812. The Clans module delivers its clan tag the same way.
How it works
On a server with a plugin, send() runs nothing right away. It checks the arguments and action support, puts a row into the delivery queue and returns a CommandDelivery. The plugin picks the command up on its next sync: every 5 seconds while the queue has work, every 10 seconds with players online, every 30 seconds on an empty server. A repeat with the same (server, source, dedupeKey) returns the row already queued, so a key built from the grant id protects against delivering twice.
The plugin itself reports which actions it supports: every sync carries the list of actions it can run. supports() answers Yes if the action is on the list, No if the plugin is connected and does not know the action, and Unknown if the plugin has not reported yet (the core queues such a delivery and waits). The game plugin has to be able to run your module's action, otherwise it answers failed: unsupported_action. Standard actions (group.add, notify.player, economy.deposit) need no registration: use GameAction::GroupAdd and the other cases.
A server without a GameLink token gets only raw, player.kick, group.add and group.remove, and only if its game has an adapter: the command runs right away, through RCON or the game's database. Anything else on such a server gives no_link, and no row is created.
CommandDeliveryFinished fires once per row, after the status is stored, inside the plugin's request or cron. Filter by source and keep the listener fast.
GameLinkEventReceived fires for every plugin event (POST /link/events or inside a sync). The core takes up to 100 events per request and accepts the same event id from a server only once in 24 hours. Everything a plugin reports is unverified: check it against what your module planned itself.
Reference
| Call | What it does |
|---|---|
GameActions::register(ModuleAction $action): void | declares a module action. The name is owner.action, and the first part must not be core vocabulary (group, ban, comms, player…) |
new ModuleAction(string $name, array $requiredArgs = [], bool $needsPlayer = false, ?int $ttl = null, bool $requireOnline = false, array $games = []) | ttl in seconds, games lists the server mod values the action exists on (empty means every game) |
GameActions::send(Server $server, GameAction|string $action, array $args, string $source, ?IdentityRef $player = null, ?string $dedupeKey = null, ?DateTimeImmutable $expiresAt = null, ?bool $requireOnline = null, ?DateTimeImmutable $notBefore = null): CommandDelivery | queues the action. Up to 16 scalar arguments, dedupeKey 1–96 bytes |
supports(Server $server, GameAction|string $action): ActionSupport | Yes, No or Unknown. Check it before taking money |
capableServers(iterable $servers, GameAction|string $action): array, offeredServers(iterable $servers, GameAction|string $action): array | servers where the action will run; servers for a picker in a form |
identityFor(User $user, Server $server): ?IdentityRef, identitiesFor(User $user, Server $server): array | the user's game account for this server's game |
PlayerNotice::tell($server, $player, $title, $text, 'example', key: 'crate:812', refreshMenu: true) | a message to one player in whatever form the plugin supports |
DeliveryStatus::describe(?CommandDelivery $delivery): array | a status badge linking to /admin/servers/deliveries?source=…. You need no delivery screen of your own |
Event (NAME) | Fields |
|---|---|
CommandDeliveryFinished (flute.server.delivery_finished) | deliveryId, serverId, source, dedupeKey, action, status (done, failed, expired), reason, playerId |
GameLinkEventReceived (flute.gamelink.event) | server, type, at, identity, name, data, id, playerId |
PlayerKilled (flute.player.killed), MatchRoundEnded (flute.match.round_ended) | typed events from Flute\Core\Players\Events, fired right after the raw one |
PlayerLinked (flute.player.linked) | a player linked a game account to a site account: playerId, userId, identityType |
Known types such as player.death, player.stats, round.end and wipe are normalized by the core before your listener sees them. Your own event type (example.crate_opened) arrives as is, up to 64 characters. The Votes module catches vote events this way, and the Wipes module catches wipe.
Server, online count and game accounts
A server is the Flute\Core\Database\Entities\Server entity: id, name, ip, port, mod (the game key), enabled, additional, getSettings(), getConnectionString(). Find one by id with Server::byId($id). The game plugin database the owner attached to the server opens with $server->db('DarkRP'), and $server->hasDb('DarkRP') checks for it. The integration key here comes from the GiveCore module.
Take the online count, map and player count for a page from CachedServerQuery::forDisplay(array $servers): array<int, QueryResult>, as the Monitoring module does. A server that did not answer is missing from the array.
The answer is cached. If the site runs a background queue, the cache is refreshed there and the page does not wait for the query. QueryResult has online, players, maxPlayers, hostname, map, playersData and error with a code (closed, no_answer, refused…).
The game account for a delivery comes from identityFor(): an IdentityRef of type steam, minecraft, minecraft_offline, steam_offline or a type a game module registered. Register your own with LinkIdentity::register(string $type, ?callable $normalize = null, bool $verified = true, array $games = []); use verified: false for ids a player can claim freely, such as an offline-mode nickname.
A module route the plugin calls uses middleware: ['link', 'throttle:bearer,60,60']. The server is then in the link_server request attribute (LinkGuard::ATTRIBUTE). Do not write a second token check.
A module adds pages and buttons to the in-game menu with a LinkMenuContributor class registered through SurfaceRegistry->add(SurfaceKeys::LINK_MENU_CONTRIBUTOR, MyMenu::class). LINK_SYNC_CONTRIBUTOR adds data to every sync, and LINK_PROFILE_CONTRIBUTOR adds fields to the player card.
A hosting panel connects through a class implementing Flute\Admin\Packages\Server\Contracts\PanelProvider (handles(), listServers(), power(), sendCommand(), liveStatus(), consoleLog() and others), registered with $container->get(PanelProviderRegistry::class)->register(new ExamplePanel()). Throw a panel error as a RuntimeException with a translated message. The type list in the panel connection form is fixed for now, so a module's type cannot be picked in the admin.
What the core requires from a module
| Rule | How it gets broken |
|---|---|
commands reach a server only through GameActions::send() | your own RCON client or writing into the queue table |
dedupeKey built from the id of what you deliver | a key from the time or a random one, a double delivery on retry |
a server's online count only from CachedServerQuery | your own UDP query in a controller, the page waits for the timeout |
requests to a user-typed address through Resilience\PublicHost::resolve() and PublicHost::transport() | Guzzle or file_get_contents on an address from a form |
requests to a fixed external API through Integrations\ExternalApi::get() with a service registered in ExternalApiRegistry | your own HTTP client without a cache or limits |
a server's mod value never changes | a renamed game key, servers lose their settings |
Common mistakes
GameActionUnavailable puts the reason into $e->reason, and the exception message starts with it.
| What you see | Cause |
|---|---|
InvalidArgumentException: 'crate' is not a module action name. | the name has no owner part, or the owner part is core vocabulary. Write example.crate |
| the delivery sits in the queue and never expires, no player required | register() was not called: an unregistered action goes out without ttl, requireOnline and argument checks |
invalid_args: unknown action 'crate' | send() got a name without the owner part |
invalid_args: missing 'crate', invalid_args: example.crate needs a player | a required argument or the player is missing |
unsupported_action: example.crate | the plugin is connected and did not report this action |
no_link: example.crate | the server has no GameLink token, and the action needs the plugin |
no_identity | the user has no account for this game: no SteamID linked |
InvalidArgumentException: Invalid identity type 'Example'. | an identity type with a capital letter, or user |