summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/Worth.java
blob: ee894fe3cb6d34674976076181082b13548bfaaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.earth2me.essentials;

import java.io.File;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.logging.Logger;
import org.bukkit.inventory.ItemStack;


public class Worth implements IConf
{
	private static final Logger logger = Logger.getLogger("Minecraft");
	private final EssentialsConf config;

	public Worth(File dataFolder)
	{
		config = new EssentialsConf(new File(dataFolder, "worth.yml"));
		config.setTemplateName("/worth.yml");
		config.load();
	}

	public BigDecimal getPrice(ItemStack itemStack)
	{
		String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
		double result;
		result = config.getDouble("worth." + itemname + "." + itemStack.getDurability(), Double.NaN);
		if (Double.isNaN(result))
		{
			result = config.getDouble("worth." + itemname + ".0", Double.NaN);
		}
		if (Double.isNaN(result))
		{
			result = config.getDouble("worth." + itemname, Double.NaN);
		}
		if (Double.isNaN(result))
		{
			result = config.getDouble("worth-" + itemStack.getTypeId(), Double.NaN);
		}
		if (Double.isNaN(result))
		{
			return null;
		}
		return BigDecimal.valueOf(result);
	}

	public void setPrice(ItemStack itemStack, double price)
	{
		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);
		}
		config.removeProperty("worth-" + itemStack.getTypeId());
		config.save();
	}

	@Override
	public void reloadConfig()
	{
		config.load();
	}
}