Skip to content

Commit

Permalink
Updated VoiceCommand TabCompletion
Browse files Browse the repository at this point in the history
- Updated VoiceCommand TabCompletion -> is now shared/common function
- Updated voicecommands's /voice setup since it didn't save port as Integer
- Updated the network thing to show more logical error
Miniontoby committed Jan 24, 2025
1 parent 46e9101 commit 1240edb
Showing 6 changed files with 109 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.greitan.avion.bungeecord.commands;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.lang.NumberFormatException;

import net.md_5.bungee.api.plugin.Command;
@@ -15,6 +13,8 @@

import io.greitan.avion.bungeecord.GeyserVoice;
import io.greitan.avion.bungeecord.utils.Language;
import io.greitan.avion.common.utils.HasPermissionOperation;
import io.greitan.avion.common.utils.VoiceCommandCompletions;

public class VoiceCommand extends Command implements TabExecutor {

@@ -69,13 +69,21 @@ public void execute(CommandSender sender, String[] args) {
// Setup command - setup the configuration.
else if (args[0].equalsIgnoreCase("setup") && player.hasPermission("voice.setup")) {
String newHost = args[1];
String newPort = args[2];
String newPortString = args[2];
Integer newPort = -1;
String newKey = args[3];
try {
if (Objects.nonNull(newPortString)) {
newPort = Integer.parseInt(newPortString);
}
} catch (NumberFormatException e) {
newPort = -1;
}

if (Objects.nonNull(newHost) && Objects.nonNull(newPort) && Objects.nonNull(newKey)) {
GeyserVoice.getConfig().set("config.host", newHost);
GeyserVoice.getConfig().set("config.port", newPort);
GeyserVoice.getConfig().set("config.server-key", newKey);
if (Objects.nonNull(newHost) && Objects.nonNull(newPortString) && Objects.nonNull(newKey) && newPort != -1) {
plugin.getConfig().set("config.host", newHost);
plugin.getConfig().set("config.port", newPort);
plugin.getConfig().set("config.server-key", newKey);
plugin.saveConfig();
plugin.reloadConfig();
plugin.reload();
@@ -148,26 +156,11 @@ else if (args.length >= 1) {

@Override
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
List<String> completions = List.of();

// Main command arguments.
if (args.length == 1) {
List<String> options = List.of("bind", "setup", "connect", "reload");
completions = options.stream().filter(val -> val.startsWith(args[0])).collect(Collectors.toList());
}

// Setup command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("setup")) {
List<String> options = List.of("host port key");
completions = options.stream().filter(val -> val.startsWith(args[1])).collect(Collectors.toList());
}

// Connect command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("connect")) {
List<String> options = List.of("true", "false");
completions = options.stream().filter(val -> val.startsWith(args[1])).collect(Collectors.toList());
}

return completions;
return VoiceCommandCompletions.execute(args, new HasPermissionOperation() {
@Override
public boolean execute(String permission) {
return sender.hasPermission(permission);
}
});
}
}
20 changes: 10 additions & 10 deletions src/main/java/io/greitan/avion/common/network/Network.java
Original file line number Diff line number Diff line change
@@ -54,7 +54,9 @@ public MCCommPacket sendPostRequest(String url, MCCommPacket data) // was (Strin
return null;
}
} catch (Exception e) {
Logger.error("Can't connect to voice chat server! " + e.toString());
String message = e.getMessage();
if (message == null) message = e.toString();
Logger.error("Can't connect to voice chat server! " + message + ". Please check your address and port!");
return null;
}
}
@@ -78,15 +80,13 @@ public String sendLoginRequest(String link, String serverKey) {
AcceptPacket packetData = objectMapper.convertValue(response, AcceptPacket.class);
return packetData.Token;
}
/*
* else if (response.PacketId == PacketType.Deny.ordinal())
* {
* DenyPacket packetData = objectMapper.convertValue(response,
* DenyPacket.class);
* // Login Denied. Server denied link request! Reason:
* return "DENIED! " + packetData.Reason;
* }
*/
else if (response.PacketId == PacketType.Deny.ordinal() || response instanceof DenyPacket)
{
DenyPacket packetData = objectMapper.convertValue(response, DenyPacket.class);
Logger.error(
"Login request denied by server. Reason: " + packetData.Reason);
return null;
}
}
return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.greitan.avion.common.utils;

public interface HasPermissionOperation {
boolean execute(String permission);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.greitan.avion.common.utils;

import java.util.List;
import java.util.stream.Collectors;

import io.greitan.avion.common.utils.HasPermissionOperation;

public class VoiceCommandCompletions {
public static List<String> execute(String[] args, HasPermissionOperation hasPermission) {
List<String> completions = List.of();

// Main command arguments.
if (args.length == 1 || args.length == 0) {
List<String> options = List.of("bind", "setup", "connect", "reload", "settings");
completions = options.stream().filter(val -> (args.length == 0 || val.startsWith(args[0])) && hasPermission.execute("voice." + val)).collect(Collectors.toList());
}

// Setup command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("setup") && hasPermission.execute("voice.setup")) {
List<String> options = List.of("host port key");
completions = options.stream().filter(val -> val.startsWith(args[1])).collect(Collectors.toList());
}

// Connect command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("connect") && hasPermission.execute("voice.connect")) {
List<String> options = List.of("true", "false");
completions = options.stream().filter(val -> val.startsWith(args[1])).collect(Collectors.toList());
}

return completions;
}
}
45 changes: 19 additions & 26 deletions src/main/java/io/greitan/avion/paper/commands/VoiceCommand.java
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

