Command Issues?

Discussion in 'Plugin Development' started by Reestv, Apr 17, 2014.

Thread Status:
Not open for further replies.
  1. Hi,

    I'm still learning Bukkit but have an issue with the commands. I've complied a simple script which should make it so, when a player types /myinfo, it will open a chest-type interface in which the user will be presented with various options. I wasn't sure whether to use various classes or not, so I've just clumped everything in one 'main' class. Here's my code:
    Code:java
    1. package net.blastmc.playerstats;
    2.  
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6.  
    7. import org.bukkit.Material;
    8.  
    9. import org.bukkit.command.Command;
    10.  
    11. import org.bukkit.command.CommandSender;
    12.  
    13. import org.bukkit.entity.Player;
    14.  
    15. import org.bukkit.event.EventHandler;
    16.  
    17. import org.bukkit.event.Listener;
    18.  
    19. import org.bukkit.event.inventory.InventoryClickEvent;
    20.  
    21. import org.bukkit.inventory.Inventory;
    22.  
    23. import org.bukkit.inventory.ItemStack;
    24.  
    25. import org.bukkit.plugin.java.JavaPlugin;
    26.  
    27.  
    28.  
    29. public class main extends JavaPlugin implements Listener {
    30.  
    31.  
    32.  
    33. public void onEnable() {
    34.  
    35. getServer().getPluginManager().registerEvents(this, this);
    36.  
    37. }
    38.  
    39.  
    40.  
    41. public static Inventory myInventory = Bukkit.createInventory(null, 9, "Player Info");
    42.  
    43.  
    44.  
    45. static {
    46.  
    47. myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
    48.  
    49. myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    50.  
    51. }
    52.  
    53.  
    54.  
    55. @EventHandler
    56.  
    57. public void onInventoryClick(InventoryClickEvent event) {
    58.  
    59. ItemStack clicked = event.getCurrentItem();
    60.  
    61. Inventory inventory = event.getInventory();
    62.  
    63. if (inventory.getName().equals(myInventory.getName())) {
    64.  
    65. if (clicked.getType() == Material.DIRT) {
    66.  
    67. event.setCancelled(true);
    68.  
    69. }
    70.  
    71. }
    72.  
    73. }
    74.  
    75.  
    76.  
    77. @Override
    78.  
    79. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    80.  
    81. if (cmd.getName().equalsIgnoreCase("myinfo")) {
    82.  
    83. Player player = (Player) sender;
    84.  
    85. player.openInventory(myInventory);
    86.  
    87. return true;
    88.  
    89. }
    90.  
    91. return false;
    92.  
    93. }
    94.  
    95. }


    And here is the plugin.yml file that the plugin (should) be using:

    Code:
    name: PlayerStats
    version: 1.1
    description: Allows a player to view their own economics, or an Administrator to view anybody's economics (soon!).
     
    main: net.blastmc.playerstats.main
     
    commands:
      myinfo:
        description: Allows you to view your economics.
    As I say, I'm pretty new to this and am still learning as days pass by - so if anybody would be able to lend a simple solution to the issue (as to why the command won't do anything) it would be a great assistance.

    Best Regards,
    Rees.
     
  2. Offline

    will181

    What sort of error are you getting? Is it that the plugin isn't loading in at all (try /pl or /?) or something different?

    One thing, I personally like to write in different classses for larger projects, as it keeps the code clear and easy to alter.
     
  3. Offline

    TheMcScavenger

    Like will181 said, multiple classes makes a project more organised. I usually have:
    • Main
    • MyCommandHander
    • MyEventHandler
    If I use more than one command:
    • Cmd_NAME
    • Cmd_NAME2
    • Cmd_NAME3
    And if I use other methods, like connecting to / getting from / adding to a database, or making an inventory:
    • DatabaseHandler
    • InventoryHandler
     
Thread Status:
Not open for further replies.

Share This Page