1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Hello again.

Discussion in 'Help' started by firen, Jun 2, 2021.

  1. firen

    firen Spider

    Messages:
    14
    I have a plugin called "GroupJoin", it works like this:

    - You determine via configuration which groups could announce their entry on the server, for example:

    Owner: "Owner {player} has joined in the game"

    (plugin is linked directly to PurePerms and PureChat).

    I would like to make these messages only appear in a world that I determine via configuration, is that possible?

    Summary: I want incoming messages to work only in a world that I define, not globally.

    Main Code:



    <?php
    declare(strict_types=1);
    namespace Himbeer\GroupJoin;
    use _64FF00\PurePerms\PurePerms;
    use pocketmine\event\Listener;
    use pocketmine\event\player\PlayerJoinEvent;
    use pocketmine\event\player\PlayerQuitEvent;
    use pocketmine\Player;
    use pocketmine\plugin\PluginBase;
    class Main extends PluginBase implements Listener {
    /** @var bool */
    private $hideOther;
    /** @var string[][] */
    private $messages;
    /** @var PurePerms */
    private $purePerms;
    public function onEnable(): void {
    $this->saveDefaultConfig();
    $this->hideOther = (bool)$this->getConfig()->get("hide-other");
    $this->messages = $this->getConfig()->get("groups");
    $this->purePerms = $this->getServer()->getPluginManager()->getPlugin("PurePerms");
    $this->getServer()->getPluginManager()->registerEvents($this, $this);
    }
    private function getGroupNameForPlayer(Player $player): string {
    $ppGroup = $this->purePerms->getUserDataMgr()->getGroup($player);
    if ($ppGroup === null) {
    // This should never happen, if it does, the server owner messed up their PurePerms config
    // We don't need to log this, PurePerms does that already
    return "";
    }
    return $ppGroup->getName();
    }
    private function getMessageForPlayer(Player $player, string $type): ?string {
    $groupName = $this->getGroupNameForPlayer($player);
    if (!isset($this->messages[$groupName])) {
    return null;
    }
    $messages = $this->messages[$groupName];
    if (!isset($messages[$type])) {
    return null;
    }
    return str_replace("{player}", $player->getName(), $messages[$type]);
    }
    public function onJoin(PlayerJoinEvent $event) {
    $message = $this->getMessageForPlayer($event->getPlayer(), "join");
    if ($message) {
    $event->setJoinMessage($message);
    } else if ($this->hideOther) {
    $event->setJoinMessage("");
    }
    }
    public function onQuit(PlayerQuitEvent $event) {
    $message = $this->getMessageForPlayer($event->getPlayer(), "leave");
    if ($message) {
    $event->setQuitMessage($message);
    } else if ($this->hideOther) {
    $event->setQuitMessage("");
    }
    }
    }

    Summary: I want incoming messages to work only in a world that I define, not globally.
    Repository: https://github.com/HimbeersaftLP/GroupJoin
     
  2. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Foreach all players in a world and send message. If there’s a function that does the same thing in PMMP, please let me know.
     
    firen and Axon like this.
  3. Axon

    Axon Zombie

    Messages:
    276
    Also add PHP blocks around your code when you are on the forums, it makes it look neater. Eg:
    [P HP]
    // code here
    [/P HP]

    Also remove the space on the codeblock.
     
    Agent, NTT and firen like this.
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    Why don't you use the PurePerms to assign permission for the announcement? By doing that you have more flexibility.
     
    NTT, firen, EdwardHamHam and 2 others like this.
  5. firen

    firen Spider

    Messages:
    14
    Thanks for letting me know.

    I didn't know before.

    THX!
     
  6. firen

    firen Spider

    Messages:
    14
    Ok!

    THX!
     
    minijaham likes this.
  7. firen

    firen Spider

    Messages:
    14
    Could you explain better?
     
  8. Primus

    Primus Zombie Pigman

    Messages:
    749
    [​IMG]
     
    firen, Axon and minijaham like this.
  9. Axon

    Axon Zombie

    Messages:
    276
    This is funny.
    If you want to send players who have the permission "plugin.seeJoin", then something like this could work.
    PHP:
    foreach ($this->getServer()->getOnlinePlayers() as $player) {
        if(!
    $player->hasPermission("plugin.seeJoin")){continue;}
        
    $player->sendMessage(str_replace("{name}",$player->getName(),"§b{name} has joined the server"));
    If you want to let players see the player join message if they are on a certain world, then something like this could work.
    PHP:
    foreach ($this->getServer()->getOnlinePlayers() as $player) {
        if(!
    $player->getPlayer()->getLevel()->getName() == "LevelName"){continue;}
        
    $player->sendMessage(str_replace("{name}",$player->getName(),"§b{name} has joined the server"));
     
    Agent, firen, minijaham and 1 other person like this.
  10. Primus

    Primus Zombie Pigman

    Messages:
    749
    Or Server::broadcastMessage($message, Server::getLevelByName("LevelName")?->getPlayers() ?? []);
     
    firen, minijaham and Axon like this.
  11. Axon

    Axon Zombie

    Messages:
    276
    Surprisingly, I never knew about that.
     
    Agent, firen and Primus like this.
  12. Primus

    Primus Zombie Pigman

    Messages:
    749
    Which part tho?

    That's "pseudocode" I used Paamayim Nekudotayim aka. double-colon symbol to reference the method in that class, saying that as a disclaimer before anyone cusses me out saying that there aren't such static methods.

    The ?-> operator is php8 feature, that I used for pseudocode. And because the second argument of broadcastMessage only accepts an array, I used the null coalescing operator ??

    TL;DR: I wanted to represent the idea and not write the actual code
     
    Axon and firen like this.
  13. firen

    firen Spider

    Messages:
    14
    Bro, i love you.
    Thanks
     
  14. Axon

    Axon Zombie

    Messages:
    276
    I never knew about pseudocode :p, EG: `$OnlinePlayers ?? []`. I always use `if($OnlinePlayers == []){$data = "" }` instead
    Thanks for informing me about it! This information is going to my PHP useful slot :)
    I'm officially a Baby Zombie, 100th milestone reached!
     
    Agent, NTT and Primus like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.