Search in sources :

Example 1 with CommandHandler

use of net.minecraft.command.CommandHandler 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)

Example 2 with CommandHandler

use of net.minecraft.command.CommandHandler in project MinecraftForge by MinecraftForge.

the class FMLServerStartingEvent method registerServerCommand.

public void registerServerCommand(ICommand command) {
    CommandHandler ch = (CommandHandler) getServer().getCommandManager();
    ch.registerCommand(command);
}
Also used : CommandHandler(net.minecraft.command.CommandHandler)

Example 3 with CommandHandler

use of net.minecraft.command.CommandHandler in project AppleCore by squeek502.

the class Commands method init.

public static void init(MinecraftServer server) {
    CommandHandler commandHandler = (CommandHandler) server.getCommandManager();
    commandHandler.registerCommand(new CommandHunger());
}
Also used : CommandHandler(net.minecraft.command.CommandHandler)

Example 4 with CommandHandler

use of net.minecraft.command.CommandHandler in project Railcraft by Railcraft.

the class Railcraft method serverStarting.

@Mod.EventHandler
public void serverStarting(FMLServerStartingEvent event) {
    CommandHandler commandManager = (CommandHandler) event.getServer().getCommandManager();
    commandManager.registerCommand(ROOT_COMMAND);
}
Also used : CommandHandler(net.minecraft.command.CommandHandler)

Aggregations

CommandHandler (net.minecraft.command.CommandHandler)4 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