Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree-Structure does not inherit generics #112

Open
Placeblock opened this issue Jul 16, 2022 · 3 comments
Open

Tree-Structure does not inherit generics #112

Placeblock opened this issue Jul 16, 2022 · 3 comments

Comments

@Placeblock
Copy link

This problem is linked to this issue.

I tried implementing Brigadier, but if i try to create my argumentbuilder structure there is an exception.
The problem is that the generic information of the CommandDispatcher isn't passed to the added child-argumentbuilder

You can simplify the problem to theese few lines of code:

public class Main {

    public static void main(String[] args) {
        Node.<String>node().addChild(
            Node.node().addChild(
                Node.node()
            )
        );
    }

    private static class Node<S> {
        public static <S> Node<S> node() {
            return new Node<>();
        }

        public Node<S> addChild(Node<S> node) {
            return this;
        }
    }
}

If i try to execute this i get a compiler error saying: error: incompatible types: Node<Object> cannot be converted to Node<String>
Its obvious that my child node doesn't get the generic Type S from the root node and i think it is because of type erasure.
I am using Java 17 and because brigadier is written in java 8 i changed my java version to 8 but it doesn't work too.

I just don't understand how it can work for all the other devs using brigadier, has somebody a solution for that?

@portlek
Copy link

portlek commented Jul 16, 2022

#111 (comment)

@Placeblock
Copy link
Author

I know that it works if you specify the generic type S in every single child, but this reduces code readability a lot if your generic type has many letters. In the examples given in the readme it works without specifying the generic type in every single child... i understand why it doesn't work but i wonder why it works for all the other people and devs using brigadier...

@Veritaris
Copy link

Veritaris commented Oct 10, 2022

On new MC versions there are Commands.literal that u can use, not from Brigadier
I also find it too messy to call builder with types explicit writing so
As for me works well class I named BrigadierExtensions that contains the following code (commonly I use MC 1.7.10 so using ServerCommandSender works good to me):

public class BrigadierExtensions {
    public static LiteralArgumentBuilder<ServerCommandSender> literal(String name) {
        return LiteralArgumentBuilder.literal(name);
    }

    public static <T> RequiredArgumentBuilder<ServerCommandSender, T> argument(final String name, final ArgumentType<T> type) {
        return RequiredArgumentBuilder.argument(name, type);
    }
}

After I made this I had no need to declare my command and arguments explicitly before dispatcher.register() call
I suppose if you need extra methods that does not fit you can do same for them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants