命令没有做任何事情

时间:2015-08-10 23:19:38

标签: java bukkit

我正在尝试创建一个向发件人发送邮件的命令,但它无效。

这是主要课程:

public void onEnabled() {
getLogger().info("Enabled!");
}

public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("hi")) {
        sender.sendMessage("Hey there " + sender);
    }
    return false;
}

2 个答案:

答案 0 :(得分:3)

我不知道你的 plugin.yml ,但如果它是正确的,那么如果你从以下地方改变它应该有效:

onCommand(CommandSender sender, Command cmd, String label, String[] args)

要:

onCommand()

是的,订单在sender.sendMessage("Hey there " + sender); 方法中很重要。

要显示发件人的姓名,您需要更改:

sender.sendMessage("Hey there " + sender.getName());

要:

public async Task Invoke(HttpContext context)
{
    var requestMessage = new HttpRequestMessage();
    if (string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
    {
        var streamContent = new StreamContent(context.Request.Body);
        requestMessage.Content = streamContent;
    }

    // Copy the request headers
    foreach (var header in context.Request.Headers)
    {
        if (!requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value) && requestMessage.Content != null)
        {
            requestMessage.Content?.Headers.TryAddWithoutValidation(header.Key, header.Value);
        }
    }

    requestMessage.Headers.Host = _options.Host + ":" + _options.Port;
    var uriString = $"{_options.Scheme}://{_options.Host}:{_options.Port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}";
    requestMessage.RequestUri = new Uri(uriString);
    requestMessage.Method = new HttpMethod(context.Request.Method);
    using (var responseMessage = await _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, context.RequestAborted))
    {
        context.Response.StatusCode = (int)responseMessage.StatusCode;
        foreach (var header in responseMessage.Headers)
        {
            context.Response.Headers.SetValues(header.Key, header.Value.ToArray());
        }

        foreach (var header in responseMessage.Content.Headers)
        {
            context.Response.Headers.SetValues(header.Key, header.Value.ToArray());
        }

        // SendAsync removes chunking from the response. This removes the header so it doesn't expect a chunked response.
        context.Response.Headers.Remove("transfer-encoding");
        await responseMessage.Content.CopyToAsync(context.Response.Body);
    }
}

答案 1 :(得分:0)

onCommand()方法应返回boolean。您只需要返回truefalsetrue表示命令已完成某项操作,因此您应将其作为if语句的一部分返回。否则只返回false。 以下应该有效:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("hi")) {
        sender.sendMessage("Hey there " + sender.getName());
        return true;
    }
    return false;
}

如果这不起作用,请尝试发布您的 plugin.yml 以及您收到的错误,因为这可能会告诉我们更多信息。