--- a/net/minecraft/server/dedicated/Settings.java +++ b/net/minecraft/server/dedicated/Settings.java @@ -20,20 +20,40 @@ import java.util.function.Supplier; import java.util.function.UnaryOperator; import javax.annotation.Nullable; -import net.minecraft.core.RegistryAccess; import org.slf4j.Logger; +import joptsimple.OptionSet; // CraftBukkit +import net.minecraft.core.RegistryAccess; + public abstract class Settings> { private static final Logger LOGGER = LogUtils.getLogger(); public final Properties properties; + // CraftBukkit start + private OptionSet options = null; - public Settings(Properties properties) { + public Settings(Properties properties, final OptionSet options) { this.properties = properties; + + this.options = options; } + private String getOverride(String name, String value) { + if ((this.options != null) && (this.options.has(name))) { + return String.valueOf(this.options.valueOf(name)); + } + + return value; + // CraftBukkit end + } + public static Properties loadFromFile(Path path) { try { + // CraftBukkit start - SPIGOT-7465, MC-264979: Don't load if file doesn't exist + if (!path.toFile().exists()) { + return new Properties(); + } + // CraftBukkit end Properties properties; Properties properties1; @@ -97,6 +117,12 @@ public void store(Path path) { try { + // CraftBukkit start - Don't attempt writing to file if it's read only + if (path.toFile().exists() && !path.toFile().canWrite()) { + Settings.LOGGER.warn("Can not write to file {}, skipping.", path); // Paper - log message file is read-only + return; + } + // CraftBukkit end BufferedWriter bufferedwriter = Files.newBufferedWriter(path, StandardCharsets.UTF_8); try { @@ -125,7 +151,7 @@ private static Function wrapNumberDeserializer(Function parser) { return (s) -> { try { - return (Number) parser.apply(s); + return (V) parser.apply(s); // CraftBukkit - decompile error } catch (NumberFormatException numberformatexception) { return null; } @@ -144,7 +170,7 @@ @Nullable public String getStringRaw(String key) { - return (String) this.properties.get(key); + return (String) this.getOverride(key, this.properties.getProperty(key)); // CraftBukkit } @Nullable @@ -160,10 +186,20 @@ } protected V get(String key, Function parser, Function stringifier, V fallback) { - String s1 = this.getStringRaw(key); - V v1 = MoreObjects.firstNonNull(s1 != null ? parser.apply(s1) : null, fallback); + // CraftBukkit start + try { + return this.get0(key, parser, stringifier, fallback); + } catch (Exception ex) { + throw new RuntimeException("Could not load invalidly configured property '" + key + "'", ex); + } + } - this.properties.put(key, stringifier.apply(v1)); + private V get0(String s, Function function, Function function1, V v0) { + // CraftBukkit end + String s1 = this.getStringRaw(s); + V v1 = MoreObjects.firstNonNull(s1 != null ? function.apply(s1) : null, v0); + + this.properties.put(s, function1.apply(v1)); return v1; } @@ -172,7 +208,7 @@ V v1 = MoreObjects.firstNonNull(s1 != null ? parser.apply(s1) : null, fallback); this.properties.put(key, stringifier.apply(v1)); - return new Settings.MutableValue<>(key, v1, stringifier); + return new Settings.MutableValue(key, v1, stringifier); // CraftBukkit - decompile error } protected V get(String key, Function parser, UnaryOperator parsedTransformer, Function stringifier, V fallback) { @@ -236,7 +272,7 @@ return properties; } - protected abstract T reload(RegistryAccess registryManager, Properties properties); + protected abstract T reload(RegistryAccess iregistrycustom, Properties properties, OptionSet optionset); // CraftBukkit public class MutableValue implements Supplier { @@ -244,7 +280,7 @@ private final V value; private final Function serializer; - MutableValue(final String s, final Object object, final Function function) { + MutableValue(final String s, final V object, final Function function) { // CraftBukkit - decompile error this.key = s; this.value = object; this.serializer = function; @@ -258,7 +294,7 @@ Properties properties = Settings.this.cloneProperties(); properties.put(this.key, this.serializer.apply(value)); - return Settings.this.reload(registryManager, properties); + return Settings.this.reload(registryManager, properties, Settings.this.options); // CraftBukkit } } }