Minestom / Minestom

1.21 Lightweight Minecraft server
https://minestom.net
Apache License 2.0
2.48k stars 395 forks source link

Command error #1190

Closed DevBlokJochem closed 2 years ago

DevBlokJochem commented 2 years ago

I have loaded multiple different command from different extensions, and they are all giving this error:

> java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
    at java.base/java.util.Objects.checkIndex(Objects.java:359)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at net.minestom.server.command.CommandManager.createCommandNodes(CommandManager.java:348)
    at net.minestom.server.command.CommandManager.serializeCommand(CommandManager.java:250)
    at net.minestom.server.command.CommandManager.buildPacket(CommandManager.java:196)
    at net.minestom.server.command.CommandManager.createDeclareCommandsPacket(CommandManager.java:170)
    at net.minestom.server.entity.Player.refreshCommands(Player.java:461)
    at net.minestom.server.entity.Player.UNSAFE_init(Player.java:281)
    at net.minestom.server.network.ConnectionManager.lambda$updateWaitingPlayers$5(ConnectionManager.java:266)
    at org.jctools.queues.MessagePassingQueueUtil.drain(MessagePassingQueueUtil.java:39)
    at org.jctools.queues.BaseMpscLinkedArrayQueue.drain(BaseMpscLinkedArrayQueue.java:638)
    at org.jctools.queues.MpscUnboundedArrayQueue.drain(MpscUnboundedArrayQueue.java:23)
    at org.jctools.queues.MpscUnboundedArrayQueue.drain(MpscUnboundedArrayQueue.java:63)
    at net.minestom.server.network.ConnectionManager.updateWaitingPlayers(ConnectionManager.java:256)
    at net.minestom.server.ServerProcessImpl$TickerImpl.tick(ServerProcessImpl.java:283)
    at net.minestom.server.thread.TickSchedulerThread.run(TickSchedulerThread.java:24)

Im taking the permission extension for example. This is the code to register the command:

private fun registerCommands() {
        val commandManager = MinecraftServer.getCommandManager()
        commandManager.register(PermissionCommand())
    }

And this is the code of the command:

class PermissionCommand : Command("permission") {
    private fun usage(sender: CommandSender, context: CommandContext) {
        sender.msg("§cUsage: /permission")
    }

    private fun executeOnInfo(
        sender: CommandSender,
    ) {
        if(!sender.checkPermission("permissions_info")) {
            return sender.msg(messagesConfig.noPermission)
        }
        sender.sendMessage("&7/permission group create <name> \n " +
                "/permission group remove <name> \n" +
                "/permission group <name> prefix <prefix> \n" +
                "/permission group <name> suffix <suffix> \n" +
                "/permission group <name> permission remove <permission> \n" +
                "/permission group <name> permission add <permission> \n" +
                "/permission group <name> level <level> \n" +
                "/permission group <name> setname <name> \n" +
                "/permission user <name> permission add <permission> \n" +
                "/permission user <name> permission remove <permission> \n" +
                "/permission user <name> group add <group> \n" +
                "/permission user <name> group remove <group>")
    }

    private fun targetCallback(sender: CommandSender, exception: ArgumentSyntaxException) {
        sender.msg(messagesConfig.invalidPlayer)
    }

    init {
        setDefaultExecutor { sender: CommandSender, context: CommandContext ->
            usage(
                sender,
                context
            )
        }
        val player = ArgumentType.Entity("player")
            .onlyPlayers(true)
            .singleEntity(true)

        val permissions = ArgumentString("permissions")
        val prefixs = ArgumentString("prefixs")
        val groupName = ArgumentString("groupName")
        val suffixs = ArgumentString("suffixs")
        val levels = ArgumentInteger("levels")
        val newName = ArgumentString("newName")

        val groupsList : ArrayList<String> = ArrayList()
        val groups = ArgumentString("groups")
        groupData.groups.forEach {
            groupsList.add(it.key)
        }
        if(groupsList.size == 0) {
            groups.setSuggestionCallback { _, _, suggestion ->
                suggestion.addEntry(SuggestionEntry("Create a group first."))
            }
        }else{
            groups.setSuggestionCallback { _, _, suggestion ->
                groupsList.forEach {
                    suggestion.addEntry(SuggestionEntry(it))
                }
            }
        }

        //literals
        val user = Literal("user")
        val permission = Literal("permission")
        val group = Literal("group")
        val create = Literal("create")
        val add = Literal("add")
        val remove = Literal("remove")
        val prefix = Literal("prefix")
        val suffix = Literal("suffix")
        val level = Literal("level")
        val name = Literal("name")

        setArgumentCallback({ sender: CommandSender, exception: ArgumentSyntaxException ->
            targetCallback(
                sender,
                exception
            )
        }, player)

        addSyntax({ sender: CommandSender, _ -> executeOnInfo(sender) })
        //UserCommands
        addSyntax({ sender: CommandSender, context: CommandContext -> UserCommand().executeOnPermissionAdd(sender, context, permissions) }, user, player, permission, add, permissions)
        addSyntax({ sender: CommandSender, context: CommandContext -> UserCommand().executeOnPermissionRemove(sender, context, permissions) }, user, player, permission, remove, permissions)
        addSyntax({ sender: CommandSender, context: CommandContext -> UserCommand().executeOnGroupAdd(sender, context, groups) }, user, player, group, add, groups)
        addSyntax({ sender: CommandSender, context: CommandContext -> UserCommand().executeOnGroupRemove(sender, context, groups) }, user, player, group, remove, groups)
        //GroupCommands
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupCreate(sender, context, groupName) }, group, create, groupName)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupRemove(sender, context, groups) }, group, remove, groups)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupPrefix(sender, context, groups, prefixs) }, group, groups, prefix, prefixs)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupSuffix(sender, context, groups, suffixs) }, group, groups, suffix, suffixs)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupPermissionAdd(sender, context, groups, permissions) }, group, groups, permission, add, permissions)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupPermissionRemove(sender, context, groups, permissions) }, group, groups, permission, remove, permissions)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupLevel(sender, context, groups, levels) }, group, groups, permission, level, levels)
        addSyntax({ sender: CommandSender, context: CommandContext -> GroupCommand().executeOnGroupName(sender, context, groups, newName) }, group, groups, permission, name, newName)
    }

}
Kebab11noel commented 2 years ago

1200 has been merged, which has completely reworked the relevant section, can you check if the problem still exists?