检查参数时遇到问题

时间:2015-07-30 02:48:58

标签: java bukkit

当我尝试运行命令/squid时,它可以正常运行,但当我执行/squid set/squid start时,没有任何反应。

该命令应该告诉您位置已设置或事件已启动,但事实并非如此。控制台中没有错误,我也没有得到任何反馈。

package me.mcmatt.squidcp;

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

import com.sk89q.worldedit.bukkit.WorldEditPlugin;

public class Main extends JavaPlugin {


  public void onEnable() {
    Bukkit.getServer().getLogger().info("Squid CP booted up! Version: " + Bukkit.getPluginManager().getPlugin("SquidCarePackages").getDescription().getVersion());

  }
  public void onDisable() {
    Bukkit.getServer().getLogger().info("Squid CP shut down!");
  }

  public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if (commandLabel.equalsIgnoreCase("squid")) {
      if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "You must be a player to run this command");
      } else {
        Player player = (Player) sender;
        if (args.length == 0) {
          player.sendMessage(ChatColor.GOLD + "----Commands----");
          player.sendMessage(ChatColor.GOLD + "/squid -" + ChatColor.AQUA + " Displays this message");
          player.sendMessage(ChatColor.GOLD + "/squid set -" + ChatColor.AQUA + " Sets the area at wich the squids spawn");
          player.sendMessage(ChatColor.GOLD + "/squid start -" + ChatColor.AQUA + " Starts the squid care package");
          player.sendMessage(ChatColor.GOLD + "Plugin created by McMatt, @McMattGames");

        } else {
          if (args.length == 1) {
            if (args[0].equalsIgnoreCase("set")) {
              player.sendMessage("Area Set!");
            } else {
              if (args[0].equalsIgnoreCase("start")) {
                player.sendMessage("Starting!");
              } else {
                player.sendMessage(ChatColor.RED + "Invalid Arguments!");

              }

            }
          }
        }
      }
    }
    return true;

  }

  public WorldEditPlugin getWorldEdit() {
    Plugin p = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
    if (p instanceof WorldEditPlugin) return (WorldEditPlugin) p;
    else return null;

  }

}

1 个答案:

答案 0 :(得分:1)

You are not taking in consideration multiple things about Java and the command system.

First of all, you should make use of else if when needed. This prevents you from duplicating code. For example, you can use else if like this:

if (args[0].equalsIgnoreCase("set")) {
    player.sendMessage("Area Set!");
} else if (args[0].equalsIgnoreCase("start")) {
    player.sendMessage("Starting!");
} else {
    player.sendMessage(ChatColor.RED + "Invalid Arguments!");
}

Second, do not use commandLabel. You should be using command.getName() instead. This is because the command label is a volatile value; it changes if you use an alias to your command (e. g. using /s instead of /squid).

Third, please be sure you inserted the command name in the Plugin YAML. Your YML file should have at least these lines:

commands:
  squid: