Making your mod wait x (mili)seconds?

Discussion in 'Plugin Development' started by The_Guest, Jan 29, 2011.

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

    The_Guest

    Has anyone does this without ending up with bad NullPointerExceptions that crashes the server, lagging the server until the outcome is finished, or another exception:

    Basically what I'm doing is placing a trail of blocks and deleting them as they go.

    Error that makes the block stop moving. Doesn't crash the server though. (Done with Timer and Thread)

    Null Pointer that results in the server crashing. (Done with Timer and Thread.sleep())


    I've tried using the Thread Class and the Timer class but both of them end up eventually with one of these exceptions. So, any advice for doing this? The code I'm working with is 1200+ lines long so I'm not going to attempt to post any of it.

    I've gotten it to work with a max of 3 moving blocks before, but eventually one of them "gets stuck" or crashes the server.

    (And yes, I have tested it to the point where I know its not being caused by anything else. I can get the object to work fine when I don't have either of these timers going. IE making a big messy line of blocks.)
     
  2. Offline

    Snowl

    try {
    Thread.sleep (220); // miliseconds
    } catch (InterruptedException e) {}
     
  3. Offline

    8e8

    If you're using a timer you don't need thread.sleep()
    I used timers myself on 2 plugins and never encountered that error. If you want help, it really helps to see what you're doing besides the error.
     
  4. Offline

    The_Guest

    Yes, thank you both for reading my post minimally...

    I already tried that, and at difference sleep times. NullPointerExceptions when trying to do it with more than 3-5 objects.

    Yes I know that, I switched the code from using thread to using timer (which is a thread if its own).

    How often does your code actually use the timer? Mine goes nonstop with .2 second intervals until the block trail reaches its destination, which can end up badly.

    I did however find that timer acted a little better than using thread, but it still eventually crashed.



    Luckily there is a new thread that is discussing this issue and the problem is the mod gets out of sync with the game to a degree.

    http://forums.bukkit.org/threads/whats-the-best-way-to-repeat-tasks-at-set-intervals.2494/
     
  5. Offline

    void420

    There is currently no safe way to call bukkit API from a thread, your only option is to poll your thread for stuff to do from some event that gets called frequently, like onPlayerMove.
     
  6. Offline

    eisental

    Welcome to the club...
    This will happen when your timer / thread makes changes to the world often enough.
    It only happens when you try to change something in the server from another thread.
     
  7. Offline

    8e8

    I ran into a problem with my Ignite plugin which was somewhat somewhat because of the same thing. The plugin runs a timer EVERY second (during daylight hours) which will collect all the entity data in the world and set fire to any of the entities on a blacklist. I kept running into a ConcurrentModificationError because of it. The reason was that the timer would refire before it finished it's action. I ended up catching that error in the end and the plugin doesn't spaz out anymore.

    Make sure you try and catch every possible error, including modifying a block that may be null, so it doesn't get screwy. Again, I can't see your code, so I don't know what to tell you. Just make sure you're doing checks on everything you modify if you're using timers.
     
  8. Offline

    darknesschaos

    use a comparison with System.currentTimeMilis() and use it on something like a player move event so that it will almost always trigger. (or it will trigger when it is relevant). But you do not want to make the server wait or stop for something as it could cause more problems down the road. Check my voidmage code the arrowstorm spell does something to this effect.
     
  9. Offline

    DerpinLlama

    Wait what? Really? I thought my plugin handled that well. It just passed the plugin's instance down to all threads it created.
     
  10. Offline

    Myers Carpenter

  11. Offline

    void420

    The only "safe" way to interact with the Bukkit API is to implement your own communication channel and synchronization. You can do this by hooking onto an event that happens a lot, polling your code to see if something needs to be done and locking objects you are modifying or use thread safe data structure to pass data around.

    Calling your plugin object from another thread to do stuff like changing world block properties, sending player messages, or anything that isn't reading simple values will cause random exceptions and inconsistencies. Iterating through lists or hashmaps will throw random ConcurrentModificationException if the main thread touches it at the same time you do for example.

    This feature request is what's needed: http://bugs.bukkit.org/issues/154. A thread safe method to call a custom event on the main thread, allowing you to execute whatever code you need on a thread timer without having to jump through crazy hoops.

    If all you do is pass data around within your plugin without touching the main minecraft server stuff then it's much less of a concern but you still need to think about what you're doing. Main thread + custom threads trying to modify the same objects at the same time will cause you tons of fun when debugging.
     
Thread Status:
Not open for further replies.

Share This Page