Skip to content

Added tab complete. #2

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

Open
wants to merge 6 commits into
base: staging
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/main/java/network/palace/show/ShowPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.craftmend.openaudiomc.spigot.OpenAudioMcSpigot;
import lombok.Getter;
import network.palace.show.commands.*;
import network.palace.show.commands.show.ShowTabComplete;
import network.palace.show.commands.showgen.ShowGenTabComplete;
import network.palace.show.generator.ShowGenerator;
import network.palace.show.listeners.ChunkListener;
import network.palace.show.listeners.PlayerInteract;
Expand Down Expand Up @@ -83,13 +85,16 @@ public void onEnable() {
openAudioMcSpigot = OpenAudioMcSpigot.getInstance();
FileUtil.setupFiles();
this.getCommand("show").setExecutor(new ShowCommand());
this.getCommand("show").setTabCompleter(new ShowTabComplete());

this.getCommand("showdebug").setExecutor(new ShowDebugCommand());

FileConfiguration config = this.getConfig();
this.saveDefaultConfig();
if (config.getString("github.token") != null) {
githubToken = config.getString("github.token");
this.getCommand("showgen").setExecutor(new ShowgenCommand());
this.getCommand("showgen").setTabCompleter(new ShowGenTabComplete());
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "[Show] Showgen has been enabled in show!");
} else {
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "[Show] Showgen will not be running in Show! To enable it, add a github token to the config!");
Expand Down
34 changes: 30 additions & 4 deletions src/main/java/network/palace/show/commands/ShowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,64 @@ public class ShowCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GREEN + "Show Commands:");
sender.sendMessage(ChatColor.AQUA + "/show list " + ChatColor.GREEN + "- List all running shows");
sender.sendMessage(ChatColor.AQUA + "/show start [Show Name] " + ChatColor.GREEN + "- Start a show");
sender.sendMessage(ChatColor.AQUA + "/show stop [Show Name] " + ChatColor.GREEN + "- Stop a show");
sendHelpMsg(sender);
return true;
} else {
switch (args[0]) {
case "list":
new ListCommand().runList(sender);
break;
case "start":
if (sender instanceof Player) {
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "/show start <name>");
return true;
}
new StartCommand().handle(sender, args[1], ((Player) sender).getWorld());
} else if (sender instanceof BlockCommandSender) {
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "/show start <name>");
return true;
}
new StartCommand().handle(sender, args[1], ((BlockCommandSender) sender).getBlock().getWorld());
} else {
sender.sendMessage(ChatColor.RED + "You cannot run this from the console!");
}
break;
case "stop":
if (sender instanceof Player) {
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "/show stop <name>");
return true;
}
new StopCommand().handle(sender, args[1]);
} else if (sender instanceof CommandBlock) {
if (args.length < 2) {
sender.sendMessage(ChatColor.RED + "/show stop <name>");
return true;
}
new StopCommand().handle(sender, args[1]);
} else {
sender.sendMessage(ChatColor.RED + "You cannot run this from the console!");
}
break;
default:
sendHelpMsg(sender);
break;
}
}

return true;
}

/**
* Sends a help message to the player.
* @param sender Who to send it to.
*/
private void sendHelpMsg(CommandSender sender) {
sender.sendMessage(ChatColor.GREEN + "Show Commands:");
sender.sendMessage(ChatColor.AQUA + "/show list " + ChatColor.GREEN + "- List all running shows");
sender.sendMessage(ChatColor.AQUA + "/show start [Show Name] " + ChatColor.GREEN + "- Start a show");
sender.sendMessage(ChatColor.AQUA + "/show stop [Show Name] " + ChatColor.GREEN + "- Stop a show");
}
}
21 changes: 13 additions & 8 deletions src/main/java/network/palace/show/commands/ShowgenCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public class ShowgenCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GREEN + "ShowGen Commands:");
sender.sendMessage(ChatColor.AQUA + "/showgen generate [action] [bottom/top] [delay per layer] [timestamp]" + ChatColor.GREEN + "- Generate blocks of show actions with one command");
sender.sendMessage(ChatColor.AQUA + "/showgen setcorner x,y,z" + ChatColor.GREEN + "- Set the location of the final north-west-bottom corner to help give real coordinate values");
sender.sendMessage(ChatColor.AQUA + "/showgen setinitialscene " + ChatColor.GREEN + "- Set the initial scene for a generator session");
sendHelpMsg(sender);
return true;
}

