Adds a visualizer as described in #2

This commit is contained in:
2022-05-31 14:38:24 +02:00
parent 690db141d8
commit 268f0a3209
11 changed files with 190 additions and 21 deletions

View File

@@ -20,6 +20,7 @@ Stargate-Command is an addon for Stargate which adds additional useful commands
```
stargate.command.config - Gives access to the `/sgc config` command
stargate.command.dial - Gives access to the `/sgc dial` command
stargate.command.visualizer- Gives access to the `/sgc visualizer` command
```
# Changes

View File

@@ -37,6 +37,10 @@ public class CommandDial implements CommandExecutor {
commandSender.sendMessage("This command can only be used by players");
return true;
}
if (!player.hasPermission("stargate.command.dial")) {
player.sendMessage("Permission Denied");
return true;
}
if (args.length < 2) {
player.sendMessage("You need to provide a network name and a portal name to dial");

View File

@@ -39,6 +39,8 @@ public class CommandStarGateCommand implements CommandExecutor {
commandSender, command, s, subArgs);
} else if (args[0].equalsIgnoreCase("dial")) {
return new CommandDial(stargateAPI).onCommand(commandSender, command, s, subArgs);
} else if (args[0].equalsIgnoreCase("visualizer")) {
return new CommandVisualizer(stargateAPI.getRegistry()).onCommand(commandSender, command, s, subArgs);
}
}
return false;

View File

@@ -0,0 +1,86 @@
package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.network.Network;
import net.TheDgtl.Stargate.network.RegistryAPI;
import net.TheDgtl.Stargate.network.portal.FixedPortal;
import net.TheDgtl.Stargate.network.portal.Portal;
import net.TheDgtl.Stargate.network.portal.PortalFlag;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* This command represents the command for visualizing a Stargate network
*/
public class CommandVisualizer implements CommandExecutor {
private final RegistryAPI registryAPI;
/**
* Instantiates a visualizer command
*
* @param registryAPI <p>A reference to the registry API</p>
*/
public CommandVisualizer(RegistryAPI registryAPI) {
this.registryAPI = registryAPI;
}
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] args) {
if (!commandSender.hasPermission("stargate.command.visualizer")) {
commandSender.sendMessage("Permission Denied");
return true;
}
if (args.length < 1) {
commandSender.sendMessage("A network must be provided");
return true;
}
Network network = registryAPI.getNetwork(args[0], false);
if (network == null) {
commandSender.sendMessage("You must provide a valid network to visualize");
return true;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("All portals in network: ").append(network.getName()).append("\n");
stringBuilder.append("Symbol explanation: ").append("\n");
stringBuilder.append("# = hidden, ¤ = not hidden").append("\n");
stringBuilder.append("O = always on, - = not always on").append("\n");
stringBuilder.append("| = fixed, > = destination choose-able");
//Print info about all portals in the network
for (Portal portal : network.getAllPortals()) {
stringBuilder.append("\n");
if (portal.hasFlag(PortalFlag.HIDDEN)) {
stringBuilder.append('#');
} else {
stringBuilder.append('¤');
}
if (portal.hasFlag(PortalFlag.ALWAYS_ON)) {
stringBuilder.append('O');
} else {
stringBuilder.append('-');
}
//TODO: Look for the fixed flag instead of FixedPortal once it's fixed
if (portal instanceof FixedPortal) {
stringBuilder.append('|');
} else {
stringBuilder.append('>');
}
stringBuilder.append(" ").append(portal.getName());
if (portal instanceof FixedPortal) {
stringBuilder.append(" -> ");
stringBuilder.append(portal.getDestinationName());
}
}
commandSender.sendMessage(stringBuilder.toString());
return true;
}
}

View File

