Skip to content

Commit

Permalink
Better number formatting for currencies based on viewer locale, chang…
Browse files Browse the repository at this point in the history
…e permission on pay to prevent possible permission parent issue with pay-other
  • Loading branch information
NickImpact committed Nov 14, 2023
1 parent f9cc4a9 commit a400031
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void reset(

@ProxiedBy("pay")
@CommandMethod("economy|eco pay <amount> <target> [currency] [source]")
@CommandPermission("impactor.commands.economy.pay")
@CommandPermission("impactor.commands.economy.pay.base")
public void transfer(
final @NotNull CommandSource source,
@Argument("amount") double amount,
Expand All @@ -220,17 +220,18 @@ public void transfer(

Context context = Context.empty();
context.append(Currency.class, c);
if(target.equals(focus)) {
ImpactorTranslations.ECONOMY_CANT_PAY_SELF.send(source, context);
return;
}

if(from != null) {
PermissionsService permissions = Impactor.instance().services().provide(PermissionsService.class);
if(!permissions.hasPermission(source.source(), "impactor.commands.economy.pay.other")) {
ImpactorTranslations.NO_PERMISSION.send(source, context);
return;
}
} else {
if(target.uuid().equals(source.uuid())) {
ImpactorTranslations.ECONOMY_CANT_PAY_SELF.send(source, context);
return;
}
}

if(c.transferable() == TriState.FALSE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@

package net.impactdev.impactor.core.economy.currency;

import com.google.common.base.Strings;
import net.impactdev.impactor.api.economy.currency.Currency;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.TriState;
import net.luckperms.api.util.Tristate;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import static net.kyori.adventure.text.Component.text;
Expand All @@ -50,7 +52,7 @@ public class ImpactorCurrency implements Currency {
private final boolean primary;
private final TriState transferable;

private final String pattern;
private final String formatPattern;

private ImpactorCurrency(final ImpactorCurrencyBuilder builder) {
this.key = builder.key;
Expand All @@ -63,7 +65,14 @@ private ImpactorCurrency(final ImpactorCurrencyBuilder builder) {
this.primary = builder.primary;
this.transferable = builder.transferable;

this.pattern = "%." + this.decimals + "f";
StringBuilder sb = new StringBuilder();
sb.append("#,##0");
if(this.decimals > 0) {
sb.append(".");
sb.append(Strings.repeat("0", this.decimals));
}

this.formatPattern = sb.toString();
}

@Override
Expand Down Expand Up @@ -93,7 +102,8 @@ public SymbolFormatting formatting() {

@Override
public Component format(@NotNull BigDecimal amount, boolean condensed, @NotNull Locale locale) {
Component value = text(String.format(locale, this.pattern, amount.doubleValue()));
DecimalFormat formatter = new DecimalFormat(this.formatPattern, new DecimalFormatSymbols(locale));
Component value = text(formatter.format(amount.doubleValue()));
if(condensed) {
return this.formatting.modify(this, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ public void construct() {

Platform platform = Impactor.instance().platform();
if(platform.info().plugin("luckperms").isPresent()) {
this.logger().info("LuckPerms detected, initializing luckperms integration...");
service.services().register(PermissionsService.class, new LuckPermsPermissionsService());
} else {
this.logger().info("No particular permissions service located, all permissions are now allowed!");
service.services().register(PermissionsService.class, new NoOpPermissionsService());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ public void verifyFormatting() {

condensed = currency.format(amount, true, Locale.CANADA_FRENCH);
assertEquals("$17,38", PlainTextComponentSerializer.plainText().serialize(condensed));

BigDecimal large = BigDecimal.valueOf(100_540_233);
condensed = currency.format(large, Locale.US);
assertEquals("$100,540,233.00", PlainTextComponentSerializer.plainText().serialize(condensed));

condensed = currency.format(large, Locale.ITALIAN);
assertEquals("$100.540.233,00", PlainTextComponentSerializer.plainText().serialize(condensed));
}

@Test
Expand Down

0 comments on commit a400031

Please sign in to comment.