summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/Kits.java
blob: 6f6d3e2c647fa8aa9d5a355547daf576e4b13a5c (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
package net.ess3;

import java.io.File;
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, new File(ess.getPlugin().getDataFolder(), "kits.yml"));
		onReload();
	}

	@Override
	public Kit getKit(String kitName) throws Exception
	{
		if (getData().getKits() == null || kitName == null || !getData().getKits().containsKey(kitName.toLowerCase(Locale.ENGLISH)))
		{
			throw new Exception(_("§4That kit does not exist or is improperly defined."));
		}
		final Kit kit = getData().getKits().get(kitName.toLowerCase(Locale.ENGLISH));
		if (kit == null)
		{
			throw new Exception(_("§4That kit does not exist or is improperly defined."));
		}
		return kit;
	}

	@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
	{
		if (getData().getKits() == null)
		{
			return Collections.emptyList();
		}
		return new ArrayList<String>(getData().getKits().keySet());
	}

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

	@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 benefit 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(_("§4You can''t use that kit again for another§c {0}§4.", DateUtil.formatDateDiff(time.getTimeInMillis())));
			throw new NoChargeException();
		}
	}
}