-
Notifications
You must be signed in to change notification settings - Fork 0
/
IConfig.java
160 lines (127 loc) · 5.66 KB
/
IConfig.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
package fi.natroutter.natlibs.config;
import fi.natroutter.natlibs.handlers.guibuilder.Rows;
import fi.natroutter.natlibs.utilities.Colors;
import fi.natroutter.natlibs.utilities.Utilities;
import lombok.SneakyThrows;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.*;
import org.bukkit.block.BlockFace;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
public interface IConfig {
MiniMessage mm = MiniMessage.miniMessage();
LegacyComponentSerializer lcs = LegacyComponentSerializer.legacyAmpersand();
HashMap<String, SimpleYml> saved = new HashMap<>();
String getPath();
File getDataFolder();
private String identifier() {
return getDataFolder().getName().toLowerCase() + "-" + fileName();
}
default String fileName() {
return getClass().getSimpleName().toLowerCase();
}
default File file() {
return new File(getDataFolder(), fileName() + ".yml");
}
default void reloadFile() {
if (resourceLocation() != null) {
File file = new File(getDataFolder(), resourceLocation());
SimpleYml simpleYml = new SimpleYml(getClass(), file, resourceLocation());
Bukkit.getConsoleSender().sendMessage("§6USING CUSTOM RESOURCE FILE! §e" + resourceLocation());
saved.put(identifier(), simpleYml);
} else {
saved.put(identifier(), new SimpleYml(getClass(), file()));
Bukkit.getConsoleSender().sendMessage("§6USING DEFAULT RESOURCE FILE! §e" + file().getName());
}
}
default String resourceLocation() { return null; }
default SimpleYml yml() {
if (!saved.containsKey(identifier())) {
reloadFile();
}
return saved.get(identifier());
}
@SneakyThrows
private <T> T getObj(String name, Class<T> clazz) {
T obj = clazz.getConstructor().newInstance();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
Object val = yml().get(getPath() + "." + name + "." + field.getName());
field.set(obj, val);
}
return obj;
}
@SneakyThrows
default <T> List<T> asObjectList(Class<T> clazz) {
return yml().getConfigurationSection(getPath()).getKeys(false)
.stream()
.map(key -> getObj(key, clazz))
.toList();
}
default String asString() {return yml().getString(getPath());}
default int asInteger() {return yml().getInt(getPath());}
default long asLong() {return yml().getLong(getPath());}
default double asDouble() {return yml().getDouble(getPath());}
default boolean asBoolean() {return yml().getBoolean(getPath());}
default List<String> asStringList() {return yml().getStringList(getPath());}
default float asFloat() {return (float)yml().getDouble(getPath());}
default byte asByte() {return (byte)yml().getInt(getPath());}
default short asShort() {return (short)yml().getInt(getPath());}
default Material asMaterial() {
return Utilities.findEnumValue(yml().getString(getPath()), Material.class);
}
default Sound asSound() {
return Utilities.findEnumValue(yml().getString(getPath()), Sound.class);
}
default SoundCategory asSoundCategory() {
return Utilities.findEnumValue(yml().getString(getPath()), SoundCategory.class);
}
default BlockFace asBlockFace() {
return Utilities.findEnumValue(yml().getString(getPath()), BlockFace.class);
}
default Particle asParticle() {
return Utilities.findEnumValue(yml().getString(getPath()), Particle.class);
}
default Rows asRows() {
return Utilities.findEnumValue(yml().getString(getPath()), Rows.class);
}
default Component asComponent(){return asComponent(null);}
default List<Component> asComponentList(){return asComponentList(null);}
default String asLegacy(){return asLegacy(null);}
default List<String> asLegacyList(){return asLegacyList(null);}
default List<Component> asComponentList(TagResolver... tagResolvers) {
List<String> values = yml().getStringList(getPath());
return values.stream().map(entry-> Colors.translate(entry,tagResolvers)).toList();
}
default Component asSingleComponent(TagResolver... tagResolvers) {
return Component.join(JoinConfiguration.newlines(),asComponentList(tagResolvers));
}
default String asJson() {
return GsonComponentSerializer.gson().serialize(asComponent());
}
default Component asComponent(TagResolver... tagResolvers){
String entry = yml().getString(getPath());
if (entry != null) {
return Colors.translate(entry, tagResolvers);
}
return Component.text(" [Invalid value in "+file().getName()+": " + getPath() + "] ");
}
default String asLegacy(TagResolver... tagResolvers){
String entry = yml().getString(getPath());
if (entry != null) {
return Colors.legacy(Colors.translate(entry, tagResolvers));
}
return " [Invalid value in "+file().getName()+": " + getPath() + "] ";
}
default List<String> asLegacyList(TagResolver... tagResolvers){
List<String> values = yml().getStringList(getPath());
return values.stream().map(entry -> Colors.legacy(Colors.translate(entry, tagResolvers))).toList();
}
}