Expand All @@ -40,12 +37,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
new SetInitialCommand().handle(sender);
break;
default:
sender.sendMessage(ChatColor.GREEN + "ShowGen Commands:");
sender.sendMessage(ChatColor.AQUA + "/showgen generate [action] [bottom/top] [delay per layer] [timestamp]" + ChatColor.GREEN + "- Generate blocks of show actions with one command");
sender.sendMessage(ChatColor.AQUA + "/showgen setcorner x,y,z" + ChatColor.GREEN + "- Set the location of the final north-west-bottom corner to help give real coordinate values");
sender.sendMessage(ChatColor.AQUA + "/showgen setinitialscene " + ChatColor.GREEN + "- Set the initial scene for a generator session");
sendHelpMsg(sender);
break;
}
return true;
}

/**
* Sends a help message to the player.
* @param sender Who to send it to.
*/
private void sendHelpMsg(CommandSender sender) {
sender.sendMessage(ChatColor.GREEN + "ShowGen Commands:");
sender.sendMessage(ChatColor.AQUA + "/showgen generate [action] [bottom/top] [delay per layer] [timestamp]" + ChatColor.GREEN + "- Generate blocks of show actions with one command");
sender.sendMessage(ChatColor.AQUA + "/showgen setcorner x,y,z" + ChatColor.GREEN + "- Set the location of the final north-west-bottom corner to help give real coordinate values");
sender.sendMessage(ChatColor.AQUA + "/showgen setinitialscene " + ChatColor.GREEN + "- Set the initial scene for a generator session");
}
}
110 changes: 110 additions & 0 deletions src/main/java/network/palace/show/commands/show/ShowTabComplete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package network.palace.show.commands.show;

import network.palace.show.ShowPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ShowTabComplete implements TabCompleter {

List<String> shows;
public ShowTabComplete() {
shows = getShows();
}

/*
Commands:

/show start <name>
/show stop <name>
/show list

/showgen generate <type> <time>
/showgen setinitialscene
/showgen setcorner <x,y,z>

/showdebug
*/

private static final String[] baseShowCmds = { "start", "stop", "list" };

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
final List<String> completions = new ArrayList<>();

// Return all subcommands if nothing has been typed
if (args.length == 0) {
completions.addAll(Arrays.asList(baseShowCmds));
return completions;
}

// Return applicable commands if started typing
if (args.length == 1) {
StringUtil.copyPartialMatches(args[0], Arrays.asList(baseShowCmds), completions);
return completions;
}

// Handle subcommand args
switch (args[0]) {
case "start": {
StringUtil.copyPartialMatches(args[1], getStoppedShows(), completions);
return completions;
}
case "stop": {
StringUtil.copyPartialMatches(args[1], ShowPlugin.getShows().keySet(), completions);
return completions;
}
}

// Everything else without args
return completions;
}

/**
* Gets stopped shows, not from disk.
* @return Stopped show names (minus .show)
*/
private List<String> getStoppedShows() {
List<String> stoppedShows = shows;
for (String name : ShowPlugin.getShows().keySet()) stoppedShows.remove(name);
return stoppedShows;
}

/**
* Gets all shows directly from disk.
* @return The show names (minus .show)
*/
private List<String> getShows() {
List<String> names = new ArrayList<>();
listFiles("plugins/Show/shows/", names);

// Strip ".show"
ArrayList<String> tempNames = new ArrayList<>();
for (String name : names) {
tempNames.add(name.replaceAll(".show", ""));
}
return tempNames;
}

