/mcp/src/minecraft_server/net/minecraft/src/PropertyManager.java

https://bitbucket.org/vkorolik/twoleanfour-client · Java · 137 lines · 96 code · 17 blank · 24 comment · 2 complexity · 7dcf1417d03d6482223f4f9a93812fa2 MD5 · raw file

  1. package net.minecraft.src;
  2. import java.io.*;
  3. import java.util.Properties;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. public class PropertyManager
  7. {
  8. /** Reference to the logger. */
  9. public static Logger logger = Logger.getLogger("Minecraft");
  10. /** The server properties object. */
  11. private Properties serverProperties;
  12. /** The server properties file. */
  13. private File serverPropertiesFile;
  14. public PropertyManager(File par1File)
  15. {
  16. serverProperties = new Properties();
  17. serverPropertiesFile = par1File;
  18. if (par1File.exists())
  19. {
  20. try
  21. {
  22. serverProperties.load(new FileInputStream(par1File));
  23. }
  24. catch (Exception exception)
  25. {
  26. logger.log(Level.WARNING, (new StringBuilder()).append("Failed to load ").append(par1File).toString(), exception);
  27. generateNewProperties();
  28. }
  29. }
  30. else
  31. {
  32. logger.log(Level.WARNING, (new StringBuilder()).append(par1File).append(" does not exist").toString());
  33. generateNewProperties();
  34. }
  35. }
  36. /**
  37. * Generates a new properties file.
  38. */
  39. public void generateNewProperties()
  40. {
  41. logger.log(Level.INFO, "Generating new properties file");
  42. saveProperties();
  43. }
  44. /**
  45. * Writes the properties to the properties file.
  46. */
  47. public void saveProperties()
  48. {
  49. try
  50. {
  51. serverProperties.store(new FileOutputStream(serverPropertiesFile), "Minecraft server properties");
  52. }
  53. catch (Exception exception)
  54. {
  55. logger.log(Level.WARNING, (new StringBuilder()).append("Failed to save ").append(serverPropertiesFile).toString(), exception);
  56. generateNewProperties();
  57. }
  58. }
  59. /**
  60. * Returns this PropertyManager's file object used for property saving.
  61. */
  62. public File getPropertiesFile()
  63. {
  64. return serverPropertiesFile;
  65. }
  66. /**
  67. * Returns a string property. If the property doesn't exist the default is returned.
  68. */
  69. public String getStringProperty(String par1Str, String par2Str)
  70. {
  71. if (!serverProperties.containsKey(par1Str))
  72. {
  73. serverProperties.setProperty(par1Str, par2Str);
  74. saveProperties();
  75. }
  76. return serverProperties.getProperty(par1Str, par2Str);
  77. }
  78. /**
  79. * Returns an integer property. If the property doesn't exist the default is returned.
  80. */
  81. public int getIntProperty(String par1Str, int par2)
  82. {
  83. try
  84. {
  85. return Integer.parseInt(getStringProperty(par1Str, (new StringBuilder()).append("").append(par2).toString()));
  86. }
  87. catch (Exception exception)
  88. {
  89. serverProperties.setProperty(par1Str, (new StringBuilder()).append("").append(par2).toString());
  90. }
  91. return par2;
  92. }
  93. /**
  94. * Returns a boolean property. If the property doesn't exist the default is returned.
  95. */
  96. public boolean getBooleanProperty(String par1Str, boolean par2)
  97. {
  98. try
  99. {
  100. return Boolean.parseBoolean(getStringProperty(par1Str, (new StringBuilder()).append("").append(par2).toString()));
  101. }
  102. catch (Exception exception)
  103. {
  104. serverProperties.setProperty(par1Str, (new StringBuilder()).append("").append(par2).toString());
  105. }
  106. return par2;
  107. }
  108. /**
  109. * Saves an Object with the given property name
  110. */
  111. public void setProperty(String par1Str, Object par2Obj)
  112. {
  113. serverProperties.setProperty(par1Str, (new StringBuilder()).append("").append(par2Obj).toString());
  114. }
  115. public void setProperty(String par1Str, boolean par2)
  116. {
  117. serverProperties.setProperty(par1Str, (new StringBuilder()).append("").append(par2).toString());
  118. saveProperties();
  119. }
  120. }