Command Executor I'm Probably Retarded?

Discussion in 'Plugin Development' started by JBoss925, Mar 26, 2014.

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

    JBoss925

    I am trying to reference a command in another class. I have done this countless times yet for some reason it is not working anymore. I'm extremely confused. What I want is for "/code" to run the stuff in the money.java . Here's my main class:
    Code:java
    1. package me.JBoss925.bits;
    2.  
    3.  
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin implements Listener, CommandExecutor{
    9.  
    10. public void onEnable(){
    11. System.out.print("Bit Currency v1.0 has been enabled!");
    12. this.getServer().getPluginManager().registerEvents(new Money(), this);
    13. getCommand("code").setExecutor(new Money());
    14. }
    15.  
    16. public void onDisable(){
    17. System.out.print("Bit Currency v1.0 has been disabled!");
    18. }
    19.  
    20.  
    21. }


    My money class:
    Code:java
    1. package me.JBoss925.bits;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.EntityType;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. import java.util.ArrayList;
    16. import java.util.HashMap;
    17. import java.util.List;
    18.  
    19. public class Money implements Listener, CommandExecutor{
    20.  
    21. HashMap<Player, Integer> bits = new HashMap<Player, Integer>();
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. Player p = (Player) sender;
    25. if(cmd.getName().equalsIgnoreCase("code")){
    26. if(args.length == 0){
    27. if(bits.containsKey(p)){
    28. Integer money = bits.get(p);
    29. p.sendMessage(ChatColor.GOLD + "Hi, welcome to the computer. Would you like to use your bits to code something? Just use /code [item #] to code it! \n" +
    30. ChatColor.AQUA + "1. Diamond Sword - 40 bits\n2. Diamond Pickaxe - 35 bits\n3. Diamond Axe - 30 bits\n4. " + ChatColor.AQUA +
    31. "Diamond Shovel - 25 bits\n5. Diamond Hoe - 20 bits");
    32. }
    33. if(!(bits.containsKey(p))){
    34. p.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "You have no bits!");
    35. }
    36. }
    37. if(args.length == 1){
    38. if(bits.containsKey(p)){
    39. String item = args[0];
    40. if(item.equalsIgnoreCase("1") && bits.get(p) >= 40){
    41. Integer i = bits.get(p);
    42. Integer in = bits.get(p) - 40;
    43. bits.remove(p);
    44. bits.put(p, in);
    45. }
    46.  
    47. }
    48. if(!(bits.containsKey(p))){
    49. p.sendMessage(ChatColor.BOLD + "" + ChatColor.RED + "You have no bits!");
    50. }
    51. }
    52. }
    53. return true;
    54. }
    55. @EventHandler
    56. public void onEntityDeath(EntityDeathEvent e){
    57. Player killed = (Player) e.getEntity();
    58. Player killer = (Player) e.getEntity().getKiller();
    59. if(e.getEntity().getType() == EntityType.PLAYER && e.getEntity().getKiller().getType() == EntityType.PLAYER){
    60. if(bits.containsKey(e.getEntity().getKiller())){
    61. Integer o = bits.get(e.getEntity().getKiller());
    62. Integer on = bits.get(e.getEntity().getKiller()) + 5;
    63. bits.remove(killer);
    64. bits.put(killer, on);
    65. killer.sendMessage(ChatColor.GREEN + "You have recieved " + ChatColor.GOLD + "5" + ChatColor.GREEN + " bits for killing " + ChatColor.GREEN + killed.getName());
    66. }
    67. if(!(bits.containsKey(killer))){
    68. bits.put(killer, 5);
    69. killer.sendMessage(ChatColor.GREEN + "You have recieved " + ChatColor.GOLD + "5" + ChatColor.GREEN + " bits for killing " + ChatColor.GREEN + killed.getName());
    70. }
    71. }
    72. }
    73. }
    74.  


    And the plugin.yml:
    Code:
    name: Bits
    version: 1.0
    description: Bits as currency!
    main: me.JBoss925.bits.Main
    commands:
      code:
        description: Code an item!
        usage: /code
    I don't know why it won't execute. I've defined it in the plugin.yml and everything.

    bumpin it

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

    adam753

    What exactly happens when you use the command? Does it give the "unknown command" message and nothing else?
    Nice plugin idea by the way.
     
  3. Offline

    JBoss925

    Well, it doesn't give an error message and it recognizes it as a command, it just doesn't do anything. And thanks:)
     
  4. Offline

    adam753

    JBoss925
    Well, I can't see any reason this code wouldn't work. You should be using the same instance of Money on lines 12 and 13 of Main, but that wouldn't stop the command from working. Have you tried putting in debug messages, at the very top of onCommand for example?
     
  5. Offline

    JBoss925

    adam753
    Deug messages? I've never used those? Is this a bukkit thing or did I just not learn this when I learned java?
     
  6. Offline

    adam753

    JBoss925
    Simple output messages. If you put a line at the top of the onCommand function that prints the word "test", then you know whether or not that point of the code is being reached. You can narrow down the source of the problem by doing this.
     
  7. Offline

    JBoss925

    adam753 oohhh yeah I've done that before. Well I actually gtg for tonight so I'll do that tomorrow.
     
  8. Offline

    JBoss925

Thread Status:
Not open for further replies.

Share This Page