[Unsolved] How to send a message to a player at random intervals between 30s and 5m?

Discussion in 'Plugin Development' started by malon, Sep 13, 2014.

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

    malon

    I know I need a bukkit scheduler of some type, but wiki page on shows several ways to do this, I'm not clear on which one to use (I'm new to schedulers/runnable)

    Here's what I'm really trying to do:

    So I have a server with a jail (for hackers/cheaters). I allow players to rejoin the server as normal non-jailed players if they can escape jail. To escape jail, they must walk 3 hours through cobweb, soul sand, backwards flowing water, and ice.They cannot stop because the backwards flowing water pushes them backwards. Also if they relog, they are TPed back to the start of the walk.

    So what I want this:
    A hacker is always a hacker, right? So the idea is that if a hacker player is jailed and knows he has to do a grueling walk, he will think of a way around this, i.e. using a walking macro. So what I want is for jailed players to recieve a message randomly that forces the player to have to type something in chat (anything) within say, 10 seconds of receiving the message. If the player does not respond to that message in 10 seconds, they are automatically TPed back to the beginning of the walk, as it is presumed they are not at their keyboard and using a macro.
     
  2. Offline

    mythbusterma

    malon

    Why does it need to be a random interval? If you want to do the prompt at a random interval, you can just schedule the repeating task at a random time after the last one, use Random#nextInt() to find it.
     
  3. Offline

    teej107

  4. Offline

    malon

    Well, lets say the message was sent every 3 minutes. This would allow the hacker to go AFK for 3 minutes, return for the message, then leave again, defeating the purpose

    I'm not asking about the random function, I'm asking the best way to implement a bukkitscheduler, because I've never done it. Reading the wiki page for it, I'm not clear which method to use...anonymous? standard?

    And you're right, most people would leave, but sometimes my regular players get cocky, try out hacks, get caught but dont want to leave the server forever, so they endure the horseshit grueling walk haha

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

    teej107

    Here is how I would do it for complete randomness of the times.
    • In it's own method that takes a long in the parameters (this will be your delay time) , create a regular non-repeating task (async or not, doesn't matter)
    • Obtain a random number
    • Call method again with the random number in the parameters.
    You will also have to make a way so the method can stop itself. Recursion can be dangerous.
     
  6. Offline

    malon


    Code:
    package code.malon;
     
    import java.util.Random;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
     
    public final class RandomResponse extends JavaPlugin {
        Random rand = new Random();
        int min = 10;
        int max = 300;
     
        // I'd like to trigger onChangeGroup (permissionsex), but not sure how
        public void onEnable() {
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                        int randomNum = rand.nextInt((max - min) + 1) + min;
                        // some sort of code goes here
                }
            }, 0L, 20L);
        }
    }
    
    That's my code so far, but I'm not sure how best to start the scheduler. Basically, the scheduler should start when a user is changed to a jailed group, and if there are no users in the jailed group, then it should cancel.
     
  7. Offline

    4thegame3

    Hm the first way that comes to my mind is start a non repeating task
    and as first value put the delay
    Code:
    , 0L, 20L);
    //should be
    , delayinticks);
    Code:java
    1. scheduler.scheduleSyncDelayedTask(this, new Runnable() {
    2. @Override
    3. public void run() {
    4.  
    5. p.sendMessage("hello");
    6. }
    7. }, (random int here))


    EDIT: if you want repeat this then put all inside another task and check if the syncDelayedTask has ended, if yes create a new one

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

    malon

    Hey, thanks for your help so far. I must be missing something, because I thought the below code would produce the word "test" for someone in group "default" every second, but it does not.

    What am I doing wrong?

    Code:
    import net.milkbowl.vault.permission.Permission;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    public final class RandomResponse extends JavaPlugin {
    
        Permission permission;
        Player player;
    
        public void run() {
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Player player : Bukkit.getOnlinePlayers())
                        if (permission.getPrimaryGroup(player).equals("default"))
                            player.sendMessage("test");
                }
            }, 0L, 20L);
        }
    }
    
     
  9. Offline

    4thegame3

    equalsignorecase();
    i had this issue too
     
  10. Offline

    malon

    It didn't work for me (exact same code as above just with equalsIgnoreCase()). Console reports it is loading the plugin fine, no errors.

    EDIT: And I definintely have a group named default, because "/pex user <user> group set default" i use extremely frequently
     
  11. Offline

    4thegame3

    maybe this isnt your primary group, check in the permissions.yml if you have more than 1 group
    EDIT:
    • wich external libraries did you use for the class Permission
    • you didnt inizialize "Permission permission"
     
  12. Offline

    malon

    Well, I'm using "/pex user malon group set default" which according to pex removes the user from all other groups. I did verify that my user is only part of default group in permissions.yml.

    I ran "/pex user malon" and it said

    '87fb3c6c-5ff6-4bf6-b326-992c6137ea12/MALON' is a member of:
    default (rank 29 @ default)

    and that was it.
     
  13. Offline

    4thegame3

    malon you didnt answer to that
    • wich external libraries did you use for the class Permission
    • you didnt inizialize "Permission permission"
     
  14. Offline

    mythbusterma

    malon

    Your class shouldn't extend JavaPlugin, it's not a plugin. Extend BukkitRunnable and then schedule it in a different class.

    You should probably try learning basic Java before you try to come here.
     
  15. Offline

    malon

    My code is based on the last example from here (titled Repeated Example):
    http://wiki.bukkit.org/Scheduler_Programming
    and it definitely extends javaplugin in the example. So that example is wrong?


    I tried initializing Permission permission = null, still no luck. (I also tried initializing the player object).
    The external libraries in the buildpath are bukkit.jar (obv), vault1.4.1.jar, and permissionsex.jar.
     
  16. Offline

    mythbusterma

    malon

    No, you are using the example out of context, unless the code you posted above is your entire plugin.

    Again, learn Java before Bukkit and you'll see where your problems are.
     
  17. Offline

    malon

    Yes that code is my entire plugin, there is no other code besides that.
     
  18. Offline

    mythbusterma

    malon

    Oh, I thought you had actually done some work before coming to us.

    Both of those are null in that scope, and "run()" will never be called because there is no "entry point" to your plugin, the onEnable() method.
     
  19. Offline

    malon

    Dear godddddddddd i had two run() methods defined and no onEnable(). I'm retarded. Thank you!


    Console is throwing "[RandomResponse] Task #19 for RandomResponse v1.0 generated an exception" so that means I'm going to have to throw a try/catch in there I think, but I don't know what part is throwing the exception.

    Code:
    package code.malon;
    
    import net.milkbowl.vault.permission.Permission;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    public final class RandomResponse extends JavaPlugin {
        Permission permission = null;
        Player player = null;
    
        public void onEnable() {
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Player player : Bukkit.getOnlinePlayers())
                        if (permission.getPrimaryGroup(player).equalsIgnoreCase("default"))
                            player.sendMessage("test");
                }
            }, 0L, 20L);
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  20. Offline

    mythbusterma

    malon

    We already told you why, so fix your code.
     
  21. Offline

    malon

    Because it shouldn't be extending JavaPlugin?
     
  22. Offline

    mythbusterma

    malon

    No, if that's your entire plugin it should extend JavaPlugin. It's that "permission" is null.
     
Thread Status:
Not open for further replies.

Share This Page