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

[IMPROVEMENT]Use SQLite to improve your plugins!

Comments in 'Plugin Development' started by Humerus, Dec 9, 2013.

  1. Humerus
    Offline

    Humerus Staff Member Plugin Reviewer

    Joined:
    Aug 23, 2013
    Posts:
    114
    Plugins:
    1
    Now that many people have picked up PocketMine-MP plugin development, and PHP development in general, I would like to make a proposal. For those large applications that require large amounts of read and write, can we all please begin to use a database engine. For this objective, SQLite would be the perfect database to use. SQLite is precompiled with the same PHP binary used by all PocketMine servers. It is used by all servers, mainly because PocketMine uses SQLite. Here is an example plugin using SQLite. It is a simple VoteKick plugin that kicks a user after he/she has accumulated a certain amount of votes. Here is the code:

    PHP:
    <?php

    /*
    __PocketMine Plugin__
    name=VoteKick
    version=0.0.1
    author=humerusj
    apiversion=10
    class=vk
    */

    class vk implements Plugin {
        private 
    $api;
            public function 
    __construct(ServerAPI $api$server false) {
                
    $this->api $api;
            }
            public function 
    initialize() {
                
    $this->db = new SQLite3($this->api->plugin->configPath($this) . "vk.sq3");
                
    $this->db->exec("CREATE TABLE IF NOT EXISTS users (username TEXT, votes TEXT);");
            
    $this->db->exec("CREATE TABLE IF NOT EXISTS config (votestokick INT);");
            
    $this->db->exec("INSERT INTO config (votestokick) VALUES (5);");
            }
            public function 
    init() {
            
    $this->initialize();
                
    $this->api->console->register("vote-kick""<user> <reason>", array($this"commandHandler"), 1);
            
    $this->api->addHandler("player.join", array($this"eventHandler"), 1);
            
    $this->api->console->alias("vk""vote-kick");
            
    $this->api->ban->cmdwhitelist("vote-kick");
            }
            public function 
    __destruct() {
            }
            public function 
    commandHandler($cmd$params$issuer){
            
    $config $this->db->query("SELECT * FROM config");
            
    $config $config->fetchArray();
            switch(
    $cmd) {
                case 
    "vote-kick":
                    
    $player $this->api->player->get($params[0]);
                    if(!(
    $player instanceof Player)){
                        
    $issuer->sendChat("[VoteKick] Player does not exist!");
                        break;
                    }
                    
    $vkinfo $this->db->prepare("SELECT * FROM users WHERE username = :user");
                    
    $vkinfo->bindValue(":user"$player->username);
                    
    $vkinfo $vkinfo->execute();
                    
    $vkinfo $vkinfo->fetchArray();
                    if(
    strpos($vkinfo['votes'], $issuer->username) != false){
                        
    $issuer->sendChat("[VoteKick] You have already voted!");
                        break;
                    }elseif((
    count(explode(" "$vkinfo['votes'])) + 1) === $config[0]){
                        
    $player->close("You were vote kicked!");
                    }
                    
    $update $this->db->prepare("UPDATE users SET votes = :votes WHERE username = :user");
                    
    $update->bindValue(":votes", ($vkinfo['votes']." ".$issuer->username));
                    
    $update->bindValue(":user"$player->username);
                    
    $update->execute();
                                
    $vkinfo $this->db->prepare("SELECT * FROM users WHERE username = :user");
                                
    $vkinfo->bindValue(":user"$player->username);
                                
    $vkinfo $vkinfo->execute();
                                
    $vkinfo $vkinfo->fetchArray();
                    
    $this->api->chat->broadcast("[VoteKick] ".$issuer->username." has started a vote to kick");
                    
    $this->api->chat->broadcast("[VoteKick] ".$player->username.". ".($config[0] - (count(explode(" "$vkinfo['votes']))-1))." votes needed to kick user.");
                    break;
            }
            }
            public function 
    eventHandler($data$event){
                switch(
    $event) {
                        case 
    "player.join":
                                
    $clear $this->db->prepare("UPDATE users SET votes = :votes WHERE username = :username");
                                
    $clear->bindValue(":username"$data->username);
                                
    $clear->bindValue(":votes""");
                                
    $clear->execute();
                                
    $vkinfo $this->db->prepare("SELECT * FROM users WHERE username = :user");
                    
    $vkinfo->bindValue(":user"$data->username);
                                
    $vkinfo $vkinfo->execute();
                                if(!
    $vkinfo->fetchArray()){
                        
    console("User not found");
                        
    $insert $this->db->prepare("INSERT INTO users (username, votes) VALUES (:username, :votes)");
                        
    $insert->bindValue(":username"$data->username);
                        
    $insert->bindValue(":votes""");
                        
    $insert->execute();
                    }
                                break;
                }
            }
    }
    Take a look at this example plugin. Initializing SQLite is extremely simple, and everything can be entered into the database without needing too much work. SQLite is the perfect thing for this task since it is fast, lightweight and can be used in most, if not all, situations. If you are aspiring to become a greater developer, SQLite will also help you with dealing with database engines such as MySQL, which is used in most websites today.

    Please take a look at this proposal, and let me know what you think.

    It is for the sake of the community, to create better and more effective plugins.

    Thank you and have fun,

    Humerus
  2. Glitchmaster_PE
    Offline

    Glitchmaster_PE Notable Member Plugin Developer

    Joined:
    Aug 31, 2013
    Posts:
    834
    Plugins:
    8
    Minecraft User:
    Glitchmaster_PE
    Awesome! If I ever make a big plugin, I will use SQLite xD
  3. JassperBeastHD
    Offline

    JassperBeastHD Banned

    Joined:
    Aug 27, 2013
    Posts:
    436
    Minecraft User:
    JassperBeastHD
    your awesome, mine didnt work :(
  4. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    392
    Databases are not always as fast as you may think. If you want to use a database, then load the files into a database in the memory and store the database into the files when the server shuts down. Otherwise the database needs to read and write to its file every time you do a query.
    SOFe, Killman2, Tuff and 1 other person like this.
  5. ZacHack
    Offline

    ZacHack Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    326
    Plugins:
    6
    Minecraft User:
    _ZacHack_
    :) thank you i will put this in my plugins that require ally of data :D
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    How to store the database into the files?

    Sorry for bumping but... it turned out that this thread is the best place to ask, even better than starting a new thread.

Share This Page

Advertisement