Command Timer help

Discussion in 'Plugin Development' started by wesley27, Nov 16, 2014.

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

    wesley27

    Hello, I have a shop plugin that when people sell items, it adds the price and item info to a MySQL database, and then broadcasts the information to the whole server. So, currently every time someone does /sell <amount> <item> <price> it broadcasts "player is selling xamount item for price." As you can imagine, this gets a little spammy at times.

    What I'd like to do is, after the person executes the command, before it broadcasts it, create at sort of timer. So that when the player executes the command, wait 5 seconds before broadcasting it. If the player executes the command again within those 5 seconds, restart the timer. This way, it shows their total sale and doesn't spam. I'm having trouble getting a way to do this properly, could anyone share any ideas?

    Thanks!
     
  2. Offline

    JordyPwner

    Use a Bukkit Runnable AKA a scheduler
     
  3. Offline

    wesley27

    JordyPwner Isn't that just like a normal java timer though? How do I specifically make it start and while its running check if the command is executed again, and if it is, restart it?
     
  4. Offline

    JordyPwner

    USe a arraylist to check
     
  5. Offline

    WesJD

    When they click the sign:
    Check if they're in the arraylist, if so, don't do anything.
    If they're not, make a runnable, add them to the arraylist, and remove them when the timer announces.
     
  6. Offline

    Gnat008

    wesley27
    Used with permission from ColonelHedgehog:

    Should I use Runnable cooldowns?

    Nope. It's redundant. There's no point and it can cause problems. Use "lazy data". Instead of storing an ArrayList with the player's UUID, make a HashMap storing his UUID as well as the System.currentTimeInMilis() as the value. For a command cool down, for example, add the player's UUID and the current time to the HashMap. When he tries to use the command again, check and see the current time and compare it to the HashMap's time. If it has been long enough since he first used the command, remove him from the HashMap and allow him to use it. No need for a runnable at all! In fact, this gives you more flexibility. You can compare the times to send a message to the player telling him exactly how much time must elapse before he can use the command again. Otherwise, you could potentially cause lag. No fun at all.
     
  7. Offline

    wesley27

    Gnat008 Thanks, I will definitely use that advice in the future its very helpful! However, that isn't what I'm trying to do here. I'm not trying to make a command cooldown. I want the player to be able to "spam" the command, because this is how players efficiently sell things with the command. What I don't want it to do, is broadcast the sellers total amount of items for sale and the price EVERY TIME he runs the command. This is what spams the global chat. I want to let the player spam the command, but only broadcast the sale once it has been 5 seconds since the latest use of the command. This would prevent the spamming of all users' chat. That's what I'm having trouble doing.
     
  8. Offline

    Gnat008

    wesley27
    You can use the method above to do that too. Instead of blocking the action, only block the broadcast using the check.
     
  9. Offline

    wesley27

    Gnat008 Alright I tried doing this. After writing most of it, I realized that that still won't work. I want it to broadcast the message 5 seconds after the person entered the command. If they type the command again in those 5 seconds, it will wait another 5 seconds, and then broadcast. That's what I want to happen. If I do the map idea you gave me, it's only checking the time when the person enters a command. What happens if they only enter the command once? Or if they wait a very long time, are they ever removed from the map? That's why I think I need a timer and what I'm unsure of how. I can only do what you said if it's occurring depending on the command being run again. But it isn't. I need the broadcast to occur depending on if the command isn't run again.
     
  10. Offline

    xTrollxDudex

    Use a repeating task to increment 5 times, if within the iterations, a player is found to be in an arraylist which is populated with players that chat, reschedule the task and cancel the current one.
     
Thread Status:
Not open for further replies.

Share This Page