summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/economy/WorthHolder.java
blob: fbd20360c79d19917177bc241fa045f1c0d63b25 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package net.ess3.economy;

import net.ess3.api.server.ItemStack;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.ess3.api.IEssentials;
import net.ess3.api.IWorth;
import net.ess3.storage.AsyncStorageObjectHolder;
import net.ess3.storage.EnchantmentLevel;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.material.MaterialData;


public class WorthHolder extends AsyncStorageObjectHolder<net.ess3.economy.Worth> implements IWorth
{

	@Override
	public void finishRead()
	{
		
	}

	@Override
	public void finishWrite()
	{
		
	}
	
	
	public WorthHolder(final IEssentials ess)
	{
		super(ess, net.ess3.economy.Worth.class);
		onReload(false);
	}

	@Override
	public double getPrice(final ItemStack itemStack)
	{
		this.acquireReadLock();
		try
		{
			final Map<MaterialData, Double> prices = this.getData().getSell();
			if (prices == null || itemStack == null)
			{
				return Double.NaN;
			}
			final Double basePrice = prices.get(itemStack.getData());
			if (basePrice == null || Double.isNaN(basePrice))
			{
				return Double.NaN;
			}
			double multiplier = 1.0;
			if (itemStack.getType().getMaxDurability() > 0) {
				multiplier *= (double)itemStack.getDurability() / (double)itemStack.getType().getMaxDurability();
			}
			if (itemStack.getEnchantments() != null && !itemStack.getEnchantments().isEmpty())
			{
				final Map<EnchantmentLevel, Double> enchantmentMultipliers = this.getData().getEnchantmentMultiplier();
				if (enchantmentMultipliers != null)
				{
					for (Map.Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet())
					{
						final Double enchMult = enchantmentMultipliers.get(new EnchantmentLevel(entry.getKey(), entry.getValue()));
						if (enchMult != null)
						{
							multiplier *= enchMult;
						}
					}
				}
			}
			return basePrice * multiplier;
		}
		finally
		{
			this.unlock();
		}
	}

	@Override
	public void setPrice(final ItemStack itemStack, final double price)
	{
		acquireWriteLock();
		try {
			if (getData().getSell() == null) {
				getData().setSell(new HashMap<MaterialData, Double>());
			}
			getData().getSell().put(itemStack.getData(), price);
		} finally {
			unlock();
		}
	}

	@Override
	public File getStorageFile() throws IOException
	{
		return new File(ess.getPlugin().getDataFolder(), "worth.yml");
	}
}