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

how can i make global $sender

Discussion in 'Development' started by Levi, Nov 23, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how can I make $sender of commandSender available for any events? So that I can do $sender->getName() in JoinEvent
     
  2. dirouc

    dirouc Silverfish

    Messages:
    19
    GitHub:
    dirouc
    A separate function or variable, what's the problem? compare with Player or CommandSender...
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Can you explain what you're trying to do?
     
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    hey sofe i had made a ban plugin and onLogiEVENT I have a $player->close(“”, “You have been banned”);
    but I want to do $player->close(“”, “You have been banned by “.$sender->getName());
    sender getname as in the CommandSender
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    1. Use $player->kick(), not $player->close().
    2. Do you know what $sender->getName() means? If you understand it better, you would solve this problem.
     
  6. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Rather than making $sender global why don't you define the variable $sender on whatever event your trying to use, here il give you some code
    PHP:
    $sender $event->getPlayer();//defined variable $sender
    Either way you will still get their name by using
    PHP:
    $sender->getName();//$sender has to be defined
    Hope this helped :)
     
    Last edited: Nov 24, 2017
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You already have the $player variable. Why do you have to call it $sender? Please, learn about variables; it's very simple to understand.
     
  8. NickTehUnicorn

    NickTehUnicorn Zombie

    Messages:
    200
    GitHub:
    unickorn
    If you were trying to do a ban command, store the name of the person who banned the player and show the name of the person who banned to the banned person: (the sentence was less complicated in my mind)

    outside of a function:
    PHP:
    public $person;
    public 
    $banned;
    in the command:
    PHP:
    $this->person $sender->getName();
    $this->banned $args[0];
    in PlayerJoinEvent function:
    PHP:
    $player $event->getPlayer();
    if(
    $player->getName() === $this->banned){
        
    $player->kick("You have been banned by ".$this->person);
    }
    Let's say player Steve banned player Alex by running the command /serverban Alex.
    When Steve bans her, his name gets written to $person and the first argument gets written to $banned.
    When Alex joins, she gets kicked saying "You have been banned by Steve"
     
    Last edited: Nov 24, 2017
  9. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    Not a good idea... once server restarts the global variables will loss there data.
     
  10. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Global variables in a plugin will be reset after the server reboots because they store in the server's RAM. RAM's are called as a Volatile Memory. They lose data when they lose power. Something similar to computer's RAM.
     
  11. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    an easy way around this (but not correct) is to use permissions; when the player is banned give the player permission 'player.banned' and just change the join event to:
    PHP:
    $player $event->getPlayer();
    if(
    $player->hasPermission("player.banned")) {
        
    $player->kick("You have been banned by ".$this->person);
    }
    I do this for a lot of things. Simple and works ;)
     
  12. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    You're doing a lot for simple thing, my suggestions:
    1) Store all this in a YAML File - May eat up your RAM and take time for writing and reading..
    2) Use SQLite3 - Simple, lightweight and fast!

    With all these, you don't need to get a global variable. It's all there, you just have to get them correctly! :)
     
    NickTehUnicorn likes this.
  13. NickTehUnicorn

    NickTehUnicorn Zombie

    Messages:
    200
    GitHub:
    unickorn
    Well, I just used global variables class properties (sorry SOFe) because the thread is about that ¯\_(ツ)_/¯
     
    Last edited: Nov 25, 2017
  14. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Not true. Permissions are not saved after player rejoins.
     
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    1. It is not called global variables. It is called class properties. Plugins should never declare global variables anyway.
    2. This is still not the best. YAML/SQLite3 are saved in the secondary memory (harddisk). Loading them into the primary memory (class properties) (a.k.a. caching the values) will be better for performance if you need to read the values frequently.
     
    HimbeersaftLP and Jack Noordhuis like this.
  16. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    he probably did the adding through PurePerm's API, therfore PP saves them.
     
  17. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    I thought config does cache the values? (Well, if you are abusing the Config class for data)
     
  18. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    You can always store global variables and objects by serialize and file_put_contents method
     
  19. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    ?! and what do you accomplish then. Using global vars has no point in plugin dev.
     
  20. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    $this->xxxx is class properties, not global variables!
     
    HimbeersaftLP and CaptainDuck 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.