Command not doing anything?

Discussion in 'Plugin Development' started by frogman6102, Apr 18, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    frogman6102

    I type /eat in minecraft and it says /eat in the chat and nothing happens?
    Here's my code:

    package com.gmail.frogman6102;




     

    import org.bukkit.Bukkit;

    import org.bukkit.ChatColor;

    import org.bukkit.Material;

    import org.bukkit.command.Command;

    import org.bukkit.command.CommandSender;

    import org.bukkit.entity.Player;

    import org.bukkit.event.Listener;

    import org.bukkit.event.player.PlayerInteractEntityEvent;

    import org.bukkit.inventory.ItemStack;

    import org.bukkit.inventory.ShapelessRecipe;

    import org.bukkit.inventory.meta.ItemMeta;

    import org.bukkit.plugin.java.JavaPlugin;


    publicclass Beavers extends JavaPlugin implements Listener{


    publicvoid onEnable() {

    Bukkit.getServer().getPluginCommand("eat");

    Bukkit.getServer().getPluginManager().registerEvents(this, this);

    System.out.println("Beavers plugin enabled!");


    ItemStack BeaverStick = new ItemStack(Material.STICK, 1);


    ItemMeta m = BeaverStick.getItemMeta();

    m.setDisplayName(ChatColor.AQUA + "Beaver Stick");


    BeaverStick.setItemMeta(m);



    ShapelessRecipe BStick = new ShapelessRecipe(BeaverStick);

    BStick.addIngredient(Material.STICK);

    BStick.addIngredient(Material.STICK);




    Bukkit.getServer().addRecipe(BStick);

    }

    publicvoid onDisable() {




    }



    publicboolean onCommand(CommandSender p, Command c, String[] label, String args[]) {

    if(c.getName().equalsIgnoreCase("eat")) {




    Player player = (Player) p;

    if (player instanceof Player) {


    if (player.getInventory().contains(Material.WOOD)) {

    if (player.getItemInHand().getData().equals(Material.WOOD)) {


    player.setFoodLevel(20);

    player.getInventory().remove(Material.WOOD);


    player.sendMessage(ChatColor.AQUA + "You have eaten some wood!");

    }else{

    player.sendMessage(ChatColor.RED + "You can only eat wood with this comand!");


    }



    }else{




    }



    }else{

    p.sendMessage(ChatColor.RED + "You must be a player or in-game!");


    }







    }else{




    }

    returnfalse;


    }

    publicvoid PlayerInteractEvent(PlayerInteractEntityEvent event) {








    Player p = event.getPlayer();



    if (p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "Beaver Stick")) {

    if (p.getEyeLocation().equals(Material.WATER)) {

    if (p.isSneaking()) {


    p.setFoodLevel(20);

    p.sendMessage(ChatColor.AQUA + "You have drank some water!");

    }else{




    }





    }else{

    return;


    }



    }else{

    return;


    }







    }

    }





    Here's my plugin.yml:

    name: Beavers

    main: com.gmail.frogman6102.Beavers

    version: 0.1

    commands:

    eat:

    description: Eat wood.

    usage: /eat

    permission: Beavers.eat

    permission-message: you can't eat wood!
     
  2. Offline

    Epicballzy

    frogman6102
    Could you please use the code tags. It's hard to see your code like this.
     
  3. Offline

    Heirteir

    frogman6102
    Could you use the code option when you make the thread so we can read it?
    Example:
    Code:java
    1. Example


    This would be great help so I can help you!
     
  4. Offline

    TGRHavoc

    frogman6102 Could you put this in the code tag as it's kind of hard to read...
     
  5. Offline

    frogman6102

    Code:
    plugin.yml:
    name: Beavers
    main: com.gmail.frogman6102.Beavers
    version: 0.1
    commands:
      eat:
          description: Eat wood.
          usage: /eat
          permission: Beavers.eat
          permission-message: you can't eat wood! 
     
     
    Code:
     
    package com.gmail.frogman6102;
     
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapelessRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Beavers extends JavaPlugin implements Listener{
       
        public void onEnable() {
            Bukkit.getServer().getPluginCommand("eat");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            System.out.println("Beavers plugin enabled!");
           
            ItemStack BeaverStick = new ItemStack(Material.STICK, 1);
            ItemMeta m = BeaverStick.getItemMeta();
            m.setDisplayName(ChatColor.AQUA + "Beaver Stick");
            BeaverStick.setItemMeta(m);
           
            ShapelessRecipe BStick = new ShapelessRecipe(BeaverStick);
            BStick.addIngredient(Material.STICK);
            BStick.addIngredient(Material.STICK);
           
            Bukkit.getServer().addRecipe(BStick);
        }
        public void onDisable() {
           
        }
     
        public boolean onCommand(CommandSender p, Command c, String[] label, String args[]) {
            if(c.getName().equalsIgnoreCase("eat")) {
               
                Player player = (Player) p;
                if (player instanceof Player) {
                   
                    if (player.getInventory().contains(Material.WOOD)) {
                        if (player.getItemInHand().getData().equals(Material.WOOD)) {
                            player.setFoodLevel(20);
                            player.getInventory().remove(Material.WOOD);
                           
                            player.sendMessage(ChatColor.AQUA + "You have eaten some wood!");
                        }else{
                            player.sendMessage(ChatColor.RED + "You can only eat wood with this comand!");
                        }
                       
                    }else{
                   
                    }
                   
                }else{
                    p.sendMessage(ChatColor.RED + "You must be a player or in-game!");
                }
               
               
               
            }else{
           
            }
            return false;
        }
        public void PlayerInteractEvent(PlayerInteractEntityEvent event) {
           
           
           
            Player p = event.getPlayer();
     
            if (p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "Beaver Stick")) {
                if (p.getEyeLocation().equals(Material.WATER)) {
                    if (p.isSneaking()) {
                        p.setFoodLevel(20);
                        p.sendMessage(ChatColor.AQUA + "You have drank some water!");
                    }else{
                       
                    }
                   
                   
                }else{
                    return;
                }
               
            }else{
                return;
            }
           
           
           
        }
        }
     
    
    Sorry, I am new and didn't know. Heirteir Epicballzy TGRHavoc

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Unfooishly

    frogman6102 do you have a plugin.yml and if so do you have your command stated in it
    Code:
    name: Plugin Name
    author: your name
    version: 1
    main: me.yourname.plugin name.Main
    commands:
      Eat:
      description: u can eat 
    I think thats the correct setup.... but don't know if I'm correct
     
  7. Offline

    Epicballzy

    frogman6102
    Here's a fix for your /eat command. One thing though is that it takes all the wood that's in your hand. Not just one.
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("eat")) {
    2. Player player = (Player) sender;
    3. if(player instanceof Player) {
    4. if (player.getInventory().getItemInHand().getType().equals(Material.WOOD)) {
    5. player.setFoodLevel(20);
    6. player.getInventory().remove(Material.WOOD);
    7. player.sendMessage(ChatColor.AQUA + "You have eaten some wood!");
    8. }else{
    9. player.sendMessage(ChatColor.RED + "You can only eat wood with this comand!");
    10. }
    11. } else {
    12. player.sendMessage("You must be a player or in-game!");
    13. }
    14. }

    And to remove the /eat problem, remove the "usage: /eat" from your plugin.yml.
    Code:
    name: Beavers
    version: o.1
    main: com.gmail.frogman6102.Beavers
    commands:
      Eat:
      permission: Beavers.eat
      permissions-message: You can't eat wood!
      description: Eat Wood.
       
     
  8. Offline

    frogman6102

    Ok, thanks! It removed the telling me what I put in the chat done, but now the /eat command does Epicballzy nothing?
     
  9. Offline

    TGRHavoc

    I thought the the onCommand method always had to look like this:
    Code:java
    1. public boolean onCommand(COmmandSender s, Command cmd, String label, String[] args)


    and not like this:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender p, Command c, String[] label, String args[]) {


    correct me if I'm wrong.
     
  10. Offline

    TomFromCollege

    You need to set it as a CommandExecutor:
    Code:
    public class Beavers extends JavaPlugin implements Listener, CommandExecutor{
    and in your constructor this:
    Code:
     Bukkit.getServer().getPluginCommand("eat");
    becomes this:
    Code:
    getServer().getPluginCommand("eat").setExecutor(
                    this);
    Also, make sure you add the @EventHandler on your PlayerInteractEntityEvent
     
  11. Offline

    Unfooishly

    frogman6102 i cant see anything wrong with the code but it seams the only thing that works is the Beaver Stick.
    I'm going keep messing around with your code on till i get it to work
     
  12. Offline

    TomFromCollege

    Readable (mostly, Bukkit still messes with tabbing) - Fixed code.
    Please use the autoformat available to you in your ide.
    In NetBeans the default shortcut is: ALT+SHIFT+F
    Code:java
    1. package com.gmail.frogman6102;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerInteractEntityEvent;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.ShapelessRecipe;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Beavers extends JavaPlugin implements Listener, CommandExecutor {
    17.  
    18. public void onEnable() {
    19. Bukkit.getServer().getPluginCommand("eat").setExecutor(this);
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21. System.out.println("Beavers plugin enabled!");
    22.  
    23. ItemStack BeaverStick = new ItemStack(Material.STICK, 1);
    24. ItemMeta m = BeaverStick.getItemMeta();
    25. m.setDisplayName(ChatColor.AQUA + "Beaver Stick");
    26. BeaverStick.setItemMeta(m);
    27.  
    28. ShapelessRecipe BStick = new ShapelessRecipe(BeaverStick);
    29. BStick.addIngredient(Material.STICK);
    30. BStick.addIngredient(Material.STICK);
    31.  
    32. Bukkit.getServer().addRecipe(BStick);
    33. }
    34.  
    35. public void onDisable() {
    36.  
    37. }
    38.  
    39. @Override
    40. public boolean onCommand(CommandSender p, Command c, String label, String args[]) {
    41. if (c.getName().equalsIgnoreCase("eat")) {
    42.  
    43. Player player = (Player) p;
    44. if (player instanceof Player) {
    45.  
    46. if (player.getInventory().contains(Material.WOOD)) {
    47. if (player.getItemInHand().getData().equals(Material.WOOD)) {
    48. player.setFoodLevel(20);
    49. player.getInventory().remove(Material.WOOD);
    50.  
    51. player.sendMessage(ChatColor.AQUA + "You have eaten some wood!");
    52. } else {
    53. player.sendMessage(ChatColor.RED + "You can only eat wood with this comand!");
    54. }
    55.  
    56. } else {
    57.  
    58. }
    59.  
    60. } else {
    61. p.sendMessage(ChatColor.RED + "You must be a player or in-game!");
    62. }
    63.  
    64. } else {
    65.  
    66. }
    67. return false;
    68. }
    69.  
    70. @EventHandler
    71. public void PlayerInteractEvent(PlayerInteractEntityEvent event) {
    72.  
    73. Player p = event.getPlayer();
    74.  
    75. if (p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "Beaver Stick")) {
    76. if (p.getEyeLocation().equals(Material.WATER)) {
    77. if (p.isSneaking()) {
    78. p.setFoodLevel(20);
    79. p.sendMessage(ChatColor.AQUA + "You have drank some water!");
    80. } else {
    81.  
    82. }
    83. } else {
    84. return;
    85. }
    86.  
    87. } else {
    88. return;
    89. }
    90. }
    91. }
    92.  
     
  13. Offline

    frogman6102

    Ok, TomFromCollege I added the EvenHandler and set up the constructor. But nothing is happening still?
     
Thread Status:
Not open for further replies.

Share This Page