EventHandler on Command

Discussion in 'Plugin Development' started by McStormify, Apr 1, 2013.

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

    McStormify

    Very simple question I'm sure:

    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageEvent event) {
    3. event.setCancelled(true);
    4. }


    How can I only enable this when a user types a command?
     
  2. Offline

    1SmallVille1

    On an unrelated note, how do you get your code that color? I understand you made it java but how? Now to your question:
    in your command, you can add a player to a hashmap. Then you can do a check that if the map contains the player, do whatever
     
  3. Offline

    ZeusAllMighty11

  4. Offline

    McStormify

    Instead of
    Code:
    [code]
    Use:
    Code:
    [syntax=java]
    About the hashmap, could you provide an example? I'm fairly new to this, haha!

    Again, could you provide an example please? :)
     
  5. Offline

    1SmallVille1

    I will in a sec, just let me write it out
     
    McStormify likes this.
  6. Offline

    ZeusAllMighty11

  7. Offline

    1SmallVille1

    McStormify
    here is the command:
    Code:JAVA
    1.  
    2. public class Sets implements CommandExecutor {
    3.  
    4. public static Set<String> set = new HashSet<String>();
    5.  
    6. public boolean onCommand(CommandSender sender, Command cmd, String label,
    7. String[] args) {
    8.  
    9. if (cmd.getName().equalsIgnoreCase("addme")) {
    10.  
    11. if (sender instanceof Player) {
    12.  
    13. Player player = (Player) sender;
    14.  
    15. String name = player.getName();
    16.  
    17. set.add(name);
    18. }
    19. }
    20.  
    21. return false;
    22. }
    23.  
    24. }


    and then the listener:
    Code:JAVA
    1.  
    2. public class DamageListener implements Listener {
    3.  
    4. public Set<String> set = Sets.set;
    5.  
    6. @EventHandler
    7. public void onPlayerDamage(EntityDamageEvent evt) {
    8.  
    9. if (evt.getEntity() instanceof Player) {
    10.  
    11. Player player = (Player) evt.getEntity();
    12.  
    13. String name = player.getName();
    14.  
    15. if (set.contains(name)) {
    16.  
    17. evt.setCancelled(true);
    18. }
    19. }
    20. }
    21. }
     
    McStormify likes this.
Thread Status:
Not open for further replies.

Share This Page