The method execute(CommandSource, CommandContext) of type new CommandExecutor(){} must override a superclass method

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???

Are you sure that your class is implementing CommandExecutor?

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;

public class Test implements CommandExecutor {

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        return CommandResult.success()
    }
}

If you’re going to put the command executor in the main class, a reference lambda would be a better idea.

That’s really more of a matter of function versus location.

Yes, I have imported CommandExecutor.

I also get another message/error that says:

implements org.spongepowered.api.command.spec.CommandExecutor.execute.

Could you copy/paste your entire code here in a block, like I did above?

I have tried this and it didn’t work.

.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.

Code works fine for me. Did you make sure that CommandExecutor was imported?

Yes, it is. Hmmm… I have no idea at this point.

This is the full code.

package mycommand;

import com.google.inject.Inject;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.entity.living.player.Player;
import com.google.common.base.Function;

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

Someone else is having the same issue…

Can you read over this post so I don’t have to write it out twice? Hopefully it helps.

This problem can be worked around by using a lambda instead of an anonymous class.

.executor((src, args) -> {
    game.getCommandManager().process(game.getServer().getConsole(), "random text");
    game.getServer().getBroadcastChannel().send(Text.of("message"));
    return CommandResult.success();
})

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?

2 Likes