diff --git a/Essentials/src/com/earth2me/essentials/Worth.java b/Essentials/src/com/earth2me/essentials/Worth.java index 804797dd393..bddef39a35f 100644 --- a/Essentials/src/com/earth2me/essentials/Worth.java +++ b/Essentials/src/com/earth2me/essentials/Worth.java @@ -27,33 +27,33 @@ public Worth(File dataFolder) { * * @param ess The Essentials instance. * @param itemStack The item stack to look up in the config. + * @param key The key to look under in the config * @return The price from the config. */ - public BigDecimal getPrice(IEssentials ess, ItemStack itemStack) { - BigDecimal result; - + public BigDecimal getPrice(IEssentials ess, ItemStack itemStack, String key) { + BigDecimal negativeOne = BigDecimal.ONE.negate(); String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); // Check for matches with data value from stack // Note that we always default to BigDecimal.ONE.negate(), equivalent to -1 - result = config.getBigDecimal("worth." + itemname + "." + itemStack.getDurability(), BigDecimal.ONE.negate()); + BigDecimal result = config.getBigDecimal(key + "." + itemname + "." + itemStack.getDurability(), negativeOne); // Check for matches with data value 0 if (result.signum() < 0) { - final ConfigurationSection itemNameMatch = config.getConfigurationSection("worth." + itemname); + final ConfigurationSection itemNameMatch = config.getConfigurationSection(key + "." + itemname); if (itemNameMatch != null && itemNameMatch.getKeys(false).size() == 1) { - result = config.getBigDecimal("worth." + itemname + ".0", BigDecimal.ONE.negate()); + result = config.getBigDecimal(key + "." + itemname + ".0", negativeOne); } } // Check for matches with data value wildcard if (result.signum() < 0) { - result = config.getBigDecimal("worth." + itemname + ".*", BigDecimal.ONE.negate()); + result = config.getBigDecimal(key + "." + itemname + ".*", negativeOne); } // Check for matches with item name alone if (result.signum() < 0) { - result = config.getBigDecimal("worth." + itemname, BigDecimal.ONE.negate()); + result = config.getBigDecimal(key + "." + itemname, negativeOne); } if (result.signum() < 0) { @@ -62,6 +62,51 @@ public BigDecimal getPrice(IEssentials ess, ItemStack itemStack) { return result; } + /** + * Get the value of an item stack from the worth array in worth.yml. + * + * @param ess The Essentials instance. + * @param itemStack The item stack to look up in the config. + * @return The price from the config. + */ + public BigDecimal getWorthPrice(IEssentials ess, ItemStack itemStack) { + return getPrice(ess, itemStack, "worth"); + } + + /** + * Get the value of an item stack from the buy array in worth.yml. + * Falls back to worth section if the item is not found in the buy section. + * + * @param ess The Essentials instance. + * @param itemStack The item stack to look up in the config. + * @return The price from the config. + */ + public BigDecimal getBuyPrice(IEssentials ess, ItemStack itemStack) { + BigDecimal result = getPrice(ess, itemStack, "buy"); + + if (result.signum() < 0) { + return getWorthPrice(ess, itemStack); + } + return result; + } + + /** + * Get the value of an item stack from the sell array in worth.yml. + * Falls back to worth section if the item is not found in the buy section. + * + * @param ess The Essentials instance. + * @param itemStack The item stack to look up in the config. + * @return The price from the config. + */ + public BigDecimal getSellPrice(IEssentials ess, ItemStack itemStack) { + BigDecimal result = getPrice(ess, itemStack, "sell"); + + if (result.signum() < 0) { + return getWorthPrice(ess, itemStack); + } + return result; + } + /** * Get the amount of items to be sold from a player's inventory. * diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbuy.java b/Essentials/src/com/earth2me/essentials/commands/Commandbuy.java new file mode 100644 index 00000000000..d65b18dbda6 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbuy.java @@ -0,0 +1,98 @@ +package com.earth2me.essentials.commands; + +import com.earth2me.essentials.Trade; +import com.earth2me.essentials.User; +import com.earth2me.essentials.craftbukkit.InventoryWorkaround; +import com.earth2me.essentials.utils.NumberUtil; +import com.google.common.collect.Lists; + +import org.apache.commons.lang.math.NumberUtils; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.inventory.ItemStack; + +import static com.earth2me.essentials.I18n.tl; + +import java.math.BigDecimal; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.logging.Level; + +public class Commandbuy extends EssentialsCommand { + public Commandbuy() { + super("buy"); + } + + @Override + public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { + if (args.length < 1) { + throw new NotEnoughArgumentsException(); + } + + int amountToGive; + + if (args[1] != null && NumberUtil.isInt(args[1])) { + amountToGive = NumberUtils.toInt(args[1]); + } else if (args[1] == null) { + amountToGive = 1; + } else { + throw new Exception("The second argument must be an integer."); + } + + if (amountToGive <= 0) { + throw new Exception("You cannot buy 0 items."); + } + + ItemStack is = ess.getItemDb().get(args[0]); + is.setAmount(amountToGive); + BigDecimal worthSingleItem = ess.getWorth().getBuyPrice(ess, is); + BigDecimal worth = worthSingleItem.multiply(BigDecimal.valueOf(amountToGive)); + + if (worth == null) { + throw new Exception(tl("itemCannotBeSold")); + } + + if (worth.compareTo(user.getMoney()) == -1) { + boolean isDropItemsIfFull = ess.getSettings().isDropItemsIfFull(); + BigDecimal leftoverValue = BigDecimal.ZERO; + Map leftovers; + + if (user.isAuthorized("essentials.oversizedstacks")) { + leftovers = InventoryWorkaround.addOversizedItems(user.getBase().getInventory(), ess.getSettings().getOversizedStackSize(), is); + } else { + leftovers = InventoryWorkaround.addItems(user.getBase().getInventory(), is); + } + + if (isDropItemsIfFull) { + for (ItemStack item : leftovers.values()) { + World w = user.getWorld(); + w.dropItemNaturally(user.getLocation(), item); + } + } else if(!leftovers.values().isEmpty()) { + for (ItemStack item : leftovers.values()) { + leftoverValue = leftoverValue.add(worthSingleItem.multiply(BigDecimal.valueOf(item.getAmount()))); + } + + user.sendMessage("Not enough inventory space. Refunding $."); + } + + user.takeMoney(worth.subtract(leftoverValue)); + user.getBase().updateInventory(); + } else { + throw new Exception("You do not have enough money."); + } + } + + @Override + protected List getTabCompleteOptions(Server server, User user, String commandLabel, String[] args) { + if (args.length == 1) { + return getItems(); + } else if (args.length == 2) { + return Lists.newArrayList("1", "64"); + } else { + return Collections.emptyList(); + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java index e6c41a98b4f..2f70dfea43a 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java @@ -69,7 +69,7 @@ public void run(final Server server, final User user, final String commandLabel, private BigDecimal sellItem(User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception { int amount = ess.getWorth().getAmount(ess, user, is, args, isBulkSell); - BigDecimal worth = ess.getWorth().getPrice(ess, is); + BigDecimal worth = ess.getWorth().getSellPrice(ess, is); if (worth == null) { throw new Exception(tl("itemCannotBeSold")); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandworth.java b/Essentials/src/com/earth2me/essentials/commands/Commandworth.java index 31b55be5544..492d4136f17 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandworth.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandworth.java @@ -84,7 +84,7 @@ private BigDecimal itemWorth(CommandSource sender, User user, ItemStack is, Stri amount = ess.getWorth().getAmount(ess, user, is, args, true); } - BigDecimal worth = ess.getWorth().getPrice(ess, is); + BigDecimal worth = ess.getWorth().getSellPrice(ess, is); if (worth == null) { throw new Exception(tl("itemCannotBeSold")); diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index 094e93b7383..4ddfd72bfb0 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -215,6 +215,7 @@ player-commands: - balance.others - balancetop - build + - buy - chat.color - chat.format - chat.shout diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index 26add785aef..496ec478dba 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -65,6 +65,9 @@ commands: description: Set a player on fire. usage: / aliases: [eburn] + buy: + description: Buy the specified item. + usage: / <|> [-][amount] clearinventory: description: Clear all items in your inventory. usage: / [player|*] [item[:]|*|**] [amount] diff --git a/Essentials/src/worth.yml b/Essentials/src/worth.yml index 7d273e45dac..02a14823839 100644 --- a/Essentials/src/worth.yml +++ b/Essentials/src/worth.yml @@ -184,3 +184,10 @@ worth: ironboots: 22.0 step: 1.5 sugar: 10.0 + +# Override prices from the section above for buying and selling specifically. +buy: + diamond: 2000 + +sell: + diamond: 20