Skip to content

Commit

Permalink
add support for /money give/take %.
Browse files Browse the repository at this point in the history
  • Loading branch information
creatorfromhell committed Sep 11, 2023
1 parent 1006639 commit 9e10b6f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public HoldingsModifier(final String region, final UUID currency, final PercentB

if(modifier.isPercent()) {
this.operation = HoldingsOperation.PERCENT_ADD;
this.percent = true;
} else {
this.operation = HoldingsOperation.ADD;
}
Expand Down Expand Up @@ -120,6 +121,7 @@ public HoldingsModifier(final String region, final UUID currency, final PercentB

if(modifier.isPercent()) {
this.operation = HoldingsOperation.PERCENT_ADD;
this.percent = true;
} else {
this.operation = HoldingsOperation.ADD;
}
Expand Down Expand Up @@ -199,7 +201,10 @@ public HoldingsModifier(final HoldingsEntry entry, final HoldingsOperation opera
* @return The new opposite holdings modifier object.
*/
public HoldingsModifier counter() {
return new HoldingsModifier(region, currency, modifier.negate(), operation, holdingsID);
final HoldingsModifier mod = new HoldingsModifier(region, currency, modifier.negate(), operation, holdingsID);
System.out.println("Percent Counter: " + percent);
mod.setPercent(percent);
return mod;
}

/**
Expand All @@ -209,7 +214,9 @@ public HoldingsModifier counter() {
* @return The new opposite holdings modifier object.
*/
public HoldingsModifier counter(final Identifier type) {
return new HoldingsModifier(region, currency, modifier.negate(), operation, type);
final HoldingsModifier mod = new HoldingsModifier(region, currency, modifier.negate(), operation, type);
mod.setPercent(percent);
return mod;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.tnemc.core.account.Account;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
Expand Down Expand Up @@ -53,13 +54,18 @@ public BigDecimal perform(final BigDecimal value, final BigDecimal modifier) {
PERCENT_ADD {
@Override
public BigDecimal perform(final BigDecimal value, final BigDecimal modifier) {
return value.add(value.multiply(modifier));

final BigDecimal decimal = value.multiply(modifier.divide(new BigDecimal(100), new MathContext(2, RoundingMode.DOWN)));
if(modifier.compareTo(BigDecimal.ZERO) < 0) {
return decimal.multiply(new BigDecimal(-1));
}
return value.add(decimal);
}
},
PERCENT_SUBTRACT {
@Override
public BigDecimal perform(final BigDecimal value, final BigDecimal modifier) {
return value.subtract(value.multiply(modifier));
return value.subtract(value.multiply(modifier.divide(new BigDecimal(100), new MathContext(2, RoundingMode.DOWN))));
}
},
SET {
Expand Down
13 changes: 7 additions & 6 deletions Core/src/net/tnemc/core/command/MoneyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static void onDeposit(CmdSource<?> sender, PercentBigDecimal amount, Curr

final HoldingsModifier modifier = new HoldingsModifier(sender.region(),
currency.getUid(),
amount.value().setScale(currency.getDecimalPlaces(), RoundingMode.DOWN),
amount,
EconomyManager.VIRTUAL
);

Expand All @@ -189,7 +189,7 @@ public static void onGive(CmdSource<?> sender, Account player, PercentBigDecimal

final HoldingsModifier modifier = new HoldingsModifier(region,
currency.getUid(),
amount.value().setScale(currency.getDecimalPlaces(), RoundingMode.DOWN));
amount);

final Transaction transaction = new Transaction("give")
.to(player, modifier)
Expand Down Expand Up @@ -372,7 +372,7 @@ public static void onPay(CmdSource<?> sender, Account player, PercentBigDecimal

final HoldingsModifier modifier = new HoldingsModifier(sender.region(),
currency.getUid(),
amount.value().setScale(currency.getDecimalPlaces(), RoundingMode.DOWN)
amount
);

final Transaction transaction = new Transaction("pay")
Expand Down Expand Up @@ -502,9 +502,10 @@ public static void onTake(CmdSource<?> sender, Account player, PercentBigDecimal

region = TNECore.eco().region().resolve(region);


final HoldingsModifier modifier = new HoldingsModifier(region,
currency.getUid(),
amount.value().setScale(currency.getDecimalPlaces(), RoundingMode.DOWN)
amount
);

final Transaction transaction = new Transaction("take")
Expand Down Expand Up @@ -596,7 +597,7 @@ public static void onWithdraw(CmdSource<?> sender, PercentBigDecimal amount, Cur

final HoldingsModifier modifier = new HoldingsModifier(sender.region(),
currency.getUid(),
amount.value().setScale(currency.getDecimalPlaces(), RoundingMode.DOWN),
amount,
EconomyManager.ITEM_ONLY
);

Expand All @@ -608,12 +609,12 @@ public static void onWithdraw(CmdSource<?> sender, PercentBigDecimal amount, Cur

final Optional<Receipt> receipt = processTransaction(sender, transaction);
if(receipt.isPresent()) {

final MessageData data = new MessageData("Messages.Money.Withdrawn");
data.addReplacement("$currency", currency.getIdentifier());
data.addReplacement("$amount", CurrencyFormatter.format(senderAccount.get(),
modifier.asEntry()));
sender.message(data);

}
}

Expand Down
24 changes: 22 additions & 2 deletions Core/src/net/tnemc/core/transaction/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,17 @@ public Transaction from(final Account account, final HoldingsModifier modifier)
final boolean take = (modifier.getModifier().compareTo(BigDecimal.ZERO) < 0);

if(take) {
working = modifier.getModifier().multiply(new BigDecimal(-1));
if(modifier.isPercent()) {

BigDecimal total = BigDecimal.ZERO;
for(HoldingsEntry entry : balances) {
total = total.add(entry.getAmount());
}
working = modifier.modify(total);
total = null;
} else {
working = modifier.getModifier().multiply(new BigDecimal(- 1));
}
}

boolean done = false;
Expand Down Expand Up @@ -233,7 +243,17 @@ public Transaction to(final Account account, final HoldingsModifier modifier) {
final boolean take = (modifier.getModifier().compareTo(BigDecimal.ZERO) < 0);

if(take) {
working = modifier.getModifier().multiply(new BigDecimal(-1));
if(modifier.isPercent()) {

BigDecimal total = BigDecimal.ZERO;
for(HoldingsEntry entry : balances) {
total = total.add(entry.getAmount());
}
working = modifier.modify(total);
total = null;
} else {
working = modifier.getModifier().multiply(new BigDecimal(- 1));
}
}

boolean done = false;
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
- Added Redis Support. This will allow data syncing without the need for bungee
- Added a currency template module(requested by stoffeh)
- This contains some example pre-configured commonly used currency setups for TNE.
- Added $currency, $player variables for /money give/take messages in messages.yml
- Added $currency, $player variables for /money give/take messages in messages.yml
- Updated Turkish Translation(thanks to Colonel Kai)

0 comments on commit 9e10b6f

Please sign in to comment.