Make something that lasts an amount of time and cooldown?

Discussion in 'Plugin Development' started by mineman451, Jul 17, 2013.

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

    mineman451

    Hi. I just started making plugins a few days ago, and I don't know much.
    I'm making a kitpvp server, with a custom plugin for it. Here is an example:
    there is a kit called ghost that makes you invisible when you hit the ground with wool.
    Here's my question:
    In this scenario, how would I make it so that the invisibility lasts 30 seconds, then shows the player again, and then make sure that the player can't do it again for 30 seconds?

    (Sorry, I'm a noob :p)
     
  2. Offline

    Metal Julien

    1. Give the player a potion effect, invisible, for 30 seconds.
    2. Add player to a global list and check whenever the event happens if the player isn't in that list
    3. With a scheduler, after 60 seconds, remove him from the list.
    Scheduler:
    Bukkit.getScheduler().runTaskLater(Globals.plugin, new Runnable() {


    @Override

    publicvoid run() {
    //remove him from list

    }}, 60*20); //60 seconds * 20 ticks per second
     
  3. Offline

    mineman451

    Metal Julien When the player does /kit ghost, it adds their name to a global list of ghost players. When a ghost player hits a block with wool, it hides them from everyone. (using .hideplayer()) I made a scheduler-task thing, GhostUnhideTask:
    It undoes the .hidePlayer(), but I can't figure out one thing: with .showPlayer(), you need to specify what player to show. The scheduler is in a different class, so how would I transfer the player that is being hidden in the main plugin class to the GhostUnhideTask class?

    I can't make the Player player public, so I don't see how I would be able to access it from the GhostUnhideTask class...

    Wait.. would this work, and I wouldn't need the GhostUnhideTask?
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractBlock(PlayerInteractEvent event)
    3. {
    4. final Player player = event.getPlayer();
    5. if(ghostPlayers.contains(player.getName()) && player.getItemInHand().getType() == Material.WOOL && !ghostsOnCooldown.contains(player.getName()));
    6. {
    7. for(Player other : getServer().getOnlinePlayers())
    8. {
    9. other.hidePlayer(player);
    10. }
    11. Bukkit.getScheduler().runTaskLater(this, new Runnable()
    12. {
    13. @Override
    14. public void run()
    15. {
    16. for(Player other : getServer().getOnlinePlayers())
    17. {
    18. other.showPlayer(player);
    19. }
    20. }
    21. },60*20);
    22. player.sendMessage("Starting cooldown...");
    23. ghostsOnCooldown.add(player.getName());
    24. Bukkit.getScheduler().runTaskLater(this, new Runnable()
    25. {
    26. @Override
    27. public void run()
    28. {
    29. ghostsOnCooldown.remove(player.getName());
    30. }
    31. },60*20);
    32. }
    33. }


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

    Tirelessly

    EZ https://forums.bukkit.org/threads/easycooldown.128219/

    You don't have to use the class but use the idea. Using schedulers is a bad way to do this.
     
  5. Offline

    Metal Julien

    mineman451 You add the player after 60 seconds to the list, do it so:
    Code:
    @EventHandler
        public void onPlayerInteractBlock(PlayerInteractEvent event)
        {
            final Player player = event.getPlayer();
            if(ghostPlayers.contains(player.getName()) && player.getItemInHand().getType() == Material.WOOL && !ghostsOnCooldown.contains(player.getName()));
            {
                for(Player other : getServer().getOnlinePlayers())
                {
                    other.hidePlayer(player);
                }
     
                player.sendMessage("You're invisible. Starting cooldown...");
                ghostsOnCooldown.add(player.getName());
     
     
                Bukkit.getScheduler().runTaskLater(this, new Runnable()
                {
                    @Override
                    public void run()
                    {
                        ghostsOnCooldown.remove(player.getName());
     
                        for(Player other : getServer().getOnlinePlayers())
                        {
                            other.showPlayer(player);
                        }
                    }
                },60*20);
            }
        }
     
    
    I hope that should work
     
  6. Offline

    mineman451

    That would put the cooldown into no use, wouldn;t it? you have it so that it starts the cooldown once the player becomes invisible, and it removes him/her from the cooldown before it even un-hides them.
    PS
    Does },60*20);
    result in waiting 60 seconds? If it does, it would keep the player invisible for twice the amount of time it should.
     
  7. Offline

    Metal Julien

    mineman451 So do a bukkittask 30*20 inwhat you unhide the player, and in that bukkittask do another bukkittask 30*20 inwhere you remove him from list?
     
Thread Status:
Not open for further replies.

Share This Page