I want to change my Nametag with a command. The command is done and works but if I relog, the nametag is default. How do I save the nametag so it is still the new one after a relog/restart?
Yes and how? Just a new File which saves nametags of players? And how to I get them out of the file then?
What? There are multiple types of databases. You can find the way that suits you best. If you don't mind having many files, you can use: PHP: $filename=$this->getDataFolder() . strtolower($player->getName()) . ".txt"; // the filename for the playerif(!is_file($filename)){ file_put_contents($filename, $player->getName()); $name=$player->getName();}else{ $name=file_get_contents($filename);} In here, it checks if the file exists. If it doesn't exist, it calls file_put_contents() to save the default name for the player by creating the file and saving the name into it directly. If it exists, it loads the name into $name. You cana use file_put_contents()and file_get_contents() to write/read a file.
PHP: $filename=$this->getDataFolder() . strtolower($player->getName()) . ".txt";if(!is_file($filename)){ file_put_contents($filename, $player->getNameTag()); $name=$player->getName();}else{ $name=file_get_contents($filename);} Is that correct? Do I have to put it in a PlayerPreLoginEvent? Also, how does the Server know that this is the Nametag and it has to set it?
Just make it so that if the command is ran and the file does exist, that it will remove the current saved nametag, and replace it with the new one.
No. I just told you how to save data. Example plugin: PHP: public function onEnable(){ if(!is_dir($this->getDataFolder() . "players")) mkdir($this->getDataFolder() . "players", 0777, true);}public function onLogin(PlayerJoinEvent $e){ $e->getPlayer()->setNametag($tag = $this->loadNametag($e->getPlayer())); $e->getPlayer()->setDisplayName($tag);}public function loadNametag(Player $player){ $file = $this->getDataFolder() . "players/" . strtolower($player->getName()) . ".txt"; if(!is_file($file)){ return $player->getName(); // no file created; no need to change name tag }else{ return file_get_contents($file); // returned the changed nametag }}public function onCommand(...){ ... /** @var string $nametag */ /** @var Player $player */ file_put_contents($this->getDataFolder() . "players/" . strtolower($player->getName()) . ".txt", $nametag); ...}