|
1 | 1 | package dev.jorel.commandapi.test;
|
2 | 2 |
|
3 |
| -import be.seeseemelk.mockbukkit.MockBukkit; |
4 | 3 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
5 | 4 | import com.mojang.brigadier.tree.CommandNode;
|
6 | 5 | import com.mojang.brigadier.tree.RootCommandNode;
|
|
17 | 16 | import org.bukkit.command.SimpleCommandMap;
|
18 | 17 | import org.bukkit.entity.Player;
|
19 | 18 | import org.bukkit.event.server.ServerLoadEvent;
|
| 19 | +import org.bukkit.permissions.Permission; |
| 20 | +import org.bukkit.permissions.PermissionAttachment; |
20 | 21 | import org.junit.jupiter.api.AfterEach;
|
21 | 22 | import org.junit.jupiter.api.BeforeEach;
|
22 | 23 | import org.junit.jupiter.api.Disabled;
|
@@ -68,7 +69,9 @@ Player enableWithNamespaces() {
|
68 | 69 | // Get a CraftPlayer for running VanillaCommandWrapper commands
|
69 | 70 | Player runCommandsPlayer = Mockito.mock(MockPlatform.getInstance().getCraftPlayerClass());
|
70 | 71 | // 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 | + );*/ |
72 | 75 | // Get location is used when creating the BrigadierSource in MockNMS
|
73 | 76 | Mockito.when(runCommandsPlayer.getLocation()).thenReturn(new Location(null, 0, 0, 0));
|
74 | 77 |
|
@@ -143,123 +146,6 @@ public void testSameCommandNameWithDefaultNamespaceAndCustomNamespace(boolean en
|
143 | 146 | assertNoMoreResults(results);
|
144 | 147 | }
|
145 | 148 |
|
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 |
| - } |
263 | 149 |
|
264 | 150 | @Test
|
265 | 151 | public void testNullNamespace() {
|
@@ -726,4 +612,154 @@ public void testCommandNameConflictButDifferentNamespace(boolean enableBeforeReg
|
726 | 612 |
|
727 | 613 | assertNoMoreResults(results);
|
728 | 614 | }
|
| 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 | + |
729 | 765 | }
|
0 commit comments