3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-25 15:50:06 +01:00
Paper/src/main/java/net/minecraft/server/PropertyManager.java

129 Zeilen
3.9 KiB
Java

package net.minecraft.server;
2011-01-29 22:50:29 +01:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
2012-07-29 09:33:13 +02:00
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import joptsimple.OptionSet; // CraftBukkit
public class PropertyManager {
2013-03-13 23:33:27 +01:00
public final Properties properties = new Properties(); // CraftBukkit - private -> public
private final IConsoleLogManager loggingAgent;
private final File c;
2013-03-13 23:33:27 +01:00
public PropertyManager(File file1, IConsoleLogManager iconsolelogmanager) {
2011-01-29 22:50:29 +01:00
this.c = file1;
2013-03-13 23:33:27 +01:00
this.loggingAgent = iconsolelogmanager;
2011-01-29 22:50:29 +01:00
if (file1.exists()) {
2012-07-29 09:33:13 +02:00
FileInputStream fileinputstream = null;
try {
2012-07-29 09:33:13 +02:00
fileinputstream = new FileInputStream(file1);
this.properties.load(fileinputstream);
} catch (Exception exception) {
2013-03-13 23:33:27 +01:00
iconsolelogmanager.warning("Failed to load " + file1, exception);
2011-01-29 22:50:29 +01:00
this.a();
2012-07-29 09:33:13 +02:00
} finally {
if (fileinputstream != null) {
try {
fileinputstream.close();
} catch (IOException ioexception) {
;
}
}
}
} else {
2013-03-13 23:33:27 +01:00
iconsolelogmanager.warning(file1 + " does not exist");
2011-01-29 22:50:29 +01:00
this.a();
}
}
2011-01-29 22:50:29 +01:00
// CraftBukkit start
private OptionSet options = null;
2013-03-13 23:33:27 +01:00
public PropertyManager(final OptionSet options, IConsoleLogManager iconsolelogmanager) {
this((File) options.valueOf("config"), iconsolelogmanager);
this.options = options;
}
private <T> T getOverride(String name, T value) {
if ((this.options != null) && (this.options.has(name))) {
return (T) this.options.valueOf(name);
}
return value;
}
2011-01-29 22:50:29 +01:00
// CraftBukkit end
public void a() {
2013-03-13 23:33:27 +01:00
this.loggingAgent.info("Generating new properties file");
this.savePropertiesFile();
}
public void savePropertiesFile() {
2012-07-29 09:33:13 +02:00
FileOutputStream fileoutputstream = null;
try {
// CraftBukkit start - Don't attempt writing to file if it's read only
if (this.c.exists() && !this.c.canWrite()) {
return;
}
// CraftBukkit end
2012-07-29 09:33:13 +02:00
fileoutputstream = new FileOutputStream(this.c);
this.properties.store(fileoutputstream, "Minecraft server properties");
} catch (Exception exception) {
2013-03-13 23:33:27 +01:00
this.loggingAgent.warning("Failed to save " + this.c, exception);
2011-01-29 22:50:29 +01:00
this.a();
2012-07-29 09:33:13 +02:00
} finally {
if (fileoutputstream != null) {
try {
fileoutputstream.close();
} catch (IOException ioexception) {
;
}
}
}
}
2011-11-20 09:01:14 +01:00
public File c() {
return this.c;
}
public String getString(String s, String s1) {
if (!this.properties.containsKey(s)) {
this.properties.setProperty(s, s1);
this.savePropertiesFile();
}
2011-01-29 22:50:29 +01:00
return this.getOverride(s, this.properties.getProperty(s, s1)); // CraftBukkit
}
public int getInt(String s, int i) {
try {
return this.getOverride(s, Integer.parseInt(this.getString(s, "" + i))); // CraftBukkit
} catch (Exception exception) {
this.properties.setProperty(s, "" + i);
return this.getOverride(s, i); // CraftBukkit
}
}
public boolean getBoolean(String s, boolean flag) {
try {
return this.getOverride(s, Boolean.parseBoolean(this.getString(s, "" + flag))); // CraftBukkit
} catch (Exception exception) {
this.properties.setProperty(s, "" + flag);
return this.getOverride(s, flag); // CraftBukkit
}
}
2011-02-23 03:37:56 +01:00
2011-11-20 09:01:14 +01:00
public void a(String s, Object object) {
this.properties.setProperty(s, "" + object);
}
}