Run EventHandler for 30 Seconds

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

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


    Just for practice, I'm working on a Ninja plugin which will add speed and jump potion effects for 30 seconds when a user types /ninja.

    The only trouble is, with a Jump Boost of 10, I lose almost all of my hearts whenever I jump, so I created an EventHandler to prevent all damage, but I have two problems.
    1. How can I make the event handler target only the player who typed the command?
    2. How can I make the event handler run for only 30 seconds?
    My event handler:
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageEvent event) {
    3. if(godModed.containsKey(event.getEntity())) {
    4. event.setCancelled(true);
    5. }
    6. }

    Thanks in advance! :3
  2. You can unregister individual events after a certain amount of time but it would be pointless because you can't make events individual for players so when you remove the event it will affect all players.
    EntityDamageEvent.getHandlerList().unregister() I belive it is, the same for any other event.

    However, I suggest you don't use that, I just told you for future reference, instead just leave the event running at all times and do that check... but I suggest you check if it contains entity's name instead of entity and you should store String as map key instead of Entity or Player or whatever you're using because those tend to give you troubles if stored.

    If you don't have values to that map you can just use a HashSet, it's like a HashMap but only with keys for quick-check.
  3. Offline


    Thanks for the reply! I decided to go with your idea. If it makes a check, how do I make it only last for 30 seconds?
  4. in a plugin i use this

    2. public static void saveLogoutPlayer(final Player player) {
    3. if(WorldUtils.getZworld() == player.getWorld()){
    4. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, new Runnable(){
    5. int logouttime = 30
    6. public void run() {
    7. if(logouttime != -1){
    8. if(logouttime != 0){
    9. player.sendMessage(ChatColor.RED + "" + logouttime + " seconds until save logout");
    11. logouttime--;
    12. }else{
    13. player.kickPlayer("Logged out safe");
    14. }
    15. }
    16. }
    17. }, 0L, 20L);
    18. }
    19. }

    i think u can change the code a bit to fit ur needs

    oops didnt saw digi's idea, now im going to eat lasagna...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 31, 2016
    McStormify likes this.
  5. Hmm, actually the Map might be useful, use Map<String, Integer> and store player's name and System.currentTimeMilis() divided by 1000 to store seconds.
    Then in the event just get the value and if it's null then it doesn't exist and move on, otherwise compare it against the current System.currentTimeMilis() divided by 1000 and see if the diference is not bigger than 30 seconds, if it is then you should remove the entry because it's a waste of space, otherwise block damage.
    McStormify likes this.
Thread Status:
Not open for further replies.

Share This Page