private void listFiles(String directoryName, List<String> files) {
File root = new File(directoryName);

// Get all files from a directory.
File[] fList = root.listFiles();
if(fList != null)
for (File file : fList) {
if (file.isFile()) {
files.add(file.getName());
} else if (file.isDirectory()) {
listFiles(file.getAbsolutePath(), files);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public void handle(CommandSender sender, String filename, World world) {
sender.sendMessage(ChatColor.RED + "Invalid world!");
return;
}
if (filename == null | filename.equals("")) {
sender.sendMessage(ChatColor.RED + "/show start [Show Name]");
return;
}
if (ShowPlugin.getShows().containsKey(filename)) {
sender.sendMessage(ChatColor.RED + "----------------------------------------------");
sender.sendMessage(ChatColor.RED + "That show is already running!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
public class StopCommand {

public void handle(CommandSender sender, String filename) {
if (filename == null | filename.equals("")) {
sender.sendMessage(ChatColor.RED + "/show stop [Show Name]");
return;
}
if (!ShowPlugin.getShows().containsKey(filename)) {
sender.sendMessage(ChatColor.RED + "----------------------------------------------");
sender.sendMessage(ChatColor.GOLD + filename + ChatColor.AQUA + " is not running!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,36 @@ public class SetCornerCommand {
public void handle(CommandSender sender, String[] args) {
Player player = (Player) sender;
if (args.length < 2) {
player.sendMessage(ChatColor.RED + "/showgen setcorner x,y,z");
player.sendMessage(ChatColor.RED + "/showgen setcorner <x> <y> <z>");
return;
}
String[] locData = args[1].split(",");
if (locData.length < 3) {
player.sendMessage(ChatColor.RED + "/showgen setcorner x,y,z");
return;

String[] locData;

if (args[1].contains(",")) { // Legacy coordinate format - showgen setcorner x,y,z
locData = args[1].split(",");
if (locData.length < 3) {
player.sendMessage(ChatColor.RED + "/showgen setcorner <x> <y> <z>");
return;
}

} else { // New format - showgen setcorner x y z
if (args.length != 4) {
player.sendMessage(ChatColor.RED + "/showgen setcorner <x> <y> <z>");
return;
}

locData = new String[]{args[1], args[2], args[3]};
}

try {
int x = Integer.parseInt(locData[0]);
int y = Integer.parseInt(locData[1]);
int z = Integer.parseInt(locData[2]);
int x = (int) Double.parseDouble(locData[0]);
int y = (int) Double.parseDouble(locData[1]);
int z = (int) Double.parseDouble(locData[2]);
World w = player.getWorld();
GeneratorSession session = ShowPlugin.getShowGenerator().getOrCreateSession(player.getUniqueId());
session.setCorner(new Location(w, x, y, z));
player.sendMessage(ChatColor.GREEN + "Set north-west-bottom corner to " + x + "," + y + "," + z + "!");
player.sendMessage(ChatColor.GREEN + "Set north-west-bottom corner to " + x + ", " + y + ", " + z + "!");
} catch (NumberFormatException e) {
player.sendMessage(ChatColor.RED + "Are you sure those are all numbers?");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package network.palace.show.commands.showgen;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ShowGenTabComplete implements TabCompleter {

/*
Commands:

/show start <name>
/show stop <name>
/show reload
/show list

/showgen generate <type> [top/bottom] <layer-delay> <timestamp>
/showgen setinitialscene
/showgen setcorner <x,y,z>

/showdebug
*/

private static final String[] baseShowGenCmds = { "generate", "setinitialscene", "setcorner" };
private static final String[] actionTypes = { "fakeblock" };
private static final String[] topOrBottom = { "top", "bottom" };

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
final List<String> completions = new ArrayList<>();

// Return all subcommands if nothing has been typed
if (args.length == 0) {
completions.addAll(Arrays.asList(baseShowGenCmds));
return completions;
}

// Return applicable commands if started typing
if (args.length == 1) {
StringUtil.copyPartialMatches(args[0], Arrays.asList(baseShowGenCmds), completions);
return completions;
}

// Handle subcommand args
switch (args[0]) {
case "generate": {

// Params
if (args.length == 2) {
StringUtil.copyPartialMatches(args[1], Arrays.asList(actionTypes), completions);
} else if (args.length == 3 && !args[2].equals("")) { // Tab complete when start typing, but not if empty
StringUtil.copyPartialMatches(args[2], Arrays.asList(topOrBottom), completions);
}

return completions;
}
case "setcorner": {
Player player = (Player) sender;

// Return players coords
if (args.length == 2) { // X
completions.add(player.getLocation().getBlockX()+"");
} else if (args.length == 3) { // Y
completions.add(player.getLocation().getBlockY()+"");
} else if (args.length == 4) { // Z
completions.add(player.getLocation().getBlockZ()+"");
}

return completions;
}
}

// Everything else without args
return completions;
}

}