@@ -11,8 +11,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import static net.knarcraft.stargatecommand.util.TabCompleterHelper.filterMatching;
/**
* This is the completer for stargates config sub-command (/sg config)
* This is the completer for stargates config sub-command (/sgc config)
*/
public class ConfigTabCompleter implements TabCompleter {
@@ -60,23 +62,6 @@ public class ConfigTabCompleter implements TabCompleter {
}
}
/**
* Find completable strings which match the text typed by the command's sender
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values which start with the player's typed text</p>
*/
private List<String> filterMatching(List<String> values, String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().startsWith(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
/**
* Get possible values for the selected option
*

View File

@@ -20,6 +20,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static net.knarcraft.stargatecommand.util.TabCompleterHelper.filterMatching;
/**
* A tab completer for the /sgc dial command
*/
@@ -56,12 +58,12 @@ public class DialTabCompleter implements TabCompleter {
if (args.length > 1) {
Network network = registryAPI.getNetwork(args[0], false);
if (network != null && availablePortals.containsKey(network)) {
return availablePortals.get(network);
return filterMatching(availablePortals.get(network), args[1]);
} else {
return new ArrayList<>();
}
} else {
return availableNetworks;
return filterMatching(availableNetworks, args[0]);
}
}

View File

@@ -52,6 +52,8 @@ public class StargateCommandTabCompleter implements TabCompleter {
return new ConfigTabCompleter(bannedConfigOptions).onTabComplete(commandSender, command, s, subArgs);
} else if (args[0].equalsIgnoreCase("dial")) {
return new DialTabCompleter(stargateAPI).onTabComplete(commandSender, command, s, subArgs);
} else if (args[0].equalsIgnoreCase("visualizer")) {
return new VisualizerTabCompleter(stargateAPI.getRegistry()).onTabComplete(commandSender, command, s, subArgs);
}
}
return new ArrayList<>();
@@ -71,6 +73,9 @@ public class StargateCommandTabCompleter implements TabCompleter {
if (commandSender instanceof Player player && player.hasPermission("stargate.command.dial")) {
commands.add("dial");
}
if (commandSender instanceof Player player && player.hasPermission("stargate.command.visualizer")) {
commands.add("visualizer");
}
return commands;
}

View File

@@ -0,0 +1,44 @@
package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.network.RegistryAPI;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import static net.knarcraft.stargatecommand.util.TabCompleterHelper.filterMatching;
/**
* A tab completer for the /sgc visualizer command
*/
public class VisualizerTabCompleter implements TabCompleter {
private final RegistryAPI registryAPI;
/**
* Instantiates a visualizer tab completer
*
* @param registryAPI <p>A reference to the registry API</p>
*/
public VisualizerTabCompleter(RegistryAPI registryAPI) {
this.registryAPI = registryAPI;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
@NotNull String[] args) {
if (args.length < 2) {
List<String> networkNames = new ArrayList<>();
registryAPI.getNetworkMap().values().forEach(item -> networkNames.add(item.getName()));
return filterMatching(networkNames, args[0]);
} else {
return new ArrayList<>();
}
}
}

View File

@@ -10,7 +10,11 @@ import org.bukkit.util.Vector;
/**
* A helper class for helping with finding portals in the world
*/
public class PortalFinderHelper {
public final class PortalFinderHelper {
private PortalFinderHelper() {
}
/**
* Find the portal a living entity is looking at using a single traced ray

View File

@@ -0,0 +1,32 @@
package net.knarcraft.stargatecommand.util;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for helping with tab completion
*/
public final class TabCompleterHelper {
private TabCompleterHelper() {
}
/**
* Find completable strings which match the text typed by the command's sender
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values which start with the player's typed text</p>
*/
public static List<String> filterMatching(List<String> values, String typedText) {
List<String> configValues = new ArrayList<>();
for (String value : values) {
if (value.toLowerCase().startsWith(typedText.toLowerCase())) {
configValues.add(value);
}
}
return configValues;
}
}

View File

@@ -14,10 +14,14 @@ commands:
/<command> <config/dial>
/<command> config <config option> <new value>
/<command> dial <network name> <portal name>
/<command> visualizer <network name>
permissions:
stargate.command.config:
description: Allows the use of /sgc config
default: false
stargate.command.dial:
description: Allows the use of /sgc dial
default: op
stargate.command.visualizer:
description: Allows the use of /sgc visualizer
default: op