Disable sorting on TabExecutor

Discussion in 'Plugin Development' started by bennie3211, Aug 2, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    bennie3211

    Hi all, I've a question about the TabExecutor implementation for commands, I'm trying to list all possible items within a class and after sorting them to put the keyword 'all' on top of the list. But after experimenting for a while I noticed that the list is sorted again after putting the keyword 'all' at index 0 which I don't want. Is there anything I can do to stop this?

    Code I'm using:
    Code:
    package me.gonnakillyou2.commands;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.TabExecutor;
    import org.bukkit.entity.Player;
    
    import me.gonnakillyou2.HunterGames;
    import me.gonnakillyou2.managers.classes.Kit;
    
    public class KitRemovePotionCmd implements TabExecutor {
    
    private HunterGames plugin;
       
        public KitRemovePotionCmd(HunterGames hg) {
            this.plugin = hg;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("This command is only available for ingame players!");
                return true;
            }
           
            getPlugin().getKitManager().removeEffectFromKit((Player) sender, args);
            return true;
        }
    
        @Override
        public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                return null;
            }
           
            if (args.length == 1) {
                List<String> kitsSuggestions = new ArrayList<String>();
               
                for (Kit k : getPlugin().getKitManager().getKits()) {
                    if (k.getName().startsWith(args[0])) {
                        kitsSuggestions.add(k.getName());
                    }
                }
               
                Collections.sort(kitsSuggestions);
                return kitsSuggestions;
            }
           
            if (args.length == 2) {
                List<String> potionEffects = new ArrayList<String>();
                Kit k = getPlugin().getKitManager().getKitByName(args[0]);
               
                if (k != null) {
                   
                    for (String p : k.getPotionEffects()) {
                        potionEffects.add(p.split("\\;")[0]);
                    }
                   
                    Collections.sort(potionEffects);
                   
                    if (potionEffects.size() >= 2) {
                        potionEffects.add(0, "all");
                    }
                   
                    return potionEffects;
                }
               
                return null;
            }
           
            return null;
        }
       
        private HunterGames getPlugin() {
            return this.plugin;
        }
    }
    So if the kit contains the effects absorption and glowing I want to get the list as: all, absorption and glowing. But now it returns: absorption, all and glowing. Any way to manipulate the order or is it always auto sorting the list you give back and is Collections.sort() unnecessary at all?
     
  2. Offline

    Eisi05

    I don't know if this works, but try this:
    Code:
     if (k.getPotionEffect()s.size() >= 2) {
           potionEffects.add("all");
    }
    
    for (String p : k.getPotionEffects()) {
          potionEffects.add(p.split("\\;")[0]);
    }
                
    return potionEffects;
     
  3. Offline

    KarimAKL

    @bennie3211 I assume you would have to bypass it using reflection to access some CraftBukkit or NMS classes. You could try taking a look at the CraftBukkit source code to figure out what happens and when it is sorted.
     
Thread Status:
Not open for further replies.

Share This Page