Skip to content

Commit

Permalink
add methods for controlling the ATM via OfflinePlayer and World
Browse files Browse the repository at this point in the history
This should allow developers to also manage bank accounts in worlds when
the player is offline.

Also, fixed ATM help command and syntax (added opt. world name)

closes #75
  • Loading branch information
mastercake10 committed Aug 23, 2020
1 parent e99b409 commit 64c7d60
Showing 1 changed file with 64 additions and 37 deletions.
101 changes: 64 additions & 37 deletions Plugin/src/main/java/de/Linus122/TimeIsMoney/ATM.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -93,26 +94,34 @@ public ATM(Main plugin) {
/**
* Withdraws the specified amount of money from the specified player's bank.
*
* @param p The player to withdraw money from.
* @param player The player to withdraw money from.
* @param amount The amount of money to withdraw.
*/
private static void withdrawBank(Player p, double amount) {
String bankString = getBankString(p);
private static void withdrawBank(Player player, double amount) {
withdrawBank(player, player.getWorld(), amount);
}

private static void withdrawBank(OfflinePlayer offlinePlayer, World inWorld, double amount) {
String bankString = getBankString(offlinePlayer, inWorld);
if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
bankAccountsConfig.set(bankString, getBankBalance(p) - amount);
bankAccountsConfig.set(bankString, getBankBalance(offlinePlayer, inWorld) - amount);
saveBanks();
}

/**
* Deposits the specified amount of money to the specified player's bank.
*
* @param p The player to deposit money to.
* @param player The player to deposit money to.
* @param amount The amount of money to deposit.
*/
public static void depositBank(Player p, double amount) {
String bankString = getBankString(p);
public static void depositBank(Player player, double amount) {
depositBank(player, player.getWorld(), amount);
}

public static void depositBank(OfflinePlayer offlinePlayer, World inWorld, double amount) {
String bankString = getBankString(offlinePlayer, inWorld);
if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
bankAccountsConfig.set(bankString, getBankBalance(p) + amount);
bankAccountsConfig.set(bankString, getBankBalance(offlinePlayer, inWorld) + amount);
saveBanks();
}

Expand All @@ -123,33 +132,41 @@ public static void depositBank(Player p, double amount) {
* @param amount The amount of money.
* @return True if the player has the specified amount of money, false otherwise.
*/
private static boolean bankHas(Player p, double amount) {
String bankString = getBankString(p);
private static boolean bankHas(Player player, double amount) {
return bankHas(player, player.getWorld(), amount);
}

private static boolean bankHas(OfflinePlayer offlinePlayer, World inWorld, double amount) {
String bankString = getBankString(offlinePlayer, inWorld);
if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
return getBankBalance(p) >= amount;
return getBankBalance(offlinePlayer) >= amount;

}

/**
* Gets the balance of the specified player's bank (doesn't support groups).
*
* @param p The offline player to get the balance of.
* @param offlinePlayer The offline player to get the balance of.
* @return The offline player's balance in the bank.
*/
public static double getBankBalance(OfflinePlayer p) {
String bankString = p.getName() + "_TimBANK";
public static double getBankBalance(OfflinePlayer offlinePlayer) {
String bankString = offlinePlayer.getName() + "_TimBANK";
if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
return bankAccountsConfig.getDouble(bankString);
}

/**
* Gets the balance of the specified player's bank.
*
* @param p The player to get the balance of.
* @param player The player to get the balance of.
* @return The player's balance in the bank.
*/
public static double getBankBalance(Player p) {
String bankString = getBankString(p);
public static double getBankBalance(Player player) {
return getBankBalance(player, player.getWorld());
}

public static double getBankBalance(OfflinePlayer offlinePlayer, World inWorld) {
String bankString = getBankString(offlinePlayer, inWorld);
if (!bankAccountsConfig.contains(bankString)) bankAccountsConfig.set(bankString, 0.0);
return bankAccountsConfig.getDouble(bankString);
}
Expand All @@ -167,9 +184,11 @@ private static void saveBanks() {

/**
* Converts the old TimeIsMoney bank data to the new format.
*
*
* @deprecated ancient method, will be deleted soon
* @param p The player to convert data for.
*/
@Deprecated
private static void convertOldBank(Player p) {
String bankString = getBankString(p);
if (Main.economy.hasAccount(bankString)) {
Expand All @@ -184,21 +203,25 @@ private static void convertOldBank(Player p) {
/**
* Gets the bank string for the specified player.
*
* @param p The player to get the bank string of.
* @param player The player to get the bank string of.
* @return The bank string of the specified player.
*/
private static String getBankString(Player p) {
private static String getBankString(Player player) {
return getBankString(player, player.getWorld());
}

private static String getBankString(OfflinePlayer offlinePlayer, World inWorld) {
if (!Main.finalconfig.getBoolean("group-atms")) {
return p.getName() + "_TimBANK";
return offlinePlayer.getName() + "_TimBANK";
} else {
for (String key : Main.finalconfig.getConfigurationSection("atm_groups").getKeys(false)) {
List<String> list = Main.finalconfig.getStringList("atm_groups." + key);
if (list.contains(p.getWorld().getName())) {
return p.getName() + "_TimBANK_" + key;
if (list.contains(inWorld.getName())) {
return inWorld.getName() + "_TimBANK_" + key;
}
}
}
return p.getName() + "_TimBANK";
return offlinePlayer.getName() + "_TimBANK";
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down Expand Up @@ -424,7 +447,9 @@ public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] a
break;
case "take":
if(args.length > 2) {
Player playerToTake = Bukkit.getPlayer(args[1]);
OfflinePlayer playerToTake = Bukkit.getOfflinePlayer(args[1]);
String inWorld = args.length > 3 ? args[3] : "world";

if(playerToTake == null) {
cs.sendMessage("§cThis player does not exists");
return true;
Expand All @@ -436,32 +461,35 @@ public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] a
cs.sendMessage("§cAmount to high! Player only has " + Economy.format(bal));
return true;
}
ATM.withdrawBank(playerToTake, amount);
ATM.withdrawBank(playerToTake, Bukkit.getWorld(inWorld), amount);
cs.sendMessage("§aWithdrew §2" + Economy.format(amount) + ".");
}catch(NumberFormatException e) {
cs.sendMessage("§cPlease enter a valid decimal");
}
}else {
cs.sendMessage("§c/tim take <player> <amount>");
cs.sendMessage("§c/tim take <player> <amount> [world]");
}
break;
case "give":
if(args.length > 2) {
Player playerToGive = Bukkit.getPlayer(args[1]);

OfflinePlayer playerToGive = Bukkit.getOfflinePlayer(args[1]);
String inWorld = args.length > 3 ? args[3] : "world";

if(playerToGive == null) {
cs.sendMessage("§cThis player does not exists");
return true;
}
try {
double amount = Double.parseDouble(args[2]);
double bal = ATM.getBankBalance(playerToGive);
ATM.depositBank(playerToGive, amount);
ATM.depositBank(playerToGive, Bukkit.getWorld(inWorld), amount);
cs.sendMessage("§aDeposited §2" + Economy.format(amount) + ".");
}catch(NumberFormatException e) {
cs.sendMessage("§cPlease enter a valid decimal");
}
}else {
cs.sendMessage("§c/tim give <player> <amount>");
cs.sendMessage("§c/tim give <player> <amount> [world]");
}
break;
default:
Expand All @@ -474,16 +502,15 @@ public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] a
if (op.isOnline()) {
openGUI(op.getPlayer());
cs.sendMessage("opened!");
return true;
}
cs.sendMessage(CC("&c/atm <player> &a- opens atm for player"));
cs.sendMessage(CC("&c/atm balance <player> &a- gets balance of player"));
cs.sendMessage(CC("&c/atm balancetop - Shows the top 10 player atm balances"));
cs.sendMessage(CC("&c/atm give <player> [world] &a- Deposits money into a players atm"));
cs.sendMessage(CC("&c/atm take <player> [world] &a- Withdraws money from a players atm"));
break;
}
} else {
cs.sendMessage(CC("&c/atm <player> &a- opens atm for player"));
cs.sendMessage(CC("&c/atm balance <player> &a- gets balance of player"));
cs.sendMessage(CC("&c/atm balancetop - Shows the top 10 player atm balances"));
cs.sendMessage(CC("&c/atm give <player> &a- Deposits money into a players atm"));
cs.sendMessage(CC("&c/atm take <player> &a- Withdraws money from a players atm"));
return true;
}
}
return true;
Expand Down

0 comments on commit 64c7d60

Please sign in to comment.