Skip to content

Commit

Permalink
Rounded scale (#13)
Browse files Browse the repository at this point in the history
* #11 Fixed rounded money.

* Removed old formatting type.
  • Loading branch information
arthurr0 authored Mar 10, 2022
1 parent c89564a commit 95de939
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@
import pl.minecodes.mineeconomy.profile.ProfileService;
import pl.minecodes.mineeconomy.runnable.ProfileSaveTask;

import java.text.DecimalFormat;

public class EconomyPlugin extends JavaPlugin {

public static final DecimalFormat FORMAT = new DecimalFormat("#.##");

private Injector injector;
private Messages messages;
private Configuration configuration;
Expand Down Expand Up @@ -91,6 +87,9 @@ public void onEnable() {

@Override
public void onDisable() {
if (this.liteCommands != null) {
this.liteCommands.getPlatformManager().unregisterCommands();
}
vaultHook.unregisterHook();
}

Expand All @@ -117,4 +116,5 @@ private void registerCommands() {
.message(ValidationInfo.INVALID_USE, messageInfoContext -> this.messages.getCommandUsage().replace("{usage}", messageInfoContext.getUseScheme()))
.register();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public void done() {
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}, this.configuration.getRoundedScale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class EconomyDepositCommand {

@Execute(route = "deposit", required = 2)
public void executeDeposit(CommandSender sender, @Arg(0) OfflinePlayer offlinePlayer, @Arg(1) Double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));
AtomicDouble atomicValue = new AtomicDouble(value);

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.deposit(atomicValue.get(), new BalanceOperationCallback() {
Expand All @@ -47,6 +47,6 @@ public void done() {
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}, this.configuration.getRoundedScale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class EconomySetCommand {

@Execute(route = "set", required = 2)
public void executeSet(CommandSender sender, @Arg(0) OfflinePlayer offlinePlayer, @Arg(1) Double balance) {
AtomicDouble atomicBalance = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(balance)));
AtomicDouble atomicBalance = new AtomicDouble(balance);

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.setupBalance(atomicBalance.get(), new BalanceOperationCallback() {
Expand All @@ -47,6 +47,6 @@ public void done() {
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}, this.configuration.getRoundedScale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class EconomyWithdrawCommand {

@Execute(route = "withdraw", required = 2)
public void executeWithdraw(CommandSender sender, @Arg(0) OfflinePlayer offlinePlayer, @Arg(1) Double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));
AtomicDouble atomicValue = new AtomicDouble(value);

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.withdraw(atomicValue.get(), new BalanceOperationCallback() {
Expand All @@ -57,6 +57,6 @@ public void cancel(CancelReason reason) {
break;
}
}
});
}, this.configuration.getRoundedScale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import dev.rollczi.litecommands.argument.SingleArgumentHandler;
import dev.rollczi.litecommands.valid.ValidationCommandException;
import eu.okaeri.injector.annotation.Inject;
import pl.minecodes.mineeconomy.data.configuration.Configuration;
import pl.minecodes.mineeconomy.data.configuration.Messages;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

@ArgumentName("value")
public class DoubleArgument implements SingleArgumentHandler<Double> {

@Inject private Messages messages;
@Inject private Configuration configuration;

@Override
public Double parse(LiteInvocation invocation, String argument) throws ValidationCommandException {
Expand All @@ -24,7 +28,7 @@ public Double parse(LiteInvocation invocation, String argument) throws Validatio
throw new ValidationCommandException(this.messages.getBalanceOperationParameterIsNotCorrect());
}

return parseDouble;
return new BigDecimal(parseDouble).setScale(this.configuration.getRoundedScale(), RoundingMode.HALF_UP).doubleValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TransferCommand {

@Execute(required = 2)
public void executeTransfer(Player sender, @Arg(0) Player target, @Arg(1) Double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));
AtomicDouble atomicValue = new AtomicDouble(value);

Profile senderProfile = this.profileService.getProfile(sender.getUniqueId());
if (!(senderProfile.has(atomicValue.get()))) {
Expand All @@ -46,7 +46,7 @@ public void done() {
public void cancel(CancelReason reason) {
//ignore
}
});
}, this.configuration.getRoundedScale());

targetProfile.deposit(atomicValue.get(), new BalanceOperationCallback() {
@Override
Expand All @@ -70,7 +70,7 @@ public void done() {
public void cancel(CancelReason reason) {
MessageUtil.sendMessage(sender, messages.getBalanceOperationParameterIsNegative());
}
});
}, this.configuration.getRoundedScale());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Configuration extends OkaeriConfig {
private CurrencyPositionVault currencyPositionVault = CurrencyPositionVault.BEHIND;
@Comment("Początkowy stan konta gracza.")
private double startBalance = 10;
@Comment("Do ilu liczb po przecinku powinna być zaokrąglana kwota.")
private int roundedScale = 2;
private DatabaseData databaseData = new DatabaseData();


Expand Down Expand Up @@ -62,4 +64,12 @@ public void setDatabaseData(DatabaseData databaseData) {
}

public enum CurrencyPositionVault {AHEAD, BEHIND}

public int getRoundedScale() {
return roundedScale;
}

public void setRoundedScale(int roundedScale) {
this.roundedScale = roundedScale;
}
}
25 changes: 14 additions & 11 deletions src/main/java/pl/minecodes/mineeconomy/hook/vault/VaultManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import pl.minecodes.mineeconomy.profile.ProfileService;
import pl.minecodes.mineeconomy.profile.helper.BalanceOperationCallback;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -43,13 +45,14 @@ public int fractionalDigits() {

@Override
public String format(double value) {
double doubleValue = new BigDecimal(value).setScale(this.configuration.getRoundedScale(), RoundingMode.HALF_UP).doubleValue();
switch (this.configuration.getCurrencyPositionVault()) {
case AHEAD:
return this.configuration.getCurrency(value) + EconomyPlugin.FORMAT.format(value);
return this.configuration.getCurrency(doubleValue) + doubleValue;
case BEHIND:
return EconomyPlugin.FORMAT.format(value) + this.configuration.getCurrency(value);
return doubleValue + this.configuration.getCurrency(doubleValue);
}
return this.configuration.getCurrency(value) + EconomyPlugin.FORMAT.format(value);
return this.configuration.getCurrency(doubleValue) + doubleValue;
}

@Override
Expand Down Expand Up @@ -162,7 +165,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -181,7 +184,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -205,7 +208,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -224,7 +227,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -248,7 +251,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -267,7 +270,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -291,7 +294,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand All @@ -310,7 +313,7 @@ public void done() {
public void cancel(CancelReason reason) {
economyResponse.set(new EconomyResponse(value, profile.getBalance(), EconomyResponse.ResponseType.FAILURE, reason.toString()));
}
});
}, this.configuration.getRoundedScale());
return economyResponse.get();
}

Expand Down
17 changes: 11 additions & 6 deletions src/main/java/pl/minecodes/mineeconomy/profile/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import pl.minecodes.mineeconomy.data.configuration.Configuration;
import pl.minecodes.mineeconomy.profile.helper.BalanceOperationCallback;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.UUID;

public class Profile {
Expand Down Expand Up @@ -37,18 +39,19 @@ public double getBalance() {
return balance;
}

public void deposit(double money, BalanceOperationCallback callback) {
public void deposit(double money, BalanceOperationCallback callback, int roundedScale) {
if (money < 0) {
callback.cancel(BalanceOperationCallback.CancelReason.NEGATIVE_PARAMETER);
return;
}

callback.done();
this.balance += money;
BigDecimal decimal = new BigDecimal(money).setScale(roundedScale, RoundingMode.HALF_UP);
this.balance += decimal.doubleValue();
this.needUpdate = true;
}

public void withdraw(double money, BalanceOperationCallback callback) {
public void withdraw(double money, BalanceOperationCallback callback, int roundedScale) {
if (this.balance < 0) {
callback.cancel(BalanceOperationCallback.CancelReason.NEGATIVE_BALANCE);
return;
Expand All @@ -63,18 +66,20 @@ public void withdraw(double money, BalanceOperationCallback callback) {
}

callback.done();
this.balance -= money;
BigDecimal decimal = new BigDecimal(money).setScale(roundedScale, RoundingMode.HALF_UP);
this.balance -= decimal.doubleValue();
this.needUpdate = true;
}

public void setupBalance(double balance, BalanceOperationCallback callback) {
public void setupBalance(double balance, BalanceOperationCallback callback, int roundedScale) {
if (balance < 0) {
callback.cancel(BalanceOperationCallback.CancelReason.NEGATIVE_PARAMETER);
return;
}

callback.done();
this.balance = balance;
BigDecimal decimal = new BigDecimal(balance).setScale(roundedScale, RoundingMode.HALF_UP);
this.balance = decimal.doubleValue();
this.needUpdate = true;
}

Expand Down

0 comments on commit 95de939

Please sign in to comment.