Skip to content

Using The API

Snow-Developer edited this page Aug 19, 2021 · 6 revisions

This page shows how to use the StatsSBAPI.

Overview


First off, You need the premium version to use the API.

Eclipse

  1. Rightclick your project and choose Properties -> Java Build Path.
  2. Click on Add external jar and add the StatsSB.jar to your project.

IntelliJ

  1. Click on File -> Project Structure (Ctrl + Alt + Shift + S on windows)
  2. Go to the tab Modules
  3. Click the + on the right side and choose Library... -> Java
  4. Select the StatsSB.jar

Set StatsSB as depend or softdepend.

Next you need to go to your plugin.yml and specifiy if your plugin depends on or softdepends on StatsSB.

Example:

name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.here

softdepend: [StatsSB] # This is used, if your plugin works without StatsSB. Use "depend" if it needs StatsSB to work.

Check if StatsSB is loaded.

Next up you need to make sure the plugin is on the server before using the API.

This is an example for if your plugin depends on StatsSB. Example:

    @Override
    public void onEnable() {

        if (Bukkit.getPluginManager().getPlugin("StatsSB") != null) {
            // StatsSB is on the server! Now you can do stuff with it!
        } else {
            throw new RuntimeException("Could not find StatsSB!! Plugin can not work without it!");
        }
    }

Otherwise you can set a boolean if its enabled. And before you use the API just check the boolean.

Using the API

There are a few simple methods to get the players stats.

    public int getStatsSBKills(Player player) {
        return StatsSBAPI.getKills(player);
    }
    public int getStatsSBDeaths(Player player) {
        return StatsSBAPI.getDeaths(player);
    }
    public int getStatsSBStreak(Player player) {
        return StatsSBAPI.getStreak(player);
    } 

I also added the Raw PlayerData Object that you can use to get a few extra stats.

    public int getStatsSBPlayerRank(Player player) {
        return StatsSBAPI.getPlayerData(player).getKillsRank();
    }
    // Example use of the PlayerData object
    public int getMobKillsAndPlayerKills(Player player) {
        SBPlayer sbPlayer = StatsSBAPI.getPlayerData(player);
        return sbPlayer.getAllMobKills() + sbPlayer.getKills();
    }

There is now also a way to "Simulate" a Kill/Death NOTICE: If you use "simulateKill()" IT WILL ALSO SIMULATE A DEATH FOR THE VICTIM

    public int addStatsSBKill(Player victim, Player attacker) {
        return StatsSBAPI.simulateKill(victim, attacker);
    }
    public int addStatsSBDeath(Player victim) {
        return StatsSBAPI.simulateDeath(victim);
    }

There is also some Events you can use.

    @EventHandler
    public void onEnterCombat(PlayerEnteredCombatEvent event) {
        // Do stuff here
    }
    @EventHandler
    public void onLeaveCombat(PlayerLeftCombatEvent event) {
        // Do stuff here
    }

API Use Example:

package me.extremesnow.joinexample;

import me.extremesnow.statssb.api.StatsSBAPI;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class JoinExample extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {

        if (Bukkit.getPluginManager().getPlugin("StatsSB") != null) {
            /*
             * We register the EventListeners here, when StatsSB is installed.
             * Since all events are in the main class (this class), we simply use "this"
             */
            Bukkit.getPluginManager().registerEvents(this, this);
        } else {
            throw new RuntimeException("Could not find StatsSB!! Plugin can not work without it!");
        }
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent event) {
        int kills = StatsSBAPI.getKills(event.getPlayer());
        int deaths = StatsSBAPI.getDeaths(event.getPlayer());
        event.getPlayer().sendMessage("You have " + kills + " kills and " + deaths + " deaths!");
    }
}
Clone this wiki locally