a method of cooldown?

Discussion in 'Plugin Development' started by trotski94, Dec 22, 2011.

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

    trotski94

    i cant find a way to do this anywhere, basically, i have a code that every time a player moves... it sends the new position co-ords to my web server, i want a way of stopping it spamming my web server, so that it only re-sends the co-ordinates if a second has passed or so... is there any way to do this?

    shameless self bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  2. Offline

    unicode21B9

    I'd recommend making a repetitive task that sends all player's coords if they have moved since the last call. This would have the advantage that it only makes one request to the server for all players, so you could even skip the test for whether a player has moved as most likely at least one player has moved since the last execution.
     
  3. Offline

    trotski94

    heh... i have no idea how to do that either, though that sounds a MUCH nicer way of doing it, GOOGLE SEARCH GOOOO!
     
  4. Offline

    unicode21B9

    place this in your plugin's onEnable():
    Code:
        Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
            @Override
            public void run() {
                yourMethodToSendData();
            }
        }, 0, 1000);
    
    replacing yourMethodToSendData with your method to send the locations to the server. You can also replace the '1000' with another time in ticks (1/20 second).
     
  5. Offline

    Father Of Time

  6. Offline

    halley

    Basically, keep a timestamp of the last time you PASSED your message to the service. If you're trying to send a new message, but the current time is too close to the timestamp, refuse to pass the message entirely. Slightly more complicated, you can schedule it to be sent at the appropriate time; if you get a new message and you've already scheduled one for sending, discard the old one in favor of the new one. Remember to update that timestamp only when you actually do send the message.
     
  7. Offline

    LartTyler

    If it were me, I'd do something like this. In your plugin's main class, have a private long variable called something like lastUpdate. Every time your plugin makes the update to your web server, you could do something like this:
    Code:
    if (System.currentTimeMillis() - lastUpdate > 5000) {
        lastUpdate = System.currentTimeMillis();
        // Send the new coordinates to your web server
    }
    Since this would measure the time in milliseconds, be sure to multiply the number of seconds between updates by 1000.
    To me, this seems like the easiest way of doing this. The above code could be inserted into you onPlayerMove method, or whatever you're using to track player movement.
    Hope this helps! :)
     
Thread Status:
Not open for further replies.

Share This Page