Random Event?

Discussion in 'Plugin Development' started by scarabcoder, Jan 3, 2015.

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

    scarabcoder

    Hello,

    I am creating a plugin that will randomly give out a player's location, but I have a problem.
    How do I make the server randomly do something? I don't want to use PlayerMoveEvent, and creating a timer from onEnable would be annoying.
    Is there any other way to do it, without any triggers?
     
  2. Offline

    TGRHavoc

    Create a scheduler that has a random time with it so that the server appears to execute your code randomly...
     
  3. Offline

    scarabcoder

    Where do I put the scheduler code, though? In the onEnable code?
    @TGRHavoc
     
  4. Offline

    TGRHavoc

    Yes.. Unless you want it to happen after certain events..
     
  5. Offline

    nj2miami

    Not sure what your end game is on this, but here is my opinion.

    I'll place it in the perspective of when a player logs in. (You could use this when a mini game starts or something else happens in game)

    This is quasi-code, its a rough draft for you and it is NOT tested but should give you an idea of how to proceed I think. I would probably create some sort of a Map to store the UUID and whether or not this should even fire so you can stop the loop, otherwise it will continue to fire forever the way it is written.

    Code:
    onPlayerLoginEvent() {
      sayRandomLocation(player.getUUID())
    }
    sayRandomLocation(final UUID pid) {
    final Long randomTime = getSomeRandomLong();
      // Sets the timer
      new BukkitRunnable()
      {
      @Override
      public void run()
      {
        Player p = Bukkit.getPlayer(pid);
        Bukkit.broadcast("Hey I am over here! " + p.getLocation();
        sayRandomLocation(player.getUUID());
      }
      }.runTaskTimer(this, 0L, randomTime);
    }
     
  6. Offline

    TheMintyMate

    Guys, you would need another scheduler to change the random time... Otherwise you would make a random time gap, and it would stay consistently to that said time gap until the plugin is reloaded ;)

    - Minty
     
  7. Offline

    mythbusterma

    @TheMintyMate

    Or just have the Runnable schedule another Runnable with a random delay and don't make it repeating. It's quite simple.
     
    TheMintyMate likes this.
Thread Status:
Not open for further replies.

Share This Page