Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Set a variable to a players message.

Comments in 'Plugin Development' started by xFlare, Nov 29, 2014.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    How can I set a message to a variable? Example:

    PHP:
    public function (PlayerChatEvent $event) {
        
    $player $event->getPlayer();
        
    $name $player->getName();
        if (
    $name == "xFlare") {
            
    //$message = whatever xFlare said
            
    $event->setCancelled();
            
    $this->getServer()->broadcastMessage("---------------------------------"); //these lines are the purpose of this plugin.
            
    $this->getServer()->broadcastMessage("[Owner] xFlare $message//broadcaste my message that got canceled
            
    $this->getServer()->broadcastMessage("---------------------------------");
  2. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Use
    PHP:
    Server::getInstance()->broadcastMessage($message);
    xFlare likes this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why do you Server::getInstance()? Can you start using $this->getServer()?
    iJoshuaHD and xFlare like this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    This is already correct
  5. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    How?, I mean, I didn't put anything to define what $message is?
  6. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    define $message as $event->getMessage();
    PEMapModder and xFlare like this.
  7. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    dude! you forgot to add ');' on $this->getServer()->broadcastMessage("[Owner] xFlare $message"

    it should be: $this->getServer()->broadcastMessage("[Owner] xFlare $message");
    xFlare, PEMapModder and RekkuzaRage like this.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    $a === $b
    This checks if $a is equal to $b and that $a is the same type of $b (e.g. both booleans)
    $a == $b
    This checks if $a is equal to $b. You are recommended to use === instead.
    $a = $b
    This changes the value of $a to $b. Left hand side must be a variable, an array variable element or a call to an & function (if you don't view what a & function is, don't even think about it), and the right hand side is an expression of any values.
    iJoshuaHD likes this.
  9. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Hehe, Took me 5 minutes to notice that after posting that xD
  10. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    DevTools was pointing at the wrong line though :O
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    post your whole code so we could check.
  12. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    PHP:
    <?php

    namespace xFlare\OwnerChat;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\event\Listener;


    class 
    Main extends PluginBase implements Listener
    {
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("Everything loaded!");
        }

        public function 
    PlayerChatEvent(PlayerChatEvent $event) {
            
    $player $event->getPlayer();
            
    $name $player->getName();
            if (
    $name == "xFlare") {
                
    $message $event->getMessage();
                
    $event->setCancelled();
                
    $this->getServer()->broadcastMessage("---------------------------------");
                
    $this->getServer()->broadcastMessage("[Owner] xFlare $message");
                
    $this->getServer()->broadcastMessage("---------------------------------");
            }
        }
    }
    ?>
  13. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    We need to define $message using a better way.
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    there is no other way because its correct.
  15. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Somthing else is wrong in that case. Hmm. When I run the plugin console says undefined variable $message.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You are loading source plugin or what? Did you forget to update the phar maybe?
    xFlare likes this.
  17. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,167
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    oops. instead of using PlayerChatEvent, use this instead:
    PlayerCommandPreprocessEvent
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Both are same.
  19. Sergey_D
    Offline

    Sergey_D Active Member

    Joined:
    Jan 19, 2014
    Posts:
    75
    Minecraft User:
    CLILLZ
    PHP:
    $msg $e->getMessage();
    $this->getServer()->broadcastMessage("[OWNER] xFlare " implode(" "$msg));
    Last edited: Nov 29, 2014
  20. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What? The message is an array? And what is $msg there for if you don't use it?
    xFlare, iJoshuaHD and EvolSoft like this.

Share This Page

Advertisement