Skip to content

Commit

Permalink
Suggestion commande avec filtre intelligent
Browse files Browse the repository at this point in the history
  • Loading branch information
Euphillya committed Dec 23, 2024
1 parent 1de5e52 commit 986e0db
Show file tree
Hide file tree
Showing 22 changed files with 273 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

public class SkylliaAdminCommand implements SkylliaCommandInterface {

Expand Down Expand Up @@ -52,19 +51,23 @@ public void execute(CommandSourceStack sender, String @NotNull [] args) {
return;
}

public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
List<String> tab = new ArrayList<>();
if (args.length == 1) {
tab.addAll(registry.getCommandMap().keySet());
@Override
public Collection<String> suggest(CommandSourceStack sender, String[] args) {
Set<String> commands = registry.getCommandMap().keySet();
if (args.length == 0) {
return commands;
} else if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
return commands.stream().filter(command -> command.toLowerCase().startsWith(partial)).toList();
} else {
String subCommand = args[0].trim().toLowerCase();
String[] listArgs = Arrays.copyOfRange(args, 1, args.length);
SubCommandInterface subCommandInterface = registry.getSubCommandByName(subCommand);
if (subCommandInterface != null) {
return subCommandInterface.onTabComplete(this.plugin, sender, listArgs);
return subCommandInterface.onTabComplete(this.plugin, sender.getSender(), listArgs);
}
}
return tab;
return Collections.emptyList();
}

private void registerDefaultCommands() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ForceDeleteSubCommands implements SubCommandInterface {

Expand Down Expand Up @@ -87,16 +89,25 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (!sender.hasPermission("skyllia.admins.commands.island.delete")) {
return new ArrayList<>();
return Collections.emptyList();
}

if (args.length == 1) {
return Bukkit.getOnlinePlayers().stream().map(Player::getName).filter(name -> name.toLowerCase().startsWith(args[0].toLowerCase())).collect(Collectors.toList());
String partial = args[0].trim().toLowerCase();

return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(partial))
.collect(Collectors.toList());

} else if (args.length == 2) {
return List.of("confirm");
String partial = args[1].trim().toLowerCase();

return Stream.of("confirm")
.filter(word -> word.startsWith(partial))
.toList();
}

return new ArrayList<>();
return Collections.emptyList();
}

private void updatePlayer(Main plugin, SkyblockManager skyblockManager, Island island) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ForceTransferSubCommands implements SubCommandInterface {

Expand Down Expand Up @@ -120,23 +122,36 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (!sender.hasPermission("skyllia.admins.commands.island.transfer")) {
return new ArrayList<>();
return Collections.emptyList();
}

// ARG 1 → Nom du premier joueur
if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(args[0].toLowerCase()))
.filter(name -> name.toLowerCase().startsWith(partial))
.collect(Collectors.toList());
} else if (args.length == 2) {
}

// ARG 2 → Nom du deuxième joueur
if (args.length == 2) {
String partial = args[1].trim().toLowerCase();
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(args[1].toLowerCase()))
.filter(name -> name.toLowerCase().startsWith(partial))
.collect(Collectors.toList());
}

// ARG 3 → Saisie pour "confirm"
if (args.length == 3) {
String partial = args[2].trim().toLowerCase();
return Stream.of("confirm")
.filter(word -> word.startsWith(partial))
.collect(Collectors.toList());
} else if (args.length == 3) {
return List.of("confirm");
}

return new ArrayList<>();
// Aucun autre argument
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SetMaxMembersSubCommands implements SubCommandInterface {

Expand Down Expand Up @@ -80,19 +78,35 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (!sender.hasPermission("skyllia.admins.commands.island.setmaxmembers")) {
return new ArrayList<>();
return Collections.emptyList();
}

// ARG #1 → Nom du joueur
if (args.length == 1) {
return Bukkit.getOnlinePlayers().stream().map(Player::getName).filter(name -> name.toLowerCase().startsWith(args[0].toLowerCase())).collect(Collectors.toList());
} else if (args.length == 2) {
return Arrays.asList("5", "10", "15", "20", "25");
} else if (args.length == 3) {
if ("confirm".startsWith(args[2].toLowerCase())) {
return List.of("confirm");
}
String partial = args[0].trim().toLowerCase();
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(partial))
.collect(Collectors.toList());
}

else if (args.length == 2) {
String partial = args[1].trim().toLowerCase();
List<String> possibleValues = Arrays.asList("5", "10", "15", "20", "25");
return possibleValues.stream()
.filter(value -> value.startsWith(partial))
.collect(Collectors.toList());
}

