SteamWar/BauSystem
Archiviert
13
0
Dieses Repository wurde am 2024-08-04 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
BauSystem/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandTraceTabCompleter.java

87 Zeilen
3.4 KiB
Java

2020-08-26 18:48:57 +02:00
/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-07-18 23:01:50 +02:00
package de.steamwar.bausystem.commands;
2020-12-18 22:38:31 +01:00
import de.steamwar.bausystem.tracer.record.RecordStateMachine;
import de.steamwar.bausystem.tracer.record.RecordStatus;
2020-07-18 23:01:50 +02:00
import org.bukkit.command.Command;
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.function.BiPredicate;
2020-07-18 23:01:50 +02:00
public class CommandTraceTabCompleter implements TabCompleter {
private static List<TabComplete> tabCompletes = new ArrayList<>();
static {
tabCompletes.add(new TabComplete((player, args) -> args.length == 1 && (RecordStateMachine.getRecordStatus() == RecordStatus.IDLE || RecordStateMachine.getRecordStatus() == RecordStatus.IDLE_AUTO), "start"));
tabCompletes.add(new TabComplete((player, args) -> args.length == 1 && (RecordStateMachine.getRecordStatus() != RecordStatus.IDLE && RecordStateMachine.getRecordStatus() != RecordStatus.IDLE_AUTO), "stop"));
tabCompletes.add(new TabComplete((player, args) -> args.length == 1, "toggleauto", "auto", "show", "hide", "delete", "clear"));
tabCompletes.add(new TabComplete((player, args) -> args.length == 2 && args[0].equalsIgnoreCase("show"), "nowater", "basic", "advanced", "advanced-nowater"));
}
2020-07-18 23:01:50 +02:00
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if(!(sender instanceof Player)) return new ArrayList<>();
2020-08-30 02:23:23 +02:00
return tracerTabComplete((Player) sender, args);
2020-07-18 23:01:50 +02:00
}
2020-08-30 02:23:23 +02:00
private List<String> tracerTabComplete(Player player, String[] args) {
2020-07-18 23:01:50 +02:00
List<String> tabComplete = new ArrayList<>();
for (TabComplete tab : tabCompletes) {
if (tab.test(player, args)) tabComplete.addAll(Arrays.asList(tab.getTabCompletes()));
2020-07-18 23:01:50 +02:00
}
return manageList(tabComplete, args);
2020-07-18 23:01:50 +02:00
}
private List<String> manageList(List<String> strings, String[] args) {
2020-07-18 23:01:50 +02:00
for (int i = strings.size() - 1; i >= 0; i--) {
if (!strings.get(i).startsWith(args[args.length - 1])) strings.remove(i);
2020-07-18 23:01:50 +02:00
}
return strings;
}
private static class TabComplete {
private BiPredicate<Player, String[]> function;
private String[] tabCompletes;
private TabComplete(BiPredicate<Player, String[]> function, String... tabCompletes) {
this.function = function;
this.tabCompletes = tabCompletes;
}
public boolean test(Player player, String[] args) {
return function.test(player, args);
}
public String[] getTabCompletes() {
return tabCompletes;
}
}
2020-07-18 23:01:50 +02:00
}