Run event after command is executed

Discussion in 'Plugin Development' started by Demoz, Apr 2, 2014.

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

    Demoz

    Hey, I was wondering how I would run an event after a command is executed?

    I want it to be after the person runs the command, say "test", they cannot pick up items.
    I'm a little confused on this aspect of Bukkit. All help is appreciated. Thanks!

    The event:
    Code:
    public  void PickUpEvent(PlayerPickupItemEvent e) {
    if (cantPickup.contains(e.getPlayer().getName())) {
    e.setCancelled(true);
     }
     }
    
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if (cmd.getName().equalsIgnoreCase("test") {
    // Here I want it to start the PickUpEvent so the player cannot pick up items.
    }
    }
    
     
  2. Offline

    2MBKindiegames

    Hi there,
    If you create a ArrayList<String> containing the PlayerNames of the people who have performed the command, you can check if that list contains the PlayerName and (if so) cancel the event. It would be something like this:
    Code:java
    1. //Create the list
    2. ArrayList<String> noPickupPlayersList = new ArrayList<String>();
    3. public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
    4. //Check for command
    5. if (cmd.getName().equalsIgnoreCase("test")) {
    6. //Check if commandsender is a player
    7. if (sender instanceof Player) {
    8. //Converts it
    9. Player player = (Player) sender;
    10. //Adds the player to the list
    11. noPickupPlayersList.add(player.getName());
    12. return true;
    13. }
    14. }
    15. return false;
    16. }
    17.  
    18. @EventHandler
    19. public void onPlayerPickupItem(PlayerPickupItemEvent event) {
    20. //Get player
    21. Player player = event.getPlayer();
    22. //Check if the player is in the list
    23. if (noPickupPlayersList.contains(player.getName())) {
    24. //Cancel the event
    25. event.setCancelled(true);
    26. }
    27. }


    Using that technic, you can also easily 'reverse' the command by deleting the playername from the list.
     
  3. Offline

    BillyBobJoe168

    player.setCanPickupItems(false); isn't this much simpler?
     
    TheHandfish and 2MBKindiegames like this.
  4. Offline

    2MBKindiegames

    That's even better! But if you want some control over the type of object to dissallow, or an area of some sort, mine the thing to do. But if it's indeed just for all item, the right thing to do is yours! :D
     
  5. Offline

    Demoz

    BillyBobJoe168 Sorry for the late reply, I haven't been on my computer much. Anyways, thanks! That's exactly what I was looking for.
     
  6. Offline

    BillyBobJoe168

    np
     
Thread Status:
Not open for further replies.

Share This Page