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

Solved onArenaJoin event

Comments in 'Plugin Development' started by FlamingGenius, Oct 1, 2015.

  1. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    I feel like I am doing this wrong
    the goal was when user joins the arena a message gets sent to everyone
    "Players name has connected" I am 50-50 on this I think I did it right

    PHP:
    namespace FlamingGenius\SandRun;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\PlayerJoinEvent;
    class 
    arena{
      public function 
    onArenaJoin(PlayerJoinEvent $event){
      
    $players $event->getServer()->getOnlinePlayers();
      foreach(
    $players as $player){
      
    $player->getName();
      
    $this->getServer()->broadcastMessage($player "Has Connected");
      }
      }
    }
    ?>
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    You forgot to define $player. :p
  3. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    ?
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Should be:
    PHP:
    namespace FlamingGenius\SandRun;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\PlayerJoinEvent;
    class 
    arena{
      public function 
    onArenaJoin(PlayerJoinEvent $event){
      foreach(
    $this->getServer()->getOnlinePlayers() as $player){
      
    $player->sendMessage($event->getPlayer()->getName() . "Has Connected"); //gets name of player that joined the server and sends it
      
    }
      }
    }
    ?>
    But I recommend using Server::broadcastMessage() in this case.
  5. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    Thx
  6. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    why Server::broadcastMessage()?

    and I prefer variables
    PHP:
    class arena{
    public function 
    onArenaJoin(PlayerJoinEvent $event){
    $players $this->getServer()->getOnlinePlayers();
    foreach(
    $players as $player){
    $name $event->getPlayer()->getName();
    $this->getServer()->broadcastMessage($name "Has Connected");
    }
    }
    }
    ?>
  7. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    He wants the message to be seen by the players already in the arena, not the entire server.
  8. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Well the server is the arena. :p
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    He means the function, not to tell you to use static :p you can't use it with static anyway.

    You are running the code for every player $player if you write foreach. You are then broadcasting the message once for every player, so when a player joins when the server has 6 players, you are broadcasting the message 6 rimes.
  10. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    That is the point it broadcast as soon as the person joins the level or "arena"
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. I mean, each broadcast sends a message to each player, but if you foreach it, you are running the code for multiple times, the value being the number of players online.

Share This Page

Advertisement