From 27ec5a317617d2f07ee5cbb06ea3e17c224bdf82 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 16 Jun 2024 23:54:22 +0100 Subject: [PATCH] feat: fixup paper custom argument types --- .../net/william278/uniform/BaseCommand.java | 2 +- .../uniform/element/ArgumentElement.java | 19 ++++++- .../uniform/paper/PaperCommand.java | 29 +++++++++-- .../paper/element/PaperArgumentElement.java | 50 +++++++++++++++++++ 4 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 paper/src/main/java/net/william278/uniform/paper/element/PaperArgumentElement.java diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index ae613e1..b918b93 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -179,7 +179,7 @@ public abstract class BaseCommand { @NotNull public static ArgumentElement arg(@NotNull String name, @NotNull ArgumentType type, @Nullable SuggestionProvider suggestionProvider) { - return new ArgumentElement<>(name, type, suggestionProvider); + return new ArgumentElement<>(name, type, suggestionProvider, false); } @NotNull diff --git a/common/src/main/java/net/william278/uniform/element/ArgumentElement.java b/common/src/main/java/net/william278/uniform/element/ArgumentElement.java index 037f87c..6899e4a 100644 --- a/common/src/main/java/net/william278/uniform/element/ArgumentElement.java +++ b/common/src/main/java/net/william278/uniform/element/ArgumentElement.java @@ -26,11 +26,26 @@ import com.mojang.brigadier.arguments.ArgumentType; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.suggestion.SuggestionProvider; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.experimental.Accessors; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public record ArgumentElement(@NotNull String name, @NotNull ArgumentType type, - @Nullable SuggestionProvider suggestionProvider) implements CommandElement { +@Accessors(fluent = true) +@Getter +@AllArgsConstructor +@RequiredArgsConstructor +public final class ArgumentElement implements CommandElement { + + @NotNull + private final String name; + @NotNull + private final ArgumentType type; + @Nullable + private final SuggestionProvider suggestionProvider; + private boolean custom = true; @Override @NotNull diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java index ac04b76..ce3e0fd 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -24,11 +24,10 @@ package net.william278.uniform.paper; import com.mojang.brigadier.exceptions.CommandSyntaxException; import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; -import net.william278.uniform.BaseCommand; -import net.william278.uniform.Command; -import net.william278.uniform.CommandUser; -import net.william278.uniform.Uniform; +import net.william278.uniform.*; import net.william278.uniform.element.ArgumentElement; +import net.william278.uniform.element.CommandElement; +import net.william278.uniform.paper.element.PaperArgumentElement; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -107,6 +106,28 @@ public class PaperCommand extends BaseCommand { }); } + @Override + @NotNull + @SuppressWarnings("unchecked") + public List> getSyntaxes() { + return super.getSyntaxes().stream().map( + syntax -> new CommandSyntax<>( + syntax.condition(), + syntax.executor(), + syntax.elements().stream() + .filter(e -> e instanceof ArgumentElement) + .map(e -> (ArgumentElement) e) + .map(e -> e.custom() ? new ArgumentElement<>( + e.name(), + new PaperArgumentElement<>(e.type()), + e.suggestionProvider() + ) : e) + .map(e -> (CommandElement) e) + .toList() + ) + ).toList(); + } + @Override public void addSubCommand(@NotNull Command command) { addSubCommand(new PaperCommand(command)); diff --git a/paper/src/main/java/net/william278/uniform/paper/element/PaperArgumentElement.java b/paper/src/main/java/net/william278/uniform/paper/element/PaperArgumentElement.java new file mode 100644 index 0000000..82f5525 --- /dev/null +++ b/paper/src/main/java/net/william278/uniform/paper/element/PaperArgumentElement.java @@ -0,0 +1,50 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform.paper.element; + +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.arguments.ArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import io.papermc.paper.command.brigadier.argument.CustomArgumentType; +import lombok.AllArgsConstructor; +import org.jetbrains.annotations.NotNull; + +@AllArgsConstructor +@SuppressWarnings("UnstableApiUsage") +public class PaperArgumentElement implements CustomArgumentType { + + private final ArgumentType wrapped; + + @Override + @NotNull + public T parse(@NotNull StringReader reader) throws CommandSyntaxException { + return wrapped.parse(reader); + } + + @Override + @NotNull + public ArgumentType getNativeType() { + return StringArgumentType.string(); + } + +}