API

API

Overview

The new API allows for developers to get feedback on whether or not transactions occurred. While you can still use the old methods of calling console commands, these methods will allow you to accomplish the same thing without having to go through console.

In your plugin.yml, make sure to add your dependency on PlayerPoints.
Either:

  • depend: [PlayerPoints]

Or:

  • softdepend: [PlayerPoints]

During enable, you need to grab the PlayerPoints plugin instance and save that reference somewhere as you will be using the API through it.

import org.black_ixx.playerpoints.PlayerPoints;

private PlayerPoints playerPoints;

/**
 * Validate that we have access to PlayerPoints
 *
 * @return True if we have PlayerPoints, else false.
 */
private boolean hookPlayerPoints() {
    final Plugin plugin = this.getServer().getPluginManager().getPlugin("PlayerPoints");
    playerPoints = PlayerPoints.class.cast(plugin);
    return playerPoints != null; 
}

/**
 * Accessor for other parts of your plugin to retrieve PlayerPoints.
 *
 * @return PlayerPoints plugin instance
 */
public PlayerPoints getPlayerPoints() {
    return playerPoints;
}

If its not enabled, you're going to have to handle that situation accordingly.

-If it is enabled, then utilizing the API is rather simple. You don't need to get an instance, or hold onto some object. All you have to do is call the method from static class.- The static access of the API had to be changed in order to service situations where multiple linked servers are referencing the same MySQL table.

Now, you access the API through the plugin instance that we saved earlier. All the methods of the API are the same as before.
Example:

int balance = playerPoints.getAPI().look("Player");

The above code will return the amount of points that the player named "Player" has.
Another example:

if(playerPoints.getAPI().take("Player", 100)) {
	//Success
} else {
	//Failed, probably not enough points
}

The above code attempts to take 100 points away from the player "Player". As mentioned before, you get feedback on the operation on whether or not it succeeded in performing the transaction.


JavaDocs

All the methods in the API have JavaDocs. Here is a copy of that reference.

  • give(String playername, int amount)
    • Gives points to specified player
    • Returns: true if we successfully adjusted points, else false
  • take(String playername, int amount)
    • Take points from specified player. If amount given is not already negative, it is done internally.
    • Returns: true if they had enough points to be removed, else false.
  • look(String playername)
    • Look up the current points of a player, if available. If the player is not in the config file, we default to 0.
    • Returns: integer of the amount of points the player has
  • pay(String sorce, String target, int amount)
    • Transfer points from one player to another. Attempts to take the points, if available, from the source player account and then attempts to give that amount to the target player account. If the give portion fails, the amount taken is returned to the source player account.
    • Returns: true if the transfer succeeded, else false.
  • set(String playername, int amount)
    • Sets a player's points to the given value. This overwrites whatever current amount they had before.
    • Returns true if successful, else false.
  • reset(String playername)
    • Removes the player from the config, effectively removing the points that the player had.
    • Returns true if successful, else false.

Comments

  • To post a comment, please or register a new account.
Posts Quoted:
Reply
Clear All Quotes