Settings .java

net.minecraft.server.dedicated.Settings (class): members: dispatchNumberOrString, getLegacyBoolean, cloneProperties, getLegacyString, intDeserializer, key.

net.minecraft.server.dedicatedclass211 linesserverextends Settings<T>>7.1 KB

API Surface (18)

Properties loadFromFile(final Path file)
void store(final Path output)
<V> V get(final String key, final Function<String, @Nullable V> deserializer, final Function<V, String> serializer, final V defaultValue)
<V> Settings<T>.MutableValue<V> getMutable(final String key, final Function<String, @Nullable V> deserializer, final Function<V, String> serializer, final V defaultValue)
<V> V get(final String key, final Function<String, @Nullable V> deserializer, final UnaryOperator<V> validator, final Function<V, String> serializer, final V defaultValue)
<V> V get(final String key, final Function<String, V> deserializer, final V defaultValue)
<V> Settings<T>.MutableValue<V> getMutable(final String key, final Function<String, V> deserializer, final V defaultValue)
String get(final String key, final String defaultValue)
int get(final String key, final int defaultValue)
Settings<T>.MutableValue<Integer> getMutable(final String key, final int defaultValue)
Settings<T>.MutableValue<String> getMutable(final String key, final String defaultValue)
int get(final String key, final UnaryOperator<Integer> validator, final int defaultValue)
long get(final String key, final long defaultValue)
boolean get(final String key, final boolean defaultValue)
Settings<T>.MutableValue<Boolean> getMutable(final String key, final boolean defaultValue)
Properties cloneProperties()
V get()
T update(final RegistryAccess registryAccess, final V value)

Fields & Constants (7)

LOGGER
properties
intDeserializer
serializer
key
deserializer
validator

String literals (1)

