英文:
My Spigot Minecraft plugin doesn't say "yum" when I eat something
问题
Listeners.java:
package adawda.awdwa;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Listeners extends JavaPlugin implements Listener {
public Listeners(Awdwa plugin) {
}
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
event.getPlayer().sendMessage("Yum!");
}
}
Awdwa.java (Main):
package adawda.awdwa;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public final class Awdwa extends JavaPlugin implements CommandExecutor {
public Location homeLoc;
@Override
public void onEnable() {
PluginManager pm = getServer().getPluginManager();
Listeners listener = new Listeners(this);
pm.registerEvents(listener, this);
}
public void onPlayerJoin(PlayerJoinEvent event) {
Bukkit.broadcastMessage("Naber, " + event.getPlayer().getName() + "!");
}
public void onPlayerBedEnter(PlayerBedEnterEvent event) {
Bukkit.broadcastMessage("birisi uyuyor.. daha doğrusu çalışıyor.");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
String cmdName = cmd.getName().toLowerCase();
Player p = (Player) sender;
if (cmdName.equals("example")) {
sender.sendMessage("UwU it works!");
return true;
} else if (cmdName.equals("sethome")) {
homeLoc = p.getLocation();
return true;
} else if (cmdName.equals("home")) {
p.teleport(homeLoc);
return true;
} else if (cmdName.equals("kedi")) {
World w = getServer().getWorld("world");
int i = 0;
for (int j = 0; j < 16; j++) {
w.spawnEntity(p.getLocation(), EntityType.CAT);
}
return true;
} else {
return false;
}
}
}
英文:
I'm working on a Spigot plugin right now. but I'm having some problems with it. I'm having issues with the listeners. I've watched tutorials but it didn't work. When I eat something in-game it doesn't say "Yum!" It doesn't do anything. I tried to fix it but I can't. I don't think it's because I put it in the same package.
Listeners.java :
package adawda.awdwa;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Listeners extends JavaPlugin implements Listener
{
public Listeners(Awdwa plugin)
{
}
public void onEnable()
{
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onPlayerItemConsume(PlayerItemConsumeEvent event)
{
event.getPlayer().sendMessage("Yum!");
}
}
Awdwa.java ( Main ) :
package adawda.awdwa;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.util.Arrays;
public final class Awdwa extends JavaPlugin implements CommandExecutor {
public Location homeLoc;
@Override
public void onEnable() {
// Plugin startup logic
PluginManager pm = getServer().getPluginManager();
Listeners listener = new Listeners(this);
pm.registerEvents(listener,this);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
public void onPlayerJoin(PlayerJoinEvent event)
{
Bukkit.broadcastMessage("Naber, " + event.getPlayer().getName()+"!");
}
public void onPlayerBedEnter(PlayerBedEnterEvent event)
{
Bukkit.broadcastMessage("birisi uyuyor.. daha doğrusu çalışıyor.");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
String cmdName = cmd.getName().toLowerCase();
Player p = (Player) sender;
if (cmdName.equals("example")) {
sender.sendMessage("UwU it works!");
return true;
} else if(cmdName.equals("sethome")) {
homeLoc = p.getLocation();
return true;
} else if(cmdName.equals("home")) {
p.teleport(homeLoc);
return true;
} else if(cmdName.equals("kedi")) {
World w = getServer().getWorld("world");
int i = 0;
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
w.spawnEntity(p.getLocation(), EntityType.CAT);
return true;
}
else {
return false;
}
}
}
答案1
得分: 5
请在您的 Listeners.class 中删除 extends JavaPlugin
。这只需要在 Main.class 内部使用。您的 Listeners.class 应默认只实现 Listener 接口。然后,您还应删除 Listeners 类中的构造函数和 onEnable() 方法,因为它们不是必需的。
然后,您应更改在您的 Main.class 中注册事件的方式。您应该像这样注册事件:
pm.registerEvents(new Listeners(), this);
(在 registerEvents() 方法内创建一个新的 Listeners 对象)
英文:
Please remove the extends JavaPlugin
in your Listeners.class. This will only be needed inside the Main.class. Your Listeners.class should only implement Listener by default. You should then also remove the constructor and the onEnable() method inside your Listeners class as they are not needed.
You should then change your way of registering events inside your Main.class. You should register events like this:
pm.registerEvents(new Listeners(), this);
(Create a new Listeners Object inside the registerEvents() method)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论