1+ package fr .traqueur .currencies .providers ;
2+
3+ import fr .traqueur .currencies .CurrencyProvider ;
4+ import org .bukkit .Bukkit ;
5+ import org .bukkit .OfflinePlayer ;
6+ import org .bukkit .entity .Player ;
7+ import org .bukkit .plugin .RegisteredServiceProvider ;
8+ import su .nightexpress .excellenteconomy .api .ExcellentEconomyAPI ;
9+ import su .nightexpress .excellenteconomy .api .currency .operation .OperationContext ;
10+
11+ import java .math .BigDecimal ;
12+
13+ public class ExcellentEconomyProvider implements CurrencyProvider {
14+ private final ExcellentEconomyAPI api ;
15+ private final String currencyName ;
16+
17+ public ExcellentEconomyProvider (String currencyName ) {
18+ this .currencyName = currencyName ;
19+ RegisteredServiceProvider <ExcellentEconomyAPI > provider = Bukkit .getServer ().getServicesManager ().getRegistration (ExcellentEconomyAPI .class );
20+ if (provider == null ) {
21+ throw new IllegalStateException ("ExcellentEconomy service not registered" );
22+ }
23+ this .api = provider .getProvider ();
24+ }
25+
26+ @ Override
27+ public void deposit (OfflinePlayer player , BigDecimal amount , String reason ) {
28+ OperationContext ctx = OperationContext .custom (reason );
29+ if (isOnline (player )) {
30+ this .api .deposit (asOnline (player ), this .currencyName , amount .doubleValue (), ctx );
31+ } else {
32+ this .api .depositAsync (player .getUniqueId (), this .currencyName , amount .doubleValue (), ctx );
33+ }
34+ }
35+
36+ @ Override
37+ public void withdraw (OfflinePlayer player , BigDecimal amount , String reason ) {
38+ OperationContext ctx = OperationContext .custom (reason );
39+ if (isOnline (player )) {
40+ this .api .withdraw (asOnline (player ), this .currencyName , amount .doubleValue (), ctx );
41+ } else {
42+ this .api .withdrawAsync (player .getUniqueId (), this .currencyName , amount .doubleValue (), ctx );
43+ }
44+ }
45+
46+ @ Override
47+ public BigDecimal getBalance (OfflinePlayer player ) {
48+ double raw = isOnline (player )
49+ ? this .api .getBalance (asOnline (player ), this .currencyName )
50+ : this .api .getBalanceAsync (player .getUniqueId (), this .currencyName ).join ();
51+ return BigDecimal .valueOf (raw );
52+ }
53+
54+ private boolean isOnline (OfflinePlayer player ) {
55+ return player instanceof Player ;
56+ }
57+
58+ private Player asOnline (OfflinePlayer player ) {
59+ return (Player ) player ;
60+ }
61+ }
0 commit comments