how do i add a timer

Discussion in 'Spigot Discussion' started by MeneerEzra, Feb 19, 2020.

  1. Code (Text):
    package me.main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class Main extends JavaPlugin {

        @Override
        public void onEnable() {
            // Plugin startup logic
            getConfig().options().copyDefaults();
            saveDefaultConfig();
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if(command.getName().equals("printMessageFromConfig")){
                Player player = (Player) sender;
                String food = getConfig().getString("Food");
                Bukkit.broadcastMessage(food);

            }


            return false;
        }
    }
     
  2. how do i make it an event that its looping Bukkit.broadcastMessage(food);
     
  3. You should use bukkit runnable, type it in google and look for some tutorials, there are many of them.
     
  4. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/scheduler/BukkitRunnable.html
     
  5. You can use BukkitRunnable for that
    Example:
    Code (Java):
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if(command.getName().equals("printMessageFromConfig")){
              Player player = (Player) sender;
              String food = getConfig().getString("Food");
               
              new BukkitRunnable(){
               @Override
                public void run(){
                  Bukkit.broadcastMessage(food);
                }
              }.runTaskTimer(plugin, 0, 20);

            }


            return false;
        }


            return false;
        }
    If you use that, message "Food" will be sending for every 1 seconds (20 ticks).