diff --git a/src/MessageChannelSubscriber.php b/src/MessageBroadcastSubscriber.php similarity index 70% rename from src/MessageChannelSubscriber.php rename to src/MessageBroadcastSubscriber.php index a321d1393d..11f9f38a4d 100644 --- a/src/MessageChannelSubscriber.php +++ b/src/MessageBroadcastSubscriber.php @@ -25,23 +25,16 @@ use pocketmine\command\CommandSender; use pocketmine\lang\Translatable; -use pocketmine\permission\Permissible; /** * This interface can be implemented in order to receive messages from the server's global broadcast channels. */ -interface MessageChannelSubscriber{ +interface MessageBroadcastSubscriber{ /** * Called when a message is broadcasted on any channel that this receiver is subscribed to. * * @see Server::subscribeToBroadcastChannel() */ - public function onMessage(string $channelId, CommandSender $source, Translatable|string $message) : void; - - /** - * Used to check if the subscriber is allowed to receive messages from channels with permission restrictions. - * If this function returns null, the subscriber will receive all messages regardless of permissions. - */ - public function getPermissible() : ?Permissible; + public function onMessage(CommandSender $source, Translatable|string $message, string $channelId) : void; } diff --git a/src/MessageChannel.php b/src/MessageChannel.php deleted file mode 100644 index a8149f280e..0000000000 --- a/src/MessageChannel.php +++ /dev/null @@ -1,145 +0,0 @@ - - */ - private ObjectSet $nonPermittedSubscribers; - - /** - * Only subscribers whose permissions have been checked. This is invalidated whenever a subscriber's permissions are - * updated. - * - * @var MessageChannelSubscriber[]|ObjectSet - * @phpstan-var ObjectSet - */ - private ObjectSet $permittedSubscribers; - - /** - * @var \Closure[] - * @phpstan-var array - */ - private array $permissionCallbacks = []; - - public function __construct( - private ?string $permission - ){ - $this->nonPermittedSubscribers = new ObjectSet(); - $this->permittedSubscribers = new ObjectSet(); - } - - public function getPermission() : ?string{ return $this->permission; } - - public function setPermission(?string $permission) : void{ - $this->permission = $permission; - foreach($this->permittedSubscribers as $subscriber){ - $this->updateSubscriberAccess($subscriber); - } - foreach($this->nonPermittedSubscribers as $subscriber){ - $this->updateSubscriberAccess($subscriber); - } - } - - private function updateSubscriberAccess(MessageChannelSubscriber $subscriber) : void{ - $permissible = $subscriber->getPermissible(); - - //If either the subscriber doesn't support permission restricting, or no permission is set, we send all messages - //to it. Mainly useful for programmatic message collectors like in plugins. - $permitted = $permissible === null || $this->permission === null || $permissible->hasPermission($this->permission); - - [$add, $remove] = $permitted ? - [$this->permittedSubscribers, $this->nonPermittedSubscribers] : - [$this->nonPermittedSubscribers, $this->permittedSubscribers]; - $add->add($subscriber); - $remove->remove($subscriber); - } - - /** - * Subscribes to a particular message broadcast channel. - * The channel ID can be any arbitrary string. - */ - public function subscribe(MessageChannelSubscriber $subscriber) : void{ - $this->updateSubscriberAccess($subscriber); - - $permCallback = $this->permissionCallbacks[spl_object_id($subscriber)] = fn() => $this->updateSubscriberAccess($subscriber); - $subscriber->getPermissible()?->getPermissionRecalculationCallbacks()->add($permCallback); - } - - /** - * Unsubscribes from a particular message broadcast channel. - */ - public function unsubscribe(MessageChannelSubscriber $subscriber) : void{ - $this->nonPermittedSubscribers->remove($subscriber); - $this->permittedSubscribers->remove($subscriber); - - $splObjectId = spl_object_id($subscriber); - $permCallback = $this->permissionCallbacks[$splObjectId] ?? null; - if($permCallback !== null){ - $subscriber->getPermissible()?->getPermissionRecalculationCallbacks()->remove($permCallback); - unset($this->permissionCallbacks[$splObjectId]); - } - } - - /** - * Returns a list of all subscribers with permission to receive messages from this channel. - * - * @return MessageChannelSubscriber[] - * @phpstan-return array - */ - public function getPermittedSubscribers() : array{ - //convert to an array so callers can't mess up the internal state of the channel - return $this->permittedSubscribers->toArray(); - } - - /** - * Returns a list of subscribers who don't have permission to receive messages from this channel. - * If their permissions are updated, they'll automatically start receiving new messages. - * - * @return MessageChannelSubscriber[] - * @phpstan-return array - */ - public function getNonPermittedSubscribers() : array{ - return $this->nonPermittedSubscribers->toArray(); - } - - /** - * Returns a list of all broadcast subscribers with permission to receive messages from this channel, and who can - * receive non-standard message types such as tips, popups and titles. - * - * @return MinecraftMessageChannelSubscriber[] - * @phpstan-return array - */ - public function getMinecraftPermittedSubscribers() : array{ - return array_filter($this->permittedSubscribers->toArray(), fn(MessageChannelSubscriber $s) => $s instanceof MinecraftMessageChannelSubscriber); - } -} diff --git a/src/MinecraftMessageChannelSubscriber.php b/src/MinecraftMessageBroadcastSubscriber.php similarity index 64% rename from src/MinecraftMessageChannelSubscriber.php rename to src/MinecraftMessageBroadcastSubscriber.php index f7f2f642c8..925ba8eca6 100644 --- a/src/MinecraftMessageChannelSubscriber.php +++ b/src/MinecraftMessageBroadcastSubscriber.php @@ -26,11 +26,11 @@ use pocketmine\command\CommandSender; use pocketmine\lang\Translatable; -interface MinecraftMessageChannelSubscriber extends MessageChannelSubscriber{ +interface MinecraftMessageBroadcastSubscriber extends MessageBroadcastSubscriber{ - public function onTip(string $channelId, CommandSender $source, Translatable|string $message) : void; + public function onTip(CommandSender $source, Translatable|string $message, string $channelId) : void; - public function onPopup(string $channelId, CommandSender $source, Translatable|string $message) : void; + public function onPopup(CommandSender $source, Translatable|string $message, string $channelId) : void; - public function onTitle(string $channelId, CommandSender $source, string $title, string $subtitle = "", int $fadeIn = -1, int $stay = -1, int $fadeOut = -1) : void; + public function onTitle(CommandSender $source, string $title, string $subtitle, int $fadeIn, int $stay, int $fadeOut, string $channelId) : void; } diff --git a/src/Server.php b/src/Server.php index 547738990e..3621b0574b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -70,7 +70,6 @@ use pocketmine\network\query\QueryInfo; use pocketmine\network\upnp\UPnPNetworkInterface; use pocketmine\permission\BanList; -use pocketmine\permission\DefaultPermissionNames; use pocketmine\permission\DefaultPermissions; use pocketmine\player\DatFilePlayerDataProvider; use pocketmine\player\GameMode; @@ -104,7 +103,7 @@ use pocketmine\utils\Filesystem; use pocketmine\utils\Internet; use pocketmine\utils\MainLogger; -use pocketmine\utils\MessageLoggerForwarder; +use pocketmine\utils\BroadcastLoggerForwarder; use pocketmine\utils\NotCloneable; use pocketmine\utils\NotSerializable; use pocketmine\utils\Process; @@ -157,6 +156,7 @@ use function rename; use function round; use function sleep; +use function spl_object_id; use function sprintf; use function str_repeat; use function str_replace; @@ -181,8 +181,7 @@ class Server{ use NotSerializable; public const BROADCAST_CHANNEL_ADMINISTRATIVE = "pocketmine.broadcast.admin"; - public const BROADCAST_CHANNEL_CHAT = "pocketmine.broadcast.chat"; - public const BROADCAST_CHANNEL_GAME_EVENTS = "pocketmine.broadcast.gameEvents"; + public const BROADCAST_CHANNEL_USERS = "pocketmine.broadcast.user"; public const DEFAULT_SERVER_NAME = VersionInfo::NAME . " Server"; public const DEFAULT_MAX_PLAYERS = 20; @@ -299,10 +298,10 @@ class Server{ private SignalHandler $signalHandler; /** - * @var MessageChannel[] - * @phpstan-var array + * @var MessageBroadcastSubscriber[][] + * @phpstan-var array> */ - private array $broadcastChannels = []; + private array $broadcastSubscribers = []; public function getName() : string{ return VersionInfo::NAME; @@ -978,12 +977,6 @@ public function __construct( $this->resourceManager = new ResourcePackManager(Path::join($this->dataPath, "resource_packs"), $this->logger); - $this->broadcastChannels = [ - self::BROADCAST_CHANNEL_ADMINISTRATIVE => new MessageChannel(DefaultPermissionNames::BROADCAST_ADMIN), - self::BROADCAST_CHANNEL_CHAT => new MessageChannel(DefaultPermissionNames::BROADCAST_USER), - self::BROADCAST_CHANNEL_GAME_EVENTS => new MessageChannel(DefaultPermissionNames::BROADCAST_USER), - ]; - $pluginGraylist = null; $graylistFile = Path::join($this->dataPath, "plugin_list.yml"); if(!file_exists($graylistFile)){ @@ -1063,10 +1056,10 @@ public function __construct( $this->logger->info($this->language->translate(KnownTranslationFactory::pocketmine_server_donate(TextFormat::AQUA . "https://patreon.com/pocketminemp" . TextFormat::RESET))); $this->logger->info($this->language->translate(KnownTranslationFactory::pocketmine_server_startFinished(strval(round(microtime(true) - $this->startTime, 3))))); - $forwarder = new MessageLoggerForwarder($this->logger, $this->language); + $forwarder = new BroadcastLoggerForwarder($this->logger, $this->language); $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_ADMINISTRATIVE, $forwarder); - $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_CHAT, $forwarder); - $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_GAME_EVENTS, $forwarder); + $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_USERS, $forwarder); + $this->subscribeToBroadcastChannel(self::BROADCAST_CHANNEL_USERS, $forwarder); //TODO: move console parts to a separate component if($this->configGroup->getPropertyBool(Yml::CONSOLE_ENABLE_INPUT, true)){ @@ -1270,24 +1263,29 @@ public function createBroadcastChannel(string $channelId, ?string $permission) : * Subscribes to a particular message broadcast channel. * The channel ID can be any arbitrary string. */ - public function subscribeToBroadcastChannel(string $channelId, MessageChannelSubscriber $subscriber) : void{ - $channel = $this->getBroadcastChannel($channelId) ?? throw new \InvalidArgumentException("Channel \"$channelId\" does not exist"); - $channel->subscribe($subscriber); + public function subscribeToBroadcastChannel(string $channelId, MessageBroadcastSubscriber $subscriber) : void{ + $this->broadcastSubscribers[$channelId][spl_object_id($subscriber)] = $subscriber; } /** * Unsubscribes from a particular message broadcast channel. */ - public function unsubscribeFromBroadcastChannel(string $channelId, MessageChannelSubscriber $subscriber) : void{ - $this->getBroadcastChannel($channelId)?->unsubscribe($subscriber); + public function unsubscribeFromBroadcastChannel(string $channelId, MessageBroadcastSubscriber $subscriber) : void{ + if(isset($this->broadcastSubscribers[$channelId][spl_object_id($subscriber)])){ + if(count($this->broadcastSubscribers[$channelId]) === 1){ + unset($this->broadcastSubscribers[$channelId]); + }else{ + unset($this->broadcastSubscribers[$channelId][spl_object_id($subscriber)]); + } + } } /** * Unsubscribes from all broadcast channels. */ - public function unsubscribeFromAllBroadcastChannels(MessageChannelSubscriber $subscriber) : void{ - foreach(Utils::stringifyKeys($this->broadcastChannels) as $channelId => $channel){ - $channel->unsubscribe($subscriber); + public function unsubscribeFromAllBroadcastChannels(MessageBroadcastSubscriber $subscriber) : void{ + foreach(Utils::stringifyKeys($this->broadcastSubscribers) as $channelId => $recipients){ + $this->unsubscribeFromBroadcastChannel($channelId, $subscriber); } } @@ -1295,63 +1293,86 @@ public function unsubscribeFromAllBroadcastChannels(MessageChannelSubscriber $su * Returns a list of all the broadcast subscribers subscribed to the given broadcast channel with permission to * receive messages from it. * - * @return MessageChannelSubscriber[] - * @phpstan-return array + * @return MessageBroadcastSubscriber[] + * @phpstan-return array */ public function getBroadcastChannelSubscribers(string $channelId) : array{ - return $this->getBroadcastChannel($channelId)?->getPermittedSubscribers() ?? []; + return $this->broadcastSubscribers[$channelId] ?? []; } /** - * @param MessageChannelSubscriber[]|null $recipients + * @param MessageBroadcastSubscriber[]|null $recipients */ - public function broadcastMessage(string $channelId, Translatable|string $message, CommandSender $cause, ?array $recipients = null) : int{ + public function broadcastMessage(CommandSender $source, Translatable|string $message, string $channelId = self::BROADCAST_CHANNEL_USERS, ?array $recipients = null) : int{ $recipients = $recipients ?? $this->getBroadcastChannel($channelId)?->getPermittedSubscribers() ?? []; foreach($recipients as $recipient){ - $recipient->onMessage($channelId, $cause, $message); + $recipient->onMessage($source, $message, $channelId); } return count($recipients); } /** - * @param MinecraftMessageChannelSubscriber[]|null $recipients + * @return MinecraftMessageBroadcastSubscriber[] + */ + private function getMinecraftBroadcastSubscribers(string $channelId) : array{ + /** @var Player[] $players */ + $players = []; + foreach($this->broadcastSubscribers[$channelId] ?? [] as $subscriber){ + if($subscriber instanceof MinecraftMessageBroadcastSubscriber){ + $players[spl_object_id($subscriber)] = $subscriber; + } + } + return $players; + } + + /** + * @param MinecraftMessageBroadcastSubscriber[]|null $recipients */ - public function broadcastTip(string $channelId, string $tip, CommandSender $source, ?array $recipients = null) : int{ - $recipients = $recipients ?? $this->getBroadcastChannel($channelId)?->getMinecraftPermittedSubscribers() ?? []; + public function broadcastTip(CommandSender $source, string $tip, string $channelId = self::BROADCAST_CHANNEL_USERS, ?array $recipients = null) : int{ + $recipients = $recipients ?? $this->getMinecraftBroadcastSubscribers($channelId); foreach($recipients as $recipient){ - $recipient->onTip($channelId, $source, $tip); + $recipient->onTip($source, $tip, $channelId); } return count($recipients); } /** - * @param MinecraftMessageChannelSubscriber[]|null $recipients + * @param MinecraftMessageBroadcastSubscriber[]|null $recipients */ - public function broadcastPopup(string $channelId, string $popup, CommandSender $source, ?array $recipients = null) : int{ - $recipients = $recipients ?? $this->getBroadcastChannel($channelId)?->getMinecraftPermittedSubscribers() ?? []; + public function broadcastPopup(CommandSender $source, string $popup, string $channelId = self::BROADCAST_CHANNEL_USERS, ?array $recipients = null) : int{ + $recipients = $recipients ?? $this->getMinecraftBroadcastSubscribers($channelId); foreach($recipients as $recipient){ - $recipient->onPopup($channelId, $source, $popup); + $recipient->onPopup($source, $popup, $channelId); } return count($recipients); } /** - * @param int $fadeIn Duration in ticks for fade-in. If -1 is given, client-sided defaults will be used. - * @param int $stay Duration in ticks to stay on screen for - * @param int $fadeOut Duration in ticks for fade-out. - * @param MinecraftMessageChannelSubscriber[]|null $recipients + * @param int $fadeIn Duration in ticks for fade-in. If -1 is given, client-sided defaults will be used. + * @param int $stay Duration in ticks to stay on screen for + * @param int $fadeOut Duration in ticks for fade-out. + * @param MinecraftMessageBroadcastSubscriber[]|null $recipients */ - public function broadcastTitle(string $channelId, string $title, CommandSender $source, string $subtitle = "", int $fadeIn = -1, int $stay = -1, int $fadeOut = -1, ?array $recipients = null) : int{ - $recipients = $recipients ?? $this->getBroadcastChannel($channelId)?->getMinecraftPermittedSubscribers() ?? []; + public function broadcastTitle( + CommandSender $source, + string $title, + string $subtitle = "", + int $fadeIn = -1, + int $stay = -1, + int $fadeOut = -1, + string $channelId = self::BROADCAST_CHANNEL_USERS, + ?array $recipients = null + ) : int{ + $recipients = $recipients ?? $this->getMinecraftBroadcastSubscribers($channelId); foreach($recipients as $recipient){ - $recipient->onTitle($channelId, $source, $title, $subtitle, $fadeIn, $stay, $fadeOut); + $recipient->onTitle($source, $title, $subtitle, $fadeIn, $stay, $fadeOut, $channelId); } return count($recipients); diff --git a/src/command/Command.php b/src/command/Command.php index 4c17c1107a..7f13c986ee 100644 --- a/src/command/Command.php +++ b/src/command/Command.php @@ -222,16 +222,13 @@ public function setUsage(Translatable|string $usage) : void{ } public static function broadcastCommandMessage(CommandSender $source, Translatable|string $message, bool $sendToSource = true) : void{ - $subscribers = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE); $broadcast = KnownTranslationFactory::chat_type_admin($source->getName(), $message); if($sendToSource){ $source->sendMessage($message); } - foreach($subscribers as $user){ - $user->onMessage(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $source, $broadcast); - } + $source->getServer()->broadcastMessage($source, $broadcast, Server::BROADCAST_CHANNEL_ADMINISTRATIVE); } public function __toString() : string{ diff --git a/src/command/defaults/MeCommand.php b/src/command/defaults/MeCommand.php index 1bffefe2bc..321f5bc90d 100644 --- a/src/command/defaults/MeCommand.php +++ b/src/command/defaults/MeCommand.php @@ -28,7 +28,6 @@ use pocketmine\lang\KnownTranslationFactory; use pocketmine\permission\DefaultPermissionNames; use pocketmine\player\Player; -use pocketmine\Server; use pocketmine\utils\TextFormat; use function count; use function implode; @@ -50,9 +49,8 @@ public function execute(CommandSender $sender, string $commandLabel, array $args } $sender->getServer()->broadcastMessage( - Server::BROADCAST_CHANNEL_CHAT, - KnownTranslationFactory::chat_type_emote($sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::RESET . implode(" ", $args)), - $sender + $sender, + KnownTranslationFactory::chat_type_emote($sender instanceof Player ? $sender->getDisplayName() : $sender->getName(), TextFormat::RESET . implode(" ", $args)) ); return true; diff --git a/src/command/defaults/SayCommand.php b/src/command/defaults/SayCommand.php index 9ffee62ebe..379d98dab0 100644 --- a/src/command/defaults/SayCommand.php +++ b/src/command/defaults/SayCommand.php @@ -29,7 +29,6 @@ use pocketmine\lang\KnownTranslationFactory; use pocketmine\permission\DefaultPermissionNames; use pocketmine\player\Player; -use pocketmine\Server; use pocketmine\utils\TextFormat; use function count; use function implode; @@ -51,12 +50,11 @@ public function execute(CommandSender $sender, string $commandLabel, array $args } $sender->getServer()->broadcastMessage( - Server::BROADCAST_CHANNEL_CHAT, + $sender, KnownTranslationFactory::chat_type_announcement( $sender instanceof Player ? $sender->getDisplayName() : ($sender instanceof ConsoleCommandSender ? "Server" : $sender->getName()), implode(" ", $args) - )->prefix(TextFormat::LIGHT_PURPLE), - $sender + )->prefix(TextFormat::LIGHT_PURPLE) ); return true; } diff --git a/src/event/player/PlayerChatEvent.php b/src/event/player/PlayerChatEvent.php index 2999149ee6..6658dd2590 100644 --- a/src/event/player/PlayerChatEvent.php +++ b/src/event/player/PlayerChatEvent.php @@ -25,7 +25,7 @@ use pocketmine\event\Cancellable; use pocketmine\event\CancellableTrait; -use pocketmine\MessageChannelSubscriber; +use pocketmine\MessageBroadcastSubscriber; use pocketmine\player\chat\ChatFormatter; use pocketmine\player\Player; use pocketmine\utils\Utils; @@ -37,7 +37,7 @@ class PlayerChatEvent extends PlayerEvent implements Cancellable{ use CancellableTrait; /** - * @param MessageChannelSubscriber[] $recipients + * @param MessageBroadcastSubscriber[] $recipients */ public function __construct( Player $player, @@ -72,17 +72,17 @@ public function setFormatter(ChatFormatter $formatter) : void{ } /** - * @return MessageChannelSubscriber[] + * @return MessageBroadcastSubscriber[] */ public function getRecipients() : array{ return $this->recipients; } /** - * @param MessageChannelSubscriber[] $recipients + * @param MessageBroadcastSubscriber[] $recipients */ public function setRecipients(array $recipients) : void{ - Utils::validateArrayValueType($recipients, function(MessageChannelSubscriber $_) : void{}); + Utils::validateArrayValueType($recipients, function(MessageBroadcastSubscriber $_) : void{}); $this->recipients = $recipients; } } diff --git a/src/player/Player.php b/src/player/Player.php index 305ba50cca..597e465786 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -104,7 +104,7 @@ use pocketmine\lang\Language; use pocketmine\lang\Translatable; use pocketmine\math\Vector3; -use pocketmine\MinecraftMessageChannelSubscriber; +use pocketmine\MinecraftMessageBroadcastSubscriber; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\IntTag; use pocketmine\network\mcpe\NetworkSession; @@ -116,6 +116,7 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\entity\PlayerMetadataFlags; +use pocketmine\permission\DefaultPermissionNames; use pocketmine\permission\DefaultPermissions; use pocketmine\permission\Permissible; use pocketmine\permission\PermissibleBase; @@ -168,7 +169,7 @@ /** * Main class that handles networking, recovery, and packet sending to the server part */ -class Player extends Human implements MinecraftMessageChannelSubscriber, CommandSender, ChunkListener, IPlayer{ +class Player extends Human implements MinecraftMessageBroadcastSubscriber, CommandSender, ChunkListener, IPlayer{ use PermissibleDelegateTrait; private const MOVES_PER_TICK = 2; @@ -872,6 +873,19 @@ static function() : void{ Timings::$playerChunkSend->stopTiming(); } + private function recheckBroadcastPermissions() : void{ + foreach([ + DefaultPermissionNames::BROADCAST_ADMIN => Server::BROADCAST_CHANNEL_ADMINISTRATIVE, + DefaultPermissionNames::BROADCAST_USER => Server::BROADCAST_CHANNEL_USERS + ] as $permission => $channel){ + if($this->hasPermission($permission)){ + $this->server->subscribeToBroadcastChannel($channel, $this); + }else{ + $this->server->unsubscribeFromBroadcastChannel($channel, $this); + } + } + } + /** * Called by the network system when the pre-spawn sequence is completed (e.g. after sending spawn chunks). * This fires join events and broadcasts join messages to other online players. @@ -882,17 +896,19 @@ public function doFirstSpawn() : void{ } $this->spawned = true; - //subscribing will only send us messages if we have the correct permissions - $this->server->subscribeToBroadcastChannel(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this); - $this->server->subscribeToBroadcastChannel(Server::BROADCAST_CHANNEL_CHAT, $this); - $this->server->subscribeToBroadcastChannel(Server::BROADCAST_CHANNEL_GAME_EVENTS, $this); + $this->recheckBroadcastPermissions(); + $this->getPermissionRecalculationCallbacks()->add(function(array $changedPermissionsOldValues) : void{ + if(isset($changedPermissionsOldValues[Server::BROADCAST_CHANNEL_ADMINISTRATIVE]) || isset($changedPermissionsOldValues[Server::BROADCAST_CHANNEL_USERS])){ + $this->recheckBroadcastPermissions(); + } + }); $ev = new PlayerJoinEvent($this, KnownTranslationFactory::multiplayer_player_joined($this->getDisplayName())->prefix(TextFormat::YELLOW) ); $ev->call(); if($ev->getJoinMessage() !== ""){ - $this->server->broadcastMessage(Server::BROADCAST_CHANNEL_GAME_EVENTS, $ev->getJoinMessage(), $this); + $this->server->broadcastMessage($this, $ev->getJoinMessage()); } $this->noDamageTicks = 60; @@ -1514,14 +1530,13 @@ public function chat(string $message) : bool{ $this->server->dispatchCommand($this, substr($messagePart, 1)); Timings::$playerCommand->stopTiming(); }else{ - $ev = new PlayerChatEvent($this, $messagePart, $this->server->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_CHAT), new StandardChatFormatter()); + $ev = new PlayerChatEvent($this, $messagePart, $this->server->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_USERS), new StandardChatFormatter()); $ev->call(); if(!$ev->isCancelled()){ $this->server->broadcastMessage( - Server::BROADCAST_CHANNEL_CHAT, - $ev->getFormatter()->format($ev->getPlayer()->getDisplayName(), $ev->getMessage()), $ev->getPlayer(), - $ev->getRecipients() + $ev->getFormatter()->format($ev->getPlayer()->getDisplayName(), $ev->getMessage()), + recipients: $ev->getRecipients() ); } } @@ -2122,7 +2137,7 @@ public function setTitleDuration(int $fadeIn, int $stay, int $fadeOut) : void{ } } - public function onMessage(string $channelId, CommandSender $source, Translatable|string $message) : void{ + public function onMessage(CommandSender $source, Translatable|string $message, string $channelId) : void{ if($channelId === Server::BROADCAST_CHANNEL_ADMINISTRATIVE){ if($source === $this){ return; @@ -2136,24 +2151,20 @@ public function onMessage(string $channelId, CommandSender $source, Translatable $this->sendMessage($message); } - public function onTip(string $channelId, CommandSender $source, Translatable|string $message) : void{ + public function onTip(CommandSender $source, Translatable|string $message, string $channelId) : void{ //TODO: support translations in the proper plugin methods $this->sendTip($message instanceof Translatable ? $this->getLanguage()->translate($message) : $message); } - public function onPopup(string $channelId, CommandSender $source, Translatable|string $message) : void{ + public function onPopup(CommandSender $source, Translatable|string $message, string $channelId) : void{ //TODO: support translations in the proper plugin methods $this->sendPopup($message instanceof Translatable ? $this->getLanguage()->translate($message) : $message); } - public function onTitle(string $channelId, CommandSender $source, string $title, string $subtitle = "", int $fadeIn = -1, int $stay = -1, int $fadeOut = -1) : void{ + public function onTitle(CommandSender $source, string $title, string $subtitle, int $fadeIn, int $stay, int $fadeOut, string $channelId) : void{ $this->sendTitle($title, $subtitle, $fadeIn, $stay, $fadeOut); } - public function getPermissible() : ?Permissible{ - return $this; - } - /** * Sends a direct chat message to a player */ @@ -2311,7 +2322,7 @@ public function onPostDisconnect(Translatable|string $reason, Translatable|strin $ev = new PlayerQuitEvent($this, $quitMessage ?? $this->getLeaveMessage(), $reason); $ev->call(); if(($quitMessage = $ev->getQuitMessage()) != ""){ - $this->server->broadcastMessage(Server::BROADCAST_CHANNEL_GAME_EVENTS, $quitMessage, $this); + $this->server->broadcastMessage($this, $quitMessage); } $this->save(); @@ -2442,7 +2453,7 @@ protected function onDeath() : void{ } if($ev->getDeathMessage() != ""){ - $this->server->broadcastMessage(Server::BROADCAST_CHANNEL_GAME_EVENTS, $ev->getDeathMessage(), $this); + $this->server->broadcastMessage($this, $ev->getDeathMessage()); } $this->startDeathAnimation(); diff --git a/src/utils/MessageLoggerForwarder.php b/src/utils/BroadcastLoggerForwarder.php similarity index 76% rename from src/utils/MessageLoggerForwarder.php rename to src/utils/BroadcastLoggerForwarder.php index 21fea631f8..dfc4de696e 100644 --- a/src/utils/MessageLoggerForwarder.php +++ b/src/utils/BroadcastLoggerForwarder.php @@ -26,29 +26,24 @@ use pocketmine\command\CommandSender; use pocketmine\lang\Language; use pocketmine\lang\Translatable; -use pocketmine\MessageChannelSubscriber; -use pocketmine\permission\Permissible; +use pocketmine\MessageBroadcastSubscriber; /** * Forwards any messages it receives via sendMessage() to the given logger. Used for forwarding chat messages and * command audit log messages to the server log file. */ -final class MessageLoggerForwarder implements MessageChannelSubscriber{ +final class BroadcastLoggerForwarder implements MessageBroadcastSubscriber{ public function __construct( private \Logger $logger, private Language $language ){} - public function onMessage(string $channelId, CommandSender $source, Translatable|string $message) : void{ + public function onMessage(CommandSender $source, Translatable|string $message, string $channelId) : void{ if($message instanceof Translatable){ $this->logger->info($this->language->translate($message)); }else{ $this->logger->info($message); } } - - public function getPermissible() : ?Permissible{ - return null; //we don't care about channel permissions - we just want to receive everything - } }