Skip to content

Commit

Permalink
Improve example command + update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed Dec 31, 2022
1 parent edc83eb commit 3e28985
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 96 deletions.
28 changes: 18 additions & 10 deletions patches/api/0424-Brigadier-based-command-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ index 0000000000000000000000000000000000000000..1918953532ba9e10614510b7e4da66fb
+}
diff --git a/src/main/java/io/papermc/paper/command/brigadier/CommandSourceStack.java b/src/main/java/io/papermc/paper/command/brigadier/CommandSourceStack.java
new file mode 100644
index 0000000000000000000000000000000000000000..e1fd9c49fe550619b58cfa34a2b771c48bfe901b
index 0000000000000000000000000000000000000000..83651e20bf2bdf4e078dc2b8496f31917f6e8d34
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/brigadier/CommandSourceStack.java
@@ -0,0 +1,46 @@
@@ -0,0 +1,47 @@
+package io.papermc.paper.command.brigadier;
+
+import org.bukkit.Location;
Expand All @@ -195,6 +195,7 @@ index 0000000000000000000000000000000000000000..e1fd9c49fe550619b58cfa34a2b771c4
+ * for command execution.
+ */
+@ApiStatus.NonExtendable
+@ApiStatus.Experimental
+public interface CommandSourceStack {
+
+ /**
Expand Down Expand Up @@ -310,21 +311,23 @@ index 0000000000000000000000000000000000000000..ee073108df90ffdc58b524da03e76fa6
+}
diff --git a/src/main/java/io/papermc/paper/command/brigadier/argument/ArgumentResolver.java b/src/main/java/io/papermc/paper/command/brigadier/argument/ArgumentResolver.java
new file mode 100644
index 0000000000000000000000000000000000000000..b9834f035a09b5623a231fe864c55440220cfa6d
index 0000000000000000000000000000000000000000..14ff48fb772a9f0ca9a3d37f8a600a39d1d14a8c
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/brigadier/argument/ArgumentResolver.java
@@ -0,0 +1,22 @@
@@ -0,0 +1,24 @@
+package io.papermc.paper.command.brigadier.argument;
+
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import io.papermc.paper.command.brigadier.CommandSourceStack;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Represents an argument which requires
+ * the context of a command source stack to be fully resolved.
+ * @param <T> type
+ */
+@ApiStatus.Experimental
+public interface ArgumentResolver<T> {
+
+ /**
Expand All @@ -338,23 +341,25 @@ index 0000000000000000000000000000000000000000..b9834f035a09b5623a231fe864c55440
+}
diff --git a/src/main/java/io/papermc/paper/command/brigadier/argument/MessageArgumentResponse.java b/src/main/java/io/papermc/paper/command/brigadier/argument/MessageArgumentResponse.java
new file mode 100644
index 0000000000000000000000000000000000000000..6775cee4ce4482870a3bdc62847262071967d312
index 0000000000000000000000000000000000000000..8a39d092b9d54a7ab4f4e0f65f0c4b59db1e5db9
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/brigadier/argument/MessageArgumentResponse.java
@@ -0,0 +1,39 @@
@@ -0,0 +1,41 @@
+package io.papermc.paper.command.brigadier.argument;
+
+import com.mojang.brigadier.context.CommandContext;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import io.papermc.paper.command.brigadier.CommandSourceStack;
+import net.kyori.adventure.chat.SignedMessage;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Argument type returned from the Message argument.
+ */
+@ApiStatus.Experimental
+public interface MessageArgumentResponse {
+
+ /**
Expand Down Expand Up @@ -383,10 +388,10 @@ index 0000000000000000000000000000000000000000..6775cee4ce4482870a3bdc6284726207
+}
diff --git a/src/main/java/io/papermc/paper/command/brigadier/argument/VanillaArguments.java b/src/main/java/io/papermc/paper/command/brigadier/argument/VanillaArguments.java
new file mode 100644
index 0000000000000000000000000000000000000000..6123c4c32841ff0611ce4d3924aa879cba5fdd25
index 0000000000000000000000000000000000000000..e43ce009c0245f008fa716e8f8bb6c13b8285cd0
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/brigadier/argument/VanillaArguments.java
@@ -0,0 +1,189 @@
@@ -0,0 +1,190 @@
+package io.papermc.paper.command.brigadier.argument;
+
+import com.destroystokyo.paper.profile.PlayerProfile;
Expand All @@ -411,6 +416,7 @@ index 0000000000000000000000000000000000000000..6123c4c32841ff0611ce4d3924aa879c
+ * These provide rich argument parsing on the client and
+ * may also provide additional signing context.
+ */
+@ApiStatus.Experimental
+public final class VanillaArguments {
+
+ @NotNull
Expand Down Expand Up @@ -578,15 +584,16 @@ index 0000000000000000000000000000000000000000..6123c4c32841ff0611ce4d3924aa879c
+}
diff --git a/src/main/java/io/papermc/paper/command/brigadier/argument/WrapperArgumentType.java b/src/main/java/io/papermc/paper/command/brigadier/argument/WrapperArgumentType.java
new file mode 100644
index 0000000000000000000000000000000000000000..da409fd6b5888d5d592daa6c5cb2325e6d8ea35d
index 0000000000000000000000000000000000000000..c5c912e792297ae2129e9ff56c7bcad82c0493ac
--- /dev/null
+++ b/src/main/java/io/papermc/paper/command/brigadier/argument/WrapperArgumentType.java
@@ -0,0 +1,84 @@
@@ -0,0 +1,86 @@
+package io.papermc.paper.command.brigadier.argument;
+
+import com.mojang.brigadier.StringReader;
+import com.mojang.brigadier.arguments.ArgumentType;
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collection;
Expand All @@ -599,6 +606,7 @@ index 0000000000000000000000000000000000000000..da409fd6b5888d5d592daa6c5cb2325e
+ * @param <T> type
+ * @param <B> base wrapping type
+ */
+@ApiStatus.Experimental
+public abstract class WrapperArgumentType<T, B> implements ArgumentType<T> {
+
+ @NotNull
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.papermc.paper.testplugin;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import io.papermc.paper.command.brigadier.CommandBuilder;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.argument.MessageArgumentResponse;
import io.papermc.paper.command.brigadier.argument.VanillaArguments;
import io.papermc.paper.testplugin.example.ExampleAdminCommand;
import net.kyori.adventure.chat.ChatType;
import net.kyori.adventure.chat.SignedMessage;
import net.kyori.adventure.text.Component;
import io.papermc.paper.testplugin.example.IceCreamType;
import io.papermc.paper.testplugin.example.IceCreamTypeArgument;
import org.bukkit.block.BlockState;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
Expand All @@ -27,39 +24,13 @@ public void onEnable() {
this.getServer().getPluginManager().registerEvents(this, this);

ExampleAdminCommand.register(this);
CommandBuilder.of(this, "hello")
.then(
RequiredArgumentBuilder.argument("getreal", VanillaArguments.uuid())
)
.then(
RequiredArgumentBuilder.<CommandSourceStack, BlockState>argument("getrealy", VanillaArguments.blockState()).executes((context) -> {
BlockState state = context.getArgument("getrealy", BlockState.class);
System.out.println(state);
return 1;
})
).then(
RequiredArgumentBuilder.<CommandSourceStack, Species>argument("species", new SpeciesArgument()).executes((context) -> {
Species species = context.getArgument("species", Species.class);
System.out.println(species);
return 1;
}).then(RequiredArgumentBuilder.<CommandSourceStack, Integer>argument("temperature", new TemperatureArgument()).executes((context) -> {
int temp = context.getArgument("temperature", Integer.class);
Species species = context.getArgument("species", Species.class);
System.out.println("ITS %s degrees C for the %s".formatted(temp, species));
return 1;
}))
)
.aliases("wow", "bob", "weird spaces", "oog")
.register();

this.getServer().getCommandMap().register("fallback", new BukkitCommand("hi", "cool hi command", "<>", List.of("hialias")) {
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
sender.sendMessage("hi");
return true;
}


});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public static void register(TestPlugin plugin) {
})
)
)
.then(
LiteralArgumentBuilder.<CommandSourceStack>literal("ice_cream").then(
RequiredArgumentBuilder.<CommandSourceStack, IceCreamType>argument("type", new IceCreamTypeArgument()).executes((context) -> {
IceCreamType argumentResponse = context.getArgument("type", IceCreamType.class); // Gets the raw argument
context.getSource().getBukkitSender().sendMessage(Component.text("You like: " + argumentResponse));
return 1;
})
)
)
.then(
LiteralArgumentBuilder.<CommandSourceStack>literal("execute")
.redirect(Bukkit.getCommandDispatcher().getRoot().getChild("execute"))
Expand Down Expand Up @@ -90,23 +99,13 @@ public static void register(TestPlugin plugin) {
return 1;
})
)
.executes((context) -> {
MessageArgumentResponse argumentResponse = context.getArgument("msg", MessageArgumentResponse.class); // Gets the raw argument

// This is a better way of getting signed messages, includes the concept of "disguised" messages.
argumentResponse.resolveSignedMessage("msg", context)
.thenAccept((signedMsg) -> {
context.getSource().getBukkitSender().sendMessage(signedMsg, ChatType.SAY_COMMAND.bind(Component.text("STATIC")));
});

return 1;
})
)
)
.description("Cool command showcasing what you can do!")
.aliases("alias_for_admin_that_you_shouldnt_use", "a")
.register();



