Broadcast a message when it is time in the world

Discussion in 'Plugin Development' started by Skyost, Oct 20, 2013.

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

    Skyost

    Hi !
    This is my problem :
    I have an HashMap<int, String>. The int contains the time and the String contains the message. (Example : 0:'It is the sunrise !').
    This HashMap is configurable so the user can add or remove messages.
    When it is the time (in the HashMap key) I want to broadcast the message (in the value).

    I have wanted to schedule a task every ticks for this but it has performance issues... :/

    Thanks for help me :)
    (I am not english, so do not judge me on my language please !)
     
  2. Offline

    TomTheDeveloper

    We can't help if you say: "I have a problem." Give more information about the problem.
     
  3. Offline

    Skyost

    TomTheDeveloper
    I am french, sorry :/
     
  4. Offline

    TomTheDeveloper

    Skyost Your English is not that bad, but please give some more information about your problem.
     
  5. Offline

    Skyost

    TomTheDeveloper I have said my problem above,
    I have a HashMap<int, String> which is configurable.
    In the int, I have the time and in the String, I have the message.
    If the current time of the world is contained in the int key, I want to broadcast the message in the String value ;)

    But I do not know how to do this, I have tried to schedule a task every ticks for verifying if the current time of the world was contained in the HashMap but it is not good for performance :/
     
  6. Offline

    TomTheDeveloper

    Weird, normally that shoud work
     
  7. Offline

    RealDope

    It does work, he just doesn't want to run this task every few ticks because he thinks it's very expensive.

    Perhaps during onEnable, loop over your HashMap, and create a task for each entry in there that executes after the necessary amount of time.

    Something like:
    Code:JAVA
    1.  
    2. HashMap<int, String> messages = new HashMap<int, String>();
    3.  
    4. public void onEnable() {
    5. for(int i : messages.keySet()) {
    6. new BukkitRunnable() {
    7. public void run() {
    8. getServer().broadcastMessage(messages.get(i);
    9. }
    10. }.runTaskLater(this, i - world.getTime());
    11. }
    12. }
    13.  


    Pseudocode, don't try to use it verbatim.
     
  8. Offline

    SacredWaste

    It's probably your computer/server. For better performance, you can check it every 20 ticks (every 1 second) and then check if the time is equal to the int in the hashmap in a range.
     
  9. Offline

    Skyost

    TomTheDeveloper Yes, I think scheduling a task every ticks is too expensive. (It will run the task 20 times in only one second and my class will be like this).
    RealDope Thanks but if the user has 30 messages, it will schedule 30 tasks and it will cause a lot of lags, no ?
     
  10. Offline

    RealDope

    Skyost
    Not necessarily, don't hold me to it, I don't know much about this stuff, but these aren't tasks running frequently. They're just created, and then they run once after a delay. They shouldn't cause performance issues, but again, I don't know how Bukkit handles this stuff.
     
  11. Offline

    Skyost

  12. Offline

    TomTheDeveloper

    Skyost It shouldn't give lagg if it is just a small task. If you are using configs, get everything from the config and put it in a hasmap/arraylist on start up. Don't load every tick your config because that will cause lagg.
     
  13. Offline

    Skyost

Thread Status:
Not open for further replies.

Share This Page