Adds an inefficient implementation of #1
Adds a technically working dial command whose tab completion is too horrendously slow to be usable
This commit is contained in:
@@ -7,6 +7,7 @@ Stargate-Command is an addon for Stargate which adds additional useful commands
|
||||
#### Features:
|
||||
|
||||
* The ability to edit the config file through commands, and automated reloading of changed values
|
||||
* The ability to dial any Stargate accessible to the Player
|
||||
|
||||
## Dependencies
|
||||
|
||||
@@ -18,6 +19,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
|
||||
```
|
||||
|
||||
# Changes
|
||||
@@ -25,6 +27,7 @@ stargate.command.config - Gives access to the `/sgc config` command
|
||||
[Version 0.1.0]
|
||||
|
||||
- Full takeover removing old functionality, and, for now, replacing it with config editing
|
||||
- Adds /sgc dial for dialing any wanted Stargate
|
||||
[Version 0.0.4]
|
||||
- Fix for Bukkit's direction fix
|
||||
[Version 0.0.3]
|
||||
|
||||
@@ -36,7 +36,7 @@ public class StargateCommand extends JavaPlugin {
|
||||
PluginCommand stargateCommand = this.getCommand("stargateCommand");
|
||||
if (stargateCommand != null) {
|
||||
stargateCommand.setExecutor(new CommandStarGateCommand(stargateAPI, bannedConfigOptions));
|
||||
stargateCommand.setTabCompleter(new StargateCommandTabCompleter(bannedConfigOptions));
|
||||
stargateCommand.setTabCompleter(new StargateCommandTabCompleter(stargateAPI, bannedConfigOptions));
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("Unable to hook into Stargate. Make sure the Stargate plugin is installed " +
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.api.StargateAPI;
|
||||
import net.TheDgtl.Stargate.manager.PermissionManager;
|
||||
import net.TheDgtl.Stargate.network.Network;
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.RealPortal;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* This command represents the dial command for dialing any available Stargate
|
||||
*/
|
||||
public class CommandDial implements CommandExecutor {
|
||||
|
||||
private final StargateAPI stargateAPI;
|
||||
private final RegistryAPI registryAPI;
|
||||
|
||||
/**
|
||||
* Instantiates a new dial command
|
||||
*
|
||||
* @param stargateAPI <p>A reference to the Stargate API</p>
|
||||
*/
|
||||
public CommandDial(StargateAPI stargateAPI) {
|
||||
this.stargateAPI = stargateAPI;
|
||||
this.registryAPI = stargateAPI.getRegistry();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMessage("This command can only be used by players");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
player.sendMessage("You need to provide a network name and a portal name to dial");
|
||||
return true;
|
||||
}
|
||||
|
||||
PermissionManager permissionManager = stargateAPI.getPermissionManager(player);
|
||||
String networkName = args[0];
|
||||
String portalName = args[1];
|
||||
Network network = registryAPI.getNetwork(networkName, false);
|
||||
if (network == null) {
|
||||
commandSender.sendMessage("Invalid network selected");
|
||||
return true;
|
||||
}
|
||||
|
||||
RealPortal targetPortal = (RealPortal) network.getPortal(portalName);
|
||||
if (targetPortal == null) {
|
||||
commandSender.sendMessage("Invalid portal selected");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!permissionManager.hasAccessPermission(targetPortal)) {
|
||||
commandSender.sendMessage("You don't have access to the selected portal");
|
||||
return true;
|
||||
}
|
||||
|
||||
//Find any Stargate block in the player's line of sight
|
||||
RealPortal originPortal = null;
|
||||
Location playerLocation = player.getLocation().add(0, player.getEyeHeight(), 0);
|
||||
Vector playerDirection = player.getLocation().getDirection();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
playerLocation.add(playerDirection);
|
||||
originPortal = registryAPI.getPortal(playerLocation);
|
||||
if (originPortal != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (originPortal == null) {
|
||||
player.sendMessage("You need to look at a portal to dial");
|
||||
return true;
|
||||
}
|
||||
originPortal.overrideDestination(targetPortal);
|
||||
originPortal.open(player);
|
||||
|
||||
player.sendMessage("Your Stargate has been prepared");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,10 +33,12 @@ public class CommandStarGateCommand implements CommandExecutor {
|
||||
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (args.length > 0) {
|
||||
String[] subArgs = (String[]) ArrayUtils.remove(args, 0);
|
||||
if (args[0].equalsIgnoreCase("config")) {
|
||||
String[] subArgs = (String[]) ArrayUtils.remove(args, 0);
|
||||
return new CommandConfig(stargateAPI.getConfigurationAPI(), bannedConfigOptions).onCommand(
|
||||
commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase("dial")) {
|
||||
return new CommandDial(stargateAPI).onCommand(commandSender, command, s, subArgs);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.api.StargateAPI;
|
||||
import net.TheDgtl.Stargate.manager.PermissionManager;
|
||||
import net.TheDgtl.Stargate.network.Network;
|
||||
import net.TheDgtl.Stargate.network.RegistryAPI;
|
||||
import net.TheDgtl.Stargate.network.portal.Portal;
|
||||
import net.TheDgtl.Stargate.network.portal.RealPortal;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A tab completer for the /sgc dial command
|
||||
*/
|
||||
public class DialTabCompleter implements TabCompleter {
|
||||
|
||||
private final StargateAPI stargateAPI;
|
||||
|
||||
/**
|
||||
* Instantiates a new dial tab completer
|
||||
*
|
||||
* @param stargateAPI <p>A reference to the Stargate API</p>
|
||||
*/
|
||||
public DialTabCompleter(StargateAPI stargateAPI) {
|
||||
this.stargateAPI = stargateAPI;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s,
|
||||
@NotNull String[] args) {
|
||||
if (!(commandSender instanceof Player player)) {
|
||||
commandSender.sendMessage("This command can only be used by players");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
List<String> availableNetworks = new ArrayList<>();
|
||||
Map<Network, List<String>> availablePortals = new HashMap<>();
|
||||
RegistryAPI registryAPI = stargateAPI.getRegistry();
|
||||
PermissionManager permissionManager = stargateAPI.getPermissionManager(player);
|
||||
|
||||
//Populate the collections with available networks and portals
|
||||
populateNetworksAndPortals(permissionManager, availableNetworks, availablePortals);
|
||||
|
||||
if (args.length > 1) {
|
||||
Network network = registryAPI.getNetwork(args[0], false);
|
||||
if (network != null && availablePortals.containsKey(network)) {
|
||||
return availablePortals.get(network);
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
} else {
|
||||
return availableNetworks;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates the given collections with available networks and portals
|
||||
*
|
||||
* @param permissionManager <p>The permission manager to use to check for availability</p>
|
||||
* @param availableNetworks <p>The list to store available networks to</p>
|
||||
* @param availablePortals <p>The map to store available portals to</p>
|
||||
*/
|
||||
private void populateNetworksAndPortals(PermissionManager permissionManager, List<String> availableNetworks,
|
||||
Map<Network, List<String>> availablePortals) {
|
||||
List<Network> networks = new LinkedList<>(stargateAPI.getRegistry().getNetworkMap().values());
|
||||
//Get all available networks and portals
|
||||
for (Network network : networks) {
|
||||
Collection<Portal> portals = network.getAllPortals();
|
||||
for (Portal portal : portals) {
|
||||
if (permissionManager.hasAccessPermission((RealPortal) portal)) {
|
||||
//Add an empty list if the network has not been encountered before
|
||||
if (!availablePortals.containsKey(network)) {
|
||||
availablePortals.put(network, new LinkedList<>());
|
||||
}
|
||||
availablePortals.get(network).add(portal.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
//Add only the network names with portals available to the player
|
||||
availablePortals.keySet().forEach((item) -> availableNetworks.add(item.getName()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.knarcraft.stargatecommand.command;
|
||||
|
||||
import net.TheDgtl.Stargate.api.StargateAPI;
|
||||
import net.TheDgtl.Stargate.config.ConfigurationOption;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.bukkit.command.Command;
|
||||
@@ -17,14 +18,17 @@ import java.util.List;
|
||||
*/
|
||||
public class StargateCommandTabCompleter implements TabCompleter {
|
||||
|
||||
private final StargateAPI stargateAPI;
|
||||
private final List<ConfigurationOption> bannedConfigOptions;
|
||||
|
||||
/**
|
||||
* Instantiates a new Stargate-command tab completer
|
||||
*
|
||||
* @param stargateAPI <p>A reference to the Stargate API</p>
|
||||
* @param bannedConfigOptions <p>A list of config options that shouldn't be available</p>
|
||||
*/
|
||||
public StargateCommandTabCompleter(List<ConfigurationOption> bannedConfigOptions) {
|
||||
public StargateCommandTabCompleter(StargateAPI stargateAPI, List<ConfigurationOption> bannedConfigOptions) {
|
||||
this.stargateAPI = stargateAPI;
|
||||
this.bannedConfigOptions = bannedConfigOptions;
|
||||
}
|
||||
|
||||
@@ -40,12 +44,17 @@ public class StargateCommandTabCompleter implements TabCompleter {
|
||||
}
|
||||
}
|
||||
return matchingCommands;
|
||||
} else if (args.length > 1 && args[0].equalsIgnoreCase("config")) {
|
||||
} else if (args.length > 1) {
|
||||
//Get the actual arguments for the specified sub-command
|
||||
String[] subArgs = (String[]) ArrayUtils.remove(args, 0);
|
||||
return new ConfigTabCompleter(bannedConfigOptions).onTabComplete(commandSender, command, s, subArgs);
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
|
||||
if (args[0].equalsIgnoreCase("config")) {
|
||||
return new ConfigTabCompleter(bannedConfigOptions).onTabComplete(commandSender, command, s, subArgs);
|
||||
} else if (args[0].equalsIgnoreCase("dial")) {
|
||||
return new DialTabCompleter(stargateAPI).onTabComplete(commandSender, command, s, subArgs);
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,6 +68,9 @@ public class StargateCommandTabCompleter implements TabCompleter {
|
||||
if (!(commandSender instanceof Player player) || player.hasPermission("stargate.command.config")) {
|
||||
commands.add("config");
|
||||
}
|
||||
if (commandSender instanceof Player player && player.hasPermission("stargate.command.dial")) {
|
||||
commands.add("dial");
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,13 @@ commands:
|
||||
- sgc
|
||||
description: The root command for all added commands
|
||||
usage: |
|
||||
/<command> <config/?>
|
||||
/<command> <config/dial>
|
||||
/<command> config <config option> <new value>
|
||||
/<command> dial <network name> <portal name>
|
||||
permissions:
|
||||
stargate.command.config:
|
||||
description: Allow the user of /sgc config
|
||||
default: false
|
||||
description: Allows the use of /sgc config
|
||||
default: false
|
||||
stargate.command.dial:
|
||||
description: Allows the use of /sgc dial
|
||||
default: op
|
||||
Reference in New Issue
Block a user