Tab completer kinda

This commit is contained in:
ruby
2024-08-05 23:46:11 +12:00
parent 03744182cb
commit 294b2c1e15
3 changed files with 65 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package com.pobnellion.floorGame; package com.pobnellion.floorGame;
import com.pobnellion.floorGame.command.CommandFloorGame; import com.pobnellion.floorGame.command.CommandFloorGame;
import com.pobnellion.floorGame.command.TabCompleterFloorGame;
import com.pobnellion.floorGame.game.GameConfig; import com.pobnellion.floorGame.game.GameConfig;
import com.pobnellion.floorGame.game.GameInstance; import com.pobnellion.floorGame.game.GameInstance;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -23,6 +24,7 @@ public final class FloorGame extends JavaPlugin {
// Register commands // Register commands
this.getCommand("floorgame").setExecutor(new CommandFloorGame()); this.getCommand("floorgame").setExecutor(new CommandFloorGame());
this.getCommand("floorgame").setTabCompleter(new TabCompleterFloorGame());
} }
@Override @Override

View File

@ -16,15 +16,13 @@ public class CommandFloorGame implements CommandExecutor {
if (args.length == 0) if (args.length == 0)
return false; return false;
switch (args[0]) { switch (args[0].toLowerCase()) {
case "start" -> Start(sender); case "start" -> Start(sender);
case "stop" -> Stop(sender); case "stop" -> Stop(sender);
case "config" -> Config(sender, args); case "config" -> Config(sender, args);
default -> sender.sendMessage(ChatColor.RED + "Usage: /floorgame < start | stop |config >"); default -> sender.sendMessage(ChatColor.RED + "Usage: /floorgame < start | stop |config >");
} }
// TODO: tab completer
return true; return true;
} }

View File

@ -0,0 +1,62 @@
package com.pobnellion.floorGame.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import java.util.List;
public class TabCompleterFloorGame implements TabCompleter {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1)
return List.of("start", "stop", "config");
if (!args[0].equalsIgnoreCase("config"))
return List.of();
if (args.length == 2)
return List.of("playerJoinArea", "spectatorJoinArea", "postGameTpLocation", "floorCenter", "tileSize",
"gridSize", "failLimit", "playersHaveKnockbackStick", "playersHaveFishingRod", "spectatorsCanMessWithPlayers");
return switch (args[1]) {
case "playerJoinArea", "spectatorJoinArea" -> AreaCompleter(sender, args, 3);
case "postGameTpLocation", "floorCenter" -> LocationCompleter(sender, args, 3);
case "playersHaveKnockbackStick", "playersHaveFishingRod", "spectatorsCanMessWithPlayers" ->
args.length == 3 ? List.of("true", "false") : List.of();
default -> List.of();
};
// TODO: limit options based on current arg value
// https://www.spigotmc.org/threads/how-to-create-a-tabcompleter-for-a-command.591865/
}
private List<String> AreaCompleter(CommandSender sender, String[] args, int locationArgIndex) {
if (!(sender instanceof Player))
return List.of();
var ret = LocationCompleter(sender, args, locationArgIndex);
if (!ret.isEmpty())
return ret;
return LocationCompleter(sender, args, locationArgIndex + 3);
}
private List<String> LocationCompleter(CommandSender sender, String[] args, int locationArgIndex) {
if (!(sender instanceof Player))
return List.of();
sender.sendMessage(Integer.toString(args.length));
if (args.length - locationArgIndex >= 3)
return List.of();
var targetBlock = ((Player) sender).getTargetBlockExact(10);
var targetXYZ = targetBlock == null
? List.of("~", "~", "~")
: List.of(Integer.toString(targetBlock.getX()), Integer.toString(targetBlock.getY()), Integer.toString(targetBlock.getZ()));
return List.of(String.join(" ", targetXYZ.subList(args.length - locationArgIndex, 3)));
}
}