当前位置: 动力学知识库 > 问答 > 编程问答 >

minecraft - Bukkit feed command does not work. No error it just does nothing

问题描述:

I been adding a heal and feed command i got into problems i finally fixed it but now when i want to use feed command it does nothing no error in the console just nothing it shows the the feed command is typed but it does NOTHING no message or anything. Here is the code:

package me.herobrine112211.ulticmdpack;

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.java.JavaPlugin;

public class System extends JavaPlugin {

public void onEnable() {

Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been actived!");

}

public void onDisable() {

Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");

}

@SuppressWarnings("deprecation")

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

if (!(sender instanceof Player)) {

sender.sendMessage(ChatColor.RED + "Console will never need owner support.");

return true;

}

Player player = (Player) sender;

if (cmd.getName().equalsIgnoreCase("contactowner")) {

player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");

}

if (cmd.getName().equalsIgnoreCase("heal")) {

if (!(sender instanceof Player)) {

sender.sendMessage(ChatColor.RED + "Console will never need owner support.");

return true;

}

if (cmd.getName().equalsIgnoreCase("contactowner")) {

player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");

}

if (cmd.getName().equalsIgnoreCase("heal")) {

if (args.length == 0) {

player.setHealth(20);

player.setFireTicks(0);

player.sendMessage(ChatColor.GREEN + "Your health is now full!");

return true;

}

Player target = Bukkit.getServer().getPlayer(args[0]);

if (target == null) {

player.sendMessage(ChatColor.RED + "Player must be online to be healed!");

return true;

}

target.setHealth(20);

player.setFireTicks(0);

target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");

player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");

}

if (cmd.getName().equalsIgnoreCase("feed")) {

if (args.length == 0) {

player.setFoodLevel(20);

player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");

return true;

}

Player target = Bukkit.getServer().getPlayer(args[0]);

if (target == null) {

player.sendMessage(ChatColor.RED + "Player must be online to be fed!");

return true;

}

target.setFoodLevel(20);

target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");

player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");

}

}

return true;

}

}

Here is the plugin.yml:

name: UltimateCommandPack

version: 1.0.0

main: me.herobrine112211.ulticmdpack.System

author: Herobrine112211

description: Ultimate Command Pack made for Herobrine112211 server.

commands:

contactowner:

usage: /<command>

aliases: [info]

description: Information to contact owner!

heal:

usage: /<command>

aliases: [health]

description: Healing command.

feed:

usage: /<command>

aliases: [hunger, food]

description: Feeding hunger command.

I hope someone can find this problem. I have no idea why its doing nothing. Heal command does work without any problems. Just feed...

网友答案:

Your problem is that your checking if the command is /feed in the block that checks if the command is /heal.

The command cannot be two values. Essentially, what you are doing is:

if(cmd.getName().equalsIgnoreCase("heal")){
    if(cmd.getName().equalsIgnoreCase("feed")){
        //feed code
    }
}

The cmd.getName().equalsIgnoreCase("feed") if statement will never be called, because the command's name has to be heal in order to reach the statement. If the command is heal, then cmd.getName().equalsIgnoreCase("feed") will always return false.

To fix this, your code should look something like this:

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

    Player player = (Player) sender;

  if(cmd.getName().equalsIgnoreCase("contactowner")){
    if(!(sender instanceof Player)){
      sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
      return true;
    }
    Player player = (Player) sender;

    //contact owner code
    return true;
  }
  else if(cmd.getName().equalsIgnoreCase("heal")){
    //heal code
    return true;
  }
  else if(cmd.getName().equalsIgnoreCase("feed")){
    //feed code
    return true;
  }
  return false;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: