Comments (9)
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.
You can for example see it being used in the tests, see:
from brigadier.
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.
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.
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.
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.
I agree that some example code is needed, especially code that shows people how to properly create new arguments!
from brigadier.
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.
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)
- [BUG] `StringReader.readUnquotedString()` does not support non-ASCII characters HOT 4
- License HOT 4
- Can node graphs have cycles? HOT 1
- Tests (where relevant) don't actually make sure that an exception was thrown. HOT 2
- [Suggestion] Allow registration of 'docs' for completions HOT 1
- Literal has no way to mark as case insensitive.
- [Suggestion] Optional Arguments HOT 7
- Why do I have to use CommandDispatcher<Object> and not CommandDispatcher<CommandSender>? HOT 15
- Tree-Structure does not inherit generics HOT 3
- Sent by mistake. HOT 1
- List argument nodes (not types)
- bug
- Add 1.1.8 to GitHub releases
- Redirected commands with arguments act as the command is valid even when it isn't HOT 3
- Arguments in a `CommandContext` disappear when a node redirects HOT 2
- My Bedrock edition of minecraft has Cherry Blossom Capes, and because I voted, I wonder if the java edition will have cherry blossom Capes. My java id and Bedrock id are different. So I'm afraid I won't get the cape for my java version of the game, and I'll be sad..... HOT 2
- `StringReader#readString` returns a blank string while `StringReader#canRead` returns true, leading a confusing result HOT 1
- Cannot use non-ascii characters HOT 1
- Crashed because of Deepslate Stairs HOT 4
- Is there a way to make my mod's commands use a different prefix than '/'? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from brigadier.