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

Variables management.

Comments in 'Plugin Development' started by xFlare, Nov 23, 2014.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Lets say I wanted to create a plugin that rewards players for building.

    Lets say if they place more then 50 blocks a line of code gets executed to reward them.

    So to track how many blocks he place we obviously need to use a variable, So here comes my question.

    Let say $Block will go up by one when a block is placed. Will $Blocks be for everyone on the server? Or will $Blocks be a personal for everyone?

    If you don't understand my question: Player1 places a block. Player2 places 5 blocks. Will $Blocks be 6 or will $Blocks be personal to each player. EX: Player1: $Blocks = 1 & Player2: $Blocks = 5?

    Thanks,
    Last edited by a moderator: Nov 23, 2014
  2. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    BlockPlaceEvent and BlockBreakEvent would do.
    just record every player place or breaks, he'll earn points and save it.
    Last edited: Nov 23, 2014
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You need to save the values in a database so that they are saved when the server stops/the plugin is disabled. Since you are doing statistics, I recommend using an SQLite3 database to save data.
    Example code for doing that:
    PHP:
    class Blah extends PluginBase implements Listener{
        private 
    $db// this will be the object that represents the SQLite3 database. To work with this variable/field, you need to use $this->db instead of $db.
        
    public function onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this); // register this Listener to handle events
            
    @mkdir($this->getDataFolder()); // create the folder (PocketMine-MP/plugins/YourPluginName) for saving plugin data
            
    $this->db = new \SQLite3($this->getDataFolder() . "blocks.sq3"); // load the database into $this->db. If the database is not created yet, it will automatically create a new one
            
    $this->db->exec("CREATE TABLE IF NOT EXISTS places (player TEXT PRIMARY KEY COLLATE NOCASE, count INTEGER);"); // if the table for saving number of block places is not created, create it.
            // This table has two columns: player is the column for saving player name, so its data type is text. It is the primary key, which means there won't be two rows with the same "player" value. Collate nocase makes it case-insensitive.
            // count is the number you want to save, so it is an integer.
        
    }
        public function 
    onBlockPlace(BlockPlaceEvent $event){
            
    $stmt $this->db->prepare("SELECT count FROM places WHERE player = :name;"); // find the value of `count` where the value of `player` is :name. :name is a SQLite3 statement variable whose value will be injected below
            
    $stmt->bindValue(":name"$event->getPlayer()->getName()); // set the value of :name to player name
            
    $result $stmt->execute(); // execute the statement
            
    $result $result->fetchArray(SQLITE3_ASSOC); // fetch the result array
            
    if(is_array($result)){ // such row exists
                
    $count $result["count"]; // player had already placed $count blocks
            
    }
            else{
                
    $count 0// player hadn't placed any blocks yet
            
    }
            
    $count++;
            
    $stmt $this->db->prepare("INSERT OR REPLACE INTO places (player, count) VALUES (:name, :count);"); // update the database
            
    $stmt->bindValue(":name"$event->getPlayer()->getName());
            
    $stmt->bindValue(":count"$count);
            
    $stmt->execute(); // execute the statement
            
    if($count 50){
                
    // do something
             
    }
        }
        public function 
    onDisable(){
            
    $this->db->close(); // close the database
        
    }
    }
    Are there such events?
    LDX, iksaku, xFlare and 1 other person like this.
  4. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    lmfao, i looked like drunk and never noticed it xD
    i meant the things u have mentioned above xX
    LDX likes this.
  5. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I understand 50% of that code. Good thing you put the notes otherwise I would be lost :D Do you have somewhere I can learn more about sqlite3 databases? ;)
  6. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    You can also store it on a YAML config file:
    PHP:
    public function onEsnable(){
         
    $this->config = new Config($this->getDataFolder() . "Blocks.yml"CONFIG::YAML);
    }

    public function 
    onBlockPlace(BlockPlaceEvent $event){
         
    player $event->getPlayer()->getName();
         
    $count $this->config->get($player);
         if(
    $count === false){
              
    $this->config->set($player1);
         }else{
              
    $this->config->set($player, ($count 1);
         }
    }

    public function 
    onBlockBreak(BlockBreakEvent $event){
         
    player $event->getPlayer()->getName();
         
    $count $this->config->get($player);
         if(
    $count 0){
              
    $this->config->set($player, ($count 1);
         }else{
              
    $this->config->set($player0);
         }
    }
    Hope this will help you :3
    iJoshuaHD likes this.
  7. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Much easier xD Thanks,
    LDX likes this.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Server owners sometimes don't like so many files created on their plugin directories, so SQLite3 is a better choice.
    http://php.net/manual/en/book.sqlite3.php
    http://sqlite.org/lang.html
    iJoshuaHD likes this.

Share This Page

Advertisement