mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat(parser): Supported more insert functions
This commit is contained in:
@@ -10,6 +10,12 @@ public class PreparedText<MSG, RECEIVER> extends TextDispatcher<MSG, RECEIVER, P
|
||||
super(texts, params);
|
||||
}
|
||||
|
||||
public PreparedText<MSG, RECEIVER> insert(@NotNull String key,
|
||||
@NotNull ConfiguredText<MSG, RECEIVER> message,
|
||||
@NotNull Object... values) {
|
||||
return insert(key, receiver -> message.parse(receiver, values));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreparedText<MSG, RECEIVER> self() {
|
||||
return this;
|
||||
|
||||
+21
-8
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -45,7 +46,7 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
|
||||
/**
|
||||
* Used to store the insertion of the message
|
||||
*/
|
||||
protected @NotNull Map<String, List<String>> insertion = new HashMap<>();
|
||||
protected @NotNull Map<String, Function<RECEIVER, List<String>>> insertion = new HashMap<>();
|
||||
protected boolean disableInsertion = false;
|
||||
|
||||
protected TextParser(@NotNull TextContents texts, @NotNull String... params) {
|
||||
@@ -148,14 +149,25 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
|
||||
* Insert the specific contents by the id.
|
||||
*
|
||||
* @param id the id of the insertion text
|
||||
* @param lines the lines to insert
|
||||
* @param linesSupplier to supply the lines to insert
|
||||
* @return the current {@link TextParser} instance
|
||||
*/
|
||||
public SELF insert(@NotNull String id, @NotNull String... lines) {
|
||||
this.insertion.put(id, Arrays.asList(lines));
|
||||
public SELF insert(@NotNull String id, @NotNull Function<RECEIVER, List<String>> linesSupplier) {
|
||||
this.insertion.put(id, linesSupplier);
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the specific contents by the id.
|
||||
*
|
||||
* @param id the id of the insertion text
|
||||
* @param lines the lines to insert
|
||||
* @return the current {@link TextParser} instance
|
||||
*/
|
||||
public SELF insert(@NotNull String id, @NotNull String... lines) {
|
||||
return insert(id, Arrays.asList(lines));
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert the specific contents by the id.
|
||||
*
|
||||
@@ -164,8 +176,7 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
|
||||
* @return the current {@link TextParser} instance
|
||||
*/
|
||||
public SELF insert(@NotNull String id, @NotNull List<String> lines) {
|
||||
this.insertion.put(id, lines);
|
||||
return self();
|
||||
return insert(id, receiver -> lines);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,8 +284,10 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
|
||||
}
|
||||
|
||||
String id = matcher.group("id");
|
||||
List<String> values = this.insertion.get(id);
|
||||
if (values == null) continue;
|
||||
List<String> values = Optional.ofNullable(this.insertion.get(id))
|
||||
.map(f -> f.apply(receiver))
|
||||
.orElse(null);
|
||||
if (values == null || values.isEmpty()) continue;
|
||||
|
||||
String prefix = matcher.group("prefix");
|
||||
String type = matcher.group("type");
|
||||
|
||||
Reference in New Issue
Block a user