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

getPlayer() and getName ().

Discussion in 'Facepalm' started by ZakousseMC, Dec 21, 2016.

  1. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    Hi,

    I would like to know if there is a way so that, when a player writes a command, a message is broadcasted with the name of the player in front. eg:

    JeanCharles:
    /afk
    --
    JeanCharles is now AFK.

    I tried this code :
    PHP:
    public function onCommand(CommandSender $senderPlayerCommandPreprocessEvent $eventCommand $cmd$label, array $args) {
        
    $sender->sendMessage(TextFormat::GREEN "Vous etes desormais AFK.");
            
    $sender $event->getPlayer();
            
    $name $sender->getName();
            
    $this->getServer()->broadcastMessage("$name est desormais AFK.");
        return 
    false;
    }
    but it dosen't work.

    Error message in crashdump :
    PHP:
    ErrorDeclaration of zakoussemc\afkplugin\Main::eek:nCommand(pocketmine\command\CommandSender $senderpocketmine\event\player\PlayerCommandPreprocessEvent $eventpocketmine\command\Command $cmd$label, array $argsmust be compatible with pocketmine\command\CommandExecutor::eek:nCommand(pocketmine\command\CommandSender $senderpocketmine\command\Command $command$label, array $args)
    Full code of Main.php :
    PHP:
    <?php
    namespace zakoussemc\afkplugin;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerCommandPreprocessEvent;

    class 
    Main extends PluginBase implements Listener {
    public function 
    onEnable() {
        @
    mkdir($this->getDataFolder());
        
    $this->saveDefaultConfig();
        
    $this->reloadConfig();
        
    $this->getLogger()->info(TextFormat::RED "its alive");
        
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
        }

    public function 
    translateColors($symbol$message) {
        
    $message str_replace($symbol."0"TextFormat::BLACK$message);
        
    $message str_replace($symbol."1"TextFormat::DARK_BLUE$message);
        
    $message str_replace($symbol."2"TextFormat::DARK_GREEN$message);
        
    $message str_replace($symbol."3"TextFormat::DARK_AQUA$message);
        
    $message str_replace($symbol."4"TextFormat::DARK_RED$message);
        
    $message str_replace($symbol."5"TextFormat::DARK_PURPLE$message);
        
    $message str_replace($symbol."6"TextFormat::GOLD$message);
        
    $message str_replace($symbol."7"TextFormat::GRAY$message);
        
    $message str_replace($symbol."8"TextFormat::DARK_GRAY$message);
        
    $message str_replace($symbol."9"TextFormat::BLUE$message);
        
    $message str_replace($symbol."a"TextFormat::GREEN$message);
        
    $message str_replace($symbol."b"TextFormat::AQUA$message);
        
    $message str_replace($symbol."c"TextFormat::RED$message);
        
    $message str_replace($symbol."d"TextFormat::LIGHT_PURPLE$message);
        
    $message str_replace($symbol."e"TextFormat::YELLOW$message);
        
    $message str_replace($symbol."f"TextFormat::WHITE$message);

        
    $message str_replace($symbol."k"TextFormat::OBFUSCATED$message);
        
    $message str_replace($symbol."l"TextFormat::BOLD$message);
        
    $message str_replace($symbol."m"TextFormat::STRIKETHROUGH$message);
        
    $message str_replace($symbol."n"TextFormat::UNDERLINE$message);
        
    $message str_replace($symbol."o"TextFormat::ITALIC$message);
        
    $message str_replace($symbol."r"TextFormat::RESET$message);
        return 
    $message;
        }
        
    // Acces aux permissions.
    private function access(CommandSender $sender$permission) {
        if(
    $sender->hasPermission($permission)) return true;
        
    $sender->sendMessage("Vous n'avez pas la permission d'utiliser cette commande.");
        return 
    false;
    }
    private function 
    inGame(CommandSender $sender,$msg true) {
        if (
    $sender instanceof Player) return true;
        if (
    $msg$sender->sendMessage("Vous pouvez seulement utiliser cette commande en jeu.");
        return 
    false;
    }
    public function 
    onCommand(CommandSender $senderPlayerCommandPreprocessEvent $eventCommand $cmd$label, array $args) {
        
    $sender->sendMessage(TextFormat::GREEN "Vous etes desormais AFK.");
            
    $sender $event->getPlayer();
            
    $name $sender->getName();
            
    $this->getServer()->broadcastMessage("$name est desormais AFK.");
        return 
    false;
    }
    }
    Thanks!
     
  2. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    I changed the code a bit, 2 spaces were not useful
    PHP:
    <?php
    namespace zakoussemc\afkplugin;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerCommandPreprocessEvent;

    class 
    Main extends PluginBase implements Listener {
    public function 
    onEnable() {
        @
    mkdir($this->getDataFolder());
        
    $this->saveDefaultConfig();
        
    $this->reloadConfig();
        
    $this->getLogger()->info(TextFormat::RED "its alive");
        
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
        }

    public function 
    translateColors($symbol$message) {
        
    $message str_replace($symbol."0"TextFormat::BLACK$message);
        
    $message str_replace($symbol."1"TextFormat::DARK_BLUE$message);
        
    $message str_replace($symbol."2"TextFormat::DARK_GREEN$message);
        
    $message str_replace($symbol."3"TextFormat::DARK_AQUA$message);
        
    $message str_replace($symbol."4"TextFormat::DARK_RED$message);
        
    $message str_replace($symbol."5"TextFormat::DARK_PURPLE$message);
        
    $message str_replace($symbol."6"TextFormat::GOLD$message);
        
    $message str_replace($symbol."7"TextFormat::GRAY$message);
        
    $message str_replace($symbol."8"TextFormat::DARK_GRAY$message);
        
    $message str_replace($symbol."9"TextFormat::BLUE$message);
        
    $message str_replace($symbol."a"TextFormat::GREEN$message);
        
    $message str_replace($symbol."b"TextFormat::AQUA$message);
        
    $message str_replace($symbol."c"TextFormat::RED$message);
        
    $message str_replace($symbol."d"TextFormat::LIGHT_PURPLE$message);
        
    $message str_replace($symbol."e"TextFormat::YELLOW$message);
        
    $message str_replace($symbol."f"TextFormat::WHITE$message);

        
    $message str_replace($symbol."k"TextFormat::OBFUSCATED$message);
        
    $message str_replace($symbol."l"TextFormat::BOLD$message);
        
    $message str_replace($symbol."m"TextFormat::STRIKETHROUGH$message);
        
    $message str_replace($symbol."n"TextFormat::UNDERLINE$message);
        
    $message str_replace($symbol."o"TextFormat::ITALIC$message);
        
    $message str_replace($symbol."r"TextFormat::RESET$message);
        return 
    $message;
        }
        
    // Acces aux permissions.
    private function access(CommandSender $sender$permission) {
        if(
    $sender->hasPermission($permission)) return true;
        
    $sender->sendMessage("Vous n'avez pas la permission d'utiliser cette commande.");
        return 
    false;
    }
    private function 
    inGame(CommandSender $sender,$msg true) {
        if (
    $sender instanceof Player) return true;
        if (
    $msg$sender->sendMessage("Vous pouvez seulement utiliser cette commande en jeu.");
        return 
    false;
    }
    public function 
    onCommand(CommandSender $sender,PlayerCommandPreprocessEvent $event,Command $cmd,$label,array $args) {
        
    $sender->sendMessage(TextFormat::GREEN "Vous etes desormais AFK.");
            
    $sender $event->getPlayer();
            
    $name $sender->getName();
            
    $this->getServer()->broadcastMessage("$name est desormais AFK.");
        return 
    false;
    }
    }

     
  3. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    :facepalm::facepalm::facepalm:

    Why do you need $sender = $event->getPlayer()?

    Just check if $sender is instanceof Player and then use it.

    PHP:
    public function onCommand(CommandSender $senderPlayerCommandPreprocessEvent $eventCommand $cmd$label, array $args) {
    becomes
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args) {
     
  4. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    So instead of

    PHP:
    public function onCommand(CommandSender $sender,PlayerCommandPreprocessEvent $event,Command $cmd,$label,array $args) {
        
    $sender->sendMessage(TextFormat::GREEN "Vous etes desormais AFK.");
            
    $sender $event->getPlayer();
            
    $name $sender->getName();
            
    $this->getServer()->broadcastMessage("$name est desormais AFK.");
        return 
    false;
    }
    I put it like this ? :
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args) {
        
    $sender->sendMessage(TextFormat::GREEN "Vous etes desormais AFK.");
        if (
    $sender instanceof Player) return true;
        
    $this->getServer()->broadcastMessage("$name est desormais AFK.");
        return 
    false;
    }
     
  5. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    No. $name would not be defined. Think about it. If the $sender is a player, you want it to broadcast his name. What you are doing here is checking if the sender is a player, return (leave) the function without broadcasting.
     
  6. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    So there is no way of broadcasting the player name with a message behind ?
     
  7. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    Learn PHP. Honestly, even if I give you the code you will just copy and paste it...

    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args) {
        
    $sender->sendMessage(TextFormat::GREEN "Vous etes desormais AFK.");
        if (!(
    $sender instanceof Player)) return true;
        
    $this->getServer()->broadcastMessage("{$sender->getName()} est desormais AFK.");
        return 
    true;
    }
     
  8. imYannic

    imYannic Baby Zombie

    Messages:
    113
    $name = $sender->getName();
    ...
     
  9. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    Oh right, I didn't think of that
     
  10. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    I'm trying to learn it, but I can't find any courses in french, I'm french not english and it's hard for me to understand everything that's writen...
     
    Legoboy0215 likes this.
  11. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    Hm... I am pretty sure there are basic syntax tutorials in French. Then learn by doing some hands on. PocketMine plugin development is/ not a good place to start with.
     
  12. ZakousseMC

    ZakousseMC Spider Jockey

    Messages:
    46
    GitHub:
    ZakousseMC
    But I'm a bit lost, isn't PHP for websites ?
     
  13. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Eh yeah
     
  14. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    It's not a matter of thinking, it's about reading docs, going through the source or using an IDE. You can't just think of a new function/method name and have it do exactly what you want it to do.

    Please learn PHP before you post anything in the plugin development section. This forum is for help with the PocketMine API, not for helping people who think programming is a matter of copying and pasting.
     
  15. TheDragonRing

    TheDragonRing Witch

    Messages:
    64
    GitHub:
    dragonwocky
    :facepalm: it is.. But it can also be used for software like PocketMine. PMMP and all it's plugins are coded in PHP.
     
  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.