英文:
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("早上好!");
}
}
那个讲师也是键入了和我一样的代码,但我有错误,而他没有。这里是截图:
有人可以解释一下我做错了什么吗?
英文:
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:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论