8000 Implement FunctionArgument tests for 1.19.2. · CommandAPI/CommandAPI@86eca71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86eca71

Browse files
committed
Implement FunctionArgument tests for 1.19.2.
1 parent 770a19f commit 86eca71

File tree

1 file changed

+76
-0
lines changed
  • commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-impl-1.19.2/src/main/java/dev/jorel/commandapi/test

1 file changed

+76
-0
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-impl-1.19.2/src/main/java/dev/jorel/commandapi/test/MockNMS.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@
5252
import be.seeseemelk.mockbukkit.WorldMock;
5353
import be.seeseemelk.mockbukkit.enchantments.EnchantmentMock;
5454
import be.seeseemelk.mockbukkit.potion.MockPotionEffectType;
55+
import dev.jorel.commandapi.Brigadier;
5556
import dev.jorel.commandapi.CommandAPIBukkit;
5657
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
5758
import dev.jorel.commandapi.commandsenders.BukkitCommandSender;
59+
import dev.jorel.commandapi.commandsenders.BukkitPlayer;
5860
import io.papermc.paper.advancement.AdvancementDisplay;
5961
import net.kyori.adventure.text.Component;
6062
import net.minecraft.SharedConstants;
6163
import net.minecraft.advancements.Advancement;
64+
import net.minecraft.commands.CommandFunction;
6265
import net.minecraft.commands.CommandSourceStack;
6366
import net.minecraft.commands.arguments.EntityAnchorArgument.Anchor;
6467
import net.minecraft.core.BlockPos;
@@ -68,13 +71,17 @@
6871
import net.minecraft.server.Bootstrap;
6972
import net.minecraft.server.MinecraftServer;
7073
import net.minecraft.server.ServerAdvancementManager;
74+
import net.minecraft.server.ServerFunctionLibrary;
75+
import net.minecraft.server.ServerFunctionManager;
7176
import net.minecraft.server.ServerScoreboard;
7277
import net.minecraft.server.level.ServerLevel;
7378
import net.minecraft.server.level.ServerPlayer;
7479
import net.minecraft.server.players.GameProfileCache;
7580
import net.minecraft.server.players.PlayerList;
81+
import net.minecraft.util.profiling.metrics.profiling.InactiveMetricsRecorder;
7682
import net.minecraft.world.item.crafting.Recipe;
7783
import net.minecraft.world.item.crafting.RecipeManager;
84+
import net.minecraft.world.level.GameRules;
7885
import net.minecraft.world.level.Level;
7986
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
8087
import net.minecraft.world.level.storage.loot.LootTables;
@@ -97,6 +104,8 @@ public class MockNMS extends Enums {
97104
List<ServerPlayer> players = new ArrayList<>();
98105
PlayerList playerListMock;
99106
final RecipeManager recipeManager;
107+
Map<ResourceLocation, CommandFunction> functions = new HashMap<>();
108+
Map<ResourceLocation, Collection<CommandFunction>> tags = new HashMap<>();
100109

101110
public MockNMS(CommandAPIBukkit<?> baseNMS) {
102111
super(baseNMS);
@@ -127,6 +136,7 @@ public MockNMS(CommandAPIBukkit<?> baseNMS) {
127136
registerDefaultEnchantments();
128137

129138
this.recipeManager = new RecipeManager();
139+
this.functions = new HashMap<>();
130140
registerDefaultRecipes();
131141
}
132142

@@ -320,6 +330,11 @@ public CommandSourceStack getBrigadierSourceFromCommandSender(AbstractCommandSen
320330
// ChatArgument, AdventureChatArgument
321331
Mockito.when(css.hasPermission(anyInt())).thenAnswer(invocation -> sender.isOp());
322332
Mockito.when(css.hasPermission(anyInt(), anyString())).thenAnswer(invocation -> sender.isOp());
333+
334+
// FunctionArgument
335+
// We don't really need to do anything funky here, we'll just return the same CSS
336+
Mockito.when(css.withSuppressedOutput()).thenReturn(css);
337+
Mockito.when(css.withMaximumPermission(anyInt())).thenReturn(css);
323338
}
324339
return css;
325340
}
@@ -449,9 +464,70 @@ public <T> T getMinecraftServer() {
449464
// RecipeArgument
450465
Mockito.when(minecraftServerMock.getRecipeManager()).thenAnswer(i -> this.recipeManager);
451466

467+
// FunctionArgument
468+
// We're using 2 as the function compilation level.
469+
Mockito.when(minecraftServerMock.getFunctionCompilationLevel()).thenReturn(2);
470+
Mockito.when(minecraftServerMock.getFunctions()).thenAnswer(i -> {
471+
ServerFunctionLibrary serverFunctionLibrary = Mockito.mock(ServerFunctionLibrary.class);
472+
473+
// Functions
474+
Mockito.when(serverFunctionLibrary.getFunction(any())).thenAnswer(invocation -> Optional.ofNullable(functions.get(invocation.getArgument(0))));
475+
Mockito.when(serverFunctionLibrary.getFunctions()).thenAnswer(invocation -> functions);
476+
477+
// Tags
478+
Mockito.when(serverFunctionLibrary.getTag(any())).thenAnswer(invocation -> tags.getOrDefault(invocation.getArgument(0), List.of()));
479+
Mockito.when(serverFunctionLibrary.getAvailableTags()).thenAnswer(invocation -> tags.keySet());
480+
481+
return new ServerFunctionManager(minecraftServerMock, serverFunctionLibrary) {
482+
483+
// Make sure we don't use ServerFunctionManager#getDispatcher!
484+
// That method accesses MinecraftServer.vanillaCommandDispatcher
485+
// directly (boo) and that causes all sorts of nonsense.
486+
@Override
487+
public CommandDispatcher<CommandSourceStack> getDispatcher() {
488+
return Brigadier.getCommandDispatcher();
489+
}
490+
};
491+
});
492+
493+
Mockito.when(minecraftServerMock.getGameRules()).thenAnswer(i -> new GameRules());
494+
Mockito.when(minecraftServerMock.getProfiler()).thenAnswer(i -> InactiveMetricsRecorder.INSTANCE.getProfiler());
495+
452496
return (T) minecraftServerMock;
453497
}
454498

499+
@SuppressWarnings("unchecked")
500+
@Override
501+
public void addFunction(NamespacedKey key, List<String> commands) {
502+
if(Bukkit.getOnlinePlayers().isEmpty()) {
503+
throw new IllegalStateException("You need to have at least one player on the server to add a function");
504+
}
505+
506+
ResourceLocation resourceLocation = new ResourceLocation(key.toString());
507+
CommandSourceStack css = getBrigadierSourceFromCommandSender(new BukkitPlayer(Bukkit.getOnlinePlayers().iterator().next()));
508+
509+
// So for very interesting reasons, Brigadier.getCommandDispatcher()
510+
// gives a different result in this method than using getBrigadierDispatcher()
511+
this.functions.put(resourceLocation, CommandFunction.fromLines(resourceLocation, Brigadier.getCommandDispatcher(), css, commands));
512+
}
513+
514+
@SuppressWarnings("unchecked")
515+
@Override
516+
public void addTag(NamespacedKey key, List<List<String>> commands) {
517+
if(Bukkit.getOnlinePlayers().isEmpty()) {
518+
throw new IllegalStateException("You need to have at least one player on the server to add a function");
519+
}
520+
521+
ResourceLocation resourceLocation = new ResourceLocation(key.toString());
522+
CommandSourceStack css = getBrigadierSourceFromCommandSender(new BukkitPlayer(Bukkit.getOnlinePlayers().iterator().next()));
523+
524+
List<CommandFunction> tagFunctions = new ArrayList<>();
525+
for(List<String> functionCommands : commands) {
526+
tagFunctions.add(CommandFunction.fromLines(resourceLocation, Brigadier.getCommandDispatcher(), css, functionCommands));
527+
}
528+
this.tags.put(resourceLocation, tagFunctions);
529+
}
530+
455531
@Override
456532
public org.bukkit.advancement.Advancement addAdvancement(NamespacedKey key) {
457533
advancementDataWorld.advancements.advancements.put(new ResourceLocation(key.toString()),

0 commit comments

Comments
 (0)
0