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

Please consider adding example code #35

Open
chrisliebaer opened this issue Oct 12, 2018 · 9 comments
Open

Please consider adding example code #35

chrisliebaer opened this issue Oct 12, 2018 · 9 comments

Comments

@chrisliebaer
Copy link

The provided snipped on the project's readme doesn't even state which classes it's importing. I was unable to find out what argument is or where it is coming from. That's just one example but there are many more. I think a full example of a few simple commands should suffice.

@grum
Copy link

grum commented Oct 12, 2018

You can for example see it being used in the tests, see:

import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;

@chrisliebaer
Copy link
Author

After digging through the test cases I think I got a fairly good understanding how to use this library, thanks for the hint. However I'm still unable to compile even the base example. Here is a minimal example I created to test the issue:

import com.mojang.brigadier.CommandDispatcher;
		
import static com.mojang.brigadier.arguments.IntegerArgumentType.*;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.*;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.*;


public class BrigadierMinimal {
	
	public static void main(String[] args) {
		CommandDispatcher<String> dispatcher = new CommandDispatcher<>();
		dispatcher.register(literal("foo")
			.then(argument("bar", integer())
				.executes(c -> {
					System.out.println("Bar is " + getInteger(c, "bar"));
					return 1;
				})
			)
			.executes(c -> {
				System.out.println("Called foo with no arguments");
				return 1;
			})
		);
	}
}

The error reported by the compiler (JDK 10.0.2) is as follows

Error:(21, 58) java: incompatible types: com.mojang.brigadier.builder.LiteralArgumentBuilder<java.lang.Object> cannot be converted to com.mojang.brigadier.builder.LiteralArgumentBuilder<java.lang.String>

Apparently, Java is unable to correctly infere the generic type of argument and literal which should be String and instead comes to the conclusion that the generic type must be Object. Explicitly setting the proper type (see https://stackoverflow.com/a/5297999) solves the issue but makes me wonder if this is actually intended behavior. I noticed that all tests are using Object as the generic sender type and thous will never encounter this issue.

So I'm uncertain if I just ran into a strange compiler bug that only affects my specific setup or if I'm actually doing something wrong.

@Pokechu22
Copy link

It's a more general thing. MC itself defines more specific versions to work around that and statically imports those instead:

    public static LiteralArgumentBuilder<CommandSource> literal(String name) {
        return LiteralArgumentBuilder.<CommandSource>literal(name);
    }

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

@remtori
Copy link

remtori commented Oct 13, 2018

Can anyone show me a good example of using fork, redirect and forward. i has look in the tests but still doesn't understand it clearly.

@sotirosn
Copy link

sotirosn commented Aug 8, 2019

So I am new to modding minecraft but figured I would mod 1.14 since it is the latest at the moment of writing this. I stuck the code from the readme into my mod but it does not seem to link to the in-game console. I want to create a custom command: "/import" that opens a dialog window but when I type the command in the console it says 'Unknown command'.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.common.MinecraftForge;
import com.mojang.brigadier.CommandDispatcher;

@Mod("importexportmod")
public class ImportExportMod {
    private final Logger log = LogManager.getLogger();
    private final ImportScreen importScreen = new ImportScreen();

    public ImportExportMod() {
        log.info("new ImportExportMod");

        // Register ourselves for server and other game events we are interested in
        MinecraftForge.EVENT_BUS.register(this);

        CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
        dispatcher.register(Commands.literal("import").executes(command -> {
            log.info("import command dispatched");

            //importScreen.show();

            return 0;
        }));
    }
}

I imagine I am doing something wrong creating a new CommandDispatcher instead of acquiring one already created and linked to the command console.

===================== EDIT =========================
Ok I figured it out.

    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
        log.info("onServerStarting");
        event.getServer().getCommandManager().getDispatcher().register(Commands.literal("import").executes(command -> {
            log.info("/import command dispatched");
            //importScreen.show();
            return 0;
        }));
    }

@limwa
Copy link

limwa commented Sep 29, 2019

I agree that some example code is needed, especially code that shows people how to properly create new arguments!

@KvaGram
Copy link

KvaGram commented Dec 17, 2019

Have a +1 from me.
I too want some kind of workable sample code of how you use this. More than the fragmented snippets seen in this thread, that is.

@i509VCB
Copy link

i509VCB commented Dec 30, 2019

So I did write up a tutorial on how to use brigadier within a fabric context but I did explain much of brigadier's logic including creating an argument type to parse a uuid which applies to about any project that would use brigadier (including some examples and even chainable commands [not exactly with any current logic to in a slight example).

https://fabricmc.net/wiki/tutorial:commands#brigadier_explained

This is also a great explanation on how to do brigadier a bit less messy. I've been using this personally in recent weeks.
https://gist.github.com/falkreon/f58bb91e45ba558bc7fd827e81c6cb45

@KvaGram
Copy link

KvaGram commented Jan 14, 2020

I struggle with creating a command source object,
Whenever I try creating a CommandDispatcher<S> with my own CommandSource as the <S> type, my compiler starts complaning that CommandSource can't be converted to Object.

Clearly something is wrong, but I can't tell if the fault lies in my code or Brigadier.
All the test-code I find in the sourcecode all use Object as the source type. And the example in the readme don't show how the argument classes are imported.

Please, we need a proper example code that shows how to properly set this up!

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

No branches or pull requests

9 participants