public static Object getRubies(Player p) { int x = (int) econ.getBalance(p); if (p == null) { return null; } return x; }
public static double getBalance(String name) { if (!hasEconomy()) { throw new IllegalStateException( "Tried to perform economy actions but no economy service installed!"); } return economy.getBalance(name); }
public boolean ensureExists(String accountId) { Economy economy = this.getEconomy(); if (economy.hasAccount(accountId)) return true; if (!economy.createPlayerAccount(accountId)) return false; if (MUtil.isValidPlayerName(accountId)) return true; double balance = economy.getBalance(accountId); economy.withdrawPlayer(accountId, balance); return true; }
private void convertCommand(CommandSender sender, String[] args) { Collection<RegisteredServiceProvider<Economy>> econs = this.getServer().getServicesManager().getRegistrations(Economy.class); if (econs == null || econs.size() < 2) { sender.sendMessage("You must have at least 2 economies loaded to convert."); return; } else if (args.length != 2) { sender.sendMessage( "You must specify only the economy to convert from and the economy to convert to. (without spaces)"); return; } Economy econ1 = null; Economy econ2 = null; for (RegisteredServiceProvider<Economy> econ : econs) { String econName = econ.getProvider().getName().replace(" ", ""); if (econName.equalsIgnoreCase(args[0])) { econ1 = econ.getProvider(); } else if (econName.equalsIgnoreCase(args[1])) { econ2 = econ.getProvider(); } } if (econ1 == null) { sender.sendMessage( "Could not find " + args[0] + " loaded on the server, check your spelling"); return; } else if (econ2 == null) { sender.sendMessage( "Could not find " + args[1] + " loaded on the server, check your spelling"); return; } sender.sendMessage("This may take some time to convert, expect server lag."); for (OfflinePlayer op : Bukkit.getServer().getOfflinePlayers()) { String pName = op.getName(); if (econ1.hasAccount(pName)) { if (econ2.hasAccount(pName)) { continue; } econ2.createPlayerAccount(pName); econ2.depositPlayer(pName, econ1.getBalance(pName)); } } }
// Handles death related events. @SuppressWarnings("deprecation") @EventHandler(priority = EventPriority.NORMAL) public void onDeath(EntityDeathEvent event) { if (event.getEntity() instanceof Player) { Player player = (Player) event.getEntity(); // Players lose 5% of their money on death. Double loss; Double amount = econ.getBalance(player.getDisplayName()); loss = amount * 0.05; econ.withdrawPlayer(player.getDisplayName(), loss); } // Powered Creepers drop diamond when killed. if (event.getEntity() instanceof Creeper) { if (((Creeper) event.getEntity()).isPowered()) { ItemStack item = new ItemStack(Material.DIAMOND); event.getDrops().add(item); } } }
public void handlePayment(Player p, double i) { if (i > 0) { EconomyResponse r = econ.depositPlayer(p.getName(), i); if (r.transactionSuccess()) { p.sendMessage(ChatColor.GREEN + "You have been awarded $" + i); } else { p.sendMessage(ChatColor.RED + "PAYMENT FAILED!"); } } else if (i < 0) { i = Math.abs(i); double bal = econ.getBalance(p.getName()); if (bal < i) { i = bal; } EconomyResponse r = econ.withdrawPlayer(p.getName(), i); if (r.transactionSuccess()) { p.sendMessage(ChatColor.RED + "You have been fined $" + i); } else { p.sendMessage(ChatColor.RED + "PAYMENT FAILED!"); } } }
@Override public double getBalance(Player player) { return economy.getBalance(player.getName()); }
/** * Sell 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 sell. * @return true on success, false on failure. */ public boolean sell(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(); 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 pay Invoice invoice = generateInvoice(0, commodity, amount); // If the player has enough of the item, perform the transaction. int id = commodity.getNumber(); Byte byteData = Byte.valueOf(String.valueOf(commodity.getData())); ItemStack its = new ItemStack(id, amount, (short) 0, byteData); if (player.getInventory().contains(id)) { // Figure out how much is left over int left = getAmountInInventory(player, its) - amount; if (left < 0) { // this indicates the correct id, but the wrong bytedata value // give nice output even if they gave a bad name. player.sendMessage(ChatColor.RED + "You don't have enough " + item); player.sendMessage( ChatColor.GREEN + "In Inventory: " + ChatColor.WHITE + getAmountInInventory(player, its)); player.sendMessage(ChatColor.GREEN + "Attempted Amount: " + ChatColor.WHITE + amount); return false; } // Take out all of the item int x = 0; for (@SuppressWarnings("unused") ItemStack stack : player .getInventory() .getContents()) { // we do it this way incase a user has an expanded inventory via // another plugin ItemStack slot = player.getInventory().getItem(x); if (slot != null) { Byte slotData = Byte.valueOf("0"); try { slotData = slot.getData().getData(); } catch (NullPointerException e) { } if ((slot.getTypeId() == id) && (slotData.compareTo(byteData) == 0)) { player.getInventory().clear(x); } } x++; } // put back what was left over if (left > 0) { ItemStack itsLeft = its; itsLeft.setAmount(left); player.getInventory().addItem(itsLeft); } // record the change in value commodity.setValue(invoice.getValue()); plugin.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); } BigDecimal spread = BigDecimal.valueOf(commodity.getSpread()); // give some nice output BigDecimal sale = BigDecimal.valueOf((invoice.getTotal() + spread.doubleValue())) .setScale(2, RoundingMode.HALF_UP); player.sendMessage(ChatColor.GREEN + "--------------------------------"); player.sendMessage( ChatColor.GREEN + "Old Balance: " + ChatColor.WHITE + BigDecimal.valueOf(economy.getBalance(player.getName())) .setScale(2, RoundingMode.HALF_UP)); // deposit the money economy.depositPlayer(player.getName(), invoice.getTotal()); player.sendMessage(ChatColor.GREEN + "Sale: " + ChatColor.WHITE + sale); player.sendMessage(ChatColor.GREEN + "Selling Fee: " + ChatColor.WHITE + spread); player.sendMessage(ChatColor.GREEN + "--------------------------------"); player.sendMessage( ChatColor.GREEN + "Net Gain: " + 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 { // give nice output even if they gave a bad number. player.sendMessage(ChatColor.RED + "You don't have enough " + item); player.sendMessage( ChatColor.GREEN + "In Inventory: " + ChatColor.WHITE + getAmountInInventory(player, its)); player.sendMessage(ChatColor.GREEN + "Attempted Amount: " + ChatColor.WHITE + amount); return false; } }
/** * 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; } }
@SuppressWarnings("deprecation") public boolean onCommand( CommandSender sender, Command command, String commandLabel, String[] args) { if (!(sender instanceof Player)) { log.info("You must be a player!"); return true; } Player player = (Player) sender; if (command.getLabel().equalsIgnoreCase("rubies")) { if (args.length < 1) { int x = (int) econ.getBalance(player.getName()); sender.sendMessage( this.getConfig() .getString("rubies-format") .replaceAll("<balance>", "" + x) .replaceAll("&", "\u00a7")); } if (args.length >= 1) { if (args[0].equalsIgnoreCase("reload")) { reloadConfig(); player.sendMessage(ChatColor.YELLOW + "Rubies config " + ChatColor.WHITE + "reloaded!"); } if (args[0].equalsIgnoreCase("config")) { // if (args.length == 1){ player.sendMessage(ChatColor.YELLOW + "Rubies config: "); player.sendMessage( ChatColor.GREEN + "\nrubies-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-format")); player.sendMessage( ChatColor.GREEN + "rubies-see-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-see-format")); player.sendMessage( ChatColor.GREEN + "give-rubies-permission: " + ChatColor.GOLD + this.getConfig().getString("give-rubies-permission")); player.sendMessage( ChatColor.GREEN + "rubies-recived-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-recived-format")); player.sendMessage( ChatColor.GREEN + "rubies-error-message: " + ChatColor.GOLD + this.getConfig().getString("rubies-error-message")); player.sendMessage( ChatColor.GREEN + "give-rubies-permission: " + ChatColor.GOLD + this.getConfig().getString("give-rubies-permission")); player.sendMessage( ChatColor.GREEN + "no-give-ruby-perms-message: " + ChatColor.GOLD + this.getConfig().getString("no-give-ruby-perms-message")); player.sendMessage( ChatColor.GREEN + "take-rubies-permission: " + ChatColor.GOLD + this.getConfig().getString("take-rubies-permission")); player.sendMessage( ChatColor.GREEN + "rubies-taken-format: " + ChatColor.GOLD + this.getConfig().getString("rubies-taken-format")); player.sendMessage( ChatColor.GREEN + "no-take-ruby-perms-message: " + ChatColor.GOLD + this.getConfig().getString("no-take-ruby-perms-message")); // player.sendMessage(ChatColor.WHITE + "\nUse " + ChatColor.YELLOW + "/rubies config // <config section> <new value>" + ChatColor.WHITE + " to edit a config value"); // } /* if (args.length == 2){ player.sendMessage("\nProper command use is " + ChatColor.YELLOW + "/rubies config <config section> <new value>"); } if (args.length == 3){ if (this.getConfig().getString(args[2]) != null){ this.getConfig().set(args[2], args[3]); player.sendMessage(ChatColor.GREEN + args[2] + ChatColor.WHITE + " set to " + ChatColor.GOLD + args[3]); reloadConfig(); } if (this.getConfig().getString(args[2]) == null) { player.sendMessage(ChatColor.RED + "Error: " + ChatColor.WHITE + " Config section " + ChatColor.YELLOW + "'" + args[2] + "'" + ChatColor.WHITE + " does not exist!"); player.sendMessage("Type " + ChatColor.YELLOW + "/rubies config " + ChatColor.WHITE + "to see avalible options."); } }*/ } if (args[0].equalsIgnoreCase("see")) { if (args.length == 2) { if (player.getServer().getPlayer(args[1]) != null) { player.sendMessage( this.getConfig() .getString("rubies-see-format") .replaceAll("<player>", args[1]) .replaceAll("<balance>", "" + econ.getBalance(args[1])) .replaceAll("&", "\u00a7")); } else { player.sendMessage( "Player " + ChatColor.YELLOW + "'" + args[1] + "'" + ChatColor.WHITE + " not found"); } } if (args.length != 2) { player.sendMessage( "Proper Command use is " + ChatColor.YELLOW + "/rubies see <player>"); } } if (args[0].equalsIgnoreCase("give")) { if (player.hasPermission(this.getConfig().getString("give-rubies-permission")) || player.isOp()) { if (args.length == 3) { if (player.getServer().getPlayer(args[1]) != null) { if (isNumeric(args[2])) { double amount = Double.parseDouble(args[2]); EconomyResponse r = econ.depositPlayer(args[1], amount); // Try to give // econ.depositPlayer(args[1], amount);//Try to give if (r.transactionSuccess()) { // Rubies given int x = (int) econ.getBalance(args[1]); player.sendMessage( this.getConfig() .getString("rubies-recived-format") .replaceAll("<given>", "" + r.amount) .replaceAll("<balance>", "" + x) .replaceAll("<player>", args[1]) .replaceAll("&", "\u00a7")); } else { // An error has occurred sender.sendMessage( this.getConfig() .getString("rubies-error-message") .replaceAll("<error>", r.errorMessage)); } } else { player.sendMessage( ChatColor.RED + "Error: " + ChatColor.YELLOW + "'" + args[2] + "'" + ChatColor.WHITE + " must be a number!"); } } else { player.sendMessage( "Player " + ChatColor.YELLOW + "'" + args[1] + "'" + ChatColor.WHITE + " not found"); } } if (args.length != 3) { player.sendMessage( "Proper Command use is " + ChatColor.YELLOW + "/rubies give <player> <amount>"); } if (!player.hasPermission(this.getConfig().getString("give-rubies-permission")) || !player.isOp()) { // Player does not have permission player.sendMessage( this.getConfig() .getString("no-give-ruby-perms-message") .replaceAll("<balance>", "" + econ.getBalance(player.getName())) .replaceAll("&", "\u00a7")); } } } if (args[0].equalsIgnoreCase("take")) { if (player.hasPermission(this.getConfig().getString("take-rubies-permission")) || player.isOp()) { if (args.length == 3) { if (player.getServer().getPlayer(args[1]) != null) { if (isNumeric(args[2])) { double amount = Double.parseDouble(args[2]); EconomyResponse r = econ.withdrawPlayer(args[1], amount); // Try to take // econ.withdrawPlayer(args[1], amount);//Try to take if (r.transactionSuccess()) { // Rubies taken player.sendMessage( this.getConfig() .getString("rubies-taken-format") .replaceAll("<player>", args[1]) .replaceAll("<taken>", "" + r.amount) .replaceAll("<balance>", "" + econ.getBalance(player.getName())) .replaceAll("&", "\u00a7")); } else { // An error has occurred sender.sendMessage( this.getConfig() .getString("rubies-error-message") .replaceAll("<error>", r.errorMessage)); } } else { player.sendMessage( ChatColor.RED + "Error: " + ChatColor.YELLOW + "'" + args[2] + "'" + ChatColor.WHITE + " must be a number!"); } } else { player.sendMessage( "Player " + ChatColor.YELLOW + "'" + args[1] + "'" + ChatColor.WHITE + " not found"); } } if (args.length != 3) { player.sendMessage( "Proper Command use is " + ChatColor.YELLOW + "/rubies take <player> <amount>"); } } if (!player.hasPermission(this.getConfig().getString("take-rubies-permission")) || !player.isOp()) { // Player does not have permission player.sendMessage( this.getConfig() .getString("no-take-ruby-perms-message") .replaceAll("<balance>", "" + econ.getBalance(player.getName())) .replaceAll("&", "\u00a7")); } } if (!args[0].equalsIgnoreCase("reload") && !args[0].equalsIgnoreCase("see") && !args[0].equalsIgnoreCase("give") && !args[0].equalsIgnoreCase("take") && !args[0].equalsIgnoreCase("config")) { player.sendMessage( "Rubies Commands: " + ChatColor.YELLOW + "\n/rubies " + ChatColor.GRAY + ": See how many rubies you have."); player.sendMessage( ChatColor.YELLOW + "/rubies see <player> " + ChatColor.GRAY + ": See how many rubies a player has."); player.sendMessage( ChatColor.YELLOW + "/rubies give <player> <amount> " + ChatColor.GRAY + ": Gives rubies to a player."); player.sendMessage( ChatColor.YELLOW + "/rubies take <player> <amount> " + ChatColor.GRAY + ": Take rubies from a player."); player.sendMessage( ChatColor.YELLOW + "/rubies config " + ChatColor.GRAY + ": Rubies config options."); player.sendMessage( ChatColor.YELLOW + "/rubies reload " + ChatColor.GRAY + ": Reloads the rubies config file."); } } return true; } return false; }