/help works, actual command returns nothing. Minecraft 1.14.4 plugin

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

/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(&quot;FirstPlugin successfully enabled!&quot;);
	}
	
	@Override
	public void onDisable() {
		System.out.println(&quot;FirstPlugin disabled with no errors!&quot;);
	}
	
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		System.out.print(cmd);
		if(cmd.toString().equals(&quot;/hello&quot;)) {
			if(sender instanceof Player) {
				Player player = (Player) sender;
				
				player.sendMessage(ChatColor.YELLOW + &quot;Hello!&quot; + ChatColor.GREEN + player.getName() + ChatColor.YELLOW + &quot;Your health has been restored!&quot;);
				player.setHealth(20.0);
				
				
			} else {
				System.out.println(&quot;You cannot use this command through console.&quot;);
			}
		}
		
		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(&quot;hello&quot;).setExecutor( this );
System.out.println(&quot;FirstPlugin successfully enabled!&quot;);
}

Additionally I suggest using Command#getName() instead of toString() in your onCommand method as stated in my comment.

huangapple
  • 本文由 发表于 2020年7月31日 23:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63194488.html
匿名

发表评论

匿名网友

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

确定