summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/Kits.java
blob: 24d7cdf5f356e89d011d9bb4f875a9ee67536086 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package net.ess3;

import java.io.File;
import java.io.IOException;
import java.util.*;
import static net.ess3.I18n._;
import net.ess3.api.IEssentials;
import net.ess3.api.IKits;
import net.ess3.api.IUser;
import net.ess3.commands.NoChargeException;
import net.ess3.settings.Kit;
import net.ess3.storage.AsyncStorageObjectHolder;
import net.ess3.user.UserData.TimestampType;
import net.ess3.utils.DateUtil;
import org.bukkit.inventory.ItemStack;


public class Kits extends AsyncStorageObjectHolder<net.ess3.settings.Kits> implements IKits
{
	public Kits(final IEssentials ess)
	{
		super(ess, net.ess3.settings.Kits.class);
		onReload();
	}

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

	@Override
	public Kit getKit(String kitName) throws Exception
	{
		acquireReadLock();
		try
		{
			if (getData().getKits() == null || kitName == null
				|| !getData().getKits().containsKey(kitName.toLowerCase(Locale.ENGLISH)))
			{
				throw new Exception(_("kitError2"));
			}
			final Kit kit = getData().getKits().get(kitName.toLowerCase(Locale.ENGLISH));
			if (kit == null)
			{
				throw new Exception(_("kitError2"));
			}
			return kit;
		}
		finally
		{
			unlock();
		}
	}

	@Override
	public void sendKit(IUser user, String kitName) throws Exception
	{
		final Kit kit = getKit(kitName);
		sendKit(user, kit);
	}

	@Override
	public void sendKit(IUser user, Kit kit) throws Exception
	{
		final List<ItemStack> itemList = kit.getItems();		
		user.giveItems(itemList, true);					
	}

	@Override
	public Collection<String> getList() throws Exception
	{
		acquireReadLock();
		try
		{
			if (getData().getKits() == null)
			{
				return Collections.emptyList();
			}
			return new ArrayList<String>(getData().getKits().keySet());
		}
		finally
		{
			unlock();
		}
	}

	@Override
	public boolean isEmpty()
	{
		return getData().getKits().isEmpty();
	}

	@Override
	public void finishRead()
	{
		
	}

	@Override
	public void finishWrite()
	{
		
	}

	@Override
	public void checkTime(IUser user, Kit kit) throws NoChargeException
	{
		final Calendar time = new GregorianCalendar();
		// Take the current time, and remove the delay from it.
		final double delay = kit.getDelay();
		final Calendar earliestTime = new GregorianCalendar();
		earliestTime.add(Calendar.SECOND, -(int)delay);
		earliestTime.add(Calendar.MILLISECOND, -(int)((delay * 1000.0) % 1000.0));

		// This value contains the most recent time a kit could have been used that would allow another use.

		final long earliestLong = earliestTime.getTimeInMillis();

		// When was the last kit used?
		final Long lastTime = user.getTimestamp(TimestampType.KIT);
		if (lastTime == null || lastTime < earliestLong)
		{
			user.setTimestamp(TimestampType.KIT, time.getTimeInMillis());
		}
		else if (lastTime > time.getTimeInMillis())
		{
			// This is to make sure time didn't get messed up on last kit use.
			// If this happens, let's give the user the benifit of the doubt.
			user.setTimestamp(TimestampType.KIT, time.getTimeInMillis());
		}
		else
		{
			time.setTimeInMillis(lastTime);
			time.add(Calendar.SECOND, (int)delay);
			time.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0));
			user.sendMessage(_("kitTimed", DateUtil.formatDateDiff(time.getTimeInMillis())));
			throw new NoChargeException();
		}
	}
}