how can I make $sender of commandSender available for any events? So that I can do $sender->getName() in JoinEvent
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
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.
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
You already have the $player variable. Why do you have to call it $sender? Please, learn about variables; it's very simple to understand.
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);} Spoiler: what this will do 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"
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.
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
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!
Well, I just used global variables class properties (sorry SOFe) because the thread is about that ¯\_(ツ)_/¯
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.