import java.util.List;
import java.util.Objects;
import java.util.Collections;
import java.util.stream.Collectors;
import java.lang.NumberFormatException;

import org.bukkit.command.Command;
@@ -17,6 +17,8 @@

import io.greitan.avion.paper.GeyserVoice;
import io.greitan.avion.paper.utils.Language;
import io.greitan.avion.common.utils.HasPermissionOperation;
import io.greitan.avion.common.utils.VoiceCommandCompletions;

public class VoiceCommand implements CommandExecutor, TabCompleter {

@@ -70,10 +72,18 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
// Setup command - setup the configuration.
else if (args[0].equalsIgnoreCase("setup") && player.hasPermission("voice.setup")) {
String newHost = args[1];
String newPort = args[2];
String newPortString = args[2];
Integer newPort = -1;
String newKey = args[3];
try {
if (Objects.nonNull(newPortString)) {
newPort = Integer.parseInt(newPortString);
}
} catch (NumberFormatException e) {
newPort = -1;
}

if (Objects.nonNull(newHost) && Objects.nonNull(newPort) && Objects.nonNull(newKey)) {
if (Objects.nonNull(newHost) && Objects.nonNull(newPortString) && Objects.nonNull(newKey) && newPort != -1) {
plugin.getConfig().set("config.host", newHost);
plugin.getConfig().set("config.port", newPort);
plugin.getConfig().set("config.server-key", newKey);
@@ -147,28 +157,11 @@ else if (args.length >= 1) {

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
List<String> completions = List.of();

// Main command arguments.
if (args.length == 1) {
List<String> options = List.of("bind", "setup", "connect", "reload");
StringUtil.copyPartialMatches(args[0], options, completions);
}

// Setup command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("setup")) {
List<String> options = List.of("host port key");
StringUtil.copyPartialMatches(args[1], options, completions);
}

// Connect command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("connect")) {
List<String> options = List.of("true", "false");
StringUtil.copyPartialMatches(args[1], options, completions);
}

Collections.sort(completions);
return completions;
return VoiceCommandCompletions.execute(args, new HasPermissionOperation() {
@Override
public boolean execute(String permission) {
return sender.hasPermission(permission);
}
});
}

}
55 changes: 22 additions & 33 deletions src/main/java/io/greitan/avion/velocity/commands/VoiceCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.greitan.avion.velocity.commands;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.lang.NumberFormatException;

@@ -15,6 +14,8 @@

import io.greitan.avion.velocity.GeyserVoice;
import io.greitan.avion.velocity.utils.Language;
import io.greitan.avion.common.utils.HasPermissionOperation;
import io.greitan.avion.common.utils.VoiceCommandCompletions;

public final class VoiceCommand implements SimpleCommand {

@@ -72,13 +73,21 @@ public void execute(final Invocation invocation) {
// Setup command - setup the configuration.
else if (args[0].equalsIgnoreCase("setup") && player.hasPermission("voice.setup")) {
String newHost = args[1];
String newPort = args[2];
String newPortString = args[2];
Integer newPort = -1;
String newKey = args[3];
try {
if (Objects.nonNull(newPortString)) {
newPort = Integer.parseInt(newPortString);
}
} catch (NumberFormatException e) {
newPort = -1;
}

if (Objects.nonNull(newHost) && Objects.nonNull(newPort) && Objects.nonNull(newKey)) {
GeyserVoice.getConfig().set("config.host", newHost);
GeyserVoice.getConfig().set("config.port", newPort);
GeyserVoice.getConfig().set("config.server-key", newKey);
if (Objects.nonNull(newHost) && Objects.nonNull(newPortString) && Objects.nonNull(newKey) && newPort != -1) {
plugin.getConfig().set("config.host", newHost);
plugin.getConfig().set("config.port", newPort);
plugin.getConfig().set("config.server-key", newKey);
plugin.saveConfig();
plugin.reloadConfig();
plugin.reload();
@@ -136,8 +145,6 @@ else if (args.length >= 1) {
}
// Command not for console.
else {
plugin.getProxy().sendMessage(Component.text("####CheckMessaging####"));

sender.sendMessage(
Component.text(Language.getMessage(lang, "cmd-not-player")).color(NamedTextColor.RED));
}
@@ -150,35 +157,17 @@ else if (args.length >= 1) {

@Override
public boolean hasPermission(final Invocation invocation) {
return true;
// return invocation.source().hasPermission("command.test");
return invocation.source().hasPermission("voice.cmd");
}

@Override
public CompletableFuture<List<String>> suggestAsync(final Invocation invocation) {
public List<String> suggest(final Invocation invocation) {
String[] args = invocation.arguments();
return CompletableFuture.supplyAsync(() -> {
List<String> completions = List.of();

// Main command arguments.
if (args.length == 1) {
List<String> options = List.of("bind", "setup", "connect", "reload");
completions = options.stream().filter(val -> val.startsWith(args[0])).collect(Collectors.toList());
return VoiceCommandCompletions.execute(args, new HasPermissionOperation() {
@Override
public boolean execute(String permission) {
return invocation.source().hasPermission(permission);
}

// Setup command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("setup")) {
List<String> options = List.of("host port key");
completions = options.stream().filter(val -> val.startsWith(args[1])).collect(Collectors.toList());
}

// Connect command arguments.
if (args.length == 2 && args[0].equalsIgnoreCase("connect")) {
List<String> options = List.of("true", "false");
completions = options.stream().filter(val -> val.startsWith(args[1])).collect(Collectors.toList());
}

return completions;
});
}
}

0 comments on commit 1240edb

Please sign in to comment.