I am tinkering around with sponge and wanted to try putting the command executor in the same class. This works except I get an error message from these lines:
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
It says that it needs to override the superclass, except it is located in the superclass… any suggestions???
.executor(new CommandExecutor() { @Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (args.getOne(“int”).isPresent() && (args.getOne(“int”).isPresent()) && (args.getOne(“message”).isPresent())) {
From this I get the error message: The method execute(CommandSource, CommandContext) of type new CommandExecutor(){} must override a superclass
method.
@Plugin(id = “mycommand”, name = “MyCommand”, version = “1.0-SNAPSHOT”, description = “Random stuff”,
authors = {“Thunder”}, url = “nul”)
public class Main {
@Inject
private Game game;
@Inject Logger logger;
@Listener
public void onGameInitializationEvent(GameInitializationEvent event) {
CommandSpec MyCommand = CommandSpec.builder()
.permission("Start command with syntax of /command [int] [int2] [message]")
.permission(Text.of("description"))
.arguments (
GenericArguments.integer(Text.of("int")),
GenericArguments.integer(Text.of("int2")),
GenericArguments.remainingJoinedStrings(Text.of("message")))
.executor(new CommandExecutor() {
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (args.getOne("itemid").isPresent() && (args.getOne("int2").isPresent()) && (args.getOne("message").isPresent())) {
Sponge.getCommandManager().process(Sponge.getServer().getConsole(), "random text "+"int"+" "+"int2");
Sponge.getServer()
.getBroadcastChannel()
.send(Text.of("message"));
return CommandResult.success();
}
}})
.build();
Why are you checking if the args are present? They are not optional therefore you do not need to check if they are present. You also have .permission twice and no description. This code works for me
@Listener
public void onGameInitializationEvent(GameInitializationEvent event) {
CommandSpec MyCommand = CommandSpec.builder()
.permission("PERMISSION HERE")
.description(Text.of("DESCRIPTION HERE"))
.arguments(GenericArguments.integer(Text.of("int")), GenericArguments.integer(Text.of("int2")), GenericArguments.remainingJoinedStrings(Text.of("message")))
.executor(new CommandExecutor() {
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
int number1 = args.<Integer>getOne("int").get();
int number2 = args.<Integer>getOne("int2").get();
String message = args.<String>getOne("message").get();
// DO STUFF HERE
return CommandResult.success();
}
}).build();
}
People arn’t always interested in workarounds, this error, on a class that is clearly overriding a method from a superclass or interface is a clear error of something else being totally wrong. A work around isn’t appropriate.
Unless you are keen on OP seeing an even more cryptic message due to Java’s “enhanced” type inference from lambda’s , Lets fix the first problem first before dealing with controversial code style issues?