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