/* * 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.GameModeMapper; import de.steamwar.command.mapper.PlayerMapper; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import java.util.logging.Level; public class SpigotCommand extends AbstractCommand { static { CommandUtils.addMapper(Player.class, new PlayerMapper()); CommandUtils.addMapper(GameMode.class, new GameModeMapper()); addExecutorMapper(SpigotCommand.class, CommandSender.class, Function.identity()); } private Command command; /* @Setter private Message message = null; */ private List defaultHelpMessages = new ArrayList<>(); public SpigotCommand(String command, Consumer> initializer, String... aliases) { super(command, initializer, aliases); } public SpigotCommand(String command, String permission, Consumer> initializer, String... aliases) { super(command, permission, initializer, aliases); } @Override protected synchronized void initCommand() { this.command = new Command(SpigotCommand.super.command, "", "/" + command, Arrays.asList(aliases)) { @Override public boolean execute(CommandSender sender, String alias, String[] args) { SpigotCommand.this.execute(sender, alias, args); return false; } @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { return SpigotCommand.this.tabComplete(sender, alias, args); } }; this.command.setPermission(permission); } @Override public void unregister() { SpigotCommandRegistering.unregister(this.command); } @Override public void register() { SpigotCommandRegistering.register(this.command); } @Override protected void commandSystemError(CommandSender sender, CommandFrameworkException e) { Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); // TODO: Core.MESSAGE.sendPrefixless("COMMAND_SYSTEM_ERROR", sender); } public void addDefaultHelpMessage(String message) { defaultHelpMessages.add(message); } @Override protected void sendMessage(CommandSender sender, String message, Object[] args) { // TODO: this.message.send(message, sender, args); sender.sendMessage(MessageFormat.format(message, args)); } /* TODO @Register(noTabComplete = true) public void internalHelp(Player p, String... args) { if (message == null) { return; } try { message.sendPrefixless("COMMAND_HELP_HEAD", p, command.getName().substring(0, 1).toUpperCase() + command.getName().substring(1)); defaultHelpMessages.forEach(s -> message.sendPrefixless(s, p)); } catch (Exception e) { Bukkit.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(p, 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(p, subCommand); } }); } } private void send(Player p, 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); message.sendPrefixless(s, p, hover, new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggest)); } } catch (Exception e) { Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e); } } */ }