package net.minecraft.server.dedicated;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@SideOnly(Side.SERVER)
public class PropertyManager
{
	private static final Logger field_164440_a = LogManager.getLogger();
	private final Properties serverProperties = new Properties();
	private final File serverPropertiesFile;
	private static final String __OBFID = "CL_00001782";

	public PropertyManager(File p_i45278_1_)
	{
		this.serverPropertiesFile = p_i45278_1_;

		if (p_i45278_1_.exists())
		{
			FileInputStream fileinputstream = null;

			try
			{
				fileinputstream = new FileInputStream(p_i45278_1_);
				this.serverProperties.load(fileinputstream);
			}
			catch (Exception exception)
			{
				field_164440_a.warn("Failed to load " + p_i45278_1_, exception);
				this.generateNewProperties();
			}
			finally
			{
				if (fileinputstream != null)
				{
					try
					{
						fileinputstream.close();
					}
					catch (IOException ioexception)
					{
						;
					}
				}
			}
		}
		else
		{
			field_164440_a.warn(p_i45278_1_ + " does not exist");
			this.generateNewProperties();
		}
	}

	public void generateNewProperties()
	{
		field_164440_a.info("Generating new properties file");
		this.saveProperties();
	}

	public void saveProperties()
	{
		FileOutputStream fileoutputstream = null;

		try
		{
			fileoutputstream = new FileOutputStream(this.serverPropertiesFile);
			this.serverProperties.store(fileoutputstream, "Minecraft server properties");
		}
		catch (Exception exception)
		{
			field_164440_a.warn("Failed to save " + this.serverPropertiesFile, exception);
			this.generateNewProperties();
		}
		finally
		{
			if (fileoutputstream != null)
			{
				try
				{
					fileoutputstream.close();
				}
				catch (IOException ioexception)
				{
					;
				}
			}
		}
	}

	public File getPropertiesFile()
	{
		return this.serverPropertiesFile;
	}

	public String getStringProperty(String par1Str, String par2Str)
	{
		if (!this.serverProperties.containsKey(par1Str))
		{
			this.serverProperties.setProperty(par1Str, par2Str);
			this.saveProperties();
			this.saveProperties();
		}

		return this.serverProperties.getProperty(par1Str, par2Str);
	}

	public int getIntProperty(String par1Str, int par2)
	{
		try
		{
			return Integer.parseInt(this.getStringProperty(par1Str, "" + par2));
		}
		catch (Exception exception)
		{
			this.serverProperties.setProperty(par1Str, "" + par2);
			this.saveProperties();
			return par2;
		}
	}

	public boolean getBooleanProperty(String par1Str, boolean par2)
	{
		try
		{
			return Boolean.parseBoolean(this.getStringProperty(par1Str, "" + par2));
		}
		catch (Exception exception)
		{
			this.serverProperties.setProperty(par1Str, "" + par2);
			this.saveProperties();
			return par2;
		}
	}

	public void setProperty(String par1Str, Object par2Obj)
	{
		this.serverProperties.setProperty(par1Str, "" + par2Obj);
	}
}