Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/command tab completion with players #397

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin/src/main/bash/data.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUILD_NUM="1133"
BUILD_NUM="1144"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.craftmend.openaudiomc.generic.commands;

import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection;
import com.craftmend.openaudiomc.generic.commands.enums.CommandContext;
import com.craftmend.openaudiomc.generic.commands.helpers.CommandMiddewareExecutor;
import com.craftmend.openaudiomc.generic.commands.interfaces.CommandMiddleware;
Expand All @@ -12,6 +13,7 @@
import com.craftmend.openaudiomc.generic.commands.objects.CommandError;
import com.craftmend.openaudiomc.generic.commands.subcommands.*;
import com.craftmend.openaudiomc.generic.environment.MagicValue;
import com.craftmend.openaudiomc.generic.networking.interfaces.NetworkingService;
import com.craftmend.openaudiomc.generic.platform.Platform;
import com.craftmend.openaudiomc.generic.service.Inject;
import com.craftmend.openaudiomc.generic.service.Service;
Expand All @@ -29,9 +31,10 @@ public class CommandService extends Service {
private Configuration configuration;

private final Map<CommandContext, Map<String, SubCommand>> subCommands = new EnumMap<>(CommandContext.class);
@Getter private final List<String> aliases = new ArrayList<>();
@Getter
private final List<String> aliases = new ArrayList<>();

private final CommandMiddleware[] defaultCommandMiddleware = new CommandMiddleware[] {
private final CommandMiddleware[] defaultCommandMiddleware = new CommandMiddleware[]{
new CatchLegalBindingMiddleware(),
new CatchCrashMiddleware(),
new CleanStateCheckMiddleware()
Expand Down Expand Up @@ -87,7 +90,7 @@ public Map<String, SubCommand> getSubCommandHandlers(CommandContext context) {
}

/**
* @param context the context to get the sub commands from
* @param context the context to get the sub commands from
* @param commandList registers one or more sub commands
*/
public void registerSubCommands(CommandContext context, SubCommand... commandList) {
Expand All @@ -102,7 +105,7 @@ public void registerSubCommands(CommandContext context, SubCommand... commandLis
}

/**
* @param context the context to get the sub commands from
* @param context the context to get the sub commands from
* @param argument get the sub command from a name
* @return returns the handler, can be null
*/
Expand Down Expand Up @@ -153,20 +156,48 @@ public void invokeCommand(User<?> sender, CommandContext context, String[] args,
}

public List<String> getTabCompletions(CommandContext context, String[] args) {
List<String> completions = new ArrayList<>();
Set<String> completions = new HashSet<>();
for (String subCommand : getSubCommands(context)) {
if (args.length <= 1 && subCommand.startsWith(args[0])) completions.add(subCommand);
if (args.length <= 1 && subCommand.startsWith(args[0])) {
// Not typing yet, add the entire damn thing
completions.add(subCommand);
}
}
if (args.length == 2) {

// do we not have anything yet?
if (completions.isEmpty()) {
SubCommand subCommand = getSubCommand(context, args[0].toLowerCase());
if (subCommand == null) return new ArrayList<>();
for (Argument argument : subCommand.getArguments()) {
if (argument.getSyntax().startsWith(args[1].toLowerCase())) {
completions.add(argument.getSyntax());
String[] argumentSyntaxParts = argument.getSyntax().split(" ");

int localArgIndex = args.length - 2;

if (argument.isPlayerArgument(localArgIndex)) {
for (String targetName : getAllTargetNames()) {
if (targetName.toLowerCase().startsWith(args[args.length - 1].toLowerCase())) {
completions.add(targetName);
}
}
} else {
if (args.length <= argumentSyntaxParts.length + 1) {
completions.add(argumentSyntaxParts[localArgIndex]);
}
}
}
}
return completions;

List<String> s = new ArrayList<>();
s.addAll(completions);
return s;
}

private List<String> getAllTargetNames() {
List<String> names = new ArrayList<>();
for (ClientConnection client : getService(NetworkingService.class).getClients()) {
names.add(client.getUser().getName());
}
return names;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.craftmend.openaudiomc.generic.commands.objects;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Argument {

/**
Expand All @@ -15,6 +13,22 @@ public class Argument {
*/
private String syntax;
private String description;
private int playerArgumentIndex = -1;

public Argument(String syntax, String description) {
this.syntax = syntax;
this.description = description;
}

public Argument(String syntax, String description, int playerArgumentIndex) {
this.syntax = syntax;
this.description = description;
this.playerArgumentIndex = playerArgumentIndex;
}

public boolean isPlayerArgument(int index) {
return index == playerArgumentIndex;
}

public String getBase() {
String base = syntax.split(" ")[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ClientsSubCommand extends SubCommand {
public ClientsSubCommand() {
super("clients");
registerArguments(
new Argument("", "List all connected clients")
new Argument("", "List all connected clients", 0)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public PlaySubCommand() {
super("play", "p");
registerArguments(
new Argument("<selector> <source>",
"Plays a sound for all the players in a selection"),
"Plays a sound for all the players in a selection", 0),
new Argument("<selector> <source> <options>",
"Plays a sound with configuration (like fade time, sync etc) for all players in a selection")
"Plays a sound with configuration (like fade time, sync etc) for all players in a selection", 0)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PreloadSubCommand extends SubCommand {
public PreloadSubCommand() {
super("preload");
registerArguments(
new Argument("<selector> <source>", "Attempt to preload a sound for all players in a selection")
new Argument("<selector> <source>", "Attempt to preload a sound for all players in a selection", 0)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public StopSubCommand() {
super("stop");
registerArguments(
new Argument("<selector>",
"Stops all manual sounds for all players in a selection"),
"Stops all manual sounds for all players in a selection", 0),
new Argument("<selector> <sound-ID>",
"Only stops one specified sound for all players in the selection with a selected ID")
"Only stops one specified sound for all players in the selection with a selected ID", 0)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
@NoArgsConstructor
public class MigrationWorker {

@Getter private int migrationsFinished = 0;
@Getter private int migrationsSkipped = 0;
@Getter
private int migrationsFinished = 0;
@Getter
private int migrationsSkipped = 0;

public void handleMigrations() {
final SimpleMigration[] migrations = new SimpleMigration[] {
final SimpleMigration[] migrations = new SimpleMigration[]{
new AddConfigKeyMigration(MESSAGE_HOVER_TO_CONNECT, "adds a config field for the hover-to-connect message"),
new AddConfigKeyMigration(SETTINGS_STAFF_TIPS, "adds a config field for the staff-tips option"),
new AddConfigKeyMigration(SETTINGS_NOTIFY_UPDATES, "adds config fields for update and announcement preferences,"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.generic.commands.CommandService;
import com.craftmend.openaudiomc.generic.commands.enums.CommandContext;
import com.craftmend.openaudiomc.generic.commands.subcommands.HelpSubCommand;
import com.craftmend.openaudiomc.generic.commands.subcommands.PlaySubCommand;
import com.craftmend.openaudiomc.generic.service.Inject;
import com.craftmend.openaudiomc.generic.service.Service;
import com.craftmend.openaudiomc.spigot.modules.commands.command.*;
Expand Down Expand Up @@ -35,9 +33,9 @@ public void onEnable() {
openAudioMcSpigot.getCommand("deafen").setExecutor(new DeafenCommand());


VoiceChatCommand voiceChatCommand = new VoiceChatCommand();
openAudioMcSpigot.getCommand("voice").setExecutor(voiceChatCommand);
openAudioMcSpigot.getCommand("voice").setTabCompleter(voiceChatCommand);
VoiceCommand voiceCommand = new VoiceCommand();
openAudioMcSpigot.getCommand("voice").setExecutor(voiceCommand);
openAudioMcSpigot.getCommand("voice").setTabCompleter(voiceCommand);

commandService.getAliases().addAll(openAudioMcSpigot.getCommand("openaudiomc").getAliases());
commandService.getAliases().add("openaudiomc");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.craftmend.openaudiomc.OpenAudioMc;
import com.craftmend.openaudiomc.generic.commands.CommandService;
import com.craftmend.openaudiomc.generic.commands.enums.CommandContext;
import com.craftmend.openaudiomc.generic.environment.MagicValue;
import com.craftmend.openaudiomc.generic.platform.Platform;
import com.craftmend.openaudiomc.generic.proxy.interfaces.UserHooks;
import com.craftmend.openaudiomc.generic.storage.enums.StorageKey;
Expand All @@ -18,7 +17,7 @@
import java.util.List;

@NoArgsConstructor
public class VoiceChatCommand implements CommandExecutor, TabCompleter {
public class VoiceCommand implements CommandExecutor, TabCompleter {

private final CommandService commandService = OpenAudioMc.getService(CommandService.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public VoiceSubCommand() {
registerArguments(
new Argument("extend", "Renew your moderation lease"),
new Argument("mod", "Toggle moderation mode for voicechat"),
new Argument("inspect <username>", "Open the moderation menu to view the status of a player or ban them")
new Argument("inspect <username>", "Open the moderation menu to view the status of a player or ban them", 1)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ChannelSubCommand() {
new Argument("leave", "Leave your current channel, back to proximity chat"),
new Argument("join <channel-name>", "Join a channel"),
new Argument("list", "List all available channels"),
new Argument("invite <player-name>", "Invite a player to your channel")
new Argument("invite <player-name>", "Invite a player to your channel", 1)
);

this.ignorePermissions = true;
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/resources/data.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BUILD_NUM="1133"
BUILD_NUM="1144"
4 changes: 2 additions & 2 deletions plugin/src/main/resources/openaudiomc-build.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
BUILD_VERSION="1133"
BUILD_COMMIT="c739589c45d3a65c069ce8b17755c59d11f0d4a4"
BUILD_VERSION="1144"
BUILD_COMMIT="aa5494852ba125782b212469f644d5bbcb9737f1"
BUILD_AUTHOR="Mats"
Loading