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