summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/Warps.java
blob: 57326b54a40d351539f5a2d8688e2478bcfabdd9 (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
package com.earth2me.essentials;

import static com.earth2me.essentials.I18n._;
import java.io.File;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Server;


public class Warps implements IConf
{
	private static final Logger logger = Logger.getLogger("Minecraft");
	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();
	}

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

	public Collection<String> getWarpNames()
	{
		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;
	}

	public Location getWarp(String warp) throws Exception
	{
		EssentialsConf conf = warpPoints.get(new StringIgnoreCase(warp));
		if (conf == null)
		{
			throw new Exception(_("warpNotExist"));
		}
		return conf.getLocation(null, server);
	}

	public void setWarp(String name, Location loc) throws Exception
	{
		String filename = Util.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(_("similarWarpExist"));
			}
			conf = new EssentialsConf(confFile);
			warpPoints.put(new StringIgnoreCase(name), conf);
		}
		conf.setProperty(null, loc);
		conf.setProperty("name", name);
		conf.save();
	}

	public void delWarp(String name) throws Exception
	{
		EssentialsConf conf = warpPoints.get(new StringIgnoreCase(name));
		if (conf == null)
		{
			throw new Exception(_("warpNotExist"));
		}
		if (!conf.getFile().delete())
		{
			throw new Exception(_("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, _("loadWarpError", filename), ex);
					}
				}
			}
		}
	}


	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;
		}
	}
}