Fixes tab completion for the add and remove commands #7
This commit is contained in:
@ -0,0 +1,69 @@
|
||||
package net.knarcraft.paidsigns.command;
|
||||
|
||||
import net.knarcraft.paidsigns.PaidSigns;
|
||||
import net.knarcraft.paidsigns.container.PaidSign;
|
||||
import net.knarcraft.paidsigns.utility.Tokenizer;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* A representation of the command for removing a paid sign
|
||||
*/
|
||||
public class RemoveTabCommand implements TabExecutor {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
|
||||
@NotNull String[] args) {
|
||||
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
||||
if (arguments.size() < 1) {
|
||||
return false;
|
||||
}
|
||||
String name = arguments.get(0);
|
||||
|
||||
try {
|
||||
if (PaidSigns.getInstance().getSignManager().removePaidSign(name)) {
|
||||
sender.sendMessage("Successfully removed paid sign");
|
||||
} else {
|
||||
sender.sendMessage("No matching paid sign was found");
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
Logger logger = PaidSigns.getInstance().getLogger();
|
||||
logger.log(Level.SEVERE, "Exception encountered while trying to write " +
|
||||
"to the data file");
|
||||
logger.log(Level.SEVERE, Arrays.toString(e.getStackTrace()));
|
||||
sender.sendMessage("An exception occurred. Please notify the server administrator or check the server log.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
|
||||
@NotNull String[] args) {
|
||||
List<String> arguments = Tokenizer.tokenize(String.join(" ", args));
|
||||
int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1;
|
||||
|
||||
if (argumentSize < 1) {
|
||||
List<PaidSign> allPaidSigns = PaidSigns.getInstance().getSignManager().getAllPaidSigns();
|
||||
List<String> signIds = new ArrayList<>();
|
||||
for (PaidSign sign : allPaidSigns) {
|
||||
signIds.add("\"" + sign.getName() + "\"");
|
||||
}
|
||||
return signIds;
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user