-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaceholder.java
More file actions
53 lines (42 loc) · 1.6 KB
/
Copy pathPlaceholder.java
File metadata and controls
53 lines (42 loc) · 1.6 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
package ru.cwcode.cwutils.messages;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
class Placeholder {
private static Placeholder instance;
private Message message;
private Placeholder() {
}
protected static Placeholder getInstance(Message message) {
if (instance == null) instance = new Placeholder();
instance.message = message.clone();
return instance;
}
Message replacePlaceholders(String placeholder, Component value) {
placeholder = this.formatPlaceholder(placeholder);
TextReplacementConfig replacement = TextReplacementConfig.builder()
.matchLiteral(placeholder)
.replacement(value)
.build();
this.message.set((TextComponent) this.message.get().replaceText(replacement));
return this.message;
}
private String formatPlaceholder(String placeholder) {
if (placeholder.startsWith(PlaceholderSymbols.left.getSymbol())
&& placeholder.endsWith(PlaceholderSymbols.right.getSymbol())) {
return placeholder;
}
return PlaceholderSymbols.left.getSymbol() + placeholder + PlaceholderSymbols.right.getSymbol();
}
private enum PlaceholderSymbols {
left("<"),
right(">");
private final String symbol;
PlaceholderSymbols(String symbol) {
this.symbol = symbol;
}
public String getSymbol() {
return this.symbol;
}
}
}