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{
use CancellableTrait;
use PlayerDisconnectEventTrait;
private Translatable|string $disconnectMessage;
public function __construct(
private NetworkSession $connectingSession,
private NetworkSession $existingSession
private NetworkSession $existingSession,
private Translatable|string $disconnectReason,
private Translatable|string|null $disconnectScreenMessage
){
$this->disconnectMessage = KnownTranslationFactory::disconnectionScreen_loggedinOtherLocation();
}

View File

@ -33,32 +33,17 @@ use pocketmine\player\Player;
*/
class PlayerKickEvent extends PlayerEvent implements Cancellable{
use CancellableTrait;
use PlayerDisconnectEventTrait;
public function __construct(
Player $player,
protected Translatable|string $reason,
protected Translatable|string $quitMessage
protected Translatable|string $disconnectReason,
protected Translatable|string $quitMessage,
protected Translatable|string|null $disconnectScreenMessage
){
$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.
*/

View File

@ -96,10 +96,13 @@ class NetworkSessionManager{
/**
* 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){
$session->disconnect($reason);
$session->disconnect($reason, $disconnectScreenMessage);
}
$this->sessions = [];
}

View File

@ -602,22 +602,25 @@ class NetworkSession{
$this->invManager = null;
}
private function sendDisconnectPacket(Translatable|string $reason) : void{
if($reason instanceof Translatable){
$translated = $this->server->getLanguage()->translate($reason);
private function sendDisconnectPacket(Translatable|string $message) : void{
if($message instanceof Translatable){
$translated = $this->server->getLanguage()->translate($message);
}else{
$translated = $reason;
$translated = $message;
}
$this->sendDataPacket(DisconnectPacket::create($translated));
}
/**
* 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{
$this->tryDisconnect(function() use ($reason, $notify) : void{
public function disconnect(Translatable|string $reason, Translatable|string|null $disconnectScreenMessage = null, bool $notify = true) : void{
$this->tryDisconnect(function() use ($reason, $disconnectScreenMessage, $notify) : void{
if($notify){
$this->sendDisconnectPacket($reason);
$this->sendDisconnectPacket($disconnectScreenMessage ?? $reason);
}
if($this->player !== 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).
*/
public function onPlayerDestroyed(Translatable|string $reason) : void{
$this->tryDisconnect(function() use ($reason) : void{
$this->sendDisconnectPacket($reason);
public function onPlayerDestroyed(Translatable|string $reason, Translatable|string $disconnectScreenMessage) : void{
$this->tryDisconnect(function() use ($disconnectScreenMessage) : void{
$this->sendDisconnectPacket($disconnectScreenMessage);
}, $reason);
}
@ -694,7 +697,7 @@ class NetworkSession{
if(!$this->authenticated){
if($authRequired){
$this->disconnect(KnownTranslationFactory::disconnectionScreen_notAuthenticated());
$this->disconnect("Not authenticated", KnownTranslationFactory::disconnectionScreen_notAuthenticated());
return;
}
if($this->info instanceof XboxLivePlayerInfo){
@ -728,14 +731,14 @@ class NetworkSession{
if($kickForXUIDMismatch($info instanceof XboxLivePlayerInfo ? $info->getXuid() : "")){
return;
}
$ev = new PlayerDuplicateLoginEvent($this, $existingSession);
$ev = new PlayerDuplicateLoginEvent($this, $existingSession, KnownTranslationFactory::disconnectionScreen_loggedinOtherLocation(), null);
$ev->call();
if($ev->isCancelled()){
$this->disconnect($ev->getDisconnectMessage());
$this->disconnect($ev->getDisconnectReason(), $ev->getDisconnectScreenMessage());
return;
}
$existingSession->disconnect($ev->getDisconnectMessage());
$existingSession->disconnect($ev->getDisconnectReason(), $ev->getDisconnectScreenMessage());
}
}

View File

@ -93,7 +93,7 @@ class ResourcePacksPacketHandler extends PacketHandler{
switch($packet->status){
case ResourcePackClientResponsePacket::STATUS_REFUSED:
//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;
case ResourcePackClientResponsePacket::STATUS_SEND_PACKS:
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
*
* @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{
$ev = new PlayerKickEvent($this, $reason, $quitMessage ?? $this->getLeaveMessage());
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(), $disconnectScreenMessage);
$ev->call();
if(!$ev->isCancelled()){
$reason = $ev->getReason();
$reason = $ev->getDisconnectReason();
if($reason === ""){
$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;
}
@ -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.
*
* @param Translatable|string $reason Shown on the disconnect screen, and in the server log
* @param Translatable|string|null $quitMessage Message to broadcast to online players (null will use default)
* @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 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()){
return;
}
$this->getNetworkSession()->onPlayerDestroyed($reason);
$this->getNetworkSession()->onPlayerDestroyed($reason, $disconnectScreenMessage ?? $reason);
$this->onPostDisconnect($reason, $quitMessage);
}
@ -2132,6 +2141,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
* @internal
* 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)
*/
public function onPostDisconnect(Translatable|string $reason, Translatable|string|null $quitMessage) : void{