一些我无法理解的Java错误。

huangapple go评论83阅读模式
英文:

Some java errors that I can't understand

问题

以下是翻译好的内容:

我正在学习制作Minecraft插件。当我键入getServer().getPluginManager().registerEvents(listener: this, plugin: this);时,会出现一些错误。我将提供一个屏幕截图,展示错误的样子。但首先,这是我的代码:

package me.eventplugin.eventsplugin;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class EventsPlugin extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        // 插件启动逻辑
        System.out.println("插件现在将启动");
        // 这是给我带来错误的代码
        getServer().getPluginManager().registerEvents(listener: this, plugin:  this);
    }

    @EventHandler
    public void onLeaveBed(PlayerBedLeaveEvent event){
        // 这里是事件触发后将运行的代码
        Player player = event.getPlayer();
        player.sendMessage("早上好!");
    }
}

那个讲师也是键入了和我一样的代码,但我有错误,而他没有。这里是截图:一些我无法理解的Java错误。

有人可以解释一下我做错了什么吗?

英文:

So I am learning to make Minecraft Plugins. When I type getServer().getPluginManager().registerEvents(listener: this, plugin: this);, I get some errors. I will provide a screenshot of how it looks. But first, here is my code:

package me.eventplugin.eventsplugin;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedLeaveEvent;
import org.bukkit.plugin.java.JavaPlugin;

public final class EventsPlugin extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        // Plugin startup logic
        System.out.println("The plugin will now startup");
        // This is the code that gives me errors
        getServer().getPluginManager().registerEvents(listener: this, plugin:  this);
    }

    @EventHandler
    public void onLeaveBed(PlayerBedLeaveEvent event){
        // Here is the code that will run once the event is triggered
        Player player = event.getPlayer();
        player.sendMessage("Good Morning!!!");
    }

}

The instructor guy had typed the same thing I did, but I have the error, and he doesn't. Here is the screenshot: 一些我无法理解的Java错误。

Can someone explain what I am doing wrong?

答案1

得分: 2

Java不使用命名参数,而是使用位置参数。即registerEvents中的第一个参数是监听器,第二个参数是插件。移除标签,你的代码就应该没问题:

getServer().getPluginManager().registerEvents(this, this);
英文:

Java doesn't use named arguments, but positional arguments. I.e, the first argument in registerEvents is the listener and the second is the plugin. Remove the labels and you should be fine:

getServer().getPluginManager().registerEvents(this, this);

huangapple
  • 本文由 发表于 2020年8月27日 21:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63617498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定