Skip to content

Commit

Permalink
✨ Created and implemented balance format.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurr0 committed Sep 30, 2021
1 parent 6ec28f9 commit 37e1cf5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/main/java/pl/minecodes/mineeconomy/EconomyPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import pl.minecodes.mineeconomy.hook.vault.VaultManager;
import pl.minecodes.mineeconomy.profile.ProfileService;

import java.text.DecimalFormat;
import java.util.Locale;

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 @@ -50,7 +53,7 @@ public void onEnable() {
vaultHook.registerHook();

PlaceholderAPIHook papiHook = this.injector.createInstance(PlaceholderAPIHook.class);
papiHook.register();
papiHook.registerHook();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.*;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.AtomicDouble;
import eu.okaeri.injector.annotation.Inject;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import pl.minecodes.mineeconomy.EconomyPlugin;
import pl.minecodes.mineeconomy.data.configuration.Configuration;
import pl.minecodes.mineeconomy.data.configuration.Messages;
import pl.minecodes.mineeconomy.profile.Profile;
Expand Down Expand Up @@ -42,21 +44,23 @@ public void help(CommandSender sender) {
@Description("Setup player balance.")
@CommandCompletion("@players 10|100|1000|10000")
public void onAdministratorSetBalance(CommandSender sender, String username, double balance) {
AtomicDouble atomicBalance = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(balance)));

OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.setupBalance(balance, new BalanceOperationCallback() {
profile.setupBalance(atomicBalance.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullySet(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"balance", balance,
"currency", configuration.getCurrency(balance))));
"balance", atomicBalance.get(),
"currency", configuration.getCurrency(atomicBalance.get()))));
}

@Override
Expand All @@ -72,21 +76,23 @@ public void cancel(CancelReason reason) {
@Description("Deposit money to player balance.")
@CommandCompletion("@players 10|100|1000|10000")
public void onAdministratorDepositMoney(CommandSender sender, String username, double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));

OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.deposit(value, new BalanceOperationCallback() {
profile.deposit(atomicValue.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyDeposit(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"value", value,
"currency", configuration.getCurrency(value))));
"value", atomicValue.get(),
"currency", configuration.getCurrency(atomicValue.get()))));
}

@Override
Expand All @@ -102,21 +108,23 @@ public void cancel(CancelReason reason) {
@Description("Withdraw money from player balance.")
@CommandCompletion("@players 10|100|1000|10000")
public void onAdministratorWithdrawMoney(CommandSender sender, String username, double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));

OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayerIfCached(username);
if (offlinePlayer == null) {
MessageUtil.sendMessage(sender, this.messages.getPlayerIsNotExistsInCache());
return;
}

Profile profile = this.profileService.getProfile(offlinePlayer.getUniqueId());
profile.withdraw(value, new BalanceOperationCallback() {
profile.withdraw(atomicValue.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyWithdraw(),
ImmutableMap.of(
"player", Objects.requireNonNull(offlinePlayer.getName(), "OfflinePlayer name is null."),
"value", value,
"currency", configuration.getCurrency(value))));
"value", atomicValue.get(),
"currency", configuration.getCurrency(atomicValue.get()))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import co.aikar.commands.annotation.*;
import co.aikar.commands.bukkit.contexts.OnlinePlayer;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.AtomicDouble;
import eu.okaeri.injector.annotation.Inject;
import org.bukkit.entity.Player;
import pl.minecodes.mineeconomy.EconomyPlugin;
import pl.minecodes.mineeconomy.data.configuration.Configuration;
import pl.minecodes.mineeconomy.data.configuration.Messages;
import pl.minecodes.mineeconomy.profile.Profile;
Expand All @@ -14,6 +16,8 @@
import pl.minecodes.mineeconomy.util.MessageUtil;
import pl.minecodes.mineeconomy.util.Placeholders;

import java.text.DecimalFormat;

@CommandAlias("transfer|pay")
public class TransferCommand extends BaseCommand {

Expand All @@ -28,29 +32,30 @@ public class TransferCommand extends BaseCommand {
@Syntax("<username> <value>")
@CommandCompletion("@players 10|100|1000|10000")
public void onPlayerTransfer(Player sender, OnlinePlayer target, double value) {
AtomicDouble atomicValue = new AtomicDouble(Double.parseDouble(EconomyPlugin.FORMAT.format(value)));

Profile senderProfile = this.profileService.getProfile(sender.getUniqueId());
if (!(senderProfile.has(value))) {
if (!(senderProfile.has(atomicValue.get()))) {
MessageUtil.sendMessage(sender, this.messages.getBalanceNoFounds());
return;
}

Profile targetProfile = this.profileService.getProfile(target.getPlayer().getUniqueId());
targetProfile.deposit(value, new BalanceOperationCallback() {
targetProfile.deposit(atomicValue.get(), new BalanceOperationCallback() {
@Override
public void done() {
MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyTransferToSender(),
ImmutableMap.of(
"target", target.getPlayer().getName(),
"value", value,
"currency", configuration.getCurrency(value)
"value", atomicValue.get(),
"currency", configuration.getCurrency(atomicValue.get())
)));

MessageUtil.sendMessage(sender, Placeholders.replace(messages.getBalanceSuccessfullyTransferToTarget(),
ImmutableMap.of(
"sender", sender.getName(),
"value", value,
"currency", configuration.getCurrency(value)
"value", atomicValue.get(),
"currency", configuration.getCurrency(atomicValue.get())
)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import pl.minecodes.mineeconomy.EconomyPlugin;
import pl.minecodes.mineeconomy.data.configuration.Configuration;
import pl.minecodes.mineeconomy.profile.Profile;
import pl.minecodes.mineeconomy.profile.ProfileService;
Expand All @@ -17,8 +18,6 @@

public class VaultManager implements Economy {

private final DecimalFormat FORMAT = new DecimalFormat("###,###,###,###.##");

@Inject
private Configuration configuration;
@Inject
Expand Down Expand Up @@ -48,11 +47,11 @@ public int fractionalDigits() {
public String format(double value) {
switch (this.configuration.getCurrencyPositionVault()) {
case AHEAD:
return this.configuration.getCurrency(value) + this.FORMAT.format(value);
return this.configuration.getCurrency(value) + EconomyPlugin.FORMAT.format(value);
case BEHIND:
return this.FORMAT.format(value) + this.configuration.getCurrency(value);
return EconomyPlugin.FORMAT.format(value) + this.configuration.getCurrency(value);
}
return this.configuration.getCurrency(value) + this.FORMAT.format(value);
return this.configuration.getCurrency(value) + EconomyPlugin.FORMAT.format(value);
}

@Override
Expand Down

0 comments on commit 37e1cf5

Please sign in to comment.