-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessagesFile.java
251 lines (228 loc) · 9.37 KB
/
MessagesFile.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
package net.codersky.mcutils.files.yaml;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import net.codersky.mcutils.MCPlugin;
import net.codersky.mcutils.files.MessagesFileHolder;
import net.codersky.mcutils.java.MCLists;
import net.codersky.mcutils.java.strings.MCStrings;
import net.codersky.mcutils.java.strings.replacers.Replacer;
/**
* A class intended to be used to create
* and use a messages file. The most notorious features
* of this file type include:
* <p>
* <b>Default replacements</b>: Defining a "%prefix%" replacement on every message
* is tedious, that's why you can define a default {@link Replacer} by using
* {@link #setDefaultReplacer(Replacer, boolean)}, this replacer will be applied on every message.
* <p>
* <b>Numeric support</b>: Disabled by default, can
* be toggled by calling {@link #setNumSupport(boolean)}.
* An explanation of this feature is provided at {@link Replacer#setNumSupport(boolean)}
* <p>
* <b>Formatted messages</b>: This feature is always enabled and you
* don't need to do anything for it to work, all messages will be sent using
* {@link MCStrings#sendMessage(CommandSender, String)}.
*
* @since MCUtils 1.0.0
*
* @author xDec0de_
*/
public class MessagesFile extends PluginFile implements MessagesFileHolder {
@Nullable
private Replacer defReplacer = null;
/**
* Creates an instance, <b>NOT</b> a file, of a {@link MessagesFile} for the specified <b>plugin</b> and <b>path</b>.
* Fast access for file creation is provided by {@link MCPlugin#registerFile(String, Class)}.
* <p>
* {@link MessagesFile}s are required to be on <b>plugin</b>'s jar file as a resource. For this
* exact reason they can be easily updated to newer versions. This files are intended to be used as
* as message files that require certain content to be present on them and are likely to be
* updated on future versions, as an extension of {@link PluginFile}.
*
* @param plugin an instance of the plugin creating the file, used to get it's data folder.
* @param path the path of the file to create, the ".yml" extension is automatically added if missing,
* if the path is null, empty or blank, "file" will be used.
* @param charset the charset to use, if null, {@link StandardCharsets#UTF_8} will be used.
*
* @throws IllegalArgumentException if <b>plugin</b> is null.
*
* @since MCUtils 1.0.0
*
* @see MCPlugin#registerFile(MessagesFile)
* @see MCPlugin#registerFile(String, Class)
* @see #create()
*/
public MessagesFile(@Nonnull JavaPlugin plugin, @Nullable String path, @Nullable Charset charset) {
super(plugin, path, charset);
if (!contains("commands.noPlayer"))
addDefault("commands.noPlayer", "&8&l[&4&l!&8&l] &cThis command cannot be executed by players&8.");
if (!contains("commands.noConsole"))
addDefault("commands.noConsole", "&8&l[&4&l!&8&l] &cThis command cannot be executed by the console&8.");
}
/**
* Creates an instance, <b>NOT</b> a file, of a {@link MessagesFile} for the specified <b>plugin</b> and <b>path</b>.
* Fast access for file creation is provided by {@link MCPlugin#registerFile(String, Class)}.
* <p>
* {@link MessagesFile}s are required to be on <b>plugin</b>'s jar file as a resource. For this
* exact reason they can be easily updated to newer versions. This files are intended to be used as
* as message files that require certain content to be present on them and are likely to be
* updated on future versions, as an extension of {@link PluginFile}.
* <p>
* This constructor uses {@link StandardCharsets#UTF_8}, to specify use {@link #PluginFile(JavaPlugin, String, Charset)}
*
* @param plugin an instance of the plugin creating the file, used to get it's data folder.
* @param path the path of the file to create, the ".yml" extension is automatically added if missing,
* if the path is null, empty or blank, "file" will be used.
*
* @throws IllegalArgumentException if <b>plugin</b> is null.
*
* @since MCUtils 1.0.0
*
* @see MCPlugin#registerFile(MessagesFile)
* @see MCPlugin#registerFile(String, Class)
* @see #create()
*/
public MessagesFile(@Nonnull JavaPlugin plugin, @Nullable String path) {
this(plugin, path, StandardCharsets.UTF_8);
}
/*
* Replacer handling
*/
/**
* Sets the default {@link Replacer} to be used on this {@link MessagesFile}.
* If the default {@link Replacer} is null, no default {@link Replacer} will be used on any
* message, the default {@link Replacer} is null by default.
*
* @param replacer the default {@link Replacer} to be used on this {@link MessagesFile}.
*
* @since MCUtils 1.0.0
*
* @see #getDefaultReplacer()
* @see #setDefaultObjReplacer(Object...)
* @see #setDefaultPathReplacer(String...)
*/
public void setDefaultReplacer(@Nullable Replacer replacer) {
this.defReplacer = replacer;
}
/**
* Creates a new {@link Replacer} with the specified {@code replacements} and
* sets it as the default {@link Replacer} to use on this {@link MessagesFile}.
*
* @param replacements the replacements to use for the new {@link Replacer}.
*
* @throws IllegalArgumentException if {@code replacements} is {@code null} or the amount of
* objects is not even, more technically, if {@code replacements}
* size % 2 is not equal to 0 as specified by {@link Replacer#Replacer(Object...)}
*
* @since MCUtils 1.0.0
*
* @see #getDefaultReplacer()
* @see #setDefaultReplacer(Replacer)
* @see #setDefaultPathReplacer(String...)
*/
public void setDefaultObjReplacer(@Nonnull Object... replacements) {
setDefaultReplacer(new Replacer(replacements));
}
/**
* Creates a new {@link Replacer} with the specified {@code replacements} and
* sets it as the default {@link Replacer} to use on this {@link MessagesFile}.
* <p>
* The difference between this method and {@link #setDefaultObjReplacer(Object...)}
* is that this method will get strings from this file for the replacements, so the
* replacements must be paths, take the following code as an example:
* <p>
* {@code setDefaultPathReplacer("%err%", "error");}
* <p>
* This default {@link Replacer} will replace every occurrence of "%error%" on the file
* with the string found at the "error" path of the file, not the literal string "error".
*
* @param replacements the replacements to use for the new {@link Replacer}.
*
* @throws IllegalArgumentException if {@code replacements} is {@code null} or the amount of
* objects is not even, more technically, if {@code replacements}
* size % 2 is not equal to 0 as specified by {@link Replacer#Replacer(Object...)}
*
* @since MCUtils 1.0.0
*
* @see #getDefaultReplacer()
* @see #setDefaultReplacer(Replacer)
* @see #setDefaultObjReplacer(Object...)
*/
public void setDefaultPathReplacer(@Nonnull String... replacements) {
final Object[] rep = new String[replacements.length];
for (int i = 0; i < replacements.length; i ++) {
final String element = i % 2 == 0 ? replacements[i] : getString(replacements[i]);
rep[i] = element == null ? "null" : element;
}
this.defReplacer = new Replacer(rep);
}
/**
* Gets a {@link Replacer#clone() clone} of the default {@link Replacer}
* being used by this file.
*
* @return A {@link Replacer#clone() clone} of The default {@link Replacer},
* {@code null} if no {@link Replacer} has been specified or if it has been
* explicitly set to {@code null}.
*
* @since MCUtils 1.0.0
*
* @see #setDefaultReplacer(Replacer)
*/
@Nullable
public Replacer getDefaultReplacer() {
return defReplacer == null ? null : defReplacer.clone();
}
/*
* Message getters
*/
// Strings //
@Nullable
@Override
public String getString(@Nonnull String path) {
final String str = super.getString(path);
return MCStrings.applyColor(defReplacer == null ? str : getDefaultReplacer().replaceAt(str));
}
@Nullable
@Override
public String getString(@Nonnull String path, @Nonnull Replacer rep) {
final String str = super.getString(path);
final Replacer finalRep = defReplacer == null ? rep : getDefaultReplacer().add(rep);
return MCStrings.applyColor(finalRep.replaceAt(str));
}
@Nullable
@Override
public String getString(@Nonnull String path, @Nonnull Object... replacements) {
final String str = super.getString(path);
final Replacer finalRep = defReplacer == null ? new Replacer(replacements) : getDefaultReplacer().add(replacements);
return MCStrings.applyColor(finalRep.replaceAt(str));
}
// Lists //
private List<String> getReplacedList(@Nonnull String path, @Nullable Replacer replacer) {
final List<String> lst = super.getStringList(path);
if (lst.isEmpty())
return null;
if (replacer == null)
return MCStrings.applyColor(lst);
return MCLists.map(msg -> replacer.replaceAt(MCStrings.applyColor(msg)), lst);
}
@Nullable
@Override
public List<String> getStringList(@Nullable String path) {
return getReplacedList(path, defReplacer);
}
@Nullable
@Override
public List<String> getStringList(@Nullable String path, @Nullable Replacer replacer) {
return getReplacedList(path, defReplacer == null ? replacer : defReplacer.clone().add(replacer));
}
@Nullable
@Override
public List<String> getStringList(@Nullable String path, @Nonnull Object... replacements) {
return getReplacedList(path, new Replacer(replacements));
}
}