Developer API
Add It to Your Build
The API lets another plugin read XShop's prices and trade through it: a spawner plugin that pays what the shop pays, a sell wand of your own, a quest that buys for a player.
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
compileOnly 'com.github.Xyness:XShop-API:1.0.0'
}
With Maven, the repository is https://jitpack.io, the artifact com.github.Xyness:XShop-API:1.0.0, in the provided scope. XShop is a Paper plugin, so a plugin that uses it declares it to see its classes: in a paper-plugin.yml, under dependencies.server, with join-classpath: true.
dependencies:
server:
XShop:
load: BEFORE
required: false
join-classpath: true
With a plugin.yml, depend: [XShop] does the same. An XCore addon needs no declaration.
compileOnly, never shaded: the classes come from XShop's own jar at runtime. Shading them gives you a different class with the same name, and a listener registered on it never fires.
Read a Price
if (XShopProvider.isRegistered()) {
XShopAPI shop = XShopProvider.get();
OptionalDouble worth = shop.getSellPrice(player, item, item.getAmount());
worth.ifPresent(price -> player.sendMessage("Worth " + price));
}
| Method | Returns |
|---|---|
getShops() | The ids of the shops of this server |
getProducts(shop) / getProduct(shop, product) | Products, as ShopProduct: the material, one unit of what is handed over (null for commands), the lot, the base prices of a lot (negative for none), the currency, and whether the price is dynamic |
findSellProduct(player, item) | The product that buys an item back at the best price for that player |
getBuyPrice(player, item, amount) | What the player would pay, at the cheapest product that sells the item |
getSellPrice(player, item, amount) | What the player would be paid, at the best product that buys it. A used item is priced as a sale would price it: refused, at its share or at full price, as selling.damaged-items says |
getBuyPrice(player, shop, product, amount) / getSellPrice(...) | The same for one product |
They answer from memory and may be called from any thread. An empty answer means no shop sells, or buys, that item to that player. Prices depend on who asks, so they take the player: with null, what belongs to a player (price groups, boosters) is left out, while the sales running for everyone, a rotating shop's discount and where a dynamic price stands still count. A renamed or enchanted item only matches a product that names it exactly.
Trade for a Player
XShopProvider.get().buy(player, "blocks", "stone", 64).thenAccept(result -> {
if (result.success()) {
// result.units(), result.total(), result.currency()
}
});
XShopProvider.get().sellAll(player, chest.getInventory(), 1.5); // what a wand would do
Both run exactly as a click in the menu: the player's limits and bonuses apply, the money is theirs, and the trade is logged, with API as its source. Call them from the thread that owns the player, or the inventory for sellAll; the future completes off the server threads. buy takes the price as it stands at that moment. sellAll sells what /sell all would, with the multiplier on top of the player's bonuses, and gives the total of the first currency when several were paid.
| TradeResult.Status | Meaning |
|---|---|
SUCCESS | It went through, for units() units |
NOTHING | Nothing to trade: no such product, nothing the shop buys in the inventory |
NOT_ENOUGH_MONEY | The player cannot pay |
NO_ROOM | No room in the inventory, or a balance at its cap |
OUT_OF_STOCK | Not enough in stock, or a stock fed by players that is full |
LIMIT_REACHED | The player's limit for the period does not allow it |
PRICE_MOVED | The price changed between the quote and the trade |
DENIED | The player may not trade it: permission, world, rank, play time |
BUSY | The player already has a trade under way |
CANCELLED | A plugin cancelled ShopPreTransactionEvent |
FAILED | The trade could not be completed; nothing was taken |
Listen to Trades
@EventHandler
public void onTrade(ShopPreTransactionEvent event) {
if (event.getType() == TradeType.SELL && inCombat(event.getPlayer())) {
event.setCancelled(true);
}
}
@EventHandler
public void onDone(ShopTransactionEvent event) {
for (TradeLine line : event.getLines()) {
quests.progress(event.getPlayerId(), line.product().material(), line.units());
}
}
| Class | Description |
|---|---|
ShopPreTransactionEvent | Before a purchase or a sale, when nothing has been taken yet, on the player's thread. Cancel it to stop the trade. Its totals are estimates, worked out at the click |
ShopTransactionEvent | Once the money has moved and the trade is logged, with the exact amounts. Asynchronous: the player may have left by then, so it carries their UUID and name |
TradeLine | One product of a trade: the product, the units, the total. A /sell all is one event with a line per product |
TradeType | BUY or SELL, seen from the player |
The API is free to use in any plugin, commercial ones included. Sources: github.com/Xyness/XShop-API.