// ARG #3 → "confirm"
else if (args.length == 3) {
String partial = args[2].trim().toLowerCase();

return Stream.of("confirm")
.filter(word -> word.startsWith(partial))
.collect(Collectors.toList());
}

return new ArrayList<>();
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SetSizeSubCommands implements SubCommandInterface {

Expand Down Expand Up @@ -80,18 +78,37 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (!sender.hasPermission("skyllia.admins.commands.island.setsize")) {
return new ArrayList<>();
return Collections.emptyList();
}

// ---------- ARG #1 : Nom du joueur ----------
if (args.length == 1) {
return Bukkit.getOnlinePlayers().stream().map(Player::getName).filter(name -> name.toLowerCase().startsWith(args[0].toLowerCase())).collect(Collectors.toList());
} else if (args.length == 2) {
return Arrays.asList("100", "200", "300", "500", "1000");
} else if (args.length == 3) {
if ("confirm".startsWith(args[2].toLowerCase())) {
return List.of("confirm");
}
String partial = args[0].trim().toLowerCase();
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(partial))
.collect(Collectors.toList());
}

// ---------- ARG #2 : Choix de la taille ----------
else if (args.length == 2) {
String partial = args[1].trim().toLowerCase();
List<String> possibleSizes = Arrays.asList("100", "200", "300", "500", "1000");

return possibleSizes.stream()
.filter(size -> size.startsWith(partial))
.collect(Collectors.toList());
}
return new ArrayList<>();

// ---------- ARG #3 : "confirm" ----------
else if (args.length == 3) {
String partial = args[2].trim().toLowerCase();

return Stream.of("confirm")
.filter(word -> word.startsWith(partial))
.collect(Collectors.toList());
}

return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;

public class SkylliaCommand implements SkylliaCommandInterface {

Expand Down Expand Up @@ -71,18 +72,20 @@ public void execute(CommandSourceStack sender, String[] args) {

@Override
public Collection<String> suggest(CommandSourceStack sender, String[] args) {
List<String> tab = new ArrayList<>();
Set<String> commands = registry.getCommandMap().keySet();
if (args.length == 0) {
tab.addAll(registry.getCommandMap().keySet());
return commands;
} else if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
return commands.stream().filter(command -> command.toLowerCase().startsWith(partial)).toList();
} else {
System.out.println(Arrays.toString(args));
String subCommand = args[0].trim().toLowerCase();
String[] listArgs = Arrays.copyOfRange(args, 1, args.length);
SubCommandInterface subCommandInterface = registry.getSubCommandByName(subCommand);
if (subCommandInterface != null) {
return subCommandInterface.onTabComplete(this.plugin, sender.getSender(), listArgs);
}
}
return tab;
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class BanSubCommand implements SubCommandInterface {

Expand Down Expand Up @@ -85,6 +86,13 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,

@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
return Bukkit.getOnlinePlayers().stream()
.map(CommandSender::getName)
.filter(name -> name.toLowerCase().startsWith(partial))
.collect(Collectors.toList());
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

public class CreateSubCommand implements SubCommandInterface {
Expand Down Expand Up @@ -141,16 +139,19 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
List<String> nameSchem = new ArrayList<>();
ConfigToml.schematicWorldMap.forEach((key, schematicWorld) -> {
if (CacheCommands.createTabCompleteCache.getUnchecked(new CacheCommands.CreateCacheCommandsTabs(sender, key))) {
if (CacheCommands.createTabCompleteCache.getUnchecked(new CacheCommands.CreateCacheCommandsTabs(sender, key))
&& key.toLowerCase().startsWith(partial)) {
nameSchem.add(key);
}
});

return nameSchem;
} else {
return new ArrayList<>();
}

return Collections.emptyList();
}

private void pasteSchematic(Main plugin, Island island, Location center, SchematicSetting schematicWorld) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class DelWarpSubCommand implements SubCommandInterface {

Expand Down Expand Up @@ -87,7 +88,13 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (sender.hasPermission("skyllia.island.command.delwarp") && sender instanceof Player player) {
return CacheCommands.warpTabCompleteCache.getUnchecked(player.getUniqueId());
if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
List<String> warpList = CacheCommands.warpTabCompleteCache.getUnchecked(player.getUniqueId());
return warpList.stream()
.filter(warp -> warp.toLowerCase().startsWith(partial))
.collect(Collectors.toList());
}
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,

@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (args.length == 1) {
String partial = args[0].trim().toLowerCase();
if ("confirm".startsWith(partial)) {
return Collections.singletonList("confirm");
}
}
return Collections.emptyList();
}

Expand Down
Loading

0 comments on commit 986e0db

Please sign in to comment.