/* * Copyright (C) <2022> * * 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 . */ 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 subCommandMap; public AbstractSubCommand(String command) { this.command = command; } public boolean onCommand(CommandSender sender, List 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 onTabComplete(CommandSender sender, List args) { if (subCommandMap == null) return Collections.singletonList(""); if (args.size() <= 1) { List 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 getSubCommands() { return Collections.unmodifiableMap(subCommandMap); } public void regSubCommand(AbstractSubCommand command) { if (subCommandMap == null) { subCommandMap = new ConcurrentHashMap<>(); } subCommandMap.put(command.getSubCommand(), command); } protected List online_players() { return Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList()); } protected List allNameplates() { return new ArrayList<>(CustomNameplates.getInstance().getNameplateManager().getNameplateConfigMap().keySet()); } protected List allBubbles() { return new ArrayList<>(CustomNameplates.getInstance().getChatBubblesManager().getBubbleConfigMap().keySet()); } protected boolean notExist(CommandSender commandSender, String type, String value) { if (type.equals("nameplate")) { if (!CustomNameplatesAPI.getInstance().doesNameplateExist(value)) { AdventureUtils.sendMessage(commandSender, MessageManager.prefix + MessageManager.np_not_exist); return true; } } else if (type.equals("bubble")) { if (!CustomNameplatesAPI.getInstance().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 filterStartingWith(List list, String prefix) { return list.stream().filter(s -> s.startsWith(prefix)).collect(Collectors.toList()); } }