Skip to content

Commit 3982838

Browse files
committed
Enhanced currency and account APIs for precision and flexibility
Updated `Currency` methods to return `Optional`, improving null safety. Redesigned `setBalance` in `WrappedAccount` to return `BigDecimal` for better precision. Adjusted `WrappedCurrency` to include editing logic and improve locale-based display name handling for currencies. Bumped version to 3.0.0-pre3.
1 parent 01585a7 commit 3982838

File tree

7 files changed

+34
-37
lines changed

7 files changed

+34
-37
lines changed

plugin/src/main/java/net/thenextlvl/service/wrapper/VaultEconomyServiceWrapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ public String format(double amount) {
6464

6565
@Override
6666
public String currencyNamePlural() {
67-
var name = economyController.getDefaultCurrency().getDisplayNamePlural(Locale.US);
68-
return PlainTextComponentSerializer.plainText().serialize(name);
67+
return economyController.getDefaultCurrency().getDisplayNamePlural(Locale.US)
68+
.map(PlainTextComponentSerializer.plainText()::serialize).orElse("");
6969
}
7070

7171
@Override
7272
public String currencyNameSingular() {
73-
var name = economyController.getDefaultCurrency().getDisplayNameSingular(Locale.US);
74-
return PlainTextComponentSerializer.plainText().serialize(name);
73+
return economyController.getDefaultCurrency().getDisplayNameSingular(Locale.US)
74+
.map(PlainTextComponentSerializer.plainText()::serialize).orElse("");
7575
}
7676

7777
@Override

plugin/src/main/java/net/thenextlvl/service/wrapper/service/BankServiceWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.thenextlvl.service.api.economy.bank.Bank;
66
import net.thenextlvl.service.api.economy.bank.BankController;
77
import net.thenextlvl.service.wrapper.service.model.WrappedBank;
8+
import net.thenextlvl.service.wrapper.service.model.WrappedCurrency;
89
import org.bukkit.OfflinePlayer;
910
import org.bukkit.World;
1011
import org.bukkit.plugin.Plugin;

plugin/src/main/java/net/thenextlvl/service/wrapper/service/EconomyServiceWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.thenextlvl.service.api.economy.Account;
55
import net.thenextlvl.service.api.economy.EconomyController;
66
import net.thenextlvl.service.wrapper.service.model.WrappedAccount;
7+
import net.thenextlvl.service.wrapper.service.model.WrappedCurrency;
78
import org.bukkit.OfflinePlayer;
89
import org.bukkit.World;
910
import org.bukkit.plugin.Plugin;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ public UUID getOwner() {
6262
}
6363

6464
@Override
65-
public void setBalance(Number balance, Currency currency) {
65+
public BigDecimal setBalance(Number balance, Currency currency) {
6666
var difference = balance.doubleValue() - getBalance(currency).doubleValue();
67-
if (difference > 0) deposit(difference, currency);
68-
else if (difference < 0) withdraw(-difference, currency);
67+
if (difference > 0) return deposit(difference, currency);
68+
else if (difference < 0) return withdraw(-difference, currency);
69+
return BigDecimal.ZERO;
6970
}
7071
}

plugin/src/main/java/net/thenextlvl/service/wrapper/service/WrappedCurrency.java renamed to plugin/src/main/java/net/thenextlvl/service/wrapper/service/model/WrappedCurrency.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package net.thenextlvl.service.wrapper.service;
1+
package net.thenextlvl.service.wrapper.service.model;
22

33
import net.kyori.adventure.text.Component;
44
import net.milkbowl.vault.economy.Economy;
55
import net.thenextlvl.service.api.economy.currency.Currency;
66
import org.jspecify.annotations.NullMarked;
77

88
import java.util.Locale;
9+
import java.util.Optional;
10+
import java.util.function.Consumer;
911

1012
@NullMarked
1113
public class WrappedCurrency implements Currency {
@@ -21,13 +23,13 @@ public String getName() {
2123
}
2224

2325
@Override
24-
public Component getDisplayNameSingular(Locale locale) {
25-
return Component.text(economy.currencyNameSingular());
26+
public Optional<Component> getDisplayNameSingular(Locale locale) {
27+
return Optional.ofNullable(economy.currencyNameSingular()).map(Component::text);
2628
}
2729

2830
@Override
29-
public Component getDisplayNamePlural(Locale locale) {
30-
return Component.text(economy.currencyNamePlural());
31+
public Optional<Component> getDisplayNamePlural(Locale locale) {
32+
return Optional.ofNullable(economy.currencyNamePlural()).map(Component::text);
3133
}
3234

3335
@Override
@@ -44,4 +46,9 @@ public Component format(Number amount, Locale locale) {
4446
public int getFractionalDigits() {
4547
return economy.fractionalDigits();
4648
}
49+
50+
@Override
51+
public boolean editCurrency(Consumer<Builder> consumer) {
52+
return false;
53+
}
4754
}

src/main/java/net/thenextlvl/service/api/economy/currency/Currency.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import net.kyori.adventure.audience.Audience;
44
import net.kyori.adventure.identity.Identity;
55
import net.kyori.adventure.text.Component;
6-
import org.jetbrains.annotations.ApiStatus;
76
import org.jetbrains.annotations.Contract;
87
import org.jetbrains.annotations.Unmodifiable;
98
import org.jspecify.annotations.NullMarked;
@@ -111,16 +110,9 @@ default Component format(Number amount, Audience audience) {
111110
* The builder allows customization of various currency properties.
112111
*
113112
* @param consumer a {@code Consumer} that accepts a {@code Builder} instance to define customizations for the currency
113+
* @return {@code true} if the edit succeeded, otherwise {@code false}
114114
*/
115-
void editCurrency(Consumer<Builder> consumer);
116-
117-
/**
118-
* Converts the current {@code Currency} instance into a {@code Builder} for modification or reconstruction.
119-
*
120-
* @return a {@code Builder} instance initialized with the properties of the current {@code Currency}
121-
*/
122-
@ApiStatus.OverrideOnly
123-
Builder toBuilder();
115+
boolean editCurrency(Consumer<Builder> consumer);
124116

125117
/**
126118
* A builder interface for constructing instances of {@link Currency}.
@@ -136,14 +128,14 @@ interface Builder {
136128
*/
137129
@Contract(value = "_ -> this")
138130
Builder name(String name);
139-
131+
140132
/**
141133
* Retrieves the name currently set on the builder.
142134
*
143135
* @return the name as a string, or {@code null} if not set
144136
*/
145137
String name();
146-
138+
147139
/**
148140
* Retrieves a map containing the singular display names of the currency for various locales.
149141
*
@@ -240,14 +232,5 @@ interface Builder {
240232
*/
241233
@Contract(value = "-> new")
242234
OptionalInt fractionalDigits();
243-
244-
/**
245-
* Builds and returns a {@link Currency} instance based on the properties set on the {@code Builder}.
246-
*
247-
* @return the constructed {@link Currency} instance
248-
*/
249-
@Contract("-> new")
250-
@ApiStatus.OverrideOnly
251-
Currency build();
252235
}
253236
}

src/main/java/net/thenextlvl/service/api/economy/currency/CurrencyHolder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,17 @@ default Currency createCurrency(String name, Consumer<Currency.Builder> builder)
9090
}
9191

9292
/**
93-
* Adds a new currency to the currency holder.
93+
* Creates a new currency using the specified builder.
94+
* <p>
95+
* This method enables modifying existing currencies by utilizing a
96+
* pre-configured builder to create a new currency.
9497
*
95-
* @param currency the {@code Currency} object to add
96-
* @return {@code true} if the currency was successfully added, otherwise {@code false}
98+
* @param builder the {@link Currency.Builder} containing the configuration for the currency creation
99+
* @return the newly created {@link Currency}
97100
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
101+
* @throws IllegalArgumentException if a currency with the same name already exists
98102
*/
99-
default boolean addCurrency(Currency currency) {
103+
default Currency createCurrency(Currency.Builder builder) {
100104
throw new UnsupportedOperationException("Not implemented yet");
101105
}
102106

0 commit comments

Comments
 (0)