8000 Update fixPermissions and add tests for commands with custom namespaces · CommandAPI/CommandAPI@709da8d · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 709da8d

Browse files
committed
Update fixPermissions and add tests for commands with custom namespaces
1 parent 3ffc44d commit 709da8d

File tree

3 files changed

+191
-120
lines changed

3 files changed

+191
-120
lines changed

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,15 @@ public static <CommandSource> String getRawArgumentInput(CommandContext<CommandS
122122
final CommandAPIPlatform<Argument, CommandSender, Source> platform;
123123
final TreeMap<String, CommandPermission> registeredPermissions = new TreeMap<>();
124124
final List<RegisteredCommand> registeredCommands; // Keep track of what has been registered for type checking
125+
final Map<String, List<RegisteredCommand>> registeredCommandMap; // Keep track of registered commands in a map for permission lookups
125126
final Map<List<String>, Previewable<?, ?>> previewableArguments; // Arguments with previewable chat
126127

127128
private static CommandAPIHandler<?, ?, ?> instance;
128129

129130
protected CommandAPIHandler(CommandAPIPlatform<Argument, CommandSender, Source> platform) {
130131
this.platform = platform;
131132
this.registeredCommands = new ArrayList<>();
133+
this.registeredCommandMap = new HashMap<>();
132134
this.previewableArguments = new HashMap<>();
133135

134136
CommandAPIHandler.instance = this;
@@ -617,6 +619,12 @@ void register(CommandMetaData<CommandSender> meta, final Argument[] args,
617619
fullDescription, usageDescription, aliases, permission, namespace);
618620
registeredCommands.add(registeredCommandInformation);
619621

622+
List<RegisteredCommand> registeredCommands = registeredCommandMap.containsKey(commandName)
623+
? registeredCommandMap.get(commandName)
624+
: new ArrayList<>();
< 8000 /td>625+
registeredCommands.add(registeredCommandInformation);
626+
registeredCommandMap.put(commandName, registeredCommands);
627+
620628
// Handle previewable arguments
621629
handlePreviewableArguments(commandName, args, aliases);
622630

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPIBukkit.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,34 @@ private void fixPermissions() {
249249
* If anyone dares tries to use testPermission() on this command, seriously,
250250
* what are you doing and why?
251251
*/
252-
for(Command command : new Command[] { map.getCommand(cmdName), map.getCommand("minecraft:" + cmdName) }) {
252+
List<Command> commands = new ArrayList<>();
253+
commands.add(map.getCommand(cmdName));
254+
255+
List<RegisteredCommand> registeredCommands = CommandAPIHandler.getInstance().registeredCommandMap.get(cmdName);
256+
257+
if (registeredCommands == null) {
258+
// This happens when dealing with aliases
259+
// Doing something here is not necessary because we're
260+
// already dealing with them further down this method
261+
continue;
262+
}
263+
264+
for (RegisteredCommand registeredCommand : registeredCommands) {
265+
String namespace = registeredCommand.namespace().isEmpty()
266+
? ""
267+
: registeredCommand.namespace() + ":";
268+
269+
if (!namespace.isEmpty()) {
270+
commands.add(map.getCommand(namespace + cmdName));
271+
}
272+
273+
for (String alias : registeredCommand.aliases()) {
274+
commands.add(map.getCommand(alias));
275+
commands.add(map.getCommand(namespace + alias));
276+
}
277+
}
278+
279+
for(Command command : commands) {
253280
if (command != null && isVanillaCommandWrapper(command)) {
254281
command.setPermission(permNode);
255282
}

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

Lines changed: 155 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.jorel.commandapi.test;
22

3-
import be.seeseemelk.mockbukkit.MockBukkit;
43
import com.mojang.brigadier.exceptions.CommandSyntaxException;
54
import com.mojang.brigadier.tree.CommandNode;
65
import com.mojang.brigadier.tree.RootCommandNode;
@@ -17,6 +16,8 @@
1716
import org.bukkit.command.SimpleCommandMap;
1817
import org.bukkit.entity.Player;
1918
import org.bukkit.event.server.ServerLoadEvent;
19+
import org.bukkit.permissions.Permission;
20+
import org.bukkit.permissions.PermissionAttachment;
2021
import org.junit.jupiter.api.AfterEach;
2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Disabled;
@@ -68,7 +69,9 @@ Player enableWithNamespaces() {
6869
// Get a CraftPlayer for running VanillaCommandWrapper commands
6970
Player runCommandsPlayer = Mockito.mock(MockPlatform.getInstance().getCraftPlayerClass());
7071
// Give player permission to run command
71-
Mockito.when(runCommandsPlayer.hasPermission(any(String.class))).thenReturn(true);
72+
/*Mockito.when(runCommandsPlayer.hasPermission(any(String.class))).thenAnswer(
73+
invocation -> invocation.getArgument(0).equals("permission")
74+
);*/
7275
// Get location is used when creating the BrigadierSource in MockNMS
7376
Mockito.when(runCommandsPlayer.getLocation()).thenReturn(new Location(null, 0, 0, 0));
7477

@@ -143,123 +146,6 @@ public void testSameCommandNameWithDefaultNamespaceAndCustomNamespace(boolean en
143146
assertNoMoreResults(results);
144147
}
145148

146-
@ParameterizedTest
147-
@ValueSource(booleans = {true, false})
148-
public void testCommandTreeRegistration(boolean enableBeforeRegistering) {
149-
Mut<String> results = Mut.of();
150-
151-
Player player = null;
152-
if (enableBeforeRegistering) {
153-
player = enableWithNamespaces();
154-
}
155-
156-
CommandTree command = new CommandTree("test")
157-
.then(new LiteralArgument("a")
158-
.then(new StringArgument("string")
159-
.executesPlayer(info -> {
160-
results.set("a");
161-
results.set(info.args().getUnchecked("string"));
162-
})
163-
)
164-
)
165-
.then(new LiteralArgument("b")
166-
.then(new IntegerArgument("integer")
167-
.executesPlayer(info -> {
168-
results.set("b");
169-
results.set(String.valueOf(info.args().get("integer")));
170-
})
171-
)
172-
);
173-
174-
// Make sure the default registration with the minecraft: namespace works
175-
command.register();
176-
177-
if (!enableBeforeRegistering) {
178-
player = enableWithNamespaces();
179-
}
180-
181-
server.dispatchCommand(player, "test a alpha");
182-
assertEquals("a", results.get());
183-
assertEquals("alpha", results.get());
184-
185-
server.dispatchCommand(player, "minecraft:test a alpha");
186-
assertEquals("a", results.get());
187-
assertEquals("alpha", results.get());
188-
189-
server.dispatchCommand(player, "test b 123");
190-
assertEquals("b", results.get());
191-
assertEquals("123", results.get());
192-
193-
server.dispatchCommand(player, "minecraft:test b 123");
194-
assertEquals("b", results.get());
195-
assertEquals("123", results.get());
196-
197-
CommandAPI.unregister("test", true);
198-
199-
command.register("namespace");
200-
201-
server.dispatchCommand(player, "test a alpha");
202-
assertEquals("a", results.get());
203-
assertEquals("alpha", results.get());
204-
205-
server.dispatchCommand(player, "namespace:test a alpha");
206-
assertEquals("a", results.get());
207-
assertEquals("alpha", results.get());
208-
209-
server.dispatchCommand(player, "test b 123");
210-
assertEquals("b", results.get());
211-
assertEquals("123", results.get());
212-
213-
server.dispatchCommand(player, "namespace:test b 123");
214-
assertEquals("b", results.get());
215-
assertEquals("123", results.get());
216-
217-
// Running the command with the minecraft: namespace should fail
218-
assertCommandFailsWith(
219-
player,
220-
"minecraft:test a alpha",
221-
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
222-
);
223-
assertCommandFailsWith(
224-
player,
225-
"minecraft:test b 123",
226-
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
227-
);
228-
229-
CommandAPI.unregister("test", true);
230-
231-
command.register(MockPlatform.getConfiguration().getPlugin());
232-
233-
server.dispatchCommand(player, "test a alpha");
234-
assertEquals("a", results.get());
235-
assertEquals("alpha", results.get());
236-
237-
server.dispatchCommand(player, "commandapitest:test a alpha");
238-
assertEquals("a", results.get());
239-
assertEquals("alpha", results.get());
240-
241-
server.dispatchCommand(player, "test b 123");
242-
assertEquals("b", results.get());
243-
assertEquals("123", results.get());
244-
245-
server.dispatchCommand(player, "commandapitest:test b 123");
246-
assertEquals("b", results.get());
247-
assertEquals("123", results.get());
248-
249-
// Running the command with the minecraft: namespace should fail
250-
assertCommandFailsWith(
251-
player,
252-
"minecraft:test a alpha",
253-
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
254-
);
255-
assertCommandFailsWith(
256-
player,
257-
"minecraft:test b 123",
258-
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
259-
);
260-
261-
assertNoMoreResults(results);
262-
}
263149

264150
@Test
265151
public void testNullNamespace() {
@@ -726,4 +612,154 @@ public void testCommandNameConflictButDifferentNamespace(boolean enableBeforeReg
726612

727613
assertNoMoreResults(results);
728614
}
615+
616+
@ParameterizedTest
617+
@ValueSource(booleans = {true, false})
618+
public void testCommandTreeRegistration(boolean enableBeforeRegistering) {
619+
Mut<String> results = Mut.of();
620+
621+
Player player = null;
622+
if (enableBeforeRegistering) {
623+
player = enableWithNamespaces();
624+
}
625+
626+
CommandTree command = new CommandTree("test")
627+
.then(new LiteralArgument("a")
628+
.then(new StringArgument("string")
629+
.executesPlayer(info -> {
630+
results.set("a");
631+
results.set(info.args().getUnchecked("string"));
632+
})
633+
)
634+
)
635+
.then(new LiteralArgument("b")
636+
.then(new IntegerArgument("integer")
637+
.executesPlayer(info -> {
638+
results.set("b");
639+
results.set(String.valueOf(info.args().get("integer")));
640+
})
641+
)
642+
);
643+
644+
// Make sure the default registration with the minecraft: namespace works
645+
command.register();
646+
647+
if (!enableBeforeRegistering) {
648+
player = enableWithNamespaces();
649+
}
650+
651+
server.dispatchCommand(player, "test a alpha");
652+
assertEquals("a", results.get());
653+
assertEquals("alpha", results.get());
654+
655+
server.dispatchCommand(player, "minecraft:test a alpha");
656+
assertEquals("a", results.get());
657+
assertEquals("alpha", results.get());
658+
659+
server.dispatchCommand(player, "test b 123");
660+
assertEquals("b", results.get());
661+
assertEquals("123", results.get());
662+
663+
server.dispatchCommand(player, "minecraft:test b 123");
664+
assertEquals("b", results.get());
665+
assertEquals("123", results.get());
666+
667+
CommandAPI.unregister("test", true);
668+
669+
command.register("namespace");
670+
671+
server.dispatchCommand(player, "test a alpha");
672+
assertEquals("a", results.get());
673+
assertEquals("alpha", results.get());
674+
675+
server.dispatchCommand(player, "namespace:test a alpha");
676+
assertEquals("a", results.get());
677+
assertEquals("alpha", results.get());
678+
679+
server.dispatchCommand(player, "test b 123");
680+
assertEquals("b", results.get());
681+
assertEquals("123", results.get());
682+
683+
server.dispatchCommand(player, "namespace:test b 123");
684+
assertEquals("b", results.get());
685+
assertEquals("123", results.get());
686+
687+
// Running the command with the minecraft: namespace should fail
688+
assertCommandFailsWith(
689+
player,
690+
"minecraft:test a alpha",
691+
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
692+
);
693+
assertCommandFailsWith(
694+
player,
695+
"minecraft:test b 123",
696+
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
697+
);
698+
699+
CommandAPI.unregister("test", true);
700+
701+
command.register(MockPlatform.getConfiguration().getPlugin());
702+
703+
server.dispatchCommand(player, "test a alpha");
704+
assertEquals("a", results.get());
705+
assertEquals("alpha", results.get());
706+
707+
server.dispatchCommand(player, "commandapitest:test a alpha");
708+
assertEquals("a", results.get());
709+
assertEquals("alpha", results.get());
710+
711+
server.dispatchCommand(player, "test b 123");
712+
assertEquals("b", results.get());
713+
assertEquals("123", results.get());
714+
715+
server.dispatchCommand(player, "commandapitest:test b 123");
716+
assertEquals("b", results.get());
717+
assertEquals("123", results.get());
718+
719+
// Running the command with the minecraft: namespace should fail
720+
assertCommandFailsWith(
721+
player,
722+
"minecraft:test a alpha",
723+
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
724+
);
725+
assertCommandFailsWith(
726+
player,
727+
"minecraft:test b 123",
728+
"Unknown or incomplete command, see below for error at position 0: <--[HERE]"
729+
);
730+
731+
assertNoMoreResults(results);
732+
}
733+
734+
@Disabled
735+
@Test
736+
public void testPermissions() {
737+
CommandAPICommand command = new CommandAPICommand("test")
738+
.withPermission("permission.node")
739+
.executesPlayer(P_EXEC);
740+
741+
// Test with default minecraft: namespace
742+
command.register();
743+
744+
Player player = enableWithNamespaces();
745+
746+
assertCommandFailsWith(player, "test", "Unknown or incomplete command, see below for error at position 0: <--[HERE]");
747+
assertCommandFailsWith(player, "minecraft:test", "Unknown or incomplete command, see below for error at position 0: <--[HERE]");
748+
749+
assertTrue(server.dispatchCommand(player, "test"));
750+
assertTrue(server.dispatchCommand(player, "minecraft:test"));
751+
752+
// Unset permission und unregister command
753+
CommandAPI.unregister("test", true);
754+
755+
// Test with custom namespace (same as with a plugins)
756+
command.register("commandnamespace");
757+
758+
assertCommandFailsWith(player, "test", "Unknown or incomplete command, see below for error at position 0: <--[HERE]");
759+
assertCommandFailsWith(player, "commandnamespace:test", "Unknown or incomplete command, see below for error at position 0: <--[HERE]");
760+
761+
assertTrue(server.dispatchCommand(player, "test"));
762+
assertTrue(server.dispatchCommand(player, "commandnamespace:test"));
763+
}
764+
729765
}

0 commit comments

Comments
 (0)
0