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

What is wrong?

Discussion in 'Development' started by #A6543, Feb 21, 2017.

  1. #A6543

    #A6543 Zombie

    Messages:
    267
    And how can I do that?
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    PHP:
    <?php
     
    namespace YourName\YourPlugin;

     use 
    pocketmine\plugin\PluginBase;
     use 
    pocketmine\Player;
     use 
    pocketmine\event\Listener;
     use 
    pocketmine\event\server\DataPacketReceiveEvent;
     use 
    pocketmine\event\player\PlayerJoinEvent;
     use 
    pocketmine\event\player\PlayerQuitEvent;
     use 
    pocketmine\network\protocol\LoginPacket;

      class 
    Main extends PluginBase implements Listener {
     
      private 
    $username[];

        public function 
    onDataPacket(DataPacketReceiveEvent $event) {
          if(
    $event->getPacket() instanceof LoginPacket) {
       
    /* Using IP as the key. Probably a bad idea, but not moreso than this entire example. */
            
    $this->username[$event->getPlayer()->getAddress()] = $event->getPacket()->username;
          }
        }
        public function 
    onJoin(PlayerJoinEvent $event) {
        if(isset(
    $this->username[$event->getPlayer()->getAddress()])) {
          echo 
    $this->username[$event->getPlayer()->getAddress()];
          }
        }

        public function 
    onQuit(PlayerQuitEvent $event) {
          if(isset(
    $this->username[$event->getPlayer()->getAddress()])) {
            unset(
    $this->username[$event->getPlayer()->getAddress()]);
          }
        }
      }
    I regret writing this, because it is absolutely pointless and, honestly, stupid. I only hope that whatever you plan to do is more logical than this.
     
  3. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    PHP:
    public $username = [];
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    There no reason to use public for $username
     
    Muqsit and corytortoise like this.
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Why do you think it needs to be public instead of private?
     
  6. ALLINSEO

    ALLINSEO Baby Zombie

    Messages:
    133
    sorry to bump this thread but it was related to my issue and thought you guys could chime in.

    also still not sure if I should use public or private for my purpose, because I need to generate username to input through the console.
    Link;
    How To Get Player Username PocketMine
     
  7. ALLINSEO

    ALLINSEO Baby Zombie

    Messages:
    133

    looking back at this code, and trying to implement parts of it for my own purpose is rather confusing.
    what is >getAddess doing? I simply just need to be able to output the plain text username "user_123"

    I also dont need it to run at login, since it activates by a "tap" ( left click ) and will be issueing commands through the console, it would have to be each unique event, cant really set variables.

    My Issue is;

    I seem to be looking for the username wrong

    Code:
    $block = $tap->getPlayer()->getLevel()->getTile($tap->getBlock());
         $PlayerName = $tap->getPlayer();
    
    When I try to output $PlayerName I get "player(1)" "player(2)" "player(3)" instead of the desired username
    if I could get their username instead of player number or convert the number to a username somehow I think I can get it going.
     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You need to use $tap->getPlayer()->getName(); to get the username. Whenever you use $anyEvent->getPlayer(), it will return a Player Object. You can then get data(or run functions like $playerObject->teleport()) with the Object.
     
  9. ALLINSEO

    ALLINSEO Baby Zombie

    Messages:
    133
    oh wow that was really simple. is there any documentation to where those functions are? getplayer, getname etc.
     
  10. Primus

    Primus Zombie Pigman

    Messages:
    749
    I still use docs.pocketmine.net although it's for API 1.10.0
     
    Sandertv and Muqsit like this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Hey, how about using PlayerLoginEvent rather than a spammy DataPacket*Event?
     
  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.