public static boolean hasBalance(String name, double balance) { if (!hasEconomy()) { throw new IllegalStateException( "Tried to perform economy actions but no economy service installed!"); } return economy.has(name, balance); }
public boolean pay(Player p) { if (!plugin.useVault) { return true; } Economy econ; RegisteredServiceProvider<Economy> rsp = plugin.getServer().getServicesManager().getRegistration(Economy.class); if (rsp == null) { return true; // No Vault found } econ = rsp.getProvider(); if (econ == null) { return true; // No Vault found } int money = plugin.warpCreatePrice; if (money == 0) { return true; } if (!econ.has(p.getName(), money)) { p.sendMessage("You do not have enough money! You need " + econ.format(money) + "!"); return false; } econ.withdrawPlayer(p.getName(), money); p.sendMessage("Withdrawing " + econ.format(money) + " from your account!"); return true; }
@Override public boolean canAfford(Player player, double money) { return economy.has(player.getName(), money); }
public boolean hasMoney(Player player, float money) { if (economy == null) return false; return economy.has(player.getName(), money); }
/** * Check whether player has money * * @param player * @param amount * @return */ public static boolean hasMoney(Player player, double amount) { return economy.has(player.getName(), amount); }
/** * Buy a specified amount of an item for the player. * * @param player The player on behalf of which these actions will be carried out. * @param item The desired item in the form of the item name. * @param amount The desired amount of the item to purchase. * @return true on success, false on failure. */ public boolean buy(Player player, String item, int amount) { // Be sure we have a positive amount if (amount < 1) { player.sendMessage(ChatColor.RED + "Invalid amount."); player.sendMessage("No negative numbers or zero, please."); return false; } // retrieve the commodity in question Commodities commodity = plugin.getDatabase().find(Commodities.class).where().ieq("name", item).findUnique(); // check that we found something if (commodity == null) { player.sendMessage(ChatColor.RED + "Not allowed to buy that item."); player.sendMessage("Be sure you typed the correct name"); return false; } // determine what it will cost Invoice invoice = generateInvoice(1, commodity, amount); // check the player's wallet if (economy.has(player.getName(), invoice.getTotal())) { // give 'em the items and drop any extra Byte byteData = Byte.valueOf(String.valueOf(commodity.getData())); int id = commodity.getNumber(); HashMap<Integer, ItemStack> overflow = player.getInventory().addItem(new ItemStack(id, amount, (short) 0, byteData)); for (int a : overflow.keySet()) { player.getWorld().dropItem(player.getLocation(), overflow.get(a)); } // save the new value commodity.setValue(invoice.getValue()); getDatabase().save(commodity); // use BigDecimal to format value for output double v = commodity.getValue(); double max = commodity.getMaxValue(); double min = commodity.getMinValue(); BigDecimal value; if (v < max && v > min) { value = BigDecimal.valueOf(v).setScale(2, RoundingMode.HALF_UP); } else if (v <= min) { value = BigDecimal.valueOf(min).setScale(2, RoundingMode.HALF_UP); } else { value = BigDecimal.valueOf(max).setScale(2, RoundingMode.HALF_UP); } // Give some nice output. player.sendMessage(ChatColor.GREEN + "--------------------------------"); player.sendMessage( ChatColor.GREEN + "Old Balance: " + ChatColor.WHITE + BigDecimal.valueOf(economy.getBalance(player.getName())) .setScale(2, RoundingMode.HALF_UP)); // Subtract the invoice (this is an efficient place to do this) economy.withdrawPlayer(player.getName(), invoice.getTotal()); player.sendMessage( ChatColor.GREEN + "Cost: " + ChatColor.WHITE + BigDecimal.valueOf(invoice.getTotal()).setScale(2, RoundingMode.HALF_UP)); player.sendMessage( ChatColor.GREEN + "New Balance: " + ChatColor.WHITE + BigDecimal.valueOf(economy.getBalance(player.getName())) .setScale(2, RoundingMode.HALF_UP)); player.sendMessage(ChatColor.GREEN + "--------------------------------"); player.sendMessage( ChatColor.GRAY + item + ChatColor.GREEN + " New Price: " + ChatColor.WHITE + value); return true; } else { // Otherwise, give nice output anyway ;) // The idea here is to show how much more money is needed. BigDecimal difference = BigDecimal.valueOf(economy.getBalance(player.getName()) - invoice.getTotal()) .setScale(2, RoundingMode.HALF_UP); player.sendMessage(ChatColor.RED + "You don't have enough money"); player.sendMessage( ChatColor.GREEN + "Balance: " + ChatColor.WHITE + BigDecimal.valueOf(economy.getBalance(player.getName())) .setScale(2, RoundingMode.HALF_UP)); player.sendMessage( ChatColor.GREEN + "Cost: " + ChatColor.WHITE + BigDecimal.valueOf(invoice.getTotal()).setScale(2, RoundingMode.HALF_UP)); player.sendMessage(ChatColor.GREEN + "Difference: " + ChatColor.RED + difference); return true; } }