Giter VIP home page Giter VIP logo

Comments (9)

Pokechu22 avatar Pokechu22 commented on September 3, 2024 8

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

from brigadier.

grum avatar grum commented on September 3, 2024 4

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

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

from brigadier.

chrisliebaer avatar chrisliebaer commented on September 3, 2024 1

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.

from brigadier.

i509VCB avatar i509VCB commented on September 3, 2024 1

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

from brigadier.

remtori avatar remtori commented on September 3, 2024

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.

from brigadier.

sotirosn avatar sotirosn commented on September 3, 2024

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

from brigadier.

limwa avatar limwa commented on September 3, 2024

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

from brigadier.

KvaGram avatar KvaGram commented on September 3, 2024

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.

from brigadier.

KvaGram avatar KvaGram commented on September 3, 2024

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!

from brigadier.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.