|
|
@@ -2,6 +2,7 @@ package me.lethunderhawk.clans.command;
|
|
|
|
|
|
import me.lethunderhawk.bazaarflux.service.Services;
|
|
|
import me.lethunderhawk.bazaarflux.util.command.CommandNode;
|
|
|
+import me.lethunderhawk.bazaarflux.util.command.CustomCommand;
|
|
|
import me.lethunderhawk.bazaarflux.util.gui.InventoryManager;
|
|
|
import me.lethunderhawk.clans.Clan;
|
|
|
import me.lethunderhawk.clans.ClanManager;
|
|
|
@@ -15,32 +16,23 @@ import net.kyori.adventure.text.Component;
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
|
import net.kyori.adventure.text.format.TextDecoration;
|
|
|
import org.bukkit.Bukkit;
|
|
|
-import org.bukkit.command.Command;
|
|
|
-import org.bukkit.command.CommandExecutor;
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
-import org.bukkit.command.TabCompleter;
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
-import java.util.function.BiConsumer;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
-public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
+public class ClanCommand extends CustomCommand{
|
|
|
private final ClanManager manager;
|
|
|
- private final CommandNode rootCommand;
|
|
|
|
|
|
public ClanCommand(ClanManager manager) {
|
|
|
+ super(new CommandNode("clan", "Main clan command", null), Services.get(ClanModule.class));
|
|
|
this.manager = manager;
|
|
|
- this.rootCommand = new CommandNode("clan", "Main clan command", null);
|
|
|
- setupDefaultCommands();
|
|
|
}
|
|
|
- private void setupDefaultCommands() {
|
|
|
-
|
|
|
+ @Override
|
|
|
+ public void createCommands() {
|
|
|
registerCommand("list", "List all clans", this::handleList);
|
|
|
- registerCommand("help", "Gives information about the use of clan commands", this::handleHelp);
|
|
|
registerCommand("leave", "Leave your current clan", this::handleLeaveClan);
|
|
|
registerCommand("reload", "Reload the clan Plugin", this::handleReload);
|
|
|
registerCommand("members", "Show the members of your clan", this::handleShowMembers);
|
|
|
@@ -68,7 +60,7 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
private void getClaimTool(CommandSender sender, String[] strings) {
|
|
|
if(!(sender instanceof Player p)) return;
|
|
|
p.getInventory().addItem(new ClaimTool(Services.get(Main.class)).createItem());
|
|
|
- ClanModule.sendText(p, Component.text("The tool magically appears in your inventory! How convenient!"));
|
|
|
+ module.sendText(p, Component.text("The tool magically appears in your inventory! How convenient!"));
|
|
|
}
|
|
|
|
|
|
private void handleSave(CommandSender sender, String[] strings) {
|
|
|
@@ -86,14 +78,14 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
Clan myClan = manager.getMyClan(uuid);
|
|
|
if(myClan == null) return;
|
|
|
- ClanModule.sendText(player, Component.text("List of all claims in clan " + myClan.getName(), NamedTextColor.GREEN, TextDecoration.UNDERLINED));
|
|
|
+ module.sendText(player, Component.text("List of all claims in clan " + myClan.getName(), NamedTextColor.GREEN, TextDecoration.UNDERLINED));
|
|
|
for(Claim claim : myClan.getClaims()){
|
|
|
- ClanModule.sendText(player, Component.text("Claim: " + claim.getName(), NamedTextColor.GOLD));
|
|
|
- ClanModule.sendText(player, Component.text("This claim spans from X: " + claim.getMaxX() + " to X: " + claim.getMinX(), NamedTextColor.GRAY)
|
|
|
+ module.sendText(player, Component.text("Claim: " + claim.getName(), NamedTextColor.GOLD));
|
|
|
+ module.sendText(player, Component.text("This claim spans from X: " + claim.getMaxX() + " to X: " + claim.getMinX(), NamedTextColor.GRAY)
|
|
|
.appendNewline()
|
|
|
.append(Component.text( " and from Z: " + claim.getMaxZ() + " to Z: " + claim.getMinZ(), NamedTextColor.GRAY)));
|
|
|
|
|
|
- ClanModule.sendText(player, "Volume of this claim is " + claim.getVolume() + " Blocks");
|
|
|
+ module.sendText(player, Component.text("Volume of this claim is " + claim.getVolume() + " Blocks", NamedTextColor.GRAY));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -114,9 +106,9 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
Clan clan = manager.getMyClan(uuid);
|
|
|
if(clan.denyRequest(uuid, strings[0])){
|
|
|
- ClanModule.sendText(player, "You declined the request from " + strings[0] + ".");
|
|
|
+ module.sendText(player, Component.text("You declined the request from " + strings[0] + "."));
|
|
|
}else{
|
|
|
- ClanModule.sendText(player, "You are not the owner, or " + strings[0] + " hasn't requested to join your clan.");
|
|
|
+ module.sendText(player, Component.text("You are not the owner, or " + strings[0] + " hasn't requested to join your clan."));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -125,9 +117,9 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
Clan clan = manager.getMyClan(uuid);
|
|
|
if(clan.acceptRequest(uuid, strings[0])){
|
|
|
- ClanModule.sendText(player, "You accepted the request from " + strings[0] + ".");
|
|
|
+ module.sendText(player, Component.text("You accepted the request from " + strings[0] + "."));
|
|
|
}else{
|
|
|
- ClanModule.sendText(player, "You are not the owner, or " + strings[0] + " hasn't requested to join your clan.");
|
|
|
+ module.sendText(player, Component.text("You are not the owner, or " + strings[0] + " hasn't requested to join your clan."));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -137,22 +129,22 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
Clan clan = manager.getMyClan(uuid);
|
|
|
if(clan == null) return;
|
|
|
- ClanModule.sendText(player, "§6=== Clan Members - " + clan.getName() + " ===");
|
|
|
+ module.sendText(player, Component.text("§6=== Clan Members - " + clan.getName() + " ==="));
|
|
|
Player owner = clan.getOwner();
|
|
|
- ClanModule.sendText(player,
|
|
|
+ module.sendText(player,
|
|
|
Component.text("Owner: ", NamedTextColor.RED)
|
|
|
.append(Component.text(owner.getName(), NamedTextColor.GRAY)));
|
|
|
for(UUID member : clan.getMembers()){
|
|
|
Player p = Bukkit.getPlayer(member);
|
|
|
if(p == null) continue;
|
|
|
- ClanModule.sendText(player, p.getName());
|
|
|
+ module.sendText(player, Component.text(p.getName()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void handleReload(CommandSender sender, String[] strings) {
|
|
|
if(sender.isOp()) {
|
|
|
reload();
|
|
|
- ClanModule.sendText(sender, "Reloading...");
|
|
|
+ module.sendText(sender, Component.text("Reloading..."));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -161,13 +153,13 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
UUID uuid = player.getUniqueId();
|
|
|
Clan clan = manager.getMyClan(uuid);
|
|
|
if(manager.leaveClan(uuid)){
|
|
|
- ClanModule.sendText(player,"You left your Clan ");
|
|
|
+ module.sendText(player,Component.text("You left your Clan "));
|
|
|
if(clan.getOwner().equals(player)){
|
|
|
manager.removeClan(clan.getName());
|
|
|
- ClanModule.sendText(player,"The clan was deleted since you are the only one left!");
|
|
|
+ module.sendText(player,Component.text("The clan was deleted since you are the only one left!"));
|
|
|
}
|
|
|
}else{
|
|
|
- ClanModule.sendText(player,"You cannot leave this clan! Either join a clan or if you are an owner, please remove all members first!");
|
|
|
+ module.sendText(player,Component.text("You cannot leave this clan! Either join a clan or if you are an owner, please remove all members first!"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -185,132 +177,28 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
|
|
|
String name = String.join("-", strings);
|
|
|
if (manager.getMyClan(player.getUniqueId()) != null){
|
|
|
- ClanModule.sendText(player,"You are already in a clan! Leave your clan first!");
|
|
|
+ module.sendText(player,Component.text("You are already in a clan! Leave your clan first!"));
|
|
|
return;
|
|
|
}
|
|
|
- if(manager.createClan(player, name)) ClanModule.sendText(player,"Creating the Clan " + name + " ...");
|
|
|
- else ClanModule.sendText(player,"The Name for the Clan " + name + " is already taken or you are already in a clan!");
|
|
|
- }
|
|
|
-
|
|
|
- // Helper method to easily register commands
|
|
|
- public CommandNode registerCommand(String name, String description, BiConsumer<CommandSender, String[]> executor) {
|
|
|
- return rootCommand.registerSubCommand(name, description, executor);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
|
|
- if (args.length == 0) {
|
|
|
- sendHelp(sender);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- List<String> argList = new ArrayList<>(Arrays.asList(args));
|
|
|
- CommandNode currentNode = rootCommand;
|
|
|
- CommandNode targetNode = null;
|
|
|
-
|
|
|
- // Traverse the command tree
|
|
|
- while (!argList.isEmpty()) {
|
|
|
- String nextArg = argList.get(0);
|
|
|
- CommandNode nextNode = currentNode.getSubCommand(nextArg);
|
|
|
-
|
|
|
- if (nextNode == null) {
|
|
|
- // No matching subcommand found, use current node if it has an executor
|
|
|
- targetNode = currentNode.getExecutor() != null ? currentNode : null;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- currentNode = nextNode;
|
|
|
- argList.remove(0);
|
|
|
-
|
|
|
- // If this is the last argument or node has no further subcommands
|
|
|
- if (argList.isEmpty() || nextNode.getSubCommands().isEmpty()) {
|
|
|
- targetNode = nextNode;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (targetNode == null) {
|
|
|
- ClanModule.sendText(sender,"§cUnknown command. Use /clan help for available commands.");
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- if (targetNode.getExecutor() == null) {
|
|
|
- ClanModule.sendText(sender,"§cThis command requires additional arguments.");
|
|
|
- sendSubCommands(sender, targetNode);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- // Execute the command with remaining arguments
|
|
|
- String[] remainingArgs = argList.toArray(new String[0]);
|
|
|
- targetNode.getExecutor().accept(sender, remainingArgs);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
|
|
- List<String> suggestions = new ArrayList<>();
|
|
|
-
|
|
|
- if (args.length == 0) {
|
|
|
- return suggestions;
|
|
|
- }
|
|
|
-
|
|
|
- // Start at root and traverse
|
|
|
- CommandNode currentNode = rootCommand;
|
|
|
- List<String> argList = new ArrayList<>(Arrays.asList(args));
|
|
|
- String lastArg = args[args.length - 1].toLowerCase();
|
|
|
-
|
|
|
- // Try to traverse as far as possible
|
|
|
- for (int i = 0; i < argList.size() - 1; i++) {
|
|
|
- String arg = argList.get(i);
|
|
|
- CommandNode nextNode = currentNode.getSubCommand(arg);
|
|
|
- if (nextNode == null) {
|
|
|
- break;
|
|
|
- }
|
|
|
- currentNode = nextNode;
|
|
|
- }
|
|
|
-
|
|
|
- // Get suggestions from current node
|
|
|
- if (currentNode.getTabCompleter() != null) {
|
|
|
- suggestions.addAll(currentNode.getTabCompleter().apply(sender, args));
|
|
|
- } else {
|
|
|
- // Suggest subcommands
|
|
|
- suggestions.addAll(currentNode.getSubCommandNames().stream()
|
|
|
- .filter(name -> name.toLowerCase().startsWith(lastArg))
|
|
|
- .collect(Collectors.toList()));
|
|
|
- }
|
|
|
-
|
|
|
- return suggestions;
|
|
|
- }
|
|
|
-
|
|
|
- private void sendHelp(CommandSender sender) {
|
|
|
- sender.sendMessage("§6=== Clan Commands ===");
|
|
|
- for (CommandNode cmd : rootCommand.getSubCommands()) {
|
|
|
- sender.sendMessage("§e/clan " + cmd.getName() + " §7- " + cmd.getDescription());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void sendSubCommands(CommandSender sender, CommandNode node) {
|
|
|
- sender.sendMessage("§6Available subcommands:");
|
|
|
- for (CommandNode subCmd : node.getSubCommands()) {
|
|
|
- sender.sendMessage("§e" + subCmd.getName() + " §7- " + subCmd.getDescription());
|
|
|
- }
|
|
|
+ if(manager.createClan(player, name)) module.sendText(player,Component.text("Creating the Clan " + name + " ..."));
|
|
|
+ else module.sendText(player,Component.text("The Name for the Clan " + name + " is already taken or you are already in a clan!"));
|
|
|
}
|
|
|
|
|
|
// Example command handlers
|
|
|
private void handleList(CommandSender sender, String[] args) {
|
|
|
- ClanModule.sendText(sender,"Aktuelle Liste an Clans: ");
|
|
|
+ module.sendText(sender,Component.text("Aktuelle Liste an Clans: "));
|
|
|
for (Clan clan : manager.getClans()) {
|
|
|
- ClanModule.sendText(sender,clan.getName());
|
|
|
+ module.sendText(sender,Component.text(clan.getName()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void handleJoin(CommandSender sender, String[] args) {
|
|
|
if (!(sender instanceof Player player)) return;
|
|
|
if (args.length < 1) {
|
|
|
- ClanModule.sendText(sender,"Usage: /clan join <clan>");
|
|
|
+ module.sendText(sender,Component.text("Usage: /clan join <clan>"));
|
|
|
return;
|
|
|
}
|
|
|
- if(manager.joinClan(player.getUniqueId(), args[0])) ClanModule.sendText(player, "Sent a request to join the clan " + args[0]);
|
|
|
+ if(manager.joinClan(player.getUniqueId(), args[0])) module.sendText(player, Component.text("Sent a request to join the clan " + args[0]));
|
|
|
}
|
|
|
|
|
|
private List<String> completeClans(CommandSender sender, String[] args) {
|
|
|
@@ -333,8 +221,4 @@ public class ClanCommand implements CommandExecutor, TabCompleter {
|
|
|
.filter(name -> name.toLowerCase().startsWith(partial))
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
-
|
|
|
- private void reload() {
|
|
|
- setupDefaultCommands();
|
|
|
- }
|
|
|
}
|