Bukkit.getCommandMap().register(
"legacy",
new Command("legacy_command") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.papermc.paper.testplugin.example;

public enum IceCreamType {
VANILLA,
CHOCOLATE,
BLUE_MOON,
STRAWBERRY,
WHOLE_MILK
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.papermc.paper.testplugin;
package io.papermc.paper.testplugin.example;

import com.mojang.brigadier.Message;
import com.mojang.brigadier.arguments.StringArgumentType;
Expand All @@ -15,16 +15,16 @@

import java.util.concurrent.CompletableFuture;

public class SpeciesArgument extends WrapperArgumentType<Species, String> {
public class IceCreamTypeArgument extends WrapperArgumentType<IceCreamType, String> {

public SpeciesArgument() {
public IceCreamTypeArgument() {
super(StringArgumentType.word());
}

@Override
public @NotNull Species convert(String baseType) throws CommandSyntaxException {
public @NotNull IceCreamType convert(String baseType) throws CommandSyntaxException {
try {
return Species.valueOf(baseType.toUpperCase());
return IceCreamType.valueOf(baseType.toUpperCase());
} catch (Exception e) {
Message message = MessageComponentSerializer.message().serialize(Component.text("Invalid species %s!".formatted(baseType), NamedTextColor.RED));

Expand All @@ -39,7 +39,7 @@ public boolean handleSuggestions() {

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
for (Species species : Species.values()) {
for (IceCreamType species : IceCreamType.values()) {
builder.suggest(species.name(), MessageComponentSerializer.message().serialize(Component.text("COOL! TOOLTIP!", NamedTextColor.GREEN)));
}

Expand Down

0 comments on commit 3e28985

Please sign in to comment.