First look at separating disconnect reason and disconnect screen messages (#4512)

This commit is contained in:
Dylan K. Taylor 2023-01-18 20:57:17 +00:00
parent 365cce9d0c
commit b8f6b66e42
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
7 changed files with 111 additions and 45 deletions

View File

@ -0,0 +1,62 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event\player;
use pocketmine\lang\Translatable;
trait PlayerDisconnectEventTrait{
/**
* Sets the kick reason shown in the server log and on the console.
*
* This should be a **short, simple, single-line** message.
* Do not use long or multi-line messages here - they will spam the log and server console with useless information.
*/
public function setDisconnectReason(Translatable|string $disconnectReason) : void{
$this->disconnectReason = $disconnectReason;
}
/**
* Returns the kick reason shown in the server log and on the console.
* When kicked by the /kick command, the default is something like "Kicked by admin.".
*/
public function getDisconnectReason() : Translatable|string{
return $this->disconnectReason;
}
/**
* Sets the message shown on the player's disconnection screen.
* This can be as long as you like, and may contain formatting and newlines.
* If this is set to null, the kick reason will be used as the disconnect screen message directly.
*/
public function setDisconnectScreenMessage(Translatable|string|null $disconnectScreenMessage) : void{
$this->disconnectScreenMessage = $disconnectScreenMessage;
}
/**
* Returns the message shown on the player's disconnection screen.
* When kicked by the /kick command, the default is something like "Kicked by admin.".
* If this is null, the kick reason will be used as the disconnect screen message directly.
*/
public function getDisconnectScreenMessage() : Translatable|string|null{ return $this->disconnectScreenMessage ?? $this->disconnectReason; }
}

View File

@ -36,12 +36,15 @@ use pocketmine\network\mcpe\NetworkSession;
*/ */
class PlayerDuplicateLoginEvent extends Event implements Cancellable{ class PlayerDuplicateLoginEvent extends Event implements Cancellable{
use CancellableTrait; use CancellableTrait;
use PlayerDisconnectEventTrait;
private Translatable|string $disconnectMessage; private Translatable|string $disconnectMessage;
public function __construct( public function __construct(
private NetworkSession $connectingSession, private NetworkSession $connectingSession,
private NetworkSession $existingSession private NetworkSession $existingSession,
private Translatable|string $disconnectReason,
private Translatable|string|null $disconnectScreenMessage
){ ){
$this->disconnectMessage = KnownTranslationFactory::disconnectionScreen_loggedinOtherLocation(); $this->disconnectMessage = KnownTranslationFactory::disconnectionScreen_loggedinOtherLocation();
} }

View File

@ -33,32 +33,17 @@ use pocketmine\player\Player;
*/ */
class PlayerKickEvent extends PlayerEvent implements Cancellable{ class PlayerKickEvent extends PlayerEvent implements Cancellable{
use CancellableTrait; use CancellableTrait;
use PlayerDisconnectEventTrait;
public function __construct( public function __construct(
Player $player, Player $player,
protected Translatable|string $reason, protected Translatable|string $disconnectReason,
protected Translatable|string $quitMessage protected Translatable|string $quitMessage,
protected Translatable|string|null $disconnectScreenMessage
){ ){
$this->player = $player; $this->player = $player;
} }
/**
* Sets the message shown on the kicked player's disconnection screen.
* This message is also displayed in the console and server log.
*/
public function setReason(Translatable|string $reason) : void{
$this->reason = $reason;
}
/**
* Returns the message shown on the kicked player's disconnection screen.
* This message is also displayed in the console and server log.
* When kicked by the /kick command, the default is something like "Kicked by admin.".
*/
public function getReason() : Translatable|string{
return $this->reason;
}
/** /**
* Sets the quit message broadcasted to other players. * Sets the quit message broadcasted to other players.
*/ */

View File

@ -96,10 +96,13 @@ class NetworkSessionManager{
/** /**
* Terminates all connected sessions with the given reason. * Terminates all connected sessions with the given reason.
*
* @param Translatable|string $reason Shown in the server log - this should be a short one-line message
* @param Translatable|string|null $disconnectScreenMessage Shown on the player's disconnection screen (null will use the reason)
*/ */
public function close(Translatable|string $reason = "") : void{ public function close(Translatable|string $reason = "", Translatable|string|null $disconnectScreenMessage = null) : void{
foreach($this->sessions as $session){ foreach($this->sessions as $session){
$session->disconnect($reason); $session->disconnect($reason, $disconnectScreenMessage);
} }
$this->sessions = []; $this->sessions = [];
} }

View File

@ -602,22 +602,25 @@ class NetworkSession{
$this->invManager = null; $this->invManager = null;
} }
private function sendDisconnectPacket(Translatable|string $reason) : void{ private function sendDisconnectPacket(Translatable|string $message) : void{
if($reason instanceof Translatable){ if($message instanceof Translatable){
$translated = $this->server->getLanguage()->translate($reason); $translated = $this->server->getLanguage()->translate($message);
}else{ }else{
$translated = $reason; $translated = $message;
} }
$this->sendDataPacket(DisconnectPacket::create($translated)); $this->sendDataPacket(DisconnectPacket::create($translated));
} }
/** /**
* Disconnects the session, destroying the associated player (if it exists). * Disconnects the session, destroying the associated player (if it exists).
*
* @param Translatable|string $reason Shown in the server log - this should be a short one-line message
* @param Translatable|string|null $disconnectScreenMessage Shown on the player's disconnection screen (null will use the reason)
*/ */
public function disconnect(Translatable|string $reason, bool $notify = true) : void{ public function disconnect(Translatable|string $reason, Translatable|string|null $disconnectScreenMessage = null, bool $notify = true) : void{
$this->tryDisconnect(function() use ($reason, $notify) : void{ $this->tryDisconnect(function() use ($reason, $disconnectScreenMessage, $notify) : void{
if($notify){ if($notify){
$this->sendDisconnectPacket($reason); $this->sendDisconnectPacket($disconnectScreenMessage ?? $reason);
} }
if($this->player !== null){ if($this->player !== null){
$this->player->onPostDisconnect($reason, null); $this->player->onPostDisconnect($reason, null);
@ -654,9 +657,9 @@ class NetworkSession{
/** /**
* Called by the Player when it is closed (for example due to getting kicked). * Called by the Player when it is closed (for example due to getting kicked).
*/ */
public function onPlayerDestroyed(Translatable|string $reason) : void{ public function onPlayerDestroyed(Translatable|string $reason, Translatable|string $disconnectScreenMessage) : void{
$this->tryDisconnect(function() use ($reason) : void{ $this->tryDisconnect(function() use ($disconnectScreenMessage) : void{
$this->sendDisconnectPacket($reason); $this->sendDisconnectPacket($disconnectScreenMessage);
}, $reason); }, $reason);
} }
@ -694,7 +697,7 @@ class NetworkSession{
if(!$this->authenticated){ if(!$this->authenticated){
if($authRequired){ if($authRequired){
$this->disconnect(KnownTranslationFactory::disconnectionScreen_notAuthenticated()); $this->disconnect("Not authenticated", KnownTranslationFactory::disconnectionScreen_notAuthenticated());
return; return;
} }
if($this->info instanceof XboxLivePlayerInfo){ if($this->info instanceof XboxLivePlayerInfo){
@ -728,14 +731,14 @@ class NetworkSession{
if($kickForXUIDMismatch($info instanceof XboxLivePlayerInfo ? $info->getXuid() : "")){ if($kickForXUIDMismatch($info instanceof XboxLivePlayerInfo ? $info->getXuid() : "")){
return; return;
} }
$ev = new PlayerDuplicateLoginEvent($this, $existingSession); $ev = new PlayerDuplicateLoginEvent($this, $existingSession, KnownTranslationFactory::disconnectionScreen_loggedinOtherLocation(), null);
$ev->call(); $ev->call();
if($ev->isCancelled()){ if($ev->isCancelled()){
$this->disconnect($ev->getDisconnectMessage()); $this->disconnect($ev->getDisconnectReason(), $ev->getDisconnectScreenMessage());
return; return;
} }
$existingSession->disconnect($ev->getDisconnectMessage()); $existingSession->disconnect($ev->getDisconnectReason(), $ev->getDisconnectScreenMessage());
} }
} }

View File

@ -93,7 +93,7 @@ class ResourcePacksPacketHandler extends PacketHandler{
switch($packet->status){ switch($packet->status){
case ResourcePackClientResponsePacket::STATUS_REFUSED: case ResourcePackClientResponsePacket::STATUS_REFUSED:
//TODO: add lang strings for this //TODO: add lang strings for this
$this->session->disconnect("You must accept resource packs to join this server.", true); $this->session->disconnect("Refused resource packs", "You must accept resource packs to join this server.", true);
break; break;
case ResourcePackClientResponsePacket::STATUS_SEND_PACKS: case ResourcePackClientResponsePacket::STATUS_SEND_PACKS:
foreach($packet->packIds as $uuid){ foreach($packet->packIds as $uuid){

View File

@ -2090,16 +2090,24 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
/** /**
* Kicks a player from the server * Kicks a player from the server
*
* @param Translatable|string $reason Shown in the server log - this should be a short one-line message
* @param Translatable|string|null $quitMessage Message to broadcast to online players (null will use default)
* @param Translatable|string|null $disconnectScreenMessage Shown on the player's disconnection screen (null will use the reason)
*/ */
public function kick(Translatable|string $reason = "", Translatable|string|null $quitMessage = null) : bool{ public function kick(Translatable|string $reason = "", Translatable|string|null $quitMessage = null, Translatable|string|null $disconnectScreenMessage = null) : bool{
$ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage()); $ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage(), $disconnectScreenMessage);
$ev->call(); $ev->call();
if(!$ev->isCancelled()){ if(!$ev->isCancelled()){
$reason = $ev->getReason(); $reason = $ev->getDisconnectReason();
if($reason === ""){ if($reason === ""){
$reason = KnownTranslationFactory::disconnectionScreen_noReason(); $reason = KnownTranslationFactory::disconnectionScreen_noReason();
} }
$this->disconnect($reason, $ev->getQuitMessage()); $disconnectScreenMessage = $ev->getDisconnectScreenMessage() ?? $reason;
if($disconnectScreenMessage === ""){
$disconnectScreenMessage = KnownTranslationFactory::disconnectionScreen_noReason();
}
$this->disconnect($reason, $ev->getQuitMessage(), $disconnectScreenMessage);
return true; return true;
} }
@ -2116,15 +2124,16 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
* *
* Note for internals developers: Do not call this from network sessions. It will cause a feedback loop. * Note for internals developers: Do not call this from network sessions. It will cause a feedback loop.
* *
* @param Translatable|string $reason Shown on the disconnect screen, and in the server log * @param Translatable|string $reason Shown in the server log - this should be a short one-line message
* @param Translatable|string|null $quitMessage Message to broadcast to online players (null will use default) * @param Translatable|string|null $quitMessage Message to broadcast to online players (null will use default)
* @param Translatable|string|null $disconnectScreenMessage Shown on the player's disconnection screen (null will use the reason)
*/ */
public function disconnect(Translatable|string $reason, Translatable|string|null $quitMessage = null) : void{ public function disconnect(Translatable|string $reason, Translatable|string|null $quitMessage = null, Translatable|string|null $disconnectScreenMessage = null) : void{
if(!$this->isConnected()){ if(!$this->isConnected()){
return; return;
} }
$this->getNetworkSession()->onPlayerDestroyed($reason); $this->getNetworkSession()->onPlayerDestroyed($reason, $disconnectScreenMessage ?? $reason);
$this->onPostDisconnect($reason, $quitMessage); $this->onPostDisconnect($reason, $quitMessage);
} }
@ -2132,6 +2141,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
* @internal * @internal
* This method executes post-disconnect actions and cleanups. * This method executes post-disconnect actions and cleanups.
* *
* @param Translatable|string $reason Shown in the server log - this should be a short one-line message
* @param Translatable|string|null $quitMessage Message to broadcast to online players (null will use default) * @param Translatable|string|null $quitMessage Message to broadcast to online players (null will use default)
*/ */
public function onPostDisconnect(Translatable|string $reason, Translatable|string|null $quitMessage) : void{ public function onPostDisconnect(Translatable|string $reason, Translatable|string|null $quitMessage) : void{