/* * 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 lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandMap; import org.bukkit.command.SimpleCommandMap; import java.lang.reflect.Field; import java.util.Map; @UtilityClass class SpigotCommandRegistering { private static final CommandMap commandMap; private static final Map knownCommandMap; static { try { final Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap"); commandMapField.setAccessible(true); commandMap = (CommandMap) commandMapField.get(Bukkit.getServer()); } catch (NoSuchFieldException | IllegalAccessException exception) { Bukkit.shutdown(); throw new SecurityException("Oh shit. Commands cannot be registered.", exception); } try { final Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands"); knownCommandsField.setAccessible(true); knownCommandMap = (Map) knownCommandsField.get(commandMap); } catch (NoSuchFieldException | IllegalAccessException exception) { Bukkit.shutdown(); throw new SecurityException("Oh shit. Commands cannot be registered.", exception); } } static void unregister(Command command) { knownCommandMap.remove(command.getName()); command.getAliases().forEach(knownCommandMap::remove); command.unregister(commandMap); } static void register(Command command) { commandMap.register("command_framework", command); } }