8000 Implement function argument tests for 1.17. Hotfix 1.18 tests blowing up · CommandAPI/CommandAPI@d603627 · GitHub
[go: up one dir, main page]

Skip to content

Commit d603627

Browse files
committed
Implement function argument tests for 1.17. Hotfix 1.18 tests blowing up
1 parent c7831de commit d603627

File tree

5 files changed

+123
-7
lines changed
  • commandapi-platforms/commandapi-bukkit
    • commandapi-bukkit-nms
    • commandapi-bukkit-test

5 files changed

+123
-7
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.17-common/src/main/java/dev/jorel/commandapi/nms/NMS_1_17_Common.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.io.IOException;
2525
import java.nio.charset.StandardCharsets;
2626
import java.util.ArrayList;
27+
import java.util.HashSet;
2728
import java.util.List;
2829
import java.util.Map;
2930
import java.util.Optional;
31+
import java.util.Set;
3032
import java.util.function.Predicate;
3133
import java.util.function.ToIntFunction;
3234

@@ -638,4 +640,26 @@ public <T> T getMinecraftServer() {
638640
}
639641
}
640642

643+
@Override
644+
public final SimpleFunctionWrapper getFunction(NamespacedKey key) {
645+
final ResourceLocation resourceLocation = new ResourceLocation(key.getNamespace(), key.getKey());
646+
Optional<CommandFunction> commandFunctionOptional = this.<MinecraftServer>getMinecraftServer().getFunctions().get(resourceLocation);
647+
if(commandFunctionOptional.isPresent()) {
648+
return convertFunction(commandFunctionOptional.get());
649+
} else {
650+
throw new IllegalStateException("Failed to get defined function " + key
651+
+ "! This should never happen - please report this to the CommandAPI"
652+
+ "developers, we'd love to know how you got this error message!");
653+
}
654+
}
655+
656+
@Override
657+
public final Set<NamespacedKey> getFunctions() {
658+
Set<NamespacedKey> result = new HashSet<>();
659+
for (ResourceLocation resourceLocation : this.<MinecraftServer>getMinecraftServer().getFunctions().getFunctionNames()) {
660+
result.add(fromResourceLocation(resourceLocation));
661+
}
662+
return result;
663+
}
664+
641665
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_Common.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ public final FloatRange getFloatRange(CommandContext<CommandSourceStack> cmdCtx,
464464
public abstract FunctionWrapper[] getFunction(CommandContext<CommandSourceStack> cmdCtx, String key) throws CommandSyntaxException;
465465

466466
@Override
467-
public final SimpleFunctionWrapper getFunction(NamespacedKey key) {
467+
// TODO: This has its own implementation for 1.17
468+
public SimpleFunctionWrapper getFunction(NamespacedKey key) {
468469
final ResourceLocation resourceLocation = new ResourceLocation(key.getNamespace(), key.getKey());
469470
Optional<CommandFunction> commandFunctionOptional = this.<MinecraftServer>getMinecraftServer().getFunctions().get(resourceLocation);
470471
if(commandFunctionOptional.isPresent()) {
@@ -477,7 +478,8 @@ public final SimpleFunctionWrapper getFunction(NamespacedKey key) {
477478
}
478479

479480
@Override
480-
public final Set<NamespacedKey> getFunctions() {
481+
// TODO: This has its own implementation for 1.17
482+
public Set<NamespacedKey> getFunctions() {
481483
Set<NamespacedKey> result = new HashSet<>();
482484
for (ResourceLocation resourceLocation : this.<MinecraftServer>getMinecraftServer().getFunctions().getFunctionNames()) {
483485
result.add(fromResourceLocation(resourceLocation));

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.HashSet;
1414
import java.util.List;
1515
import java.util.Map;
16+
import java.util.Optional;
1617
import java.util.Set;
1718
import java.util.stream.Collectors;
1819
import java.util.stream.StreamSupport;
@@ -46,11 +47,14 @@
4647
import be.seeseemelk.mockbukkit.WorldMock;
4748
import be.seeseemelk.mockbukkit.enchantments.EnchantmentMock;
4849
import be.seeseemelk.mockbukkit.potion.MockPotionEffectType;
50+
import dev.jorel.commandapi.Brigadier;
4951
import dev.jorel.commandapi.CommandAPIBukkit;
5052
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
5153
import dev.jorel.commandapi.commandsenders.BukkitCommandSender;
54+
import dev.jorel.commandapi.commandsenders.BukkitPlayer;
5255
import net.minecraft.SharedConstants;
5356
import net.minecraft.advancements.Advancement;
57+
import net.minecraft.commands.CommandFunction;
5458
import net.minecraft.commands.CommandSourceStack;
5559
import net.minecraft.commands.arguments.EntityAnchorArgument.Anchor;
5660
import net.minecraft.core.BlockPos;
@@ -61,14 +65,20 @@
6165
import net.minecraft.server.Bootstrap;
6266
import net.minecraft.server.MinecraftServer;
6367
import net.minecraft.server.ServerAdvancementManager;
68+
import net.minecraft.server.ServerFunctionLibrary;
69+
import net.minecraft.server.ServerFunctionManager;
6470
import net.minecraft.server.ServerScoreboard;
6571
import net.minecraft.server.level.ServerLevel;
6672
import net.minecraft.server.level.ServerPlayer;
6773
import net.minecraft.server.players.GameProfileCache;
6874
import net.minecraft.server.players.PlayerList;
75+
import net.minecraft.tags.Tag;
76+
import net.minecraft.tags.TagCollection;
77+
import net.minecraft.util.profiling.metrics.profiling.InactiveMetricsRecorder;
6978
import net.minecraft.world.effect.MobEffect;
7079
import net.minecraft.world.item.crafting.Recipe;
7180
import net.minecraft.world.item.crafting.RecipeManager;
81+
import net.minecraft.world.level.GameRules;
7282
import net.minecraft.world.level.Level;
7383
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
7484
import net.minecraft.world.level.storage.loot.LootTables;
@@ -84,6 +94,8 @@ public class MockNMS extends Enums {
8494
List<ServerPlayer> players = new ArrayList<>();
8595
PlayerList playerListMock;
8696
final RecipeManager recipeManager;
97+
Map<ResourceLocation, CommandFunction> functions = new HashMap<>();
98+
Map<ResourceLocation, Collection<CommandFunction>> tags = new HashMap<>();
8799

88100
public MockNMS(CommandAPIBukkit<?> baseNMS) {
89101
super(baseNMS);
@@ -113,6 +125,7 @@ public MockNMS(CommandAPIBukkit<?> baseNMS) {
113125
registerDefaultEnchantments();
114126

115127
this.recipeManager = new RecipeManager();
128+
this.functions = new HashMap<>();
116129
registerDefaultRecipes();
117130
}
118131

@@ -303,6 +316,11 @@ public CommandSourceStack getBrigadierSourceFromCommandSender(AbstractCommandSen
303316
// ChatArgument, AdventureChatArgument
304317
Mockito.when(css.hasPermission(anyInt())).thenAnswer(invocation -> sender.isOp());
305318
Mockito.when(css.hasPermission(anyInt(), anyString())).thenAnswer(invocation -> sender.isOp());
319+
320+
// FunctionArgument
321+
// We don't really need to do anything funky here, we'll just return the same CSS
322+
Mockito.when(css.withSuppressedOutput()).thenReturn(css);
323+
Mockito.when(css.withMaximumPermission(anyInt())).thenReturn(css);
306324
}
307325
return css;
308326
}
@@ -428,9 +446,79 @@ public <T> T getMinecraftServer() {
428446
// RecipeArgument
429447
Mockito.when(minecraftServerMock.getRecipeManager()).thenAnswer(i -> this.recipeManager);
430448

449+
// FunctionArgument
450+
// We're using 2 as the function compilation level.
451+
Mockito.when(minecraftServerMock.getFunctionCompilationLevel()).thenReturn(2);
452+
Mockito.when(minecraftServerMock.getFunctions()).thenAnswer(i -> {
453+
ServerFunctionLibrary serverFunctionLibrary = Mockito.mock(ServerFunctionLibrary.class);
454+
455+
// Functions
456+
Mockito.when(serverFunctionLibrary.getFunction(any())).thenAnswer(invocation -> Optional.ofNullable(functions.get(invocation.getArgument(0))));
457+
Mockito.when(serverFunctionLibrary.getFunctions()).thenAnswer(invocation -> functions);
458+
459+
// Tags
460+
Mockito.when(serverFunctionLibrary.getTag(any())).thenAnswer(invocation -> {
461+
Collection<CommandFunction> tagsFromResourceLocation = tags.getOrDefault(invocation.getArgument(0), List.of());
462+
return Tag.fromSet(Set.copyOf(tagsFromResourceLocation));
463+
});
464+
Mockito.when(serverFunctionLibrary.getTags()).thenAnswer(invocation -> {
465+
Map<ResourceLocation, Tag<?>> tagMap = new HashMap<>();
466+
for(Map.Entry<ResourceLocation, Collection<CommandFunction>> entry : tags.entrySet()) {
467+
tagMap.put(entry.getKey(), Tag.fromSet(Set.copyOf(entry.getValue())));
468+
}
469+
return TagCollection.of((Map) tagMap);
470+
});
471+
472+
return new ServerFunctionManager(minecraftServerMock, serverFunctionLibrary) {
473+
474+
// Make sure we don't use ServerFunctionManager#getDispatcher!
475+
// That method accesses MinecraftServer.vanillaCommandDispatcher
476+
// directly (boo) and that causes all sorts of nonsense.
477+
@Override
478+
public CommandDispatcher<CommandSourceStack> getDispatcher() {
479+
return Brigadier.getCommandDispatcher();
480+
}
481+
};
482+
});
483+
484+
Mockito.when(minecraftServerMock.getGameRules()).thenAnswer(i -> new GameRules());
485+
Mockito.when(minecraftServerMock.getProfiler()).thenAnswer(i -> InactiveMetricsRecorder.INSTANCE.getProfiler());
486+
431487
return (T) minecraftServerMock;
432488
}
433489

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

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,13 @@
276276
<artifactId>commandapi-bukkit-test-impl-1.18</artifactId>
277277
<version>${project.version}</version>
278278
</dependency>
279-
<dependency>
279+
<!-- For reasons beyond my knowledge right now, including paper
280+
1.18 blows things up and causes every test to fail! -->
281+
<!-- <dependency>
280282
<groupId>io.papermc.paper</groupId>
281283
<artifactId>paper-api</artifactId>
282284
<version>1.18-R0.1-SNAPSHOT</version>
283-
</dependency>
285+
</dependency> -->
284286
<dependency>
285287
<groupId>org.spigotmc</groupId>
286288
<artifactId>spigot</artifactId>

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/test/arguments/ArgumentParticleTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void executionTestWithParticleArgumentBlock() {
188188

189189
@Test
190190
void executionTestWithParticleArgumentShriek() {
191-
assumeTrue(version.greaterThanOrEqualTo(MCVersion.V1_18));
191+
assumeTrue(version.greaterThanOrEqualTo(MCVersion.V1_19));
192192
Mut<ParticleData<?>> results = Mut.of();
193193

194194
new CommandAPICommand("test")
@@ -216,7 +216,7 @@ void executionTestWithParticleArgumentShriek() {
216216

217217
@Test
218218
void executionTestWithParticleArgumentSculkCharge() {
219-
assumeTrue(version.greaterThanOrEqualTo(MCVersion.V1_18));
219+
assumeTrue(version.greaterThanOrEqualTo(MCVersion.V1_19));
220220
Mut<ParticleData<?>> results = Mut.of();
221221

222222
new CommandAPICommand("test")
@@ -244,7 +244,7 @@ void executionTestWithParticleArgumentSculkCharge() {
244244

245245
@Test
246246
void executionTestWithParticleArgumentVibration() {
247-
assumeTrue(version.greaterThanOrEqualTo(MCVersion.V1_18));
247+
assumeTrue(version.greaterThanOrEqualTo(MCVersion.V1_19));
248248
Mut<ParticleData<?>> results = Mut.of();
249249

250250
new CommandAPICommand("test")

0 commit comments

Comments
 (0)
0