1
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:
2025-02-17 03:13:35 +08:00
parent 05f504a347
commit 616314c7f0
2 changed files with 28 additions and 9 deletions
@@ -10,6 +10,12 @@ public class PreparedText<MSG, RECEIVER> extends TextDispatcher<MSG, RECEIVER, P
super(texts, params); 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 @Override
public PreparedText<MSG, RECEIVER> self() { public PreparedText<MSG, RECEIVER> self() {
return this; return this;
@@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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 * 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 boolean disableInsertion = false;
protected TextParser(@NotNull TextContents texts, @NotNull String... params) { 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. * Insert the specific contents by the id.
* *
* @param id the id of the insertion text * @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 * @return the current {@link TextParser} instance
*/ */
public SELF insert(@NotNull String id, @NotNull String... lines) { public SELF insert(@NotNull String id, @NotNull Function<RECEIVER, List<String>> linesSupplier) {
this.insertion.put(id, Arrays.asList(lines)); this.insertion.put(id, linesSupplier);
return self(); 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. * 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 * @return the current {@link TextParser} instance
*/ */
public SELF insert(@NotNull String id, @NotNull List<String> lines) { public SELF insert(@NotNull String id, @NotNull List<String> lines) {
this.insertion.put(id, lines); return insert(id, receiver -> lines);
return self();
} }
/** /**
@@ -273,8 +284,10 @@ public abstract class TextParser<RECEIVER, SELF extends TextParser<RECEIVER, SEL
} }
String id = matcher.group("id"); String id = matcher.group("id");
List<String> values = this.insertion.get(id); List<String> values = Optional.ofNullable(this.insertion.get(id))
if (values == null) continue; .map(f -> f.apply(receiver))
.orElse(null);
if (values == null || values.isEmpty()) continue;
String prefix = matcher.group("prefix"); String prefix = matcher.group("prefix");
String type = matcher.group("type"); String type = matcher.group("type");