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

Solved Player Profiles

Comments in 'Plugin Development' started by HotFireyDeath, May 2, 2015.

  1. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    How would you be able to create player files (maybe in .yml or something) and be able to assign information to them and access these information?

    Take something like SimpleAuth for example, it creates player files for every player that logs in or registers.
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    My Example: Change NameTags

    First you have to create a Place where the Files can be saved:
    PHP:
    public function onEnable(){
                if(!
    file_exists($this->getDataFolder())){ //Creates the normal DataFolder(PluginName)
                    
    mkdir($this->getDataFolder());
                } 

            
    $this->getLogger()->info("ยง4Plugin has been enabled!");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            if(!
    is_dir($this->getDataFolder() . "players")) mkdir($this->getDataFolder() . "players"0777true); //Creates the folder players in the DataFolder
        
    }
    If a player joins, the plugin will, if there is one, set the Player's nametag to the one in his file:
    PHP:
    public function onLogin(PlayerJoinEvent $e){
            
    $e->getPlayer()->setNametag($tag $this->loadNametag($e->getPlayer())); //Sets the nametag to the one which gets returned at loadNametag
            
    $e->getPlayer()->setDisplayName($tag);
            }

    public function 
    loadNametag(Player $player){
                
    $file $this->getDataFolder() . "players/" strtolower($player->getName()) . ".txt"//The Player file
                
    if(!is_file($file)){
                    return 
    $player->getName(); // No player file -> No NameTag Change -> Returns the normal Playername
                
    }else{
                    return 
    file_get_contents($file); // Returns the nametag in the file
                
    }
            }
    Of course you somehow need to set the nametag:
    PHP:
    case "nt":
                    if(
    $sender->hasPermission("Test.nametag")){
                        if(isset(
    $args[0])){
                            if(
    $player $this->getServer()->getPlayer($args[0])){
                                
    $player $this->getServer()->getPlayer($args[0]);
                                
    $nametag "A Nametag \n> " $player->getName(); //You can also set $args[1] etc as nametag - \n adds a new line
                                
    file_put_contents($this->getDataFolder() . "players/" strtolower($player->getName()) . ".txt"$nametag); //Saves the new nametag in the playerfile, no need to save or remove anything before/after
                                
    $sender->sendMessage("You succesfully set " $player->getName() . "'s Nametag.");
                                
    $player->close(" > Nametag loaded!"); //The changed nametag is only visbile after a relog, so we kick the player after a change
                                
    return true;
                            } else{
                                
    $sender->sendMessage("Invalid Playername!\nUsage: /nt <name>");
                                return 
    true;
                            }
                        } else{
                            
    $sender->sendMessage("Invalid Playername!\nUsage: /nt <name>");
                            return 
    true;
                        }
                    } else{
                        
    $sender->sendMessage("You don't have the Permission to perform this Command!");
                        return 
    true;
                    }
    That's it! :) If you have any questions, ask here :)
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or use an SQLite3 database.
    HotFireyDeath likes this.
  4. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Like SimpleAuth, it just creates player files and stores information inside.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    SimpleAuth supports SQLite3 too.
    HotFireyDeath likes this.

Share This Page

Advertisement