Skip to content

Commit 91e4172

Browse files
committed
Added CurrencyHolder interface
Introduced `CurrencyHolder` to centralize currency management logic. Applied the interface to `EconomyController`, `BankController`, and related classes to unify and enhance functionality. Deprecated older currency formatting methods in favor of `Currency` methods.
1 parent 7862985 commit 91e4172

File tree

5 files changed

+138
-33
lines changed

5 files changed

+138
-33
lines changed

plugin/src/main/java/net/thenextlvl/service/wrapper/service/model/WrappedAccount.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,25 @@
1313

1414
@NullMarked
1515
public class WrappedAccount implements Account {
16+
private final EconomyController controller;
1617
private final @Nullable World world;
1718
private final Economy economy;
1819
private final OfflinePlayer holder;
1920

20-
public WrappedAccount(@Nullable World world, Economy economy, OfflinePlayer holder) {
21+
public WrappedAccount(EconomyController controller, @Nullable World world, Economy economy, OfflinePlayer holder) {
22+
this.controller = controller;
2123
this.world = world;
2224
this.economy = economy;
2325
this.holder = holder;
2426
}
2527

2628
@Override
2729
public BigDecimal deposit(Number amount) {
30+
public CurrencyHolder getController() {
31+
return controller;
32+
}
33+
34+
@Override
2835
var response = economy.depositPlayer(holder, world != null ? world.getName() : null, amount.doubleValue());
2936
return new BigDecimal(response.balance);
3037
}

src/main/java/net/thenextlvl/service/api/economy/Account.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.thenextlvl.service.api.economy;
22

3+
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
34
import org.bukkit.World;
45
import org.jspecify.annotations.NullMarked;
56

@@ -12,6 +13,13 @@
1213
*/
1314
@NullMarked
1415
public interface Account extends Comparable<Account> {
16+
/**
17+
* Retrieves the associated {@code CurrencyHolder} for the account.
18+
*
19+
* @return the {@code CurrencyHolder} capable of managing currencies for the account
20+
*/
21+
CurrencyHolder getController();
22+
1523
/**
1624
* Deposits the specified amount into the account balance.
1725
*

src/main/java/net/thenextlvl/service/api/economy/EconomyController.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.thenextlvl.service.api.economy;
22

33
import net.thenextlvl.service.api.Controller;
4+
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
45
import org.bukkit.OfflinePlayer;
56
import org.bukkit.World;
67
import org.jetbrains.annotations.Unmodifiable;
@@ -16,22 +17,7 @@
1617
* The AccountController interface provides methods to create, retrieve and delete accounts.
1718
*/
1819
@NullMarked
19-
public interface EconomyController extends Controller {
20-
/**
21-
* Formats the specified amount as a string.
22-
*
23-
* @param amount the number amount to be formatted
24-
* @return the formatted amount as a string
25-
*/
26-
String format(Number amount);
27-
28-
/**
29-
* Retrieves the number of fractional digits used for formatting currency amounts.
30-
*
31-
* @return the number of fractional digits used for formatting currency amounts
32-
*/
33-
int fractionalDigits();
34-
20+
public interface EconomyController extends Controller, CurrencyHolder {
3521
/**
3622
* Retrieves the plural form of the currency name based on the provided locale.
3723
*

src/main/java/net/thenextlvl/service/api/economy/bank/BankController.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.thenextlvl.service.api.economy.bank;
22

33
import net.thenextlvl.service.api.Controller;
4+
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
45
import org.bukkit.OfflinePlayer;
56
import org.bukkit.World;
67
import org.jetbrains.annotations.Unmodifiable;
@@ -12,22 +13,7 @@
1213
import java.util.concurrent.CompletableFuture;
1314

1415
@NullMarked
15-
public interface BankController extends Controller {
16-
/**
17-
* Formats the specified amount as a string.
18-
*
19-
* @param amount the number amount to be formatted
20-
* @return the formatted amount as a string
21-
*/
22-
String format(Number amount);
23-
24-
/**
25-
* Retrieves the number of fractional digits used for formatting currency amounts.
26-
*
27-
* @return the number of fractional digits used for formatting currency amounts
28-
*/
29-
int fractionalDigits();
30-
16+
public interface BankController extends Controller, CurrencyHolder {
3117
/**
3218
* Creates a bank for the specified player with the given name.
3319
* <p>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package net.thenextlvl.service.api.economy.currency;
2+
3+
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
4+
import org.jetbrains.annotations.Unmodifiable;
5+
6+
import java.util.Locale;
7+
import java.util.Optional;
8+
import java.util.Set;
9+
import java.util.function.Consumer;
10+
11+
/**
12+
* Represents an entity capable of handling currencies in an economy system.
13+
* This interface provides methods for formatting, retrieving, creating,
14+
* and deleting currencies, as well as determining support for multiple currencies.
15+
*/
16+
public interface CurrencyHolder {
17+
/**
18+
* Formats the specified amount as a string.
19+
*
20+
* @param amount the number amount to be formatted
21+
* @return the formatted amount as a string
22+
* @deprecated use {@link Currency#format(Number, Locale)}
23+
*/
24+
@Deprecated(forRemoval = true, since = "2.4.0")
25+
default String format(Number amount) {
26+
return PlainTextComponentSerializer.plainText().serialize(getDefaultCurrency().format(amount, Locale.US));
27+
}
28+
29+
/**
30+
* Retrieves the number of fractional digits used for formatting currency amounts.
31+
*
32+
* @return the number of fractional digits used for formatting currency amounts
33+
* @deprecated use {@link Currency#getFractionalDigits()}
34+
*/
35+
@Deprecated(forRemoval = true, since = "2.4.0")
36+
default int fractionalDigits() {
37+
return getDefaultCurrency().getFractionalDigits();
38+
}
39+
40+
/**
41+
* Retrieves all currencies managed by the currency holder.
42+
*
43+
* @return an unmodifiable set of currencies
44+
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
45+
*/
46+
default @Unmodifiable Set<Currency> getCurrencies() {
47+
throw new UnsupportedOperationException("Not implemented yet");
48+
}
49+
50+
/**
51+
* Retrieves a currency by its name.
52+
*
53+
* @param name the name of the currency to retrieve
54+
* @return an {@code Optional} containing the currency, or empty
55+
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
56+
*/
57+
default Optional<Currency> getCurrency(String name) {
58+
throw new UnsupportedOperationException("Not implemented yet");
59+
}
60+
61+
/**
62+
* Checks if a currency with the specified name exists.
63+
*
64+
* @param name the name of the currency to check for existence
65+
* @return {@code true} if the currency exists, otherwise {@code false}
66+
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
67+
*/
68+
default boolean hasCurrency(String name) {
69+
throw new UnsupportedOperationException("Not implemented yet");
70+
}
71+
72+
/**
73+
* Creates a new currency by configuring a {@link Currency.Builder}.
74+
* <p>
75+
* If a currency with the same name already exists, this method returns empty.
76+
*
77+
* @param name the name of the new currency
78+
* @param builder a consumer to configure the {@link Currency.Builder} for currency creation
79+
* @return an optional containing the created {@link Currency}, or empty
80+
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
81+
*/
82+
default Optional<Currency> createCurrency(String name, Consumer<Currency.Builder> builder) {
83+
throw new UnsupportedOperationException("Not implemented yet");
84+
}
85+
86+
/**
87+
* Deletes a currency with the specified name.
88+
*
89+
* @param name the name of the currency to delete
90+
* @return {@code true} if the currency was successfully deleted, otherwise {@code false}
91+
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
92+
*/
93+
default boolean deleteCurrency(String name) {
94+
throw new UnsupportedOperationException("Not implemented yet");
95+
}
96+
97+
/**
98+
* Retrieves the default currency for this economy controller.
99+
*
100+
* @return the default currency
101+
*/
102+
Currency getDefaultCurrency();
103+
104+
/**
105+
* Determines whether the economy controller supports multiple currencies.
106+
*
107+
* @return {@code true} if multi-currency is supported, otherwise {@code false}
108+
* @implSpec If multiple currencies are supported, all respective methods have to be implemented.
109+
* @see #createCurrency(String, Consumer)
110+
* @see #deleteCurrency(String)
111+
* @see #getCurrencies()
112+
* @see #getCurrency(String)
113+
* @see #hasCurrency(String)
114+
*/
115+
default boolean hasMultiCurrencySupport() {
116+
return false;
117+
}
118+
}

0 commit comments

Comments
 (0)