summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/ItemDb.java
blob: ad43db3121fa0b38466f407274bf919511baa7fb (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package net.ess3;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import static net.ess3.I18n._;
import net.ess3.api.IEssentials;
import net.ess3.api.IItemDb;
import net.ess3.api.ISettings;
import net.ess3.api.IUser;
import net.ess3.permissions.Permissions;
import net.ess3.storage.ManagedFile;
import net.ess3.utils.Util;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;


public class ItemDb implements IItemDb
{
	private final IEssentials ess;

	public ItemDb(final IEssentials ess)
	{
		this.ess = ess;
		file = new ManagedFile("items.csv", ess);
	}

	private final Map<String, Long> items = new HashMap<String, Long>();
	private final ManagedFile file;
	private static final Pattern SPLIT = Pattern.compile("[^a-zA-Z0-9]");

	@Override
	public void onReload()
	{
		final List<String> lines = file.getLines();

		if (lines.isEmpty())
		{
			return;
		}

		items.clear();

		for (String line : lines)
		{
			line = line.trim();
			if (line.length() > 0 && line.charAt(0) == '#')
			{
				continue;
			}

			final String[] parts = SPLIT.split(line);
			if (parts.length < 2)
			{
				continue;
			}

			final long numeric = Integer.parseInt(parts[1]);

			final long durability = parts.length > 2 && !(parts[2].length() == 1 && parts[2].charAt(0) == '0') ? Short.parseShort(parts[2]) : 0;
			items.put(parts[0].toLowerCase(Locale.ENGLISH), numeric | (durability << 32));
		}
	}

	@Override
	public ItemStack get(final String id, final IUser user) throws Exception
	{
		final ItemStack stack = get(id.toLowerCase(Locale.ENGLISH));

		ISettings settings = ess.getSettings();

		final int defaultStackSize = settings.getData().getGeneral().getDefaultStacksize();

		if (defaultStackSize > 0)
		{
			stack.setAmount(defaultStackSize);
		}
		else
		{
			final int oversizedStackSize = settings.getData().getGeneral().getOversizedStacksize();
			if (oversizedStackSize > 0 && Permissions.OVERSIZEDSTACKS.isAuthorized(user))
			{
				stack.setAmount(oversizedStackSize);
			}
		}
		return stack;
	}

	@Override
	public ItemStack get(final String id, final int quantity) throws Exception
	{
		final ItemStack retval = get(id.toLowerCase(Locale.ENGLISH));
		retval.setAmount(quantity);
		return retval;
	}

	private final Pattern idMatch = Pattern.compile("^\\d+[:+',;.]\\d+$");
	private final Pattern metaSplit = Pattern.compile("[:+',;.]");
	private final Pattern conjoined = Pattern.compile("^[^:+',;.]+[:+',;.]\\d+$");

	@Override
	public ItemStack get(final String id) throws Exception
	{
		int itemid = 0;
		String itemname = null;
		short metaData = 0;
		if (idMatch.matcher(id).matches())
		{
			String[] split = metaSplit.split(id);
			itemid = Integer.parseInt(split[0]);
			metaData = Short.parseShort(split[1]);
		}
		else if (Util.isInt(id))
		{
			itemid = Integer.parseInt(id);
		}
		else if (conjoined.matcher(id).matches())
		{
			String[] split = metaSplit.split(id);
			itemname = split[0].toLowerCase(Locale.ENGLISH);
			metaData = Short.parseShort(split[1]);
		}
		else
		{
			itemname = id.toLowerCase(Locale.ENGLISH);
		}

		if (itemname != null)
		{
			if (items.containsKey(itemname))
			{
				long item = items.get(itemname);
				itemid = (int)(item & 0xffffffffL);
				if (metaData == 0)
				{
					metaData = (short)((item >> 32) & 0xffffL);
				}
			}
			else if (Material.matchMaterial(itemname) != null)
			{
				itemid = Material.matchMaterial(itemname).getId();
				metaData = 0;
			}
			else
			{
				throw new Exception(_("§4Unknown item name: {0}.", id));
			}
		}

		final Material mat = Material.getMaterial(itemid);
		if (mat == null)
		{
			throw new Exception(_("§4Unknown item id:§r {0}§4.", itemid));
		}
		final ItemStack retval = new ItemStack(mat, mat.getMaxStackSize(), metaData);
		return retval;
	}
}