Solved Send messages after delayed time

Discussion in 'Plugin Development' started by PieMan456, Nov 27, 2013.

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

    PieMan456

    Hello Everyone,

    I am making a plugin called tutorializer. Basically what it does is that when you join for the first time it sends you messages. I know how to send them, but I want to send several but with a delay between them. If you know how to do this please help. Thanks!
    My Code:
    Code:java
    1. package me.pieman.tutorializer;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerChatEvent;
    11. import org.bukkit.event.player.PlayerJoinEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Events extends JavaPlugin implements Listener {
    15.  
    16. private Main plugin;
    17.  
    18. public Events(Main plugin){
    19. this.plugin = plugin;
    20. }
    21.  
    22. public int delay = getConfig().getInt("msg.delay");
    23.  
    24. public ArrayList<String> map = new ArrayList<String>();
    25.  
    26. @EventHandler
    27. public void onPlayerJoin(PlayerJoinEvent e){
    28. if(e.getPlayer().hasPlayedBefore()) return;
    29. final Player p = e.getPlayer();
    30. map.add(p.getName());
    31. p.sendMessage(ChatColor.GREEN + getConfig().getString("msg.start"));
    32. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    33. public void run(){
    34. p.sendMessage();
    35. if(getConfig().getStringList("msg").isEmpty()){
    36. plugin.getServer().getScheduler().cancelTasks(plugin);
    37. p.sendMessage(ChatColor.GREEN + getConfig().getString("msg.end"));
    38. }
    39. }
    40. }, 0, this.delay * 20);
    41. }
    42.  
    43. @SuppressWarnings("deprecation")
    44. @EventHandler
    45. public void onPlayerChat(PlayerChatEvent e){
    46. Player p = e.getPlayer();
    47. if(map.contains(p.getName())){
    48. e.getRecipients().remove(p);
    49. }
    50. }
    51. }
    52.  


    Config File:
    Code:
    ### Configuration File for Tutorializer
     
    msg:
    ### Set the message when a player starts the tutorial here.
      start: You are now going to take the server tutorial!
    ### Set the message when a player ends the tutorial here.
      end: Have fun on the server!
    ### Set the delay between each message in seconds here.
      delay: 5
    ### Set the messages you want to say here
      - Hello
      - Hi
     
  2. Offline

    Adriani6

    Example
    This is an example of a plugin which registers a listener and when a player joins, schedules a task to be run 20 ticks later.
    http://wiki.bukkit.org/Scheduler_Programming
    Code:java
    1.  
    2. import org.bukkit.event.EventHandler;
    3. import org.bukkit.event.Listener;
    4. import org.bukkit.event.player.PlayerJoinEvent;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6. import org.bukkit.scheduler.BukkitRunnable;
    7. import org.bukkit.scheduler.BukkitTask;
    8.  
    9. public final class ExamplePlugin extends JavaPlugin {
    10.  
    11. @Override
    12. public void onEnable() {
    13. new ExampleListener(this);
    14. }
    15. }
    16.  
    17. class ExampleListener implements Listener {
    18.  
    19. private final ExamplePlugin plugin;
    20.  
    21. public ExampleListener(ExamplePlugin plugin) {
    22. this.plugin = plugin;
    23. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    24. }
    25.  
    26. @EventHandler
    27. public void onJoin(PlayerJoinEvent event) {
    28. BukkitTask task = new ExampleTask(this.plugin).runTaskLater(this.plugin, 20);
    29. }
    30.  
    31. }


    If that's what you're looking for.
     
  3. Offline

    PieMan456

    Adriani6
    Well that is kind of what I want. I want it to send a series of messages with a delay between each of them. And you can modify the messages in the config.

    Anyone know how to send a list of messages each having a delay between them?

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

    HyrulesLegend

    Use a Bukkit Scheduler
     
  5. Offline

    ice374

    PieMan456
    How did you solve this? I'm having a similar issue :)
     
Thread Status:
Not open for further replies.

Share This Page