Delay in X seconds to excute.

Discussion in 'Plugin Development' started by Seyzou, Jan 27, 2020.

Thread Status:
Not open for further replies.
  1. Hello, I create a plugin to make a scenario with my friends... But I’m blocking on a point. I can not make any delays to carry out the following action. Could you help me: here is my code:

    Code:
    package fr.seyzou.eventsseyzou.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class CommandEvent implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String msg, String[] args) {
           
            if(cmd.getName().equalsIgnoreCase("startev1")) {
                Bukkit.broadcastMessage("§eInformateur join the game.");
                Bukkit.broadcastMessage("<Informateur> Sa..Salut, je dois vous informer quelque chose de grave!");
                   
               
       
           
            }
           
            return false;
        }
    
    }
    Thanks for ur times
     
  2. Offline

    Strahan

    I'm not entirely clear on what you are doing, but you should read up on runnables / Bukkit scheduler. It's how you do time delayed operations without blocking the server.

    Also you should use the ChatColor enum instead of embedding the color code.
     
  3. I’m planning a scripting event on my private survival server with my friends. I have no worries with my server, I just never made java before... (I am more JS, HTML, CSS)
     
  4. Further explaining what @Strahan said, make a class that implements Runnable. This will have a run method, in which you can place the code you want to run later. Then schedule a task:
    Code:
    public class MyRunnable implements Runnable {
        public void run() {
            // Run code here
        }
    }
    Code:
    Bukkit.getScheduler().scheduleSyncDelayedTask(myPlugin, new MyRunnable(), 60);
    The "60" is the amount of delay in ticks, so 60 ticks = 3 seconds of delay.
     
  5. Offline

    cdnyassuo34

  6. Offline

    Xp10d3

    Alright, being someone who is learning Java, if you're going to put this delay in a method which is:
    Code:
    if(cmd.getName().equalsIgnoreCase("startev1")) {
    Bukkit.broadcastMessage("§eInformateur join the game.");
    Bukkit.broadcastMessage("<Informateur> Sa..Salut, je dois vous informer quelque chose de grave!");
    }
    
    that ^
    You can use a Bukkit runnable, which is:
    Code:
            new BukkitRunnable() {
             
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                 
                }
            };
    
    And then put it in your method.
    Code:
    if(cmd.getName().equalsIgnoreCase("startev1")) {
      new BukkitRunnable() {
        @Override
        public void run() {
          Bukkit.broadcastMessage("§eInformateur join the game.");
          Bukkit.broadcastMessage("<Informateur> Sa..Salut, je dois vous informer quelque chose de grave!");
        }
      };
    }
    
    Finally, use .runTaskLater() to run the code x ticks later. I think 20 ticks is one second, so if you want to run the broadcast message 5 seconds later, that will be 100 ticks. To do this, before the semi-colon do .runTaskLater().
    Code:
    if(cmd.getName().equalsIgnoreCase("startev1")) {
      new BukkitRunnable() {
        @Override
        public void run() {
          Bukkit.broadcastMessage("§eInformateur join the game.");
          Bukkit.broadcastMessage("<Informateur> Sa..Salut, je dois vous informer quelque chose de grave!");
        }
      }.runTaskLater(this, 100);
    }
    
    But the problem is, if you're putting this code in a different class (instead of your main class, Commands.java for example) instead of this, you're going to have to refer to your main class. You can do this by using your constructor.
    EDIT: Thanks Sploon.
    Code:
    JavaPlugin.getPlugin(MainClass.class)
    
    Add that to the top of your class if you're putting all this code in a different one then your main class ^
    Hope this helps!
     
    Last edited: Feb 3, 2020
  7. Offline

    Sploon

    @Xp10d3 If it works it works -- but I much prefer using:
    Code:
    JavaPlugin.getPlugin(MainClass.class)
    Instantiating a command class just to have access to the plugin object feels a little messy.
     
  8. Offline

    Xp10d3

    Eh; everyone has their own ways :) I prefer Bukkit runnables since they are quick and easy but forgive me if I'm wrong but isn't JavaPlugin deprecated...?
     
  9. Offline

    Sploon

    JavaPlugin is the class extended by your main class. That comment was not about the runnable, but about fetching an instance of your plugin. Instead of:
    Code:
    public Commands(Core core) {
    this.core = core;
    }
    private Core core;
    You could do:
    Code:
    JavaPlugin.getPlugin(MainClass.class)
    Instead. Both work, I was just making an off-topic suggestion.
     
  10. Offline

    Xp10d3

    Alright nvm. Thanks for the suggestion :p That definitely seems a lot easier to read. I edited my previous post :) Also yeah I was wrong JavaPlugin isn't deprecated xD
     
Thread Status:
Not open for further replies.

Share This Page