summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/Warps.java
blob: 3e0f93a55aa4ec23bbe6f7fc77dd92f9c122a1f2 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.earth2me.essentials;

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.commands.WarpNotFoundException;
import com.earth2me.essentials.utils.StringUtil;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.ess3.api.InvalidNameException;
import net.ess3.api.InvalidWorldException;
import org.bukkit.Location;
import org.bukkit.Server;


public class Warps implements IConf, net.ess3.api.IWarps
{
	private static final Logger logger = Logger.getLogger("Essentials");
	private final Map<StringIgnoreCase, EssentialsConf> warpPoints = new HashMap<StringIgnoreCase, EssentialsConf>();
	private final File warpsFolder;
	private final Server server;

	public Warps(Server server, File dataFolder)
	{
		this.server = server;
		warpsFolder = new File(dataFolder, "warps");
		if (!warpsFolder.exists())
		{
			warpsFolder.mkdirs();
		}
		reloadConfig();
	}

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

	@Override
	public Collection<String> getList()
	{
		final List<String> keys = new ArrayList<String>();
		for (StringIgnoreCase stringIgnoreCase : warpPoints.keySet())
		{
			keys.add(stringIgnoreCase.getString());
		}
		Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);
		return keys;
	}

	@Override
	public Location getWarp(String warp) throws WarpNotFoundException, InvalidWorldException
	{
		EssentialsConf conf = warpPoints.get(new StringIgnoreCase(warp));
		if (conf == null)
		{
			throw new WarpNotFoundException();
		}
		return conf.getLocation(null, server);
	}

	@Override
	public void setWarp(String name, Location loc) throws Exception
	{
		String filename = StringUtil.sanitizeFileName(name);
		EssentialsConf conf = warpPoints.get(new StringIgnoreCase(name));
		if (conf == null)
		{
			File confFile = new File(warpsFolder, filename + ".yml");
			if (confFile.exists())
			{
				throw new Exception(tl("similarWarpExist"));
			}
			conf = new EssentialsConf(confFile);
			warpPoints.put(new StringIgnoreCase(name), conf);
		}
		conf.setProperty(null, loc);
		conf.setProperty("name", name);
		try
		{
			conf.saveWithError();
		}
		catch (IOException ex)
		{
			throw new IOException(tl("invalidWarpName"));
		}
	}

	@Override
	public void removeWarp(String name) throws Exception
	{
		EssentialsConf conf = warpPoints.get(new StringIgnoreCase(name));
		if (conf == null)
		{
			throw new Exception(tl("warpNotExist"));
		}
		if (!conf.getFile().delete())
		{
			throw new Exception(tl("warpDeleteError"));
		}
		warpPoints.remove(new StringIgnoreCase(name));
	}

	@Override
	public final void reloadConfig()
	{
		warpPoints.clear();
		File[] listOfFiles = warpsFolder.listFiles();
		if (listOfFiles.length >= 1)
		{
			for (int i = 0; i < listOfFiles.length; i++)
			{
				String filename = listOfFiles[i].getName();
				if (listOfFiles[i].isFile() && filename.endsWith(".yml"))
				{
					try
					{
						EssentialsConf conf = new EssentialsConf(listOfFiles[i]);
						conf.load();
						String name = conf.getString("name");
						if (name != null)
						{
							warpPoints.put(new StringIgnoreCase(name), conf);
						}
					}
					catch (Exception ex)
					{
						logger.log(Level.WARNING, tl("loadWarpError", filename), ex);
					}
				}
			}
		}
	}

	//This is here for future 3.x api support. Not implemented here becasue storage is handled differently
	@Override
	public File getWarpFile(String name) throws InvalidNameException
	{
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public int getCount()
	{
		return getList().size();
	}

	private static class StringIgnoreCase
	{
		private final String string;

		public StringIgnoreCase(String string)
		{
			this.string = string;
		}

		@Override
		public int hashCode()
		{
			return getString().toLowerCase(Locale.ENGLISH).hashCode();
		}

		@Override
		public boolean equals(Object o)
		{
			if (o instanceof StringIgnoreCase)
			{
				return getString().equalsIgnoreCase(((StringIgnoreCase)o).getString());
			}
			return false;
		}

		public String getString()
		{
			return string;
		}
	}
}