Skip to content
Shared Services

Shared Services

Money

Since 2.1, one way to move money, whichever plugin holds it. A price names its currency by id: an XCore currency (dollar, gems), or vault (or an empty id) for the economy registered with Vault, which is XCore's own on most servers and another plugin's on the others. A plugin that sells things asks money() and does not care which.

String currency = money().defaultCurrency();          // XCore's primary, else Vault's
double price = money().ceil(currency, raw);           // what a player pays, rounded up
double payout = money().floor(currency, raw);         // what a player receives, rounded down
money().withdraw(uuid, currency, price).thenAccept(paid -> {
    if (!paid) return;                                // refused: nothing was taken
    // hand the item over
});
money().deposit(uuid, currency, payout);
money().format(currency, price);                      // "$1,234.50"
money().room(uuid, currency);                         // what still fits under max-balance
  • A withdrawal says whether it happened. withdraw completes with true only when the whole amount left the balance: hand nothing over before it has. deposit completes with true when the payment went through. An amount of zero or less is nothing to move, and answers true.
  • Rounding never creates money. Bring an amount to what the currency counts before it moves: ceil for what a player pays, floor for what they receive. decimals and unit say what it counts: an XCore currency's own decimals, two for another plugin's economy.
  • Room before items. A deposit that does not fit under max-balance is trimmed, so a plugin that takes items for money checks room first, or the items would be sold for nothing.
  • Another plugin's economy is only called on the server thread, since most of them are not thread-safe: withdraw and deposit go there by themselves, and balance asked from another thread answers 0. When the economy behind Vault is XCore's own, the balance is reached directly, off that thread.
  • One balance, one id. canonical(id) gives the id of the balance a currency id moves. vault and XCore's primary currency are one balance when XCore's economy is behind Vault, so a plugin comparing prices across products groups them by it; otherwise "vault" and "dollar" pass for two currencies.
  • Also there: exists(id); isCore(id), whether a balance is XCore's own, reachable offline and from any thread; currencies(), XCore's, then vault when another plugin's economy is registered; has, balance and symbol; and round, for showing an amount, never for one about to move.

Deliveries

The mailbox: things a plugin owes a player but could not hand over. Written once, given on their next join.

delivery().sendItem(uuid, item, "Auction sale", "XAuctionHouse");
delivery().sendMoney(uuid, "coins", 250.0, "Vote reward", "XVote");
delivery().countPending(uuid).thenAccept(count -> ...);

Money always goes through. An item is only closed once it is really in the inventory, so a full inventory postpones it rather than losing it.

Leaderboards

Declared once, refreshed on a timer, read from memory. Reading one never touches the database, which matters when a scoreboard asks for it several times a second.

leaderboards().define("kills")
    .table("myaddon_stats").value("kills")
    .size(10).refreshEvery(300)
    .register();

leaderboards().get("kills").top(10);   // the last snapshot

That also publishes %xcore_top_kills_1_name% and %xcore_top_kills_1_value%.

Network

Every server announces itself on the sync channel, so a plugin can know what the network looks like instead of guessing.

network().servers();       // name, players, TPS, version
network().locate(uuid);    // which server holds this player
network().totalOnline();   // across the network
network().send("lobby", "mychannel", new SyncMessage("KICK", uuid.toString()));

Discord

One queue for the whole installation, with retries and rate-limit handling. Each plugin keeps its own webhook address in its own config.

discord().send(url, DiscordNotifier.embed()
        .title("Ban")
        .description(player + " was banned by " + staff)
        .color(0xC0362C)
        .field("Reason", reason, false)
        .timestamp()
        .build());

discord().sendRaw(url, json) takes a payload a plugin has already assembled.

Toolbox

ClassPurpose
ItemsItems to base64 and back, giving without losing the overflow, counting, taking
CooldownsPer-player cooldowns that expire on their own
NotifyTitles, action bars and boss bars
FormatsDurations both ways, thousands separators, compact numbers, byte sizes
CommandHelpersSuggestions, offline target resolution, the silent flag
WorldConfigPer-world overrides, resolved once at load
BlinkCacheBoth faces of a blinking item, built once
ranks()Primary group, weight, numbered permission levels

Placeholders without an expansion

placeholders()
    .register("kits", (player, arg) -> String.valueOf(available(player)))
    .register("cooldown", (player, arg) -> Formats.duration(remaining(player, arg)));
placeholders().publish();

The identifier is the plugin name in lower case, so those become %mykits_kits% and %mykits_cooldown_starter%.

Play Time

XCore counts how long each player has been connected, across sessions, in the playtime column of the shared table.

long seconds = api().getPlaytime(uuid);

The session in progress is included, so the value moves while a player is online rather than only when they disconnect. %xcore_playtime% gives it formatted.