Register event to thread or pass data to thread

Discussion in 'Plugin Development' started by woutwoot, Jun 18, 2013.

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

    woutwoot

    I want to register an event in a thread for my game (I use the thread because I can safely use sleep in there) But, I need some way of passing an event to a thread that is running, is this possible ? What I'm trying now is:
    Code:java
    1. public void startGame(){
    2. if(this.gamers.size() == this.towers.size()){
    3. Thread game = (new Thread(new ThreadGame(this, plugin)));
    4. plugin.getServer().getPluginManager().registerEvents(game, plugin);
    5. inprogress = true;
    6. }
    7. }

    But that is giving an error. Can someone help me?

    Anyone? I need this for my new game plugin. I'm guessing it's not a simple question, but please, reply :)

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

    MCForger

    woutwoot
    You could just make some function within the thread like thread.setCurrentEvent(EVENT);. Then have a bunch of statements saying okay are you this kind of event or this kind of event and then do what you need to.
     
  3. Offline

    Ivan

    Whats the error? Also instead of using a thread you might want to consider using a Bukkit Scheduler.
     
  4. Offline

    woutwoot

    The method registerEvents(Listener, Plugin) in the type PluginManager is not applicable for the arguments (Thread, Plugin) I will also check out bukkit schedulers, however, it's more easy to use sleep in a thread I think.

    I'm not sure I understand what you're saying :)

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

    MCForger

    woutwoot
    Sorry, let me clarify. So use the BukkitSchedular and make a runnable that goes how often you want it. Within that class the implements Runnable or whatever make a function called public void setCurrentEvent(Event event). Then have if statements in that void like if (event == PLAYERMOVEEVENT) then cast it or whatever. I will write an example as soon as I am done working.
     
  6. Offline

    Comphenix

    No, because you can't safely call the Bukkit API in a different thread. It is possible, but you must synchronize all those calls on the main thread, which is usually done with schedulers anyway.

    In addition, a single thread consumes quite a good deal of memory all on its own, along with processor usage on context switcing and so on. In fact, you would rarely have more than a couple of hundred threads allocated at any time, so if you're thinking of creating multiple threads per player for instance - no, just no.

    Scheduled task are almost free in comparision, with very little memory usage and scheduling overhead. You should only use threads if you're dealing with blocking I/O (network or disk) or you're performing a long computation. Otherwise, use scheduled tasks.
     
    woutwoot and MCForger like this.
  7. Offline

    woutwoot

    Thank you, that's a good explanation :) I guess I'll learn about scheduled tasks then.
    Ok, I'll wait for an example, maybe that might help me :)

    Comphenix
    One more question, I'm currently working on a game plugin similar to the Hunger Games plugin, but, I'd like to have events, respawns on death, things like "Teleporting in 3 ... 2 ..." and all that in different arenas. What is the best way to do that sort of thing? Because I can't imagine doing all that with a bukkit scheduler? How would you start? (Same plugin as I asked this question about, but I'm going to rewrite it)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
    Comphenix likes this.
  8. Offline

    Ivan

    You can use repeating tasks. You can choose to have one general repeating task that handles everything, or you can make a separate task for each arena. I think it's certainly possible, and I even recommend, to use a scheduler for this.
     
  9. Offline

    minoneer

    If you are working with multiple arenas, you should have different objects for them running more or less independently. And yes, all of this can be done with the bukkit scheduler; the code executed can be as complex as you want. I would suggest to start with an abstract outline of your game, writing the steps and functionalities it is supposed to have in some kind of diagramm, pseudocode or simular. Then think about your objects, classes and their interaction and once you got this, start implementing it in Java. That will make thinks much easier.
     
  10. Offline

    woutwoot

    Ok, thank you! I will try to get started :) Just asking, does anyone have a good example of this ?
     
Thread Status:
Not open for further replies.

Share This Page