runTaskLater problem

Discussion in 'Spigot Plugin Development' started by pepitoe, May 26, 2018.

  1. Hello !
    I'm having trouble with my code (here is the Listener class for event) :

    Code (Text):

    package fr.pepitoe.main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.block.Biome;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.Plugin;

    public class mainEvent implements Listener {

        public void onJoin(PlayerJoinEvent event) {
            Player p = event.getPlayer();
            Inventory inv = p.getInventory();
            String name = p.getName();
            event.setJoinMessage(ChatColor.DARK_AQUA + "[JOIN] " + ChatColor.DARK_RED + name + " joined the game");
            p.sendTitle(ChatColor.GOLD + "Bienvenue sur", ChatColor.RED + "CakeTraining");
            Biome b = p.getLocation().getBlock().getBiome();
            Bukkit.getScheduler().runTaskLater(this, new Runnable() {
                public void run() {
                    p.sendTitle(ChatColor.ITALIC + "------+------", ChatColor.BLUE + "[CURRENT LOCATION" + ChatColor.BOLD + "  LOCATION: " + p.getLocation() + "  BIOME: " + b.toString());
            }, 2*20L);
    it says that the method runTaskLater is not applicable for the argument (mainEvent, new Runnable()
    What should I do ?
  2. 1. write class names with capital letter. that's a fact not to be challenged.
    2. class mainEvent does not extend JavaPlugin and therefore can not be used as parameter in runTaskLater()
  3. ohh, thanks a lot !
  4. Do not let mainEvent extend JavaPlugin. There must be only one instance of class JavaPlugin per plugin
  5. You could always use a lambda if u are about to use 1 value in a delayed task.
    Code (Text):
    Bukkit.getScheduler().runTaskLater(plugin, () -> here a value(ex: player.set..) ), delay);
  6. yes you are right, i wrote "implements listener extends javaplugin" but there is an error in the cmd, what should I do (i'm new in java)
  7. 1. post the full expception message (or forget it)
    2. do NOT make MainEvent extend JavaPlugin! You have one instance of JavaPlugin. I dont know how you called that class that extends JavaPlugin but you can only, ONLY insert that one and only existing instance of it into your BukkitScheduler.runTaskLater()
    #7 MightyOne, May 26, 2018
    Last edited: May 26, 2018
  8. My class that extends javaplugin is "ClassePrincipale" and my class for event is "mainEvents"
  9. You can use JavaPlugin.getProvidingPlugin(ClassePrincipale.class).

    So for example:
    Code (Text):
    Bukkit.getScheduler().runTaskLater(JavaPlugin.getProvidingPlugin(ClassePrincipale.class), runnable, time);[code]
  10. Pass an instance of your main class to your Listener class, then use that instance in the BukkitRunnable.
    This would go in your class implementing Listener (pseudo-code, if you copy it exactly, you'll have issues):
    Code (Text):
    private Main plugin;
    public MyClass(Main plugin) {
    this.plugin = plugin;
    This would go in your main class:
    Code (Text):
    public final MyClass myclass = MyClass(this);
    Otherwise you'll receive a NullPointerException. This is where the plugin variable is set to the plugin instance.

    Replace MyClass and Main with the necessary class names, of course.
    The RunTaskLater method takes an instance of the plugin, hence why you were getting the error you were. Then, you should be able to use it like so:
    Code (Text):
    new BukkitRunnable() {
    public void run() {
    }.runTaskLater(plugin, delayInTicks);
    • Agree Agree x 1