9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-21 07:59:22 +00:00
This commit is contained in:
Xiao-MoMi
2023-03-29 19:51:23 +08:00
parent ebd4f44f50
commit b70c7eedef
22 changed files with 53 additions and 44 deletions

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customnameplates.command;
import net.momirealms.customnameplates.CustomNameplates;
import net.momirealms.customnameplates.api.CustomNameplatesAPI;
import net.momirealms.customnameplates.manager.MessageManager;
import net.momirealms.customnameplates.utils.AdventureUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public abstract class AbstractSubCommand {
private final String command;
private Map<String, AbstractSubCommand> subCommandMap;
public AbstractSubCommand(String command) {
this.command = command;
}
public boolean onCommand(CommandSender sender, List<String> args) {
if (subCommandMap == null || args.size() < 1) {
return true;
}
AbstractSubCommand subCommand = subCommandMap.get(args.get(0));
if (subCommand == null) {
AdventureUtils.sendMessage(sender, MessageManager.prefix + MessageManager.unavailableArgs);
} else {
subCommand.onCommand(sender, args.subList(1, args.size()));
}
return true;
}
public List<String> onTabComplete(CommandSender sender, List<String> args) {
if (subCommandMap == null)
return Collections.singletonList("");
if (args.size() <= 1) {
List<String> returnList = new ArrayList<>(subCommandMap.keySet());
returnList.removeIf(str -> !str.startsWith(args.get(0)));
return returnList;
}
AbstractSubCommand subCmd = subCommandMap.get(args.get(0));
if (subCmd != null)
return subCommandMap.get(args.get(0)).onTabComplete(sender, args.subList(1, args.size()));
return Collections.singletonList("");
}
public String getSubCommand() {
return command;
}
public Map<String, AbstractSubCommand> getSubCommands() {
return Collections.unmodifiableMap(subCommandMap);
}
public void regSubCommand(AbstractSubCommand command) {
if (subCommandMap == null) {
subCommandMap = new ConcurrentHashMap<>();
}
subCommandMap.put(command.getSubCommand(), command);
}
protected List<String> online_players() {
return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList());
}
protected List<String> allNameplates() {
return new ArrayList<>(CustomNameplates.getInstance().getNameplateManager().getNameplateConfigMap().keySet());
}
protected List<String> allBubbles() {
return new ArrayList<>(CustomNameplates.getInstance().getChatBubblesManager().getBubbleConfigMap().keySet());
}
protected boolean notExist(CommandSender commandSender, String type, String value) {
if (type.equals("nameplate")) {
if (!CustomNameplatesAPI.getAPI().doesNameplateExist(value)) {
AdventureUtils.sendMessage(commandSender, MessageManager.prefix + MessageManager.np_not_exist);
return true;
}
} else if (type.equals("bubble")) {
if (!CustomNameplatesAPI.getAPI().doesBubbleExist(value)) {
AdventureUtils.sendMessage(commandSender, MessageManager.prefix + MessageManager.bb_not_exist);
return true;
}
}
return false;
}
protected boolean noConsoleExecute(CommandSender commandSender) {
if (!(commandSender instanceof Player)) {
AdventureUtils.consoleMessage(MessageManager.prefix + MessageManager.no_console);
return true;
}
return false;
}
protected boolean lackArgs(CommandSender commandSender, int required, int current) {
if (required > current) {
AdventureUtils.sendMessage(commandSender, MessageManager.prefix + MessageManager.lackArgs);
return true;
}
return false;
}
protected boolean playerNotOnline(CommandSender commandSender, String player) {
if (Bukkit.getPlayer(player) == null) {
AdventureUtils.sendMessage(commandSender, MessageManager.prefix + MessageManager.not_online.replace("{Player}", player));
return true;
}
return false;
}
protected List<String> filterStartingWith(List<String> list, String prefix) {
return list.stream().filter(s -> s.startsWith(prefix)).collect(Collectors.toList());
}
}