我在spigot插件中的命令不起作用

时间:2018-03-14 11:04:04

标签: java plugins bukkit

我的插件插件无法正常工作。在控制台上,它说插件已启用,但我无法在插件中运行该命令。请帮助。

这是主要代码Plugin.java

package lol.quacnooblol.mypvpplugin;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class Plugin extends JavaPlugin{

@Override
public void onEnable() {
    Bukkit.getServer().getLogger().info("Plugin Enabled");
}

@Override
public void onDisable() {
    Bukkit.getServer().getLogger().info("Plugin Disabled");
}

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

    if(!(sender instanceof Player)) {
        sender.sendMessage("You ran this command on the console");
    }

    Player player = (Player) sender;

    if(cmd.getName().equalsIgnoreCase("test")) {
        player.sendMessage("You ran the test command in game.");
        return true;
        }
    return true;
    }
}

这是plugin.yml

name: Plugin
version: 0.1
main: lol.quacnooblol.mypvpplugin.Plugin
author: QuacNoobLoL
description: A pvp plugin

command:
  test:
  usage: /<command>
  description: A test command

2 个答案:

答案 0 :(得分:4)

将plugin.yml command更改为commands

将来请参考plugin.yml documentation,记住一封信就会破坏你的代码!

答案 1 :(得分:0)

在你的 Plugin.yml 你需要使用三个空格而不是制表符

这是固定文件:

name: Plugin
version: 0.1
main: lol.quacnooblol.mypvpplugin.Plugin
author: QuacNoobLoL
description: A pvp plugin
commands:
   test:
   usage: /<command>
   description: A test command

你的 Plugin.java onCommand 布尔值需要像这样的 @Override 注释:

@Override
public boolean onCommand(CommandSender, Command cmd, String commandLabel, String[] args) {
   if(cmd.getName().equalsIgnoreCase("test")){
      if(!(sender instanceof Player)){
        sender.sendMessage("You ran this command on the console");
      }
      Player player = (Player)sender;
      if(sender instanceof Player){
         player.sendMessage("You ran the test command in game.");
      }
      return true;
   }   
}

这应该有效