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

How to safe nametags?

Comments in 'Plugin Development' started by CraftYourBukkit, Mar 29, 2015.

  1. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    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?
    Last edited: Mar 29, 2015
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Do you mean how to save setNametag() with a plugin? Make a database.
    xFlare likes this.
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Yes and how? Just a new File which saves nametags of players? And how to I get them out of the file then?
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Wow, the only thing you can post is mistakes... Thanks for your awesome help
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What? o_O
    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 player
    if(!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.
    LDX and CraftYourBukkit like this.
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Thanks! And do I have to Change $player->getName() into $player->getNameTag()?
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    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?
  8. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    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.
  9. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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"0777true);
    }
    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);
      ...
    }
    Angelo Vidrio likes this.

Share This Page

Advertisement