forked from HelpChat/ChatChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigFactory.java
More file actions
124 lines (102 loc) · 4.92 KB
/
ConfigFactory.java
File metadata and controls
124 lines (102 loc) · 4.92 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
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
package at.helpch.chatchat.config;
import at.helpch.chatchat.ChatChatPlugin;
import at.helpch.chatchat.api.format.Format;
import at.helpch.chatchat.api.format.PriorityFormat;
import at.helpch.chatchat.api.holder.GlobalFormatsHolder;
import at.helpch.chatchat.config.holder.AddonsHolder;
import at.helpch.chatchat.config.holder.ChannelsHolder;
import at.helpch.chatchat.config.holder.ExtensionsHolder;
import at.helpch.chatchat.config.holder.GlobalFormatsHolderImpl;
import at.helpch.chatchat.config.holder.MessagesHolder;
import at.helpch.chatchat.config.holder.MiniPlaceholdersHolder;
import at.helpch.chatchat.config.holder.SettingsHolder;
import at.helpch.chatchat.config.mapper.AddonsMapper;
import at.helpch.chatchat.config.mapper.SimpleFormatMapper;
import at.helpch.chatchat.config.mapper.ChannelMapMapper;
import at.helpch.chatchat.config.mapper.MiniMessageComponentMapper;
import at.helpch.chatchat.config.mapper.MiniPlaceholderMapper;
import at.helpch.chatchat.config.mapper.PriorityFormatMapper;
import at.helpch.chatchat.format.SimpleFormat;
import at.helpch.chatchat.format.ChatFormat;
import at.helpch.chatchat.placeholder.MiniPlaceholderImpl;
import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.serializer.configurate4.ConfigurateComponentSerializer;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.yaml.NodeStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
public final class ConfigFactory {
private @NotNull final Path dataFolder;
private @NotNull final ChatChatPlugin plugin;
public ConfigFactory(@NotNull final Path dataFolder, @NotNull final ChatChatPlugin plugin) {
this.dataFolder = dataFolder;
this.plugin = plugin;
}
public @NotNull ChannelsHolder channels() {
final var config = create(ChannelsHolder.class, "channels.yml");
return Objects.requireNonNullElseGet(config, ChannelsHolder::new);
}
public @NotNull GlobalFormatsHolder formats() {
final var config = create(GlobalFormatsHolderImpl.class, "formats.yml");
return Objects.requireNonNullElseGet(config, GlobalFormatsHolderImpl::new);
}
public @NotNull SettingsHolder settings() {
final var config = create(SettingsHolder.class, "settings.yml");
return Objects.requireNonNullElseGet(config, SettingsHolder::new);
}
public @NotNull MessagesHolder messages() {
final var config = create(MessagesHolder.class, "messages.yml");
return Objects.requireNonNullElseGet(config, MessagesHolder::new);
}
public @NotNull ExtensionsHolder extensions() {
final var config = create(ExtensionsHolder.class, "extensions.yml");
return Objects.requireNonNullElseGet(config, ExtensionsHolder::new);
}
public @NotNull MiniPlaceholdersHolder miniPlaceholders() {
final var config = create(MiniPlaceholdersHolder.class, "placeholders.yml");
return Objects.requireNonNullElseGet(config, MiniPlaceholdersHolder::new);
}
private @Nullable <T> T create(@NotNull final Class<T> clazz, @NotNull final String fileName) {
try {
if (!Files.exists(dataFolder)) {
Files.createDirectories(dataFolder);
}
final var path = dataFolder.resolve(fileName);
final var loader = loader(path);
final var node = loader.load();
final var config = node.get(clazz);
if (!Files.exists(path)) {
Files.createFile(path);
node.set(clazz, config);
loader.save(node);
}
return config;
} catch (final IOException exception) {
exception.printStackTrace();
}
return null;
}
private @NotNull YamlConfigurationLoader loader(@NotNull final Path path) {
return YamlConfigurationLoader.builder()
.path(path)
.defaultOptions(options -> options.shouldCopyDefaults(true)
.header("https://wiki.helpch.at")
.serializers(build -> build
.register(Component.class, new MiniMessageComponentMapper())
.register(SimpleFormat.class, new SimpleFormatMapper())
.register(PriorityFormat.class, new PriorityFormatMapper())
.register(ChatFormat.class, new PriorityFormatMapper())
.register(MiniPlaceholderImpl.class, new MiniPlaceholderMapper())
.register(AddonsHolder.class, new AddonsMapper())
.register(new TypeToken<>() {}, new ChannelMapMapper(plugin))
.registerAll(ConfigurateComponentSerializer.configurate().serializers())))
.nodeStyle(NodeStyle.BLOCK)
.indent(2)
.build();
}
}