-
-
Notifications
You must be signed in to change notification settings - Fork 824
/
PropertiesConfiguration.java
296 lines (272 loc) · 10.4 KB
/
PropertiesConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// $Id$
package com.sk89q.worldedit.util;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.util.report.Unreported;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
/**
* Simple LocalConfiguration that loads settings using
* {@code java.util.Properties}.
*/
public class PropertiesConfiguration extends LocalConfiguration {
@Unreported private static final Logger LOGGER = LogManagerCompat.getLogger();
@Unreported protected Properties properties;
@Unreported protected File path;
/**
* Construct the object. The configuration isn't loaded yet.
*
* @param path the path to the configuration
*/
public PropertiesConfiguration(Path path) {
this.path = path.toFile();
properties = new Properties();
}
/**
* Construct the object. The configuration isn't loaded yet.
*
* @param path the path to the configuration
* @deprecated Use {@link PropertiesConfiguration#PropertiesConfiguration(Path)}
*/
@Deprecated
public PropertiesConfiguration(File path) {
this(path.toPath());
}
@Override
public void load() {
try (InputStream stream = new FileInputStream(path)) {
properties.load(stream);
} catch (FileNotFoundException ignored) {
} catch (IOException e) {
LOGGER.warn("Failed to read configuration", e);
}
loadExtra();
profile = getBool("profile", profile);
traceUnflushedSessions = getBool("trace-unflushed-sessions", traceUnflushedSessions);
disallowedBlocks = getStringSet("disallowed-blocks", getDefaultDisallowedBlocks());
defaultChangeLimit = getInt("default-max-changed-blocks", defaultChangeLimit);
maxChangeLimit = getInt("max-changed-blocks", maxChangeLimit);
defaultVerticalHeight = getInt("default-vertical-height", defaultVerticalHeight);
defaultMaxPolygonalPoints = getInt("default-max-polygon-points", defaultMaxPolygonalPoints);
maxPolygonalPoints = getInt("max-polygon-points", maxPolygonalPoints);
defaultMaxPolyhedronPoints = getInt("default-max-polyhedron-points", defaultMaxPolyhedronPoints);
maxPolyhedronPoints = getInt("max-polyhedron-points", maxPolyhedronPoints);
shellSaveType = getString("shell-save-type", shellSaveType);
maxRadius = getInt("max-radius", maxRadius);
maxSuperPickaxeSize = getInt("max-super-pickaxe-size", maxSuperPickaxeSize);
maxBrushRadius = getInt("max-brush-radius", maxBrushRadius);
logCommands = getBool("log-commands", logCommands);
logFile = getString("log-file", logFile);
logFormat = getString("log-format", logFormat);
registerHelp = getBool("register-help", registerHelp);
wandItem = getString("wand-item", wandItem).toLowerCase(Locale.ROOT);
try {
wandItem = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(wandItem)).getId();
} catch (Throwable ignored) {
}
superPickaxeDrop = getBool("super-pickaxe-drop-items", superPickaxeDrop);
superPickaxeManyDrop = getBool("super-pickaxe-many-drop-items", superPickaxeManyDrop);
useInventory = getBool("use-inventory", useInventory);
useInventoryOverride = getBool("use-inventory-override", useInventoryOverride);
useInventoryCreativeOverride = getBool("use-inventory-creative-override", useInventoryCreativeOverride);
navigationWand = getString("nav-wand-item", navigationWand).toLowerCase(Locale.ROOT);
try {
navigationWand = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(navigationWand)).getId();
} catch (Throwable ignored) {
}
navigationWandMaxDistance = getInt("nav-wand-distance", navigationWandMaxDistance);
navigationUseGlass = getBool("nav-use-glass", navigationUseGlass);
scriptTimeout = getInt("scripting-timeout", scriptTimeout);
calculationTimeout = getInt("calculation-timeout", calculationTimeout);
maxCalculationTimeout = getInt("max-calculation-timeout", maxCalculationTimeout);
saveDir = getString("schematic-save-dir", saveDir);
scriptsDir = getString("craftscript-dir", scriptsDir);
butcherDefaultRadius = getInt("butcher-default-radius", butcherDefaultRadius);
butcherMaxRadius = getInt("butcher-max-radius", butcherMaxRadius);
allowSymlinks = getBool("allow-symbolic-links", allowSymlinks);
serverSideCUI = getBool("server-side-cui", serverSideCUI);
extendedYLimit = getBool("extended-y-limit", extendedYLimit);
setDefaultLocaleName(getString("default-locale", defaultLocaleName));
commandBlockSupport = getBool("command-block-support", commandBlockSupport);
LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15));
String snapshotsDir = getString("snapshots-dir", "");
boolean experimentalSnapshots = getBool("snapshots-experimental", false);
initializeSnapshotConfiguration(snapshotsDir, experimentalSnapshots);
path.getParentFile().mkdirs();
try (OutputStream output = new FileOutputStream(path)) {
properties.store(output, "Don't put comments; they get removed");
} catch (IOException e) {
LOGGER.warn("Failed to write configuration", e);
}
}
/**
* Called to load extra configuration.
*/
protected void loadExtra() {
}
/**
* Get a string value.
*
* @param key the key
* @param def the default value
* @return the value
*/
protected String getString(String key, String def) {
if (def == null) {
def = "";
}
String val = properties.getProperty(key);
if (val == null) {
properties.setProperty(key, def);
return def;
} else {
return val;
}
}
/**
* Get a boolean value.
*
* @param key the key
* @param def the default value
* @return the value
*/
protected boolean getBool(String key, boolean def) {
String val = properties.getProperty(key);
if (val == null) {
properties.setProperty(key, def ? "true" : "false");
return def;
} else {
return val.equalsIgnoreCase("true")
|| val.equals("1");
}
}
/**
* Get an integer value.
*
* @param key the key
* @param def the default value
* @return the value
*/
protected int getInt(String key, int def) {
String val = properties.getProperty(key);
if (val == null) {
properties.setProperty(key, String.valueOf(def));
return def;
} else {
try {
return Integer.parseInt(val);
} catch (NumberFormatException e) {
properties.setProperty(key, String.valueOf(def));
return def;
}
}
}
/**
* Get a double value.
*
* @param key the key
* @param def the default value
* @return the value
*/
protected double getDouble(String key, double def) {
String val = properties.getProperty(key);
if (val == null) {
properties.setProperty(key, String.valueOf(def));
return def;
} else {
try {
return Double.parseDouble(val);
} catch (NumberFormatException e) {
properties.setProperty(key, String.valueOf(def));
return def;
}
}
}
/**
* Get a double value.
*
* @param key the key
* @param def the default value
* @return the value
*/
protected Set<Integer> getIntSet(String key, int[] def) {
String val = properties.getProperty(key);
if (val == null) {
properties.setProperty(key, StringUtil.joinString(def, ",", 0));
Set<Integer> set = new HashSet<>();
for (int i : def) {
set.add(i);
}
return set;
} else {
Set<Integer> set = new HashSet<>();
String[] parts = val.split(",");
for (String part : parts) {
try {
int v = Integer.parseInt(part.trim());
set.add(v);
} catch (NumberFormatException ignored) {
}
}
return set;
}
}
/**
* Get a String set.
*
* @param key the key
* @param def the default value
* @return the value
*/
protected Set<String> getStringSet(String key, String[] def) {
String val = properties.getProperty(key);
if (val == null) {
properties.setProperty(key, StringUtil.joinString(def, ",", 0));
return new HashSet<>(Arrays.asList(def));
} else {
Set<String> set = new HashSet<>();
String[] parts = val.split(",");
for (String part : parts) {
try {
String v = part.trim();
set.add(v);
} catch (NumberFormatException ignored) {
}
}
return set;
}
}
}