2011-04-16 00:42:43 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2013-07-14 00:07:59 +00:00
|
|
|
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
|
2018-10-15 12:56:36 +00:00
|
|
|
import com.earth2me.essentials.utils.EnumUtil;
|
2013-07-14 00:03:08 +00:00
|
|
|
import org.bukkit.Material;
|
2013-12-21 03:28:18 +00:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2011-04-16 06:28:56 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-04-16 00:42:43 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
2011-04-16 00:42:43 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class Worth implements IConf {
|
|
|
|
private final EssentialsConf config;
|
|
|
|
|
|
|
|
public Worth(File dataFolder) {
|
|
|
|
config = new EssentialsConf(new File(dataFolder, "worth.yml"));
|
|
|
|
config.setTemplateName("/worth.yml");
|
|
|
|
config.load();
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:06:25 +00:00
|
|
|
public BigDecimal getPrice(IEssentials essentials, ItemStack itemStack) {
|
2015-04-15 04:06:16 +00:00
|
|
|
BigDecimal result;
|
2017-12-13 06:06:25 +00:00
|
|
|
|
|
|
|
String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2018-10-12 16:26:41 +00:00
|
|
|
// Check for matches with item name
|
2015-04-15 04:06:16 +00:00
|
|
|
result = config.getBigDecimal("worth." + itemname + "." + itemStack.getDurability(), BigDecimal.ONE.negate());
|
|
|
|
if (result.signum() < 0) {
|
|
|
|
final ConfigurationSection itemNameMatch = config.getConfigurationSection("worth." + itemname);
|
|
|
|
if (itemNameMatch != null && itemNameMatch.getKeys(false).size() == 1) {
|
|
|
|
result = config.getBigDecimal("worth." + itemname + ".0", BigDecimal.ONE.negate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result.signum() < 0) {
|
|
|
|
result = config.getBigDecimal("worth." + itemname + ".*", BigDecimal.ONE.negate());
|
|
|
|
}
|
|
|
|
if (result.signum() < 0) {
|
|
|
|
result = config.getBigDecimal("worth." + itemname, BigDecimal.ONE.negate());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.signum() < 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getAmount(IEssentials ess, User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception {
|
|
|
|
if (is == null || is.getType() == Material.AIR) {
|
|
|
|
throw new Exception(tl("itemSellAir"));
|
|
|
|
}
|
2017-12-13 06:06:25 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
int amount = 0;
|
|
|
|
|
|
|
|
if (args.length > 1) {
|
|
|
|
try {
|
|
|
|
amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
|
|
|
|
} catch (NumberFormatException ex) {
|
|
|
|
throw new NotEnoughArgumentsException(ex);
|
|
|
|
}
|
|
|
|
if (args[1].startsWith("-")) {
|
|
|
|
amount = -amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean stack = args.length > 1 && args[1].endsWith("s");
|
2018-10-15 12:56:36 +00:00
|
|
|
boolean requireStack = ess.getSettings().isTradeInStacks(is.getType());
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
if (requireStack && !stack) {
|
|
|
|
throw new Exception(tl("itemMustBeStacked"));
|
|
|
|
}
|
|
|
|
|
|
|
|
int max = 0;
|
|
|
|
for (ItemStack s : user.getBase().getInventory().getContents()) {
|
|
|
|
if (s == null || !s.isSimilar(is)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
max += s.getAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack) {
|
|
|
|
amount *= is.getType().getMaxStackSize();
|
|
|
|
}
|
|
|
|
if (amount < 1) {
|
|
|
|
amount += max;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requireStack) {
|
|
|
|
amount -= amount % is.getType().getMaxStackSize();
|
|
|
|
}
|
|
|
|
if (amount > max || amount < 1) {
|
|
|
|
if (!isBulkSell) {
|
|
|
|
user.sendMessage(tl("itemNotEnough2"));
|
|
|
|
user.sendMessage(tl("itemNotEnough3"));
|
|
|
|
throw new Exception(tl("itemNotEnough1"));
|
|
|
|
} else {
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2017-12-13 06:06:25 +00:00
|
|
|
public void setPrice(IEssentials ess, ItemStack itemStack, double price) {
|
2015-04-15 04:06:16 +00:00
|
|
|
if (itemStack.getType().getData() == null) {
|
|
|
|
config.setProperty("worth." + itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), price);
|
|
|
|
} else {
|
|
|
|
// Bukkit-bug: getDurability still contains the correct value, while getData().getData() is 0.
|
|
|
|
config.setProperty("worth." + itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "") + "." + itemStack.getDurability(), price);
|
|
|
|
}
|
2018-10-15 12:56:36 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void reloadConfig() {
|
|
|
|
config.load();
|
|
|
|
}
|
2011-04-16 00:42:43 +00:00
|
|
|
}
|