001package net.minecraft.server.dedicated;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.util.Properties;
008import net.minecraft.logging.ILogAgent;
009
010public class PropertyManager
011{
012    private final Properties properties = new Properties();
013
014    /** Reference to the logger. */
015    private final ILogAgent logger;
016    private final File associatedFile;
017
018    public PropertyManager(File par1File, ILogAgent par2ILogAgent)
019    {
020        this.associatedFile = par1File;
021        this.logger = par2ILogAgent;
022
023        if (par1File.exists())
024        {
025            FileInputStream fileinputstream = null;
026
027            try
028            {
029                fileinputstream = new FileInputStream(par1File);
030                this.properties.load(fileinputstream);
031            }
032            catch (Exception exception)
033            {
034                par2ILogAgent.func_98235_b("Failed to load " + par1File, exception);
035                this.logMessageAndSave();
036            }
037            finally
038            {
039                if (fileinputstream != null)
040                {
041                    try
042                    {
043                        fileinputstream.close();
044                    }
045                    catch (IOException ioexception)
046                    {
047                        ;
048                    }
049                }
050            }
051        }
052        else
053        {
054            par2ILogAgent.logWarning(par1File + " does not exist");
055            this.logMessageAndSave();
056        }
057    }
058
059    /**
060     * logs an info message then calls saveSettingsToFile Yes this appears to be a potential stack overflow - these 2
061     * functions call each other repeatdly if an exception occurs.
062     */
063    public void logMessageAndSave()
064    {
065        this.logger.logInfo("Generating new properties file");
066        this.saveProperties();
067    }
068
069    /**
070     * Writes the properties to the properties file.
071     */
072    public void saveProperties()
073    {
074        FileOutputStream fileoutputstream = null;
075
076        try
077        {
078            fileoutputstream = new FileOutputStream(this.associatedFile);
079            this.properties.store(fileoutputstream, "Minecraft server properties");
080        }
081        catch (Exception exception)
082        {
083            this.logger.func_98235_b("Failed to save " + this.associatedFile, exception);
084            this.logMessageAndSave();
085        }
086        finally
087        {
088            if (fileoutputstream != null)
089            {
090                try
091                {
092                    fileoutputstream.close();
093                }
094                catch (IOException ioexception)
095                {
096                    ;
097                }
098            }
099        }
100    }
101
102    /**
103     * Returns this PropertyManager's file object used for property saving.
104     */
105    public File getPropertiesFile()
106    {
107        return this.associatedFile;
108    }
109
110    /**
111     * Gets a property. If it does not exist, set it to the specified value.
112     */
113    public String getProperty(String par1Str, String par2Str)
114    {
115        if (!this.properties.containsKey(par1Str))
116        {
117            this.properties.setProperty(par1Str, par2Str);
118            this.saveProperties();
119        }
120
121        return this.properties.getProperty(par1Str, par2Str);
122    }
123
124    /**
125     * Gets an integer property. If it does not exist, set it to the specified value.
126     */
127    public int getIntProperty(String par1Str, int par2)
128    {
129        try
130        {
131            return Integer.parseInt(this.getProperty(par1Str, "" + par2));
132        }
133        catch (Exception exception)
134        {
135            this.properties.setProperty(par1Str, "" + par2);
136            return par2;
137        }
138    }
139
140    /**
141     * Gets a boolean property. If it does not exist, set it to the specified value.
142     */
143    public boolean getBooleanProperty(String par1Str, boolean par2)
144    {
145        try
146        {
147            return Boolean.parseBoolean(this.getProperty(par1Str, "" + par2));
148        }
149        catch (Exception exception)
150        {
151            this.properties.setProperty(par1Str, "" + par2);
152            return par2;
153        }
154    }
155
156    /**
157     * Saves an Object with the given property name.
158     */
159    public void setProperty(String par1Str, Object par2Obj)
160    {
161        this.properties.setProperty(par1Str, "" + par2Obj);
162    }
163}