Checking if time has passed

Discussion in 'Plugin Development' started by MrLizardDogMan, Mar 4, 2014.

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

    MrLizardDogMan

    I'm trying to put into my plugin to test of so many seconds have passed. I've found this,
    Code:java
    1. System.currentTimeMillis();

    but am not sure entirely how to use it. If i could get an example of code in how to check if time is passed, that would be great.

    Source:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractBlock(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. System.currentTimeMillis();
    5. if (player.getItemInHand().getType() == Material.FISHING_ROD) {
    6. // Creates a bolt of lightning at a given location. In this case, that location is where the player is looking.
    7. // Can only create lightning up to 200 blocks away.
    8. player.getWorld().strikeLightning(player.getTargetBlock(null, 10).getLocation());
    9. }
    10. }
     
  2. Offline

    Appljuze

    You should use player.getWorld().getFullTime(), which will get the absolute time value. It's essentially the same thing as System.currentTimeMillis() but it's in ticks instead of milliseconds.
    You could do something along the lines of this:
    Code:java
    1. long startTime = player.getWorld().getFullTime();
    2.  
    3. // whatever happens in between
    4.  
    5. long endTime = player.getWorld().getFullTime();
    6. long timePassed = endTime - startTime;
    7.  
    8. // This will tell you how much time passed in ticks
    9.  
    10. long timePassedInSeconds = timePassed / 20;
    11.  
    12. // That'll convert it to seconds
     
  3. Offline

    Alshain01

    You need to describe a little more what it is your trying to do. Appljuze gave a method of checking total time, but if you want something to happen if an amount of time has passed, you need a scheduler.
     
  4. Offline

    MrLizardDogMan

    I want there to be a time limit so that player can only strike lightning every 10 seconds
     
  5. Offline

    Wolfey

    Make a HashMap<String, Long> cooldown = new HashMap<String, Long>(), then, when a player strikes lightning, add the player to the cooldown hashmap. (the string being the player's name, and the long being the System.currentTimeMillis()). Then, when a player tries to strike lightning again, check the value of the player's name, and if 10,000 milliseconds have passed, then let the player strike lightning, but then you have to update the Long value in the hashmap.
     
Thread Status:
Not open for further replies.

Share This Page