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 implements RawCommand { private final CommandHandler commandHandler; private final RootLiteral rootNode; private final Class senderType; public VelocityCommand(CommandHandler commandHandler, RootLiteral rootNode, Class 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 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(" ")); } }