Skip to content
This repository was archived by the owner on May 11, 2020. It is now read-only.

PHP API

Dandielo edited this page Aug 14, 2013 · 5 revisions

Basics

The basics of this API are not hard to be honest, you can totally skip this part of your plugin, because all things can be handled by the following basic API methods:

//connect to the dtlStatsPlugin
Stat::connect("ip/name.com", port);
//send the password for verification
Stat::pass("password");

//check if the connection is valid
Stat::connected()

//get or updates stats
Stat::get("stat/path");
Stat::update("stat/path", "argument");

//close the connection
Stat::disconnect();

So you can just make the Java Side plugin and let the user use the default API that consists of all these methods.

But if you want to make your own PHP API here are some methods, classes and ideas how you can do it :)

Classes

A good idea is to group your data into classes and return specific statistics with methods, so the server admin does not need to know the stat path to get it's value. For example if you would like to show on the website the number of all worlds you got on your server, then with default API it would look like this:

echo Stat::get("worlds/count");

Ok, it's not that bad but isn't the following example much better?

echo Worlds::count();

The player knows it's about his worlds on his server and he is counting them. easy, yup?


How we can do this?

You could easy make the following thing: create a php file, like "YourPluginStats.php" and put into it the following code:

<?php
//include/load the default API library
include("");

//create a new class that is like a group of information of all server worlds :)
class Worlds 
{
    //one specific information
    public static function count()
    {
        return Stat::get("worlds/count");
    }
}

now you can use the following thing :)

echo Worlds::count();

Methods

If you want, you don't need to wrap all those things into a class you can also do simple methods :)

function worldsCount()
{
    return Stat::get("worlds/count");
}

and that method you could use like this:

echo worldsCount();

StatClass type

The StatClass class is a special class created to ease the loading of data from JSonRequests and simplify class creating. All magic is done behind and you don't need to know the whole process. I will just tell you the basics and guide you a bit :)

by default you just need to create your own class and extend it with this class so all looks like this:

class MyClass extends StatClass 
{ 
    public $plugin = "YourPlugin";
}

The $plugin class field is used to load the data from the specific plugin, if not set it will look for listeners registered under the dtlStats plugin name.

Thats it, now if you would call any method if will automatically try to load the a stat with the name of your class, so for this example it would load this:

Stat::get("MyClass"); 

if the loading was successful and the loaded data created the requested method you will get the result. Loading is done only once, so it wont affect your server much :)

If somehow the loaded data does not returns us the method we wanted to use a second request is send this time with a stat path created by the class name and method name, for example:

MyClass::secondLoading();

Lets say the first loading didn't loaded the method above, so the API sends a new request that looks like this:

Stat::get("MyClass/secondLoading");

It's a more precise loading request, if it also fails the method returns an empty string.

Custom stat requests

But this would be quite idiotic that these classes could only load paths with 2 elements. So lets force them to load specific paths, this is easily done :) Consider the following code:

class MyClass extends StatClass 
{
    public $plugin = "YourPlugin";
    public $stat = "custom/stat/path";
}

Now it will try the first load with the $stat you set, and the second load with with the $stat and method name, so the second load would look like this: custom/stat/path/secondLoad

Advanced methods

... later

Clone this wiki locally