Help with code?

Discussion in 'Plugin Development' started by Eliteninja42, Feb 15, 2014.

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

    Eliteninja42

    Code:
           public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
           {
             Player player = (Player)sender;
     
             if (commandLabel.equalsIgnoreCase("classes")){
                 player.sendMessage(ChatColor.DARK_GREEN + "---Classes---");
                 player.sendMessage(ChatColor.BLUE + "1) Miner");
                 player.sendMessage(ChatColor.BLUE + "3) Cultivator");
                 player.sendMessage(ChatColor.BLUE + "4) LumberJack");
                 player.sendMessage(ChatColor.BLUE + "8) Diver");
                 player.sendMessage(ChatColor.BLUE + "9) Fireman");
                 player.sendMessage(ChatColor.BLUE + "More to come soon!");
             }  
             if (cmd.getName().equalsIgnoreCase("class miner")){
           if (Miner.contains(sender.getName())){
           sender.sendMessage(ChatColor.BLUE + "The class 'miner' has been deactivated!");
           return true;
           }else 
           sender.sendMessage(ChatColor.BLUE + "The class miner has been activated!");
           Miner.add(sender.getName());
            
           return true;
             
             }
            
             return false;
           }
           @EventHandler
             public void onPlayerInteract(PlayerInteractEvent e) {
         Player player = (Player)e;
                 if(e.getPlayer().getItemInHand().equals(Material.APPLE)) {
               if(e.getAction() == Action.RIGHT_CLICK_AIR) {
               player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 400, 2));
               player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);
                 }else if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
               player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 400, 2));
               player.getInventory().getItemInHand().setAmount(player.getInventory().getItemInHand().getAmount()-1);
                 }
               }else 
               player.sendMessage(ChatColor.BLUE + "You must be holding an apple!");
           }
           }
    
    How do I make this so when I type /class miner it does what is in the eventhandler and the sendmessages in the boolean?
     
  2. Offline

    minecraft124_

    Check if the command name is "class", then check the arguments length, then see if the first arg is "miner".
     
  3. Offline

    ean521

    I'm new to making plugins, so I may be wrong, but I think you should create a boolean variable(s) or hashtable and test for a certain value in that hashtable or that certain variable to be true near the top of your event handler. I may be wrong, though, as I am new to plugin making.

    EDIT: You also need a way for the plugin to know wether it's true for a specific player.
     
  4. Offline

    Eliteninja42

    minecraft124_ It does that in the boolean but how do I do that for the EventHandler, it's outside of the boolean
     
  5. Offline

    minecraft124_

    You could use a hashmap to store the player's name and whether or not they have run the command, then check that when the player tries clicking with the apple.
     
  6. Offline

    Eliteninja42

    minecraft124_ I have a string list that does that, my problem is is the eventhandler can't go in the boolean but i need it to be or i need to reference to it or something

    Anyone?

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

    minecraft124_

    What do you mean exactly?
     
  8. Offline

    Eliteninja42

    The eventhandler can't be in my boolean or else it breaks, i need to know how to make it run on a certain command.
     
  9. Offline

    minecraft124_

    You can't run a command from a listener, and vice versa. What exactly are you trying to do?
     
  10. Offline

    Wizehh

  11. Offline

    Eliteninja42

    How do I get the code in the eventhandler in the Boolean then without it breaking?
     
  12. Offline

    Wizehh

    It took me forever to write that, so please read it.
     
  13. Offline

    Eliteninja42

    Wizehh I did I sent that previous post before the page refreshed. Thank you
     
    Wizehh likes this.
  14. Offline

    d3v1n302418

    Wizehh I always get confused on this, but, I always do:
    Code:java
    1. if(player.getItemInHand().getType() != Material.APPLE)return;

    You did:
    Code:java
    1. if(player.getItemInHand().getType() == Material.APPLE)

    If you right click with null or air it returns an NPE for me. Doesn't this happen to you as well?
     
  15. Offline

    Wizehh

    Code:java
    1. if (player.getItemInHand() != null) {
    2. // no npe, not null
    3. }

    d3v1n302418

    Eliteninja42
    Code:java
    1. //check
    2. if (event.getPlayer().getItemInHand() != null {
    3. if (event.getPlayer().getItemInHand().getType() == Material.APPLE) {
    4. // apple!
    5. }
    6. }

    I forgot to mention that.

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

    d3v1n302418

    Wizehh Ah thanks. That makes life so much easier. :)
     
  17. Offline

    Wizehh

Thread Status:
Not open for further replies.

Share This Page