Cooldowns Tutorial (Super Easy | Newbie Friendly)

Discussion in 'Resources' started by peabody505, Aug 7, 2014.

?

What do use for development?

  1. Eclipse

    9 vote(s)
    60.0%
  2. Sublime Text

    0 vote(s)
    0.0%
  3. Other

    6 vote(s)
    40.0%
Thread Status:
Not open for further replies.
  1. Offline

    peabody505

    I was looking for a way to do a timed cooldown, but I couldn't find anything that I liked. Looking back at some of my old code, I realized that I had a VERY easy way to do it. Here it is:

    First we define an ArrayList to hold players currently on cooldown:
    Code:java
    1. public ArrayList<String> cdlist = new ArrayList<String();


    Then we create an event listener (this can be anything you want, such as a command):
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. final Player player = event.getPlayer();
    4. if (player.getItemInHand().getType().equals(Material.DIAMOND_SWORD)){
    5. if (event.getAction().equals(Action.LEFT_CLICK_AIR)){
    6. }
    7. }
    8. }


    Now we see if the player is in the ArrayList, if not, we do an action, and add the player to the ArrayList:
    Code:java
    1. if (event.getAction().equals(Action.LEFT_CLICK_AIR)){
    2. if (!(cdlist.contains(player.getName()))){
    3. player.sendMessage(ChatColor.AQUA + "You tried to hit air...");
    4. cdlist.add(player.getName());
    5. }


    Finally, we create a BukkitRunnable to remove the player from the ArrayList in a few seconds:
    Code:java
    1. new BukkitRunnable(){
    2. public void run(){
    3. cdlist.remove(player.getName());
    4. }
    5. }.runTaskLater(this, 20*5L);


    That's it! It may not be the most efficient method, but it's what I use, and I think it's pretty simple.
    Final Code: (open)

    Code:java
    1. public ArrayList<String> cdlist = new ArrayList<String>();
    2.  
    3. @EventHandler
    4. public void onPlayerInteract(PlayerInteractEvent event){
    5. final Player player = event.getPlayer();
    6. if (player.getItemInHand().getType().equals(Material.DIAMOND_SWORD)){
    7. if (event.getAction().equals(Action.LEFT_CLICK_AIR)){
    8. if (!(cdlist.contains(player.getName()))){
    9. player.sendMessage(ChatColor.AQUA + "You tried to hit air...");
    10. cdlist.add(player.getName());
    11.  
    12. new BukkitRunnable(){
    13. public void run(){
    14. cdlist.remove(player.getName());
    15. }
    16. }.runTaskLater(this, 20*5L);
    17. }
    18. }
    19. }
    20. }



    **EDIT** I know this is not the best or most efficient, but I found it the easiest to understand.
     
  2. Offline

    Iroh

    Moved to resources.
     
  3. Offline

    Cirno

    Going to point out; by convention, you should always restrict your variable scope to the lowest possible and make use of getter-setter methods.
    That and also you should always make use of the regular scheduler rather than using BukkitRunnable's.
    You should also make use of &&'s rather than a bunch of if statements.

    Other than that, I don't see any other problems.

    A very minor note (read: I'm being super paranoid and irrational) (open)
    The minor issue I'm having is the final variable. Unless you're interacting with the Player object directly in a manner in which it's required, I would prefer to store the String as a final variable. I haven't done any testing on this, but I always had this gut feeling that when you use final, the associated Object will never be garbage collected until that variable has no more use; in this case, when the scheduled task ends. Again, I have NO proof to back up my statement and therefore, should be taken with less than a grain of salt.
     
    LegitJava and Skyost like this.
  4. Offline

    JasonDL13

    This is the wrong way to do cooldowns. Turqmelon made a way that uses System.currentTimeMillis(). That way doesn't use schedulers (if tones of people are on the server it can get resource-tense) and it can tell you how much you have left without using advanced scheduling. This is a good place to start but if you wanna make a project use Turqmelons way.

    Thanks for taking the time for posting the tutorial though.
     
  5. Offline

    xTrollxDudex

    Noob friendly? Yes.

    Correct. No.

    RawCode will be very angry.
     
    ZodiacTheories likes this.
  6. Offline

    RawCode

    xTrollxDudex

    not going to waste my time here anymore, i seen over 20 tutorials about countdowns, they all noob frendly, invalid and feature copypaste from bzbrainels youtube channel.
     
  7. Offline

    xTrollxDudex

    inb4 VoilĂ 
     
  8. Offline

    Garris0n

    /suicide
     
    Cirno, LegitJava, MCForger and 7 others like this.
  9. Offline

    ZodiacTheories

  10. Offline

    LCastr0

  11. Offline

    Garris0n

    LCastr0 likes this.
  12. Offline

    Skyost

    Cirno You forgot :
    • Enums : == instead of .equals().
    • Why an ArrayList ? A simple Set or a List is enough.
    • This is not the most efficient method.
     
  13. Offline

    Garris0n

    I find equals() more consistent, so I use that. Both work, but I'd actually recommend using equals().
     
    Skyost and LCastr0 like this.
  14. Offline

    LCastr0

    Yup, since for strings, == someties doesn't work, I use .equals() for everything :p
     
  15. Offline

    Cirno

    With equals(), you could just override it to do ==. I just prefer equals() sometimes because you can do extra checks to make it more or less strict.

    You can't create a List because it's an interface. Unless you're talking about how the variable is declared; yeah. I forgot to state that he should always use the highest "parent class" for what you're working with. e.g List for ArrayList.

    Efficiency is debatable, but you're right in this instance; a HashSet would be better for contains() than an ArrayList.
     
    Skyost likes this.
  16. Offline

    Skyost

    Garris0n I do not think so, there are a lot of arguments here.
    Cirno Yeah sorry, I talked about that :
    Code:
    public ArrayList<String> cdlist = new ArrayList<String();
    And for the efficiency, I said this is not the better way to do a cooldown like that :)
     
  17. Offline

    peabody505

    Whats bzbraniels? Never heard of it.
     
  18. Offline

    Garris0n

    I still find it more consistent. Enums are more like objects than primitives, so I use .equals(). You can start to get visual inconsistencies. For example, you would have to use .equals() on the Color class, as it's not an enum. It does, however, have a set of constants that makes it look quite a bit like an enum in code. So then you would have two sets of code that look similar but use different equality checks and it would start to look painfully inconsistent.

    To each their own, I suppose.
     
    Skyost likes this.
Thread Status:
Not open for further replies.

Share This Page