use of dev.projectg.crossplatforms.command.CommandType in project CrossplatForms by ProjectG-Plugins.
the class ProxyCommandManager method load.
private void load() {
if (configManager.getConfig(GeneralConfig.class).isEmpty()) {
return;
}
GeneralConfig config = configManager.getConfig(GeneralConfig.class).get();
commandManager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, config.isUnsafeCommandRegistration());
registeredCommands.clear();
for (ProxyCommand command : config.getCommands().values()) {
String name = command.getName();
if (command.getPermission() == null || command.getPermission().isBlank()) {
command.setPermission(Constants.ID + ".shortcut." + name);
}
CommandType type = command.getMethod();
if (type == CommandType.REGISTER) {
if (commandManager.getCommandTree().getNamedNode(name) == null) {
// any references to the command are done through the map so that it can be updated after a reload
if (commandManager.isCommandRegistrationAllowed()) {
Logger.getLogger().debug("Registering shortcut command: " + command.getName());
commandManager.command(commandManager.commandBuilder(name).permission((origin) -> {
ProxyCommand target = registeredCommands.get(name);
if (target == null) {
// no longer present in the config
return false;
} else {
return origin.isPlayer() && origin.hasPermission(target.getPermission());
}
}).handler((context) -> {
FormPlayer player = Objects.requireNonNull(serverHandler.getPlayer(context.getSender().getUUID().orElseThrow()));
registeredCommands.get(name).run(player, interfaceManager, bedrockHandler);
}));
} else {
Logger.getLogger().warn("Unable to register shortcut command '" + name + "' because registration is no longer possible. Restart the server for changes to apply.");
}
}
// store the command
registeredCommands.put(name, command);
} else if (type == CommandType.INTERCEPT_CANCEL || type == CommandType.INTERCEPT_PASS) {
serverHandler.registerProxyCommand(command);
} else {
Logger.getLogger().severe("Unhandled CommandType enum constant: " + type);
}
}
}
use of dev.projectg.crossplatforms.command.CommandType in project CrossplatForms by ProjectG-Plugins.
the class VelocityHandler method onCommandExecute.
@Subscribe
public void onCommandExecute(CommandExecuteEvent event) {
CommandSource source = event.getCommandSource();
if (!event.getResult().isAllowed() || !(source instanceof Player)) {
return;
}
String input = event.getCommand();
Logger.get().debug("preprocess command: [" + event.getCommand() + "] -> [" + input + "]");
InterceptCommand command = findCommand(input);
if (command != null) {
Player player = (Player) source;
CommandType type = command.getMethod();
BedrockHandler bedrockHandler = CrossplatForms.getInstance().getBedrockHandler();
if (command.getPlatform().matches(player.getUniqueId(), bedrockHandler)) {
String permission = command.getPermission();
if (permission == null || player.hasPermission(permission)) {
command.run(new VelocityPlayer(player));
if (type == CommandType.INTERCEPT_CANCEL) {
// todo: if this sends a message about denial we might have to replace the common with a dummy
event.setResult(CommandExecuteEvent.CommandResult.denied());
}
}
}
}
}
use of dev.projectg.crossplatforms.command.CommandType in project CrossplatForms by ProjectG-Plugins.
the class CustomCommandManager method load.
private void load() {
serverHandler.clearInterceptCommands();
if (!configManager.getConfig(GeneralConfig.class).isPresent()) {
return;
}
GeneralConfig config = configManager.getConfig(GeneralConfig.class).get();
commandManager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, config.isUnsafeCommandRegistration());
List<Arguments> currentCommands = new ArrayList<>();
for (CustomCommand command : config.getCommands().values()) {
if (command == null) {
continue;
}
CommandType type = command.getMethod();
if (type == CommandType.REGISTER) {
if (command instanceof RegisteredCommand) {
RegisteredCommand registered = (RegisteredCommand) command;
registerCommand(registered);
currentCommands.add(registered.literals());
} else {
throw new IllegalStateException("CustomCommand has method type REGISTER but is not a RegisteredCommand: " + command);
}
} else if (type == CommandType.INTERCEPT_CANCEL || type == CommandType.INTERCEPT_PASS) {
if (command instanceof InterceptCommand) {
interceptCommand((InterceptCommand) command);
} else {
throw new IllegalStateException("CustomCommand has method type INTERCEPT_CANCEL or INTERCEPT_PASS but is not a ProxiedCommand: " + command);
}
} else {
logger.severe("Unhandled CommandType enum: " + type + ". Not registering command with name: " + command.getIdentifier());
}
}
for (Map.Entry<Arguments, RegisteredCommand> entry : registeredCommands.entrySet()) {
// enable commands that are current
// disable commands that are no longer current
// cannot remove old commands from the map because a double reload could result in cloud raising exceptions due to duplicate nodes/arguments
entry.getValue().enable(currentCommands.contains(entry.getKey()));
}
}
Aggregations