"Minecraft server properties"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Settings.java211 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.dedicated;
2
3
import com.google.common.base.MoreObjects;
4
import com.mojang.logging.LogUtils;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.InputStreamReader;
8
import java.io.Reader;
9
import java.io.Writer;
10
import java.nio.charset.CharacterCodingException;
11
import java.nio.charset.CharsetDecoder;
12
import java.nio.charset.CodingErrorAction;
13
import java.nio.charset.StandardCharsets;
14
import java.nio.file.Files;
15
import java.nio.file.Path;
16
import java.util.Objects;
17
import java.util.Properties;
18
import java.util.function.Function;
19
import java.util.function.IntFunction;
20
import java.util.function.Supplier;
21
import java.util.function.UnaryOperator;
22
import net.minecraft.core.RegistryAccess;
23
import org.jspecify.annotations.Nullable;
24
import org.slf4j.Logger;
25
26
public abstract class Settings<T extends Settings<T>> {
27
	private static final Logger LOGGER = LogUtils.getLogger();
28
	protected final Properties properties;
29
30
	public Settings(final Properties properties) {
31
		this.properties = properties;
32
	}
33
34
	public static Properties loadFromFile(final Path file) {
35
		try {
36
			try (InputStream is = Files.newInputStream(file)) {
37
				CharsetDecoder reportingUtf8Decoder = StandardCharsets.UTF_8
38
					.newDecoder()
39
					.onMalformedInput(CodingErrorAction.REPORT)
40
					.onUnmappableCharacter(CodingErrorAction.REPORT);
41
				Properties properties = new Properties();
42
				properties.load(new InputStreamReader(is, reportingUtf8Decoder));
43
				return properties;
44
			} catch (CharacterCodingException e) {
45
				LOGGER.info("Failed to load properties as UTF-8 from file {}, trying ISO_8859_1", file);
46
47
				try (Reader reader = Files.newBufferedReader(file, StandardCharsets.ISO_8859_1)) {
48
					Properties properties = new Properties();
49
					properties.load(reader);
50
					return properties;
51
				}
52
			}
53
		} catch (IOException e) {
54
			LOGGER.error("Failed to load properties from file: {}", file, e);
55
			return new Properties();
56
		}
57
	}
58
59
	public void store(final Path output) {
60
		try (Writer os = Files.newBufferedWriter(output, StandardCharsets.UTF_8)) {
61
			this.properties.store(os, "Minecraft server properties");
62
		} catch (IOException e) {
63
			LOGGER.error("Failed to store properties to file: {}", output);
64
		}
65
	}
66
67
	private static <V extends Number> Function<String, @Nullable V> wrapNumberDeserializer(final Function<String, V> inner) {
68
		return s -> {
69
			try {
70
				return inner.apply(s);
71
			} catch (NumberFormatException e) {
72
				return null;
73
			}
74
		};
75
	}
76
77
	protected static <V> Function<String, @Nullable V> dispatchNumberOrString(
78
		final IntFunction<@Nullable V> intDeserializer, final Function<String, @Nullable V> stringDeserializer
79
	) {
80
		return s -> {
81
			try {
82
				return intDeserializer.apply(Integer.parseInt(s));
83
			} catch (NumberFormatException e) {
84
				return stringDeserializer.apply(s);
85
			}
86
		};
87
	}
88
89
	private @Nullable String getStringRaw(final String key) {
90
		return (String)this.properties.get(key);
91
	}
92
93
	protected <V> @Nullable V getLegacy(final String key, final Function<String, V> deserializer) {
94
		String value = this.getStringRaw(key);
95
		if (value == null) {
96
			return null;
97
		}
98
99
		this.properties.remove(key);
100
		return deserializer.apply(value);
101
	}
102
103
	protected <V> V get(final String key, final Function<String, @Nullable V> deserializer, final Function<V, String> serializer, final V defaultValue) {
104
		String value = this.getStringRaw(key);
105
		V result = MoreObjects.firstNonNull(value != null ? deserializer.apply(value) : null, defaultValue);
106
		this.properties.put(key, serializer.apply(result));
107
		return result;
108
	}
109
110
	protected <V> Settings<T>.MutableValue<V> getMutable(
111
		final String key, final Function<String, @Nullable V> deserializer, final Function<V, String> serializer, final V defaultValue
112
	) {
113
		String value = this.getStringRaw(key);
114
		V result = MoreObjects.firstNonNull(value != null ? deserializer.apply(value) : null, defaultValue);
115
		this.properties.put(key, serializer.apply(result));
116
		return new Settings.MutableValue<>(key, result, serializer);
117
	}
118
119
	protected <V> V get(
120
		final String key,
121
		final Function<String, @Nullable V> deserializer,
122
		final UnaryOperator<V> validator,
123
		final Function<V, String> serializer,
124
		final V defaultValue
125
	) {
126
		return this.get(key, s -> {
127
			V result = deserializer.apply(s);
128
			return result != null ? validator.apply(result) : null;
129
		}, serializer, defaultValue);
130
	}
131
132
	protected <V> V get(final String key, final Function<String, V> deserializer, final V defaultValue) {
133
		return this.get(key, deserializer, Objects::toString, defaultValue);
134
	}
135
136
	protected <V> Settings<T>.MutableValue<V> getMutable(final String key, final Function<String, V> deserializer, final V defaultValue) {
137
		return this.getMutable(key, deserializer, Objects::toString, defaultValue);
138
	}
139
140
	protected String get(final String key, final String defaultValue) {
141
		return this.get(key, Function.identity(), Function.identity(), defaultValue);
142
	}
143
144
	protected @Nullable String getLegacyString(final String key) {
145
		return this.getLegacy(key, Function.identity());
146
	}
147
148
	protected int get(final String key, final int defaultValue) {
149
		return this.get(key, wrapNumberDeserializer(Integer::parseInt), Integer.valueOf(defaultValue));
150
	}
151
152
	protected Settings<T>.MutableValue<Integer> getMutable(final String key, final int defaultValue) {
153
		return this.getMutable(key, wrapNumberDeserializer(Integer::parseInt), defaultValue);
154
	}
155
156
	protected Settings<T>.MutableValue<String> getMutable(final String key, final String defaultValue) {
157
		return this.getMutable(key, String::new, defaultValue);
158
	}
159
160
	protected int get(final String key, final UnaryOperator<Integer> validator, final int defaultValue) {
161
		return this.get(key, wrapNumberDeserializer(Integer::parseInt), validator, Objects::toString, defaultValue);
162
	}
163
164
	protected long get(final String key, final long defaultValue) {
165
		return this.get(key, wrapNumberDeserializer(Long::parseLong), defaultValue);
166
	}
167
168
	protected boolean get(final String key, final boolean defaultValue) {
169
		return this.get(key, Boolean::valueOf, defaultValue);
170
	}
171
172
	protected Settings<T>.MutableValue<Boolean> getMutable(final String key, final boolean defaultValue) {
173
		return this.getMutable(key, Boolean::valueOf, defaultValue);
174
	}
175
176
	protected @Nullable Boolean getLegacyBoolean(final String key) {
177
		return this.getLegacy(key, Boolean::valueOf);
178
	}
179
180
	protected Properties cloneProperties() {
181
		Properties result = new Properties();
182
		result.putAll(this.properties);
183
		return result;
184
	}
185
186
	protected abstract T reload(final RegistryAccess registryAccess, final Properties properties);
187
188
	public class MutableValue<V> implements Supplier<V> {
189
		private final String key;
190
		private final V value;
191
		private final Function<V, String> serializer;
192
193
		private MutableValue(final String key, final V value, final Function<V, String> serializer) {
194
			this.key = key;
195
			this.value = value;
196
			this.serializer = serializer;
197
		}
198
199
		@Override
200
		public V get() {
201
			return this.value;
202
		}
203
204
		public T update(final RegistryAccess registryAccess, final V value) {
205
			Properties properties = Settings.this.cloneProperties();
206
			properties.put(this.key, this.serializer.apply(value));
207
			return Settings.this.reload(registryAccess, properties);
208
		}
209
	}
210
}
211

Decompiled Minecraft 26.3 source of Settings (net/minecraft/server/dedicated/Settings.java) with Mojang official mappings, stored verbatim on the site.