Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions patches/api/0443-CommandFunction-API.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 6 Jan 2021 23:38:19 -0800
Subject: [PATCH] CommandFunction API


diff --git a/src/main/java/io/papermc/paper/datapack/CommandFunction.java b/src/main/java/io/papermc/paper/datapack/CommandFunction.java
new file mode 100644
index 0000000000000000000000000000000000000000..5e7e15af3e656cad42939002e3c5db4a3b2b74fa
--- /dev/null
+++ b/src/main/java/io/papermc/paper/datapack/CommandFunction.java
@@ -0,0 +1,31 @@
+package io.papermc.paper.datapack;
+
+import net.kyori.adventure.key.Keyed;
+import org.bukkit.command.CommandSender;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.List;
+
+public interface CommandFunction extends Keyed {
+ /**
+ * Executes the function.
+ *
+ * @return the number of commands run
+ */
+ int executeAsServer();
+
+ /**
+ * Executes the function as the specified sender.
+ *
+ * @param sender the sender to execute the function as
+ * @return the number of commands run
+ */
+ int executeAs(@NotNull CommandSender sender);
+
+ /**
+ * Gets the commands run by this function.
+ *
+ * @return the list of commands
+ */
+ @NotNull List<String> getCommands();
+}
diff --git a/src/main/java/io/papermc/paper/datapack/DatapackManager.java b/src/main/java/io/papermc/paper/datapack/DatapackManager.java
index 58f78d5e91beacaf710f62461cf869f70d08b2a2..b22d3e8f26369af500a46ea599e0595d2360820d 100644
--- a/src/main/java/io/papermc/paper/datapack/DatapackManager.java
+++ b/src/main/java/io/papermc/paper/datapack/DatapackManager.java
@@ -1,6 +1,8 @@
package io.papermc.paper.datapack;

+import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
+import org.jetbrains.annotations.Nullable;

import java.util.Collection;

