Root extends RootCommandNode<CommandSender> implements Mutable<CommandSender> { @Override public void addChild(CommandNode<CommandSender> command) { if (getChild(command.getName()) != null) { throw new IllegalArgumentException("Invalid command: '" + command.getName() + "', root already contains a child with the same name"); } else if (!(command instanceof LiteralCommandNode<?>)) { throw new IllegalArgumentException("Invalid command: '" + command.getName() + "', commands registered to root must be a literal"); } var literal = (LiteralCommandNode<CommandSender>) command; var wrapper = map.register(literal); if (wrapper == null) { return; } super.addChild(Literal.alias(literal, prefix + ":" + literal.getName())); if (wrapper.getName().equals(wrapper.getLabel())) { super.addChild(literal); } if (literal instanceof Aliasable<?>) { for (var alias : new ArrayList<>((((Aliasable<CommandSender>) literal).aliases()))) { if (wrapper.getAliases().contains(alias.getName())) { super.addChild(Literal.alias(literal, prefix + ":" + alias.getName())); super.addChild(alias); } } } } Root(String prefix, PlatformMap map); @Override void addChild(CommandNode<CommandSender> command); @Override CommandNode<CommandSender> removeChild(String child); @Override void setCommand(@Ignored com.mojang.brigadier.Command<CommandSender> command); @Override void setRedirect(@Ignored CommandNode<CommandSender> destination); PlatformMap map();  }
@Test void addChild() { when(map.register(literal)).thenReturn(new DispatcherCommand("a", null, null, null, List.of("a1"))); root.addChild(literal); assertNotNull(root.getChild("a")); assertNotNull(root.getChild("test:a")); assertNotNull(root.getChild("a1")); assertNotNull(root.getChild("test:a1")); }
@Test void addChild_bukkit_contains_name() { var command = new DispatcherCommand("a", null, null, null, List.of("a1")); command.setLabel("label"); when(map.register(literal)).thenReturn(command); root.addChild(literal); assertNull(root.getChild("a")); assertNotNull(root.getChild("test:a")); assertNotNull(root.getChild("a1")); assertNotNull(root.getChild("test:a1")); }
@Test void addChild_bukkit_contains_alias() { when(map.register(literal)).thenReturn(new DispatcherCommand("a", null, null, null, List.of())); root.addChild(literal); assertNotNull(root.getChild("a")); assertNotNull(root.getChild("test:a")); assertNull(root.getChild("a1")); assertNull(root.getChild("test:a1")); }
@Test void addChild_unregistered() { root.addChild(Literal.of("a").build()); assertEquals(0, root.getChildren().size()); }
@Test void addChild_duplicate_throws_exception() { when(map.register(any())).thenReturn(new DispatcherCommand("", null, null, null, List.of(""))); root.addChild(Literal.of("a").build()); assertEquals("Invalid command: 'a', root already contains a child with the same name", assertThrows(IllegalArgumentException.class, () -> root.addChild(Literal.of("a").build())).getMessage() ); }