Search in sources :

Example 1 with IMixinCommandHandler

use of org.spongepowered.common.interfaces.command.IMixinCommandHandler in project SpongeCommon by SpongePowered.

the class MinecraftCommandWrapper method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (!testPermission(source)) {
        throw new CommandPermissionException(Text.of(SpongeImpl.getGame().getRegistry().getTranslationById(TRANSLATION_NO_PERMISSION).get()));
    }
    CommandHandler handler = (CommandHandler) SpongeImpl.getServer().getCommandManager();
    final ICommandSender mcSender = WrapperICommandSender.of(source);
    final String[] splitArgs = splitArgs(arguments);
    int usernameIndex = 0;
    try {
        usernameIndex = handler.getUsernameIndex(this.command, splitArgs);
    } catch (net.minecraft.command.CommandException e) {
        throw new RuntimeException(e);
    }
    int successCount = 0;
    if (!throwEvent(mcSender, splitArgs)) {
        return CommandResult.empty();
    }
    // Below this is copied from CommandHandler.execute. This might need to be updated between versions.
    int affectedEntities = 1;
    if (usernameIndex > -1) {
        List<Entity> list = null;
        try {
            list = EntitySelector.matchEntities(mcSender, splitArgs[usernameIndex], Entity.class);
        } catch (net.minecraft.command.CommandException e) {
            throw new RuntimeException(e);
        }
        String previousNameVal = splitArgs[usernameIndex];
        affectedEntities = list.size();
        ((IMixinCommandHandler) handler).setExpandedSelector(true);
        for (Entity entity : list) {
            splitArgs[usernameIndex] = entity.getCachedUniqueIdString();
            if (tryExecute(handler, mcSender, splitArgs, arguments)) {
                ++successCount;
            }
        }
        splitArgs[usernameIndex] = previousNameVal;
        ((IMixinCommandHandler) handler).setExpandedSelector(false);
    } else {
        if (tryExecute(handler, mcSender, splitArgs, arguments)) {
            ++successCount;
        }
    }
    return CommandResult.builder().affectedEntities(affectedEntities).successCount(successCount).build();
}
Also used : Entity(net.minecraft.entity.Entity) CommandPermissionException(org.spongepowered.api.command.CommandPermissionException) CommandHandler(net.minecraft.command.CommandHandler) IMixinCommandHandler(org.spongepowered.common.interfaces.command.IMixinCommandHandler) ICommandSender(net.minecraft.command.ICommandSender) IMixinCommandHandler(org.spongepowered.common.interfaces.command.IMixinCommandHandler)

Aggregations

CommandHandler (net.minecraft.command.CommandHandler)1 ICommandSender (net.minecraft.command.ICommandSender)1 Entity (net.minecraft.entity.Entity)1 CommandPermissionException (org.spongepowered.api.command.CommandPermissionException)1 IMixinCommandHandler (org.spongepowered.common.interfaces.command.IMixinCommandHandler)1