9
0
mirror of https://github.com/LeavesMC/Leaves.git synced 2025-12-29 11:59:17 +00:00

refactor: refactor

This commit is contained in:
MC_XiaoHei
2025-08-24 15:53:50 +08:00
parent cfafc2638b
commit 69ac9cac26
3 changed files with 11 additions and 3 deletions

View File

@@ -27,11 +27,15 @@ public abstract class ArgumentNode<T> extends CommandNode {
return Suggestions.empty();
}
protected boolean overrideSuggestions() {
return isMethodOverridden("getSuggestions", ArgumentNode.class);
}
@Override
protected ArgumentBuilder<CommandSourceStack, ?> compileBase() {
RequiredArgumentBuilder<CommandSourceStack, T> argumentBuilder = Commands.argument(name, argumentType);
if (isMethodOverridden("getSuggestions", ArgumentNode.class)) {
if (overrideSuggestions()) {
argumentBuilder.suggests(
(context, builder) -> getSuggestions(new CommandContext(context), builder)
);

View File

@@ -50,7 +50,7 @@ public abstract class CommandNode {
builder = builder.then(child.compile());
}
if (isMethodOverridden("execute", CommandNode.class)) {
if (canExecute()) {
builder = builder.executes(mojangCtx -> {
CommandContext ctx = new CommandContext(mojangCtx);
return execute(ctx) ? 1 : 0;
@@ -60,6 +60,10 @@ public abstract class CommandNode {
return builder;
}
protected boolean canExecute() {
return isMethodOverridden("execute", CommandNode.class);
}
protected boolean isMethodOverridden(String methodName, @NotNull Class<?> baseClass) {
for (Method method : getClass().getDeclaredMethods()) {
if (method.getName().equals(methodName)) {

View File

@@ -32,7 +32,7 @@ public class CustomArgumentNode<T, B> extends ArgumentNode<B> {
protected ArgumentBuilder<CommandSourceStack, ?> compileBase() {
RequiredArgumentBuilder<CommandSourceStack, T> argumentBuilder = (RequiredArgumentBuilder<CommandSourceStack, T>) super.compileBase();
if (!isMethodOverridden("getSuggestions", ArgumentNode.class)) {
if (overrideSuggestions()) {
CustomArgumentType<T, B> customArgumentType = (CustomArgumentType<T, B>) TYPES.get(getClass());
argumentBuilder.suggests(
(context, builder) -> customArgumentType.getSuggestions(new CommandContext(context), builder)