53 lines
No EOL
1.9 KiB
Java
53 lines
No EOL
1.9 KiB
Java
package de.kentoj.kencommandapi;
|
|
|
|
import com.velocitypowered.api.command.CommandSource;
|
|
import com.velocitypowered.api.command.RawCommand;
|
|
import de.kentoj.kencommandapi.api.CommandHandler;
|
|
import de.kentoj.kencommandapi.api.literal.RootLiteral;
|
|
import de.kentoj.kencommandapi.api.platform.MessageStyle;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
public class VelocityCommand<T extends CommandSource> implements RawCommand {
|
|
|
|
private final CommandHandler<T> commandHandler;
|
|
private final RootLiteral<T> rootNode;
|
|
private final Class<T> senderType;
|
|
|
|
public VelocityCommand(CommandHandler<T> commandHandler, RootLiteral<T> rootNode, Class<T> senderType) {
|
|
this.commandHandler = commandHandler;
|
|
this.rootNode = rootNode;
|
|
this.senderType = senderType;
|
|
}
|
|
|
|
@Override
|
|
public void execute(Invocation invocation) {
|
|
if (!senderType.isInstance(invocation.source())) {
|
|
invocation.source().sendMessage(MessageStyle.PLAIN.err("You can't use that command"));
|
|
return;
|
|
}
|
|
T sender = senderType.cast(invocation.source());
|
|
|
|
var args = Arrays.stream(invocation.arguments().split(" "))
|
|
.filter(a -> !a.isEmpty());
|
|
commandHandler.invoke(rootNode, sender, args);
|
|
}
|
|
|
|
@Override
|
|
public boolean hasPermission(Invocation ctx) {
|
|
if (rootNode.permission() == null) return true;
|
|
return ctx.source().hasPermission(rootNode.permission());
|
|
}
|
|
|
|
@Override
|
|
public List<String> suggest(Invocation invocation) {
|
|
if (!senderType.isInstance(invocation.source()))
|
|
return Collections.emptyList();
|
|
T sender = senderType.cast(invocation.source());
|
|
|
|
var args = invocation.arguments().split(" ");
|
|
return commandHandler.getSuggestions(rootNode, sender, args, invocation.arguments().endsWith(" "));
|
|
}
|
|
} |