[LIB] TimerLib - Easily create delays / cooldowns!

Discussion in 'Resources' started by Milkywayz, Aug 10, 2012.

?

Do you think this is a useful library?

  1. Yes

    70.8%
  2. Somewhat

    15.4%
  3. No

    6.2%
  4. Derp...

    33.8%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    Milkywayz

    Milkywayz's TimerLib - INACTIVE
    So I spent a few hours testing and perfecting a library which can be used to easily create a timer which runs after a command, event, etc and will toggle the state after the selected time. I plan on actively supported and adding new features to this library.​
    Features
    • Support for as many timers as you would ever need
    • Support for timers that only affect a certain player and general timers
    • Support for synchronous timers
    • Delays are measured in seconds using a double for ease and flexibility
    • Clean, Powerful, and Tested code with Javadocs
    • TimerLib is licensed under ISC, meaning you can use this library.
    Download
    No longer available
    Usage
    1. Download the library with the link up, unzip the download
    2. Drag the net folder to your workspace
    3. Refresh your IDE so it recognizes the new package
    4. Begin using the libraries methods
    Future
    I plan on posting some in-depth examples, however the current code is fairly straight forward.​
     
  2. Offline

    JOPHESTUS

    You should change the link to a bit.ly that way you get detailed info of where the downloads are coming from, how many etc.
    Also, first :)
     
    Milkywayz likes this.
  3. Offline

    Milkywayz

    Done, also what do you think of the library?
     
  4. Offline

    JOPHESTUS

    It's cool. But I am a nub and so..... What does it do?
     
  5. Offline

    Milkywayz

    If you want to make a cool down for placing blocks, you can make it so people have to wait a certain time then they can continue placing. Theres a method that returns how many seconds until the cool down is up so you can send a message to a player if you cancel the event or do anything really saying that they can do something once the cool down expires in so and so seconds.
     
  6. Offline

    JOPHESTUS

    Oh, ok. Cool. I may try to use it
     
  7. Offline

    Milkywayz

    If you have any trouble I will make the methods easier, the concept is rather difficult and the libraries purpose is to make it easier. I included a sample plugin .java file in the download as well for even more ease of use.
     
  8. Offline

    lx3krypticx

    Where do I find my net folder?
     
  9. Offline

    Milkywayz

    The net folder is in the library.zip, you need to drag it into your workspace so that its next to your other package that contains your plugin's class files. Keep in mind the entire library is almost finished being re-written (Its 1000x better and easier)
     
  10. Offline

    lx3krypticx

  11. Offline

    Milkywayz

    Yes sir, remember how you did that because I'm almost finished rewriting the library. Also I'm gonna make a youtube tutorial and add some documentation on it. This librarie's purpose is to make timers extremely easy in bukkit. Currently they can be considered difficult especially if you want to create separate timers without creating another class for them.
     
  12. Offline

    lx3krypticx

    Alright I've managed to put together something and I'm trying to add a cooldown. What I have is:

    Code:
        @EventHandler
        public void onPlayerWantingToJoin(PlayerInteractEvent event){
            Player p = event.getPlayer();
            this.player = p;
            if(event.getItem() == null){
                return;
            }
            else if((event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) && ((event.getItem().getType() == Material.EMERALD))){
                if (!Timer.getState(event.getPlayer().getName())) {
                    Timer.setState(event.getPlayer().getName(), true);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.juggernaut,new Timer(event.getPlayer().getName()),
                            1000000000);
                }
                else{
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("CoolDown Active D:");
                    return;
                }
                if(this.juggernaut.mainjugs.containsKey(this.player.getName())){
                    if(this.juggernaut.mainkilled == false){
                        p.getInventory().clear();
                        ItemStack item = new ItemStack(Material.BOW,1);
                        item.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.DIAMOND_SWORD,1);
                        item.addEnchantment(Enchantment.DAMAGE_ALL, 4);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.ARROW,1);
                        player.getInventory().addItem(item);
                        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,2));
                        player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                        player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                        player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                        player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
                        MinigameListener.this.player.teleport(MinigameListener.this.juggernaut.mainjugloc);
                    }
                    else{
                        p.getInventory().clear();
                        ItemStack item = new ItemStack(Material.BOW,1);
                        item.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.DIAMOND_SWORD,1);
                        item.addEnchantment(Enchantment.DAMAGE_ALL, 1);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.ARROW,1);
                        player.getInventory().addItem(item);
                        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,0));
                        player.getInventory().setHelmet(new ItemStack(Material.GOLD_HELMET));
                        player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                        player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                        player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
                        MinigameListener.this.player.teleport(MinigameListener.this.juggernaut.mainjugloc);
                    }
                }
                else if(this.juggernaut.lowjugs.containsKey(this.player.getName())){
                    p.getInventory().clear();
                    ItemStack item = new ItemStack(Material.BOW,1);
                    item.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                    player.getInventory().addItem(item);
                    item = new ItemStack(Material.DIAMOND_SWORD,1);
                    item.addEnchantment(Enchantment.DAMAGE_ALL, 1);
                    player.getInventory().addItem(item);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,2));
                    item = new ItemStack(Material.ARROW,1);
                    player.getInventory().addItem(item);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,0));
                    player.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                    player.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
                    player.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
                    player.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
                   
                    World spawnw = this.juggernaut.lowclassloc.getWorld();
                    double spawnx9 = this.juggernaut.lowclassloc.getX();
                    double spawny9 = this.juggernaut.lowclassloc.getY();
                    double spawnz9 = this.juggernaut.lowclassloc.getZ();
                    Location one = new Location(spawnw, spawnx9, spawny9, spawnz9);
                   
                    World spawnw1 = this.juggernaut.lowclassloc2.getWorld();
                    double spawnx2 = this.juggernaut.lowclassloc2.getX();
                    double spawny2 = this.juggernaut.lowclassloc2.getY();
                    double spawnz2 = this.juggernaut.lowclassloc2.getZ();
                    Location two = new Location(spawnw1, spawnx2, spawny2, spawnz2);
                   
                    final Location[] locArray;
                    locArray = new Location[2];
                    locArray[0] = one;
                    locArray[1] = two;
                    final int locValue = new Random().nextInt(locArray.length);
                   
                    MinigameListener.this.player.teleport(locArray[locValue]);
                }
            }
        }
    most of it is unuseful to the problem. There is no error but when I interact once i can do it again. I'm trying to set a cooldown.
     
  13. Offline

    Milkywayz

    Please update your library, there has been major changes which make everything work a lot better and easier. Sorry for the inconvenience. If you still have problems i'll be willing to troubleshoot.
     
  14. Offline

    lx3krypticx

    Yeah nothing is happening still. Here is my updated code.

    Code:
        @EventHandler
        public void onPlayerWantingToJoin(PlayerInteractEvent event){
            Player p = event.getPlayer();
            this.player = p;
            if(event.getItem() == null){
                return;
            }
            else if((event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) && ((event.getItem().getType() == Material.EMERALD))){
                if (Timer.isCoolingDown(event.getPlayer().getName(), Group.EXAMPLEONE)) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage(ChatColor.RED + "Cooldown Active!");
                    return;
                }
                else {
                    Scheduler.scheduleCooldown(this.juggernaut, event.getPlayer().getName(),
                            Group.EXAMPLEONE);
                }
                if(this.juggernaut.mainjugs.containsKey(this.player.getName())){
                    if(this.juggernaut.mainkilled == false){
                        p.getInventory().clear();
                        ItemStack item = new ItemStack(Material.BOW,1);
                        item.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.DIAMOND_SWORD,1);
                        item.addEnchantment(Enchantment.DAMAGE_ALL, 4);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.ARROW,1);
                        player.getInventory().addItem(item);
                        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,2));
                        player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET));
                        player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                        player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                        player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
                        MinigameListener.this.player.teleport(MinigameListener.this.juggernaut.mainjugloc);
                    }
                    else{
                        p.getInventory().clear();
                        ItemStack item = new ItemStack(Material.BOW,1);
                        item.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.DIAMOND_SWORD,1);
                        item.addEnchantment(Enchantment.DAMAGE_ALL, 1);
                        player.getInventory().addItem(item);
                        item = new ItemStack(Material.ARROW,1);
                        player.getInventory().addItem(item);
                        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,0));
                        player.getInventory().setHelmet(new ItemStack(Material.GOLD_HELMET));
                        player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                        player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                        player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
                        MinigameListener.this.player.teleport(MinigameListener.this.juggernaut.mainjugloc);
                    }
                }
                else if(this.juggernaut.lowjugs.containsKey(this.player.getName())){
                    p.getInventory().clear();
                    ItemStack item = new ItemStack(Material.BOW,1);
                    item.addEnchantment(Enchantment.ARROW_INFINITE, 1);
                    player.getInventory().addItem(item);
                    item = new ItemStack(Material.DIAMOND_SWORD,1);
                    item.addEnchantment(Enchantment.DAMAGE_ALL, 1);
                    player.getInventory().addItem(item);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,2));
                    item = new ItemStack(Material.ARROW,1);
                    player.getInventory().addItem(item);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE,10000,0));
                    player.getInventory().setHelmet(new ItemStack(Material.LEATHER_HELMET));
                    player.getInventory().setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
                    player.getInventory().setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
                    player.getInventory().setBoots(new ItemStack(Material.LEATHER_BOOTS));
                   
                    World spawnw = this.juggernaut.lowclassloc.getWorld();
                    double spawnx9 = this.juggernaut.lowclassloc.getX();
                    double spawny9 = this.juggernaut.lowclassloc.getY();
                    double spawnz9 = this.juggernaut.lowclassloc.getZ();
                    Location one = new Location(spawnw, spawnx9, spawny9, spawnz9);
                   
                    World spawnw1 = this.juggernaut.lowclassloc2.getWorld();
                    double spawnx2 = this.juggernaut.lowclassloc2.getX();
                    double spawny2 = this.juggernaut.lowclassloc2.getY();
                    double spawnz2 = this.juggernaut.lowclassloc2.getZ();
                    Location two = new Location(spawnw1, spawnx2, spawny2, spawnz2);
                   
                    final Location[] locArray;
                    locArray = new Location[2];
                    locArray[0] = one;
                    locArray[1] = two;
                    final int locValue = new Random().nextInt(locArray.length);
                   
                    MinigameListener.this.player.teleport(locArray[locValue]);
                }
            }
        }
     
  15. Offline

    Milkywayz

    I strongly believe that:
    Code:
     this.juggernaut 
    is null or isnt a proper reference to the main class.
    Give the sample plugin a try, you will see that the library works.
    Also you need to open Group.java and change the delay time and enum names to what you like.
    Groups are what define the time, The players name is the identifier for methods / events. The scheduler can't schedule anything if the plugin isnt referenced corruptly. (Eclipse isnt always right... :p)
     
  16. Offline

    lx3krypticx

    this.juggernaut is leading straight to the main java file. It's what I use throughout the whole class so there is nothing wrong with that.
     
  17. Offline

    Milkywayz

    Is this source uploading on github? You should add some debugging methods to check and see if the libraries code even gets called... Like this:
    PHP:
    @EventHandler
        
    public void onPlayerWantingToJoin(PlayerInteractEvent event){
            
    Player p event.getPlayer();
            
    this.player p;
            if(
    event.getItem() == null){
                return;
            }
            
    System.out.println("0");
            if((
    event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) && ((event.getItem().getType() == Material.EMERALD))){
                
    System.out.println("1");
                if (
    Timer.isCoolingDown(event.getPlayer().getName(), Group.EXAMPLEONE)) {
                    
    System.out.println("2");           
                    
    event.setCancelled(true);
                    
    event.getPlayer().sendMessage(ChatColor.RED "Cooldown Active!");
                    return;
                }
                else {
                    
    System.out.println("3");
                    
    Scheduler.scheduleCooldown(this.juggernautevent.getPlayer().getName(),
                            
    Group.EXAMPLEONE);
                }
     
  18. Offline

    lx3krypticx

    Sorry I added the cooldown thing to the wrong part of my class >_> It seems to be working except I don't think the 'event' is canceling. It says Cooldown Active! but still continues on.
     
  19. Offline

    Milkywayz

    Cancelling an event is up to the developer since its just one line, there isnt a need for a method todo it. Plus cool downs won't always be just for events. What do you mean by still continues on? I see you have a "Return" where it should, it still continues? 0.o
     
  20. Offline

    lx3krypticx

    Yes it still continues on with the whole thing
     
  21. Offline

    Milkywayz

    Is your source on github? If not then post your updated code since that problem isnt this libraries problem, its more a coding error / java error, but ill do my best to help.
     
  22. Offline

    lx3krypticx

    Want me to put it on github?
     
  23. Offline

    Milkywayz

    That would make it easier for me to assist you since I could pull the whole project up in my IDE
     
  24. Offline

    lx3krypticx

    I have no clue how to setup this github stoof..
     
  25. Offline

    Milkywayz

    Don't worry about it then, I'm about to head off to bed anyways. Sorry
     
  26. Offline

    sLRastro

    WTFPL - Do What The Fuck You Want To Public License


    You should have chosen that license
     
  27. Offline

    Milkywayz

    Or just public domain?
     
  28. Offline

    sLRastro

    That one has a better name though. I like the WTFPL name more.
     
  29. Offline

    Milkywayz

    Updated thread with a video!
     
  30. Offline

    Milkywayz

    Updated #3:
    1. Fixed one timer reseting another timer when it ends
    2. Add Time.java where you put your cool downs times
    3. Benchmarked code and events using the cool down library use about 20ms per call (Not bad...)
     
Thread Status:
Not open for further replies.

Share This Page