Używanie gospodarki API

The Economy API unifies all economy plugins under one API. This means any plugin using the Economy API will be compatible with all economy plugins that implement said API. This page guides you through the steps of using the Economy API in your own plugin.

Ładowanie EconomyService

In order to utilize the Economy API, you must first load the EconomyService class using the ServiceManager.

Ostrzeżenie

Please note that you need to pay attention to different game states while the server is starting, stopping or running when using services like the Economy API. Take a look at the Usługi page for further information.

Przykład: Ładowanie EconomyService

import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.Sponge;

Optional<EconomyService> serviceOpt = Sponge.getServiceManager().provide(EconomyService.class);
if (!serviceOpt.isPresent()) {
    // handle there not being an economy implementation
}
EconomyService economyService = serviceOpt.get();

Ostrzeżenie

Keep this service in a local variable instead of a member variable, since the provider (implementation) could change at any point. If you need to place it in a member variable, for whatever reason, use ChangeServiceProviderEvent to keep the implementation updated.

Informacja

Unlike other services, you should try to use ServiceManager#provide(Class) instead of ServiceManager#provideUnchecked(Class) because Sponge does not provide a default implementation of the EconomyService, and therefore it is not guaranteed that it exists.

Używanie EconomyService

After loading the EconomyService and assigning it to a variable, you are ready to access all of the features the Economy API has to offer.

Przykład: Uzyskiwanie salda gracza

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.service.economy.account.UniqueAccount;
import java.math.BigDecimal;
import java.util.Optional;

Optional<UniqueAccount> uOpt = economyService.getOrCreateAccount(player.getUniqueId());
if (uOpt.isPresent()) {
    UniqueAccount acc = uOpt.get();
    BigDecimal balance = acc.getBalance(economyService.getDefaultCurrency());
}

Some Account methods require variables such as:

  • Waluta: Waluta uczestnicząca w wymianie

  • Przyczyna: Co spowodowało zmianę konta

  • Kontekst: Kontekst, w którym zaszła zmiana

These are for more advanced uses, but still must be filled in. Below is a list of acceptable default values: