API Usage
The API of EconomyShopGUI can be used by developers to integrate with your plugin.
Last updated
The API of EconomyShopGUI can be used by developers to integrate with your plugin.
Last updated
If any issues are found, you may submit a issue on the issue tracker on github.
The API has been added since version 3.9/1.12 of EconomyShopGUI, this means the server where the API is used needs to have the same or later version of EconomyShopGUI installed. As of now, the API is similar for the free and premium version, meaning that if the server running the API has EconomyShopGUI Premium installed that this will just work fine, the same for the free version. However, if you plan on using the API for servers with the free or premium version, you should add a softdepend to your plugin.yml for both versions.
Integrating with the API is very simple.
Just add the repository and dependancy to the pom.xml when using maven or the build.gradle when using gradle.
And don't forget to make EconomyShopGUI load before your plugin inside the plugin.yml just like so softdepend: [EconomyShopGUI, EconomyShopGUI-Premium]!
When integrated with the API, calling methods is as simple as:
To see an official example on how to use the API in a working plugin, you can see our AutoSellChests project at my Github here: https://github.com/Gypopo/AutoSellChests/blob/main/src/main/java/me/gypopo/autosellchests/scheduler/SellScheduler.java#L135
Since this API method only has been added since EconomyShopGUI v6.3 and EconomyShopGUI Premium v5.6, it is required to use any version above this or support manually checking for item prices.
This method automatically checks the following and returns an empty optional if not met:
ShopItem is found inside shop (EconomyShopGUIHook#getShopItem(ItemStack) != null)
ShopItem does not have an item error (ShopItem#hasItemError())
ShopItem is not a display item (ShopItem#isDisplayItem())
ShopItem is buyable (EconomyShopGUIHook#isBuyAble(ShopItem))
Amount of items is smaller then maximum buy limit (ShopItem#isMaxBuy(int))
If there is enough stock available (EconomyShopGUIHook#getItemStock(ShopItem, UUID))
Checks only when the player is online (OfflinePlayer#isOnline())
Player does have permissions for EconomyShopGUI.shop.<section>
Player does have permissions for this specific shop item EconomyShopGUI.shop.<section>.item.<permission>
Player does meet the requirements of this item
The above method returns a BuyPrice or SellPrice object if a shop item meets all requirements, which we will convert into the price of the item:
Before returning the price of the item, it is required to update any stock limits present on the item. This should only be done if the item(s) are bought/sold from/to shop(not when this method is used to only check the price of the items).
After that, we can safely return the requested price(s):
Since the premium version of the plugin supports multiple item prices per item, this method returns a Map with prices by default to ensure version compatibility.
If your code supports multiple prices, getting the Map of prices is as easy as:
Note that the player isn't a required argument, but using a player as argument automatically makes the API validate the following and return null if not met:
Wether this ShopItem is sell able (EconomyShopGUIHook#isSellAble(ShopItem))
Whether this ShopItem has a item error (ShopItem#hasItemError())
Whether this ShopItem is a display item (ShopItem#isDisplayItem())
Wether the player has permissions for this item (EconomyShopGUI.shop.<section>)
The premium version of the API will also check for per item permissions
Whether the player meets the shop item requirements (Premium only)
The following method can be used to get the price for the player:
Note that in EconomyShopGUI, items can have different economy types, and this method will always return the price of the current ShopItem's economy type.
So it is important to always check for the ShopItem's economy type(ShopItem#getEcoType()) before adding it to a player's balances.
Because for example, the returned amount could be experience points instead of Vault currency, and you do not want to mix them up when deposting the amount to the player's balance!
It is recommended to use the API methods to deposit/withdraw the ShopItem's EcoType from the player.
To completely support EconomyShopGUI's features:
Shop items may have item limits, meaning that it is required to check for any limits before purchasing/selling the item thru the API because this may not be allowed by the configuration of this shop item. It is also required to update the limits after.
Your code should check the following before purchasing items to completely support EconomyShopGUI's features:
And after purchasing/selling items your code should:
Note that in EconomyShopGUI Premium, items can have multiple prices per item. And getting the item's primary price like below, will create errors.
It is hardly recommended to retrieve an item's advanced pricing when using your API for public use!
This method will also calculate any discounts/sell multipliers, pricing multipliers and dynamic pricing for the player.
It is recommended to check for maximum transaction limits before purchasing/selling items thru the API, so your plugin respects any limits set on the shop item. Ignoring this might leave unexpected results when any limits are set.
These max transaction limits are a maximum amount the player can purchase at once, it is not to be confused with limited stock which is a premium only feature.
This method returns a boolean, if this returns false, it means that the amount given as argument is larger then the maximum transaction amount, so the item should not be bought/sold and the action should be cancelled.
Similar to max transaction limits, limited stock should be checked before the item can be bought/sold from the server, so your plugin respects any stock limits set on the shop item. Ignoring this might leave unexpected results when any limits are set.
Altough this is a premium only feature, calls made to the API of the free version of EconomyShopGUI will simply return 0 to ensure version compatibility.
ShopItems in EconomyShopGUI can have multiple prices per shop item, so it is required to also check for this before purchasing/selling any items to the server thru the API, so your plugin respects multiple prices set on the shop item. Ignoring this might leave unexpected results when any limits are set.
Altough this is a premium only feature, calls made to the API of the free version of EconomyShopGUI will simply return false to ensure version compatibility.
Since not all plugins support a map of prices inside their PricingProvider#getSellPrices(ItemStack)
method call, and since this is a map of different EcoType's.
It would be rather easy to get only the price of the supported economy type your plugin supports/uses. For example, plugins that rely on a Vault based price, calling something like below would do the trick of only getting the configured Vault price(if any);
Because shop items can have stock/limits or dynamic pricing set, it is required to also update these values after your plugin purchases/sells items from the shop:
Altough this is a premium only feature, calls made to the API of the free version of EconomyShopGUI will simply do nothing to ensure version compatibility.
Since there are multiple economy types for items, and we cannot expect every plugin to integrate with all of them. The API allows to interact using any registered economy providers, this can be any of the supported economy providers.
If you obtained the EconomyProvider object, you can do basic things like the following:
Get the player's balance
Deposit amounts
Withdraw amounts
The API contains two events as of now which can be used to track shop transactions.
The PreTransactionEvent is called before any shop transaction.
This allows you to cancel the event(transaction) or to modify the cost of the transaction by adding certain multipliers for example.
The PostTransactionEvent is called after any shop transaction. This allows any plugin to listern and verify that a shop transaction has come thru and check the result of the transaction.
For example this event is usefull on the DiscordSRV alerts.yml.
Both events contain information about what ShopItem(s) have been bought/sold, how much items in total, the total cost of the transaction, the TransactionType and the player triggering the event.
If your plugin requires to interact on server start with a ShopItem object, you should listern and wait for the ShopItemsLoadEvent since this event will be called when the ShopItem's become available and are pre-loaded.