Skip to content

Commit

Permalink
Implemented tab autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksilassila committed Sep 26, 2020
1 parent 2fbe85a commit 42ce774
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 8 deletions.
30 changes: 26 additions & 4 deletions src/me/aleksilassila/islands/commands/IslandManagmentCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import me.aleksilassila.islands.utils.ChatUtils;
import me.aleksilassila.islands.utils.ConfirmItem;
import me.aleksilassila.islands.utils.Messages;
import org.bukkit.Bukkit;
import org.bukkit.block.Biome;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

public class IslandManagmentCommands extends ChatUtils implements CommandExecutor {
public class IslandManagmentCommands extends ChatUtils implements TabExecutor {
private final Main plugin;
private final IslandGrid grid;
private Set<Subcommand> subcommands;
Expand Down Expand Up @@ -109,6 +109,28 @@ private void sendHelp(Player player) {
player.sendMessage(Messages.help.GIVE);
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) return null;

Player player = (Player) sender;

List<String> avalableArgs = new ArrayList<>();

if (args.length == 1) {
for (Subcommand subcommand : subcommands) {
avalableArgs.add(subcommand.getName());
}
} else if (args.length > 1) {
Subcommand currentSubcommand = getSubcommand(args[0]);
if (currentSubcommand == null) return null;

avalableArgs = currentSubcommand.onTabComplete(player, Arrays.copyOfRange(args, 1, args.length));
}

return avalableArgs;
}

public static class Utils {
@NotNull
public Islands.IslandSize parseIslandSize(String size) {
Expand Down
7 changes: 3 additions & 4 deletions src/me/aleksilassila/islands/commands/Subcommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package me.aleksilassila.islands.commands;

import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import me.aleksilassila.islands.Islands;
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;

import java.util.List;

public abstract class Subcommand {
public abstract void onCommand(Player player, String[] args, boolean confirmed);
public abstract List<String> onTabComplete(Player player, String[] args);
public abstract String getName();
public abstract String help();
public abstract String[] aliases();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import me.aleksilassila.islands.commands.Subcommand;
import me.aleksilassila.islands.generation.IslandGrid;
import me.aleksilassila.islands.utils.Messages;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Biome;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

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

Expand Down Expand Up @@ -121,6 +124,24 @@ public void onCommand(Player player, String[] args, boolean confirmed) {
player.sendTitle(Messages.success.ISLAND_GEN_TITLE, Messages.success.ISLAND_GEN_SUBTITLE, 10, 20 * 7, 10);
}

@Override
public List<String> onTabComplete(Player player, String[] args) {
if (args.length == 1) {
HashMap<Biome, List<Location>> availableLocations = plugin.islands.islandGeneration.biomes.availableLocations;
List<String> availableArgs = new ArrayList<>();

for (Biome biome : availableLocations.keySet()) {
availableArgs.add(biome.name());
}

return availableArgs;
} else if (args.length == 2) {
return new ArrayList<>(Arrays.asList("BIG", "NORMAL", "SMALL"));
}

return null;
}

@Override
public String getName() {
return "create";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import me.aleksilassila.islands.utils.Messages;
import org.bukkit.entity.Player;

import java.util.List;

public class deleteSubcommand extends Subcommand {
private Main plugin;
private IslandGrid grid;
Expand Down Expand Up @@ -49,6 +51,11 @@ public void onCommand(Player player, String[] args, boolean confirmed) {
player.sendMessage(Messages.success.DELETED);
}

@Override
public List<String> onTabComplete(Player player, String[] args) {
return null;
}

@Override
public String getName() {
return "delete";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

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

public class giveSubcommand extends Subcommand {
private Main plugin;
private IslandGrid grid;
Expand Down Expand Up @@ -70,6 +74,15 @@ public void onCommand(Player player, String[] args, boolean confirmed) {

}

@Override
public List<String> onTabComplete(Player player, String[] args) {
if (args.length == 1) {
return new ArrayList<String>(Arrays.asList("<name>"));
}

return null;
}

@Override
public String getName() {
return "give";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import me.aleksilassila.islands.utils.Messages;
import org.bukkit.entity.Player;

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

public class nameSubcommand extends Subcommand {
private Main plugin;
private IslandGrid grid;
Expand Down Expand Up @@ -61,6 +65,15 @@ public void onCommand(Player player, String[] args, boolean confirmed) {

}

@Override
public List<String> onTabComplete(Player player, String[] args) {
if (args.length == 1) {
return new ArrayList<String>(Arrays.asList("<name>"));
}

return null;
}

@Override
public String getName() {
return "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.bukkit.block.Biome;
import org.bukkit.entity.Player;

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

Expand Down Expand Up @@ -121,6 +123,24 @@ public void onCommand(Player player, String[] args, boolean confirmed) {
}
}

@Override
public List<String> onTabComplete(Player player, String[] args) {
if (args.length == 1) {
HashMap<Biome, List<Location>> availableLocations = plugin.islands.islandGeneration.biomes.availableLocations;
List<String> availableArgs = new ArrayList<>();

for (Biome biome : availableLocations.keySet()) {
availableArgs.add(biome.name());
}

return availableArgs;
} else if (args.length == 2) {
return new ArrayList<>(Arrays.asList("BIG", "NORMAL", "SMALL"));
}

return null;
}

@Override
public String getName() {
return "regenerate";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import me.aleksilassila.islands.utils.Messages;
import org.bukkit.entity.Player;

import java.util.List;

public class unnameSubcommand extends Subcommand {
private Main plugin;
private IslandGrid grid;
Expand Down Expand Up @@ -50,6 +52,11 @@ public void onCommand(Player player, String[] args, boolean confirmed) {
}
}

@Override
public List<String> onTabComplete(Player player, String[] args) {
return null;
}

@Override
public String getName() {
return "unname";
Expand Down

0 comments on commit 42ce774

Please sign in to comment.