英文:
/help <pluginname> works, actual command returns nothing. Minecraft 1.14.4 plugin
问题
以下是您要翻译的代码部分:
我正在尝试在1.14.4中编写一个Minecraft插件,`/help firstplugin` 可以正常工作,但 `/hello` 没有发送任何消息。
我的 Main.java:
```java
package com.Epic_Waffle.firstplugin;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.mojang.brigadier.Command;
import net.md_5.bungee.api.ChatColor;
public class Main extends JavaPlugin{
@Override
public void onEnable() {
System.out.println("FirstPlugin successfully enabled!");
}
@Override
public void onDisable() {
System.out.println("FirstPlugin disabled with no errors!");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
System.out.print(cmd);
if(cmd.toString().equals("/hello")) {
if(sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage(ChatColor.YELLOW + "Hello!" + ChatColor.GREEN + player.getName() + ChatColor.YELLOW + "Your health has been restored!");
player.setHealth(20.0);
} else {
System.out.println("You cannot use this command through console.");
}
}
return false;
}
}
我的 plugin.yml
version: 1.0
name: FirstPlugin
main: com.Epic_Waffle.firstplugin.Main
author: Epic_Waffle
description: FirstPlugin
commands:
hello:
aliases: []
description: Cool hello command!
英文:
I'm trying to code a minecraft plugin in 1.14.4, and /help firstplugin
works, but /hello
doesn't send any message.
My Main.Java:
package com.Epic_Waffle.firstplugin;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.mojang.brigadier.Command;
import net.md_5.bungee.api.ChatColor;
public class Main extends JavaPlugin{
@Override
public void onEnable() {
System.out.println("FirstPlugin successfully enabled!");
}
@Override
public void onDisable() {
System.out.println("FirstPlugin disabled with no errors!");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
System.out.print(cmd);
if(cmd.toString().equals("/hello")) {
if(sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage(ChatColor.YELLOW + "Hello!" + ChatColor.GREEN + player.getName() + ChatColor.YELLOW + "Your health has been restored!");
player.setHealth(20.0);
} else {
System.out.println("You cannot use this command through console.");
}
}
return false;
}
}
My plugin.yml
version: 1.0
name: FirstPlugin
main: com.Epic_Waffle.firstplugin.Main
author: Epic_Waffle
description: FirstPlugin
commands:
hello:
aliases: []
description: Cool hello command!
答案1
得分: 1
你需要在使用Spigot API之前注册每个命令(参见下面的onEnable方法)。您已经通过将命令添加到plugin.yml中执行了第一步。我还包括了此方法调用的文档。尽管这些是1.16的文档,但自1.14以来,在注册CommandExecutor方面没有进行任何更改。请确保您尝试注册的对象实现了CommandExecutor
。
public void setExecutor(@Nullable CommandExecutor executor)
设置在解析此命令时运行的CommandExecutor
参数:
executor - 新的执行器以运行
@Override
public void onEnable() {
this.getCommand("hello").setExecutor(this);
System.out.println("FirstPlugin successfully enabled!");
}
此外,我建议在您的onCommand方法中使用Command#getName()
而不是toString()
,如我的评论所述。
英文:
You have to register each command by using the Spigot API before it will be called (see the onEnable method below). You already performed the first step by adding the command to plugin.yml. I also included the documentation for this method call below. Even though these are the docs of 1.16, no changes have been made with regards to registering a CommandExecutor since 1.14. Make sure the object that you try to register implements CommandExecutor
.
public void setExecutor(@Nullable
CommandExecutor executor)
Sets the CommandExecutor to run when parsing this command
Parameters:
executor - New executor to run
Source for the documentation above.
@Override
public void onEnable() {
this.getCommand("hello").setExecutor( this );
System.out.println("FirstPlugin successfully enabled!");
}
Additionally I suggest using Command#getName()
instead of toString()
in your onCommand method as stated in my comment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论