/* * This file is a part of the SteamWar software. * * Copyright (C) 2023 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 . */ package de.steamwar.command; import de.steamwar.command.mapper.ProxiedPlayerMapper; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.TabExecutor; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import java.util.logging.Level; public class VelocityCommand extends AbstractCommand { static { CommandUtils.addMapper(ProxiedPlayer.class, new ProxiedPlayerMapper()); addExecutorMapper(VelocityCommand.class, CommandSender.class, Function.identity()); } private Command command; private List defaultHelpMessages = new ArrayList<>(); public VelocityCommand(String command, Consumer> initializer, String... aliases) { super(command, initializer, aliases); } public VelocityCommand(String command, String permission, Consumer> initializer, String... aliases) { super(command, permission, initializer, aliases); } private abstract static class TabCompletableCommand extends Command implements TabExecutor { public TabCompletableCommand(String name, String permission, String... aliases) { super(name, permission, aliases); } } @Override protected synchronized void initCommand() { if (command != null) return; command = new TabCompletableCommand(VelocityCommand.super.command, permission, aliases) { @Override public void execute(CommandSender commandSender, String[] strings) { VelocityCommand.this.execute(commandSender, null, strings); } @Override public Iterable onTabComplete(CommandSender commandSender, String[] strings) { return VelocityCommand.this.tabComplete(commandSender, null, strings); } }; } @Override public void unregister() { VelocityCommandRegistering.unregister(command); } @Override public void register() { VelocityCommandRegistering.register(command); } @Override protected void commandSystemError(CommandSender sender, CommandFrameworkException e) { ProxyServer.getInstance().getLogger().log(Level.SEVERE, e.getMessage(), e); // TODO: ChatSender.of(sender).prefixless("COMMAND_SYSTEM_ERROR"); } public void addDefaultHelpMessage(String message) { defaultHelpMessages.add(message); } @Override protected void sendMessage(CommandSender sender, String message, Object[] args) { // TODO: ChatSender.of(sender).system(message, args); sender.sendMessage(MessageFormat.format(message, args)); } /* TODO @Register(noTabComplete = true) public void internalHelp(ProxiedPlayer p, String... args) { ChatSender chatSender = ChatSender.of(p); try { chatSender.prefixless("COMMAND_HELP_HEAD", command.getName()); defaultHelpMessages.forEach(chatSender::prefixless); } catch (Exception e) { BungeeCore.get().getLogger().log(Level.WARNING, "Failed to send help message", e); return; } AtomicInteger atomicInteger = new AtomicInteger(); if (args.length != 0) { commandList.forEach(subCommand -> { List tabCompletes = subCommand.tabComplete(p, args); if (tabCompletes == null || tabCompletes.isEmpty()) { atomicInteger.incrementAndGet(); return; } boolean hasTabCompletes = tabCompletes.stream() .anyMatch(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase())); if (hasTabCompletes) { send(chatSender, subCommand); } else { atomicInteger.incrementAndGet(); } }); } if (args.length == 0 || atomicInteger.get() == commandList.size()) { commandList.forEach(subCommand -> { if (subCommand.validator == null || subCommand.validator.validate(p, p, (s, args1) -> {})) { send(chatSender, subCommand); } }); } } private void send(ChatSender chatSender, SubCommand subCommand) { try { for (String s : subCommand.description) { String hover = "§8/§e" + command.getName() + " " + String.join(" ", subCommand.subCommand); String suggest = "/" + command.getName() + " " + String.join(" ", subCommand.subCommand); chatSender.prefixless(s, new Message("PLAIN_STRING", hover), new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggest)); } } catch (Exception e) { BungeeCore.get().getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); } } */ }