[PHP] Various functions I made for my Minecraft server.

Discussion in 'Bukkit Tools' started by Magestickown, Feb 8, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Magestickown

    Feel free to use these (give me credit atleast :D).

    - iConomy 6 functions
    - Minecraft sockets
    - PermissionsEx functions

    Code:
        class xxorpheusfunc {
    // change these
            public $user = "root"; //sql username (may need to change this)
            public $password = "your sql password"; //sql password
            public $server = "localhost"; //sql server (may need to change this) 
            public $database = "database name"; //database
            public $root = "/"; //root dir with following slash (you may need to change this)
            public $iconomydb = "iconomy"; //may need to change this
           
            function sanitize($in) {
                $mysqli = $this->connect();
                $in = $mysqli->real_escape_string(strip_tags(htmlentities($in)));
                $mysqli->kill($mysqli->thread_id);
                $mysqli->close();
                return $in;
            }
     
            function connect() {
                $mysqli = new mysqli($this->server, $this->user, $this->password, $this->database) or die("ERROR: <a target='_blank' href='http://www.google.ca/#sclient=psy-ab&hl=en&source=hp&q=" . urlencode($state->error) . "&pbx=1&oq=" . urlencode($state->error) . "'>" . $state->error . "</a>");
                return $mysqli;
            }
           
     
            function giveMoney($username, $amount) {
                $mysqli = $this->connect();
                $mysqli->select_db("shadecraft");
                $username = $this->sanitize($username);
                $amount = (double)$amount;
                $sql = "SELECT * FROM ".$this->iconomydb." WHERE username='$username'";
                $res = $mysqli->query($sql) or die($mysqli->error);
                
                $row = $res->fetch_object();
                $new_balance = $row->balance + $amount;
                
                $sql = "UPDATE ".$this->iconomydb." SET balance='$new_balance' WHERE username='$username'";
                $res = $mysqli->query($sql);
                if($res) {
                    $ret = "Balance has updated by $amount dollars (".$row->balance." + $amount)";
                } else {
                    Throw new Exception("Error while updating balance for player $username");
                }
                
                return $ret;
            }
            
            function balance($username) {
                $mysqli = $this->connect();
                $mysqli->select_db("shadecraft");
                $username = $this->sanitize($username);
                
                $sql = "SELECT balance FROM ".$this->iconomydb." WHERE username='$username'";
                $res = $mysqli->query($sql);
                $row = $res->fetch_object();
                
                return $row->balance;
            }
            
            function getStaff() {
                $staff = array("owner", "admin", "supermod", "mod");
                $path = $_SERVER['DOCUMENT_ROOT'] . $root . "/spyc.php";
                require_once($path);
                $permissions = file_get_contents($path);    
                $users = Spyc::YAMLLoad($this->pexpath);
                
                $staffl = null;
                while($user = current($users['users'])) {
                    $rank = $users['users'][key($users['users'])][0];
                    if(in_array($rank, $staff)) {
                        $staffl .= "<tr><td>" . key($users['users']) . "<td>$rank</td></tr>\n";
                    }
                    next($users['users']);
                }
                echo "<table style='width: 100%; text-align: left; margin-top: 0px; padding-top: 0px'>";
                echo "<tr><td>Player name</td> <td>Rank</td></tr>";
                echo $staffl;
                echo "</table>";
            }
            
            function getPermissions($rank) { //this requires spyc class, for YAML parsing
                $path = $_SERVER['DOCUMENT_ROOT'] . $root . "/spyc.php"; //put spyc.php in your root directory 
                require_once($path);
                
                $permissions = file_get_contents($this->pexpath);
                $users = Spyc::YAMLLoad($permissions);
                
                $permissions = $users['groups'][$rank];
                
                return $permissions;    
            }            
            
            function getRank($username) {
                $path = $_SERVER['DOCUMENT_ROOT'] . "/vontracraft/common/spyc-0.5/spyc.php";
                require_once($path);
                
                $permissions = file_get_contents("C:\Users\Administrator\Desktop\BOOKEET\plugins\PermissionsEx\permissions.yml");
                $users = Spyc::YAMLLoad($permissions);
                
                $rank = $users['users'][$username][0];
                
                return $rank;
            }
    
    If there's any errors, just tell me. I just quickly revised this for your use in this post haha, didn't both to test :eek:

    How-to:
    Code:
    <?php
    require("theclassfile.php"); //whatever you saved the class file as.
    $ign = "Your ingame name";
    $amount = 500; //Change this if you want
    $xxorpheusfunc = new xxorpheusfunc();
    $balance = $xxorpheusfunc->balance($ign);
    $rank = $xxorpheusfunc->getRank($ign);
    $givemoney = $xxorpheusfunc->giveMoney($ign, $amount);
    ?>
    
    Any other questions, feel free to ask.

    Note, you _MUST_ have SPYC class (for YAML parsing, if you wish to use those functions.)
    http://code.google.com/p/spyc/
     
  2. Offline

    ssttevee

    This is very useful. Thanks.

    You may want to change the class name from xxorpheusfunc to something like bukkitFuncs or something simpler... I know thats simple, but people may be lazy.....

    Also, I suggest trying a plugin called websend for executing console commands remotely with php
     
  3. Offline

    Magestickown

    THAT'S WHAT IT'S CALLED! Thanks :D

    Haha, they can rename it if they REALLY want to :p

    Here's another quick one, which requires Justin Martin's NBT reader class, which you can get here: https://github.com/TheFrozenFire/PHP-NBT-Decoder-Encoder/blob/master/nbt.class.php

    It's a little wonky, but it gets the job done.
    Code:
    <?php
        require("class.nbt.php");
       
        $nbt = new nbt();
        $nbt->loadFile("CHANGE THIS TO YOUR PLAYER.DAT FILE");
        $user = $nbt->root[0];
        $inventory = $user['value'][7];
        $health = $user['value'][5];
       
        $hp = $health['value'] / 2;
        echo round($hp, 0, PHP_ROUND_HALF_DOWN)."<br />";
        for($i = 0; $i <= $hp; $i++) {
            if(strpos($hp, ".") !== false && $i == round($hp, 0, PHP_ROUND_HALF_DOWN)) {
                echo "<img src='http://www.minecraftwiki.net/images/thumb/4/47/Half_Heart.svg/18px-Half_Heart.svg.png' />";
            } else {
                echo "<img src='http://www.minecraftwiki.net/images/thumb/a/a7/Heart.svg/18px-Heart.svg.png' />";
            }
               
        }
    EDIT:
    Here's an example of it working:
    http://i.imgur.com/LGeQP.png

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
Thread Status:
Not open for further replies.

Share This Page