Creating a few runnables in one method.

Discussion in 'Plugin Development' started by TheManiacGamers, Feb 21, 2016.

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

    TheManiacGamers

    Okay, that title probably wasn't descriptive enough.

    What I'm trying to do, is make it so when the player clicks an enderpearl in a seperate inventory (cmRecipesMighty) it loads or "sets" the item slots in that inventory to the item, using this

    Code:
                            cmRecipesMighty.setItem(22, new ItemStack(Material.BLAZE_POWDER, 1));
    but what I'm trying to do, is make it so when the player clicks that enderpearl, it loads the Blaze Powder, then 1 second later it loads the Iron Ingots, then 1 second after that it loads the Gold Ingots.

    Putting 3 runnables inside of the method didn't work
    I then tried putting 3 "if" checks, that didn't work.

    Code:
                }
                if (clicked.getType() == Material.ENDER_PEARL) {
                    event.setCancelled(true);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            player.sendMessage(ChatColor.AQUA + "Loading the recipe for the '" + ChatColor.BLUE + "Mighty" + ChatColor.AQUA + "' item!");
                            cmRecipesMighty.setItem(22, new ItemStack(Material.BLAZE_POWDER, 1));
                        }
                    }, 20L);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            cmRecipesMighty.setItem(13, new ItemStack(Material.IRON_INGOT, 1));
                            cmRecipesMighty.setItem(31, new ItemStack(Material.IRON_INGOT, 1));
                        }
                    }, 2 * 20L);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            cmRecipesMighty.setItem(21, new ItemStack(Material.GOLD_INGOT, 1));
                            cmRecipesMighty.setItem(23, new ItemStack(Material.GOLD_INGOT, 1));
                            player.sendMessage(ChatColor.AQUA + "Recipe has loaded!");
                        }
                    }, 3 * 20L);
                    return;
    If you look there you might be able to see what I'm trying to do exactly,
    Loading 1 item after another with a delay of 1 second inbetween.

    The error it's throwing in console:

    Code:
    [23:02:21 ERROR]: Could not pass event InventoryClickEvent to CM v0.1.001
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java
    :1630) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
    [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
    [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_72]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_72]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot188.
    jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:7
    14) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
    74) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
    53) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :556) [spigot188.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_72]
    Caused by: java.lang.NullPointerException
            at me.themaniacgamers.CM.Main.listeners.RecipesListener.oncmRecipesMight
    yClick(RecipesListener.java:232) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _72]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _72]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_72]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_72]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot188.jar:git-Spigot-fdc1440-53fac9f]
            ... 15 more
    >
    
    Thanks for your help!
     
  2. Offline

    Zombie_Striker

  3. Offline

    pie_flavor

    @TheManiacGamers Either the held item is null, or cmRecipesMighty is null. Not sure how you could look at an NPE and conclude that you couldn't call the scheduler 3 times in a row.
    For future reference, you should always post your entire class, and also the main class if it's separate. Don't just post code you perceive to be relevant, because the problem is often that you don't perceive the other code to be relevant.
    Also, tip: This is what you're doing.
    Code:
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
        public void run() {
            //...
        }
    }, 20L);
    This is the easier and more supported way:
    Code:
    new BukkitRunnable() {
        public void run() {
            //...
        }
    }.runTaskLater(plugin, 20L);
     
  4. Offline

    TheManiacGamers

    Thanks! Got it working.
     
Thread Status:
Not open for further replies.

Share This Page