PageRenderTime 48ms CodeModel.GetById 35ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Essentials/src/com/earth2me/essentials/EssentialsConf.java

https://github.com/BodyshotzVG/Essentials
Java | 277 lines | 255 code | 19 blank | 3 comment | 42 complexity | 154686e42785af9ec6909c030f394521 MD5 | raw file
  1package com.earth2me.essentials;
  2
  3import java.io.File;
  4import java.io.FileInputStream;
  5import java.io.FileNotFoundException;
  6import java.io.FileOutputStream;
  7import java.io.IOException;
  8import java.io.InputStream;
  9import java.io.OutputStream;
 10import java.util.HashMap;
 11import java.util.Map;
 12import java.util.logging.Level;
 13import java.util.logging.Logger;
 14import org.bukkit.Location;
 15import org.bukkit.Material;
 16import org.bukkit.Server;
 17import org.bukkit.World;
 18import org.bukkit.inventory.ItemStack;
 19import org.bukkit.util.config.Configuration;
 20
 21
 22public class EssentialsConf extends Configuration
 23{
 24	private static final Logger LOGGER = Logger.getLogger("Minecraft");
 25	private transient File configFile;
 26	private transient String templateName = null;
 27	private transient Class<?> resourceClass = EssentialsConf.class;
 28
 29	public EssentialsConf(final File configFile)
 30	{
 31		super(configFile);
 32		this.configFile = configFile;
 33		if (this.root == null)
 34		{
 35			this.root = new HashMap<String, Object>();
 36		}
 37	}
 38
 39	@Override
 40	public void load()
 41	{
 42		configFile = configFile.getAbsoluteFile();
 43		if (!configFile.getParentFile().exists())
 44		{
 45			if (!configFile.getParentFile().mkdirs())
 46			{
 47				LOGGER.log(Level.SEVERE, Util.format("failedToCreateConfig", configFile.toString()));
 48			}
 49		}
 50		// This will delete files where the first character is 0. In most cases they are broken.
 51		if (configFile.exists() && configFile.length() != 0)
 52		{
 53			try
 54			{
 55				final InputStream input = new FileInputStream(configFile);
 56				try
 57				{
 58					if (input.read() == 0)
 59					{
 60						input.close();
 61						configFile.delete();
 62					}
 63				}
 64				catch (IOException ex)
 65				{
 66					LOGGER.log(Level.SEVERE, null, ex);
 67				}
 68				finally
 69				{
 70					try
 71					{
 72						input.close();
 73					}
 74					catch (IOException ex)
 75					{
 76						LOGGER.log(Level.SEVERE, null, ex);
 77					}
 78				}
 79			}
 80			catch (FileNotFoundException ex)
 81			{
 82				LOGGER.log(Level.SEVERE, null, ex);
 83			}
 84		}
 85
 86		if (!configFile.exists())
 87		{
 88			if (templateName != null)
 89			{
 90				LOGGER.log(Level.INFO, Util.format("creatingConfigFromTemplate", configFile.toString()));
 91				createFromTemplate();
 92			}
 93			else
 94			{
 95				try
 96				{
 97					LOGGER.log(Level.INFO, Util.format("creatingEmptyConfig", configFile.toString()));
 98					if (!configFile.createNewFile())
 99					{
100						LOGGER.log(Level.SEVERE, Util.format("failedToCreateConfig", configFile.toString()));
101					}
102				}
103				catch (IOException ex)
104				{
105					LOGGER.log(Level.SEVERE, Util.format("failedToCreateConfig", configFile.toString()), ex);
106				}
107			}
108		}
109
110		try
111		{
112			super.load();
113		}
114		catch (RuntimeException e)
115		{
116			LOGGER.log(Level.INFO, "File: " + configFile.toString());
117			throw e;
118		}
119
120		if (this.root == null)
121		{
122			this.root = new HashMap<String, Object>();
123		}
124	}
125
126	private void createFromTemplate()
127	{
128		InputStream istr = null;
129		OutputStream ostr = null;
130		try
131		{
132			istr = resourceClass.getResourceAsStream(templateName);
133			if (istr == null)
134			{
135				LOGGER.log(Level.SEVERE, Util.format("couldNotFindTemplate", templateName));
136				return;
137			}
138			ostr = new FileOutputStream(configFile);
139			byte[] buffer = new byte[1024];
140			int length = 0;
141			length = istr.read(buffer);
142			while (length > 0)
143			{
144				ostr.write(buffer, 0, length);
145				length = istr.read(buffer);
146			}
147		}
148		catch (IOException ex)
149		{
150			LOGGER.log(Level.SEVERE, Util.format("failedToWriteConfig", configFile.toString()), ex);
151			return;
152		}
153		finally
154		{
155			try
156			{
157				if (istr != null)
158				{
159					istr.close();
160				}
161			}
162			catch (IOException ex)
163			{
164				Logger.getLogger(EssentialsConf.class.getName()).log(Level.SEVERE, null, ex);
165			}
166			try
167			{
168				if (ostr != null)
169				{
170					ostr.close();
171				}
172			}
173			catch (IOException ex)
174			{
175				LOGGER.log(Level.SEVERE, Util.format("failedToCloseConfig", configFile.toString()), ex);
176			}
177		}
178	}
179
180	public void setTemplateName(final String templateName)
181	{
182		this.templateName = templateName;
183	}
184
185	public File getFile()
186	{
187		return configFile;
188	}
189
190	public void setTemplateName(final String templateName, final Class<?> resClass)
191	{
192		this.templateName = templateName;
193		this.resourceClass = resClass;
194	}
195
196	public boolean hasProperty(final String path)
197	{
198		return getProperty(path) != null;
199	}
200
201	public Location getLocation(final String path, final Server server) throws Exception
202	{
203		final String worldName = getString((path == null ? "" : path + ".") + "world");
204		if (worldName == null || worldName.isEmpty())
205		{
206			return null;
207		}
208		final World world = server.getWorld(worldName);
209		if (world == null)
210		{
211			throw new Exception(Util.i18n("invalidWorld"));
212		}
213		return new Location(world,
214							getDouble((path == null ? "" : path + ".") + "x", 0),
215							getDouble((path == null ? "" : path + ".") + "y", 0),
216							getDouble((path == null ? "" : path + ".") + "z", 0),
217							(float)getDouble((path == null ? "" : path + ".") + "yaw", 0),
218							(float)getDouble((path == null ? "" : path + ".") + "pitch", 0));
219	}
220
221	public void setProperty(final String path, final Location loc)
222	{
223		setProperty((path == null ? "" : path + ".") + "world", loc.getWorld().getName());
224		setProperty((path == null ? "" : path + ".") + "x", loc.getX());
225		setProperty((path == null ? "" : path + ".") + "y", loc.getY());
226		setProperty((path == null ? "" : path + ".") + "z", loc.getZ());
227		setProperty((path == null ? "" : path + ".") + "yaw", loc.getYaw());
228		setProperty((path == null ? "" : path + ".") + "pitch", loc.getPitch());
229	}
230
231	public ItemStack getItemStack(final String path)
232	{
233		return new ItemStack(
234				Material.valueOf(getString(path + ".type", "AIR")),
235				getInt(path + ".amount", 1),
236				(short)getInt(path + ".damage", 0)/*,
237				(byte)getInt(path + ".data", 0)*/);
238	}
239
240	public void setProperty(final String path, final ItemStack stack)
241	{
242		final Map<String, Object> map = new HashMap<String, Object>();
243		map.put("type", stack.getType().toString());
244		map.put("amount", stack.getAmount());
245		map.put("damage", stack.getDurability());
246		// getData().getData() is broken
247		//map.put("data", stack.getDurability());
248		setProperty(path, map);
249	}
250
251	public long getLong(final String path, final long def)
252	{
253		try
254		{
255			final Number num = (Number)getProperty(path);
256			return num == null ? def : num.longValue();
257		}
258		catch (ClassCastException ex)
259		{
260			return def;
261		}
262	}
263
264	@Override
265	public double getDouble(final String path, final double def)
266	{
267		try
268		{
269			Number num = (Number)getProperty(path);
270			return num == null ? def : num.doubleValue();
271		}
272		catch (ClassCastException ex)
273		{
274			return def;
275		}
276	}
277}