CommandExecutor issues

Hey guys,

So I will start by saying im new and still learning java. I am working on a /sethome command for spongeforge and im having some issues with setting a location. Does it look like I am on the right track? suggestions? or references would be deeply appreciated.

package com.mchammer.cheesepack;

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;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import com.flowpowered.math.vector.Vector3d;

public class CPSetHomeExecutor implements CommandExecutor {

public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
	if (src.getName().equalsIgnoreCase("sethome")) {
		if(src instanceof Player){
			Player player = (Player) src;
			if(src.hasPermission("CP.sethome.command")){
				Vector3d playerLocation = new Vector3d();
				playerLocation = (player.getLocation().getPosition());
				player.sendMessage(Text.of(TextColors.GREEN + "Home: ", TextColors.GOLD + "Set"));
			}else {
				src.sendMessage(Text.of("Only players are able to use this command."));
			}
		}	
	}
	return null;
}

}

  1. src.getName() return the name of the player or of the console not the command.
    On init, when you register your command you provide the command aliases with, you don’t need to check in your executor if the alias is the good one.
  2. return null You should never return null instead you should return CommandResult.success()
  3. Instead of
Vector3d playerLocation = new Vector3d();
playerLocation = (player.getLocation().getPosition());

You could directly write

Vector3d playerLocation = player.getLocation().getPosition();

You should look the documentation it can be very usefull
https://docs.spongepowered.org/en/plugin/commands/creating.html

Thanks for the reply Yeregorix and the url ill be sure to check it out.

You should make it into

public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
	if(src instanceof Player){
		Player player = (Player) src;
		if(src.hasPermission("CP.sethome.command")){
			Vector3d playerLocation = new Vector3d();
			playerLocation = (player.getLocation().getPosition());
			player.sendMessage(Text.of(TextColors.GREEN + "Home: ", TextColors.GOLD + "Set"));
                        return CommandResult.SUCCESS;
		}else {
			src.sendMessage(Text.of("Only players are able to use this command."));
                        return CommandResult.EMPTY;
		}
	}	
return CommandResult.EMPTY;
}

Why is Sponge so complicated in Bukkit you could do

import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.plugin.java.JavaPlugin
OR
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.command.CommandExecutor

Only three variables needed. Why does Sponge need so many variables imported?

EDIT: didnt see thread date sorry for the bump