@@ -18,4 +20,20 @@ public interface DatapackManager {
@NonNull
Collection<Datapack> getEnabledPacks();

+ /**
+ * Gets all command functions registered with the server.
+ *
+ * @return the list of functions
+ */
+ @NonNull
+ Collection<CommandFunction> getCommandFunctions();
+
+ /**
+ * Get the command function with this namespaced key.
+ *
+ * @param functionKey the namespaced key
+ * @return the command function if found, null if none
+ */
+ @Nullable
+ CommandFunction getCommandFunction(@Nullable Key functionKey);
}
9 changes: 7 additions & 2 deletions patches/server/0010-Adventure.patch
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,10 @@ index 0000000000000000000000000000000000000000..2fd6c3e65354071af71c7d8ebb97b559
+}
diff --git a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
new file mode 100644
index 0000000000000000000000000000000000000000..3dc613116c086444ece88bcb0a569eeea953074f
index 0000000000000000000000000000000000000000..293318b04782e3bd329252127b0f11f6a5546650
--- /dev/null
+++ b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
@@ -0,0 +1,390 @@
@@ -0,0 +1,395 @@
+package io.papermc.paper.adventure;
+
+import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down Expand Up @@ -974,6 +974,11 @@ index 0000000000000000000000000000000000000000..3dc613116c086444ece88bcb0a569eee
+ return asVanilla(key);
+ }
+
+ @SuppressWarnings("PatternValidation")
+ public static Key asAdventure(ResourceLocation resourceLocation) {
+ return Key.key(resourceLocation.getNamespace(), resourceLocation.getPath());
+ }
+
+ // Component
+
+ public static Component asAdventure(final net.minecraft.network.chat.Component component) {
Expand Down
129 changes: 129 additions & 0 deletions patches/server/1045-CommandFunction-API.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 6 Jan 2021 23:38:05 -0800
Subject: [PATCH] CommandFunction API

== AT ==
public net.minecraft.server.ServerFunctionManager library
public net.minecraft.commands.CommandFunction$CommandEntry parse

diff --git a/src/main/java/io/papermc/paper/datapack/PaperCommandFunction.java b/src/main/java/io/papermc/paper/datapack/PaperCommandFunction.java
new file mode 100644
index 0000000000000000000000000000000000000000..6fe49aa51eacc36cedc4a7ab06edb59577fd7ce2
--- /dev/null
+++ b/src/main/java/io/papermc/paper/datapack/PaperCommandFunction.java
@@ -0,0 +1,83 @@
+package io.papermc.paper.datapack;
+
+import com.mojang.brigadier.ParseResults;
+import io.papermc.paper.adventure.PaperAdventure;
+import java.util.ArrayList;
+import java.util.List;
+import net.kyori.adventure.key.Key;
+import net.minecraft.commands.CommandFunction;
+import net.minecraft.commands.CommandSourceStack;
+import net.minecraft.commands.FunctionInstantiationException;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.commands.FunctionCommand;
+import org.bukkit.command.CommandSender;
+import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.framework.qual.DefaultQualifier;
+
+@DefaultQualifier(NonNull.class)
+public class PaperCommandFunction implements io.papermc.paper.datapack.CommandFunction {
+
+ private final CommandFunction handle;
+ private @Nullable Key functionKey;
+ private @Nullable List<String> commands;
+ private @Nullable List<ParseResults<CommandSourceStack>> parseResults;
+
+ public PaperCommandFunction(CommandFunction handle) {
+ this.handle = handle;
+ }
+
+ // @Override // TODO expose once brig is in the API
+ public List<ParseResults<CommandSourceStack>> getParseResults() {
+ if (this.parseResults == null) {
+ this.parseResults = new ArrayList<>();
+ for (CommandFunction.Entry entry : this.handle.getEntries()) {
+ if (entry instanceof CommandFunction.CommandEntry commandEntry) {
+ this.parseResults.add(commandEntry.parse);
+ }
+ }
+ }
+ return this.parseResults;
+ }
+
+ @Override
+ public int executeAsServer() {
+ return execute(MinecraftServer.getServer().createCommandSourceStack());
+ }
+
+ @Override
+ public int executeAs(CommandSender sender) {
+ return execute(VanillaCommandWrapper.getListener(sender));
+ }
+
+ private int execute(CommandSourceStack source) {
+ try {
+ final FunctionCommand.FunctionResult functionResult = FunctionCommand.runFunction(source, this.handle, null);
+ return functionResult.value();
+ } catch (FunctionInstantiationException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public List<String> getCommands() {
+ if (this.commands == null) {
+ this.commands = new ArrayList<>();
+ for (CommandFunction.Entry entry : handle.getEntries()) {
+ if (entry instanceof CommandFunction.CommandEntry commandEntry) { // should always be true
+ commands.add(commandEntry.toString());
+ }
+ }
+ }
+ return this.commands;
+ }
+
+ @Override
+ public Key key() {
+ if (this.functionKey == null) {
+ this.functionKey = PaperAdventure.asAdventure(handle.getId());
+ }
+ return this.functionKey;
+ }
+}
diff --git a/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java b/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
index cf4374493c11057451a62a655514415cf6b298e0..a997cccda094e8c06c6e5dd617ffcf62ad851873 100644
--- a/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
+++ b/src/main/java/io/papermc/paper/datapack/PaperDatapackManager.java
@@ -22,4 +22,14 @@ public class PaperDatapackManager implements DatapackManager {
public Collection<Datapack> getEnabledPacks() {
return repository.getSelectedPacks().stream().map(loader -> new PaperDatapack(loader, true)).collect(Collectors.toList());
}
+
+ @Override
+ public Collection<CommandFunction> getCommandFunctions() {
+ return net.minecraft.server.MinecraftServer.getServer().getFunctions().library.getFunctions().values().stream().map(net.minecraft.commands.CommandFunction::getPaperCommandFunction).collect(Collectors.toSet());
+ }
+
+ @Override
+ public CommandFunction getCommandFunction(net.kyori.adventure.key.Key functionKey) {
+ return net.minecraft.server.MinecraftServer.getServer().getFunctions().get(io.papermc.paper.adventure.PaperAdventure.asVanilla(functionKey)).map(net.minecraft.commands.CommandFunction::getPaperCommandFunction).orElse(null);
+ }
}
diff --git a/src/main/java/net/minecraft/commands/CommandFunction.java b/src/main/java/net/minecraft/commands/CommandFunction.java
index 3fe2c28644050f04206b73b687b7e95be87cb7f7..e41ae04b3047fd2651812b516fd790e810cd162c 100644
--- a/src/main/java/net/minecraft/commands/CommandFunction.java
+++ b/src/main/java/net/minecraft/commands/CommandFunction.java
@@ -40,6 +40,7 @@ public class CommandFunction {
}
return timing;
}
+ private final io.papermc.paper.datapack.PaperCommandFunction paperCommandFunction = new io.papermc.paper.datapack.PaperCommandFunction(this); public final io.papermc.paper.datapack.PaperCommandFunction getPaperCommandFunction() { return paperCommandFunction; }
// Paper end

public CommandFunction(ResourceLocation id, CommandFunction.Entry[] elements) {