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?
Create a scheduler that has a random time with it so that the server appears to execute your code randomly...
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); }
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
@TheMintyMate Or just have the Runnable schedule another Runnable with a random delay and don't make it repeating. It's quite simple.