-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathServerModuleConfiguration.java
More file actions
75 lines (69 loc) · 2.08 KB
/
Copy pathServerModuleConfiguration.java
File metadata and controls
75 lines (69 loc) · 2.08 KB
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
package dev.plex.api.impl;
import dev.plex.api.config.ModuleConfiguration;
import dev.plex.config.ConfigDefaultsMerger;
import dev.plex.module.PlexModule;
import dev.plex.util.PlexLog;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import org.bukkit.configuration.InvalidConfigurationException;
final class ServerModuleConfiguration extends ModuleConfiguration
{
private final PlexModule module;
private final File file;
private final String from;
private final String to;
ServerModuleConfiguration(PlexModule module, String from, String to)
{
this.module = module;
this.file = new File(module.getDataFolder(), to);
this.from = from;
this.to = to;
if (!file.exists()) saveDefault();
}
@Override
public void load()
{
try
{
ConfigDefaultsMerger.Result result = ConfigDefaultsMerger.merge(file, module.getResource(from), to);
if (!result.addedKeys().isEmpty())
{
PlexLog.log("Merged default key(s) into " + to + ": " + String.join(", ", result.addedKeys()));
}
options().parseComments(true);
super.load(file);
}
catch (IOException | InvalidConfigurationException ex)
{
ex.printStackTrace();
}
}
@Override
public void save()
{
try { super.save(file); } catch (IOException ex) { ex.printStackTrace(); }
}
private void saveDefault()
{
try
{
File parent = file.getParentFile();
if (parent != null) parent.mkdirs();
try (InputStream stream = module.getResource(from))
{
if (stream == null)
{
PlexLog.warn("Unable to save default module config " + to + ": missing resource " + from);
return;
}
Files.copy(stream, file.toPath());
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}