brigadier icon indicating copy to clipboard operation
brigadier copied to clipboard

Please consider adding example code

Open chrisliebaer opened this issue 6 years ago • 9 comments

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.

chrisliebaer avatar Oct 12 '18 16:10 chrisliebaer

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

https://github.com/Mojang/brigadier/blob/bbfb8a7da1b4836eefe580438b47769fd0d69254/src/test/java/com/mojang/brigadier/CommandSuggestionsTest.java#L22

grum avatar Oct 12 '18 17:10 grum

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.

chrisliebaer avatar Oct 13 '18 04:10 chrisliebaer

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);
    }

Pokechu22 avatar Oct 13 '18 04:10 Pokechu22

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.

remtori avatar Oct 13 '18 12:10 remtori

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;
        }));
    }

sotirosn avatar Aug 08 '19 10:08 sotirosn

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

limwa avatar Sep 29 '19 19:09 limwa

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.

KvaGram avatar Dec 17 '19 14:12 KvaGram

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

i509VCB avatar Dec 30 '19 17:12 i509VCB

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!

KvaGram avatar Jan 14 '20 12:01 KvaGram