[Help] My bucket scheduler won't work!

Discussion in 'Spigot Plugin Development' started by bradley13, May 30, 2016.

  1. Hey there, I am trying to make a simple Bukkit scheduler but I keep getting a error and I don't know how to fix it.

    Console error:
    Code (Text):

    [14:41:38 ERROR]: Could not pass event PlayerInteractEvent to EULARanks v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit_server.jar:git-Bukkit-18fbb24
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:211) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:728) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_65]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit_server.jar:git-Bukkit-18fbb24]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]
    Caused by: java.lang.ClassCastException: me.bradley.ranks.Villager cannot be cast to org.bukkit.plugin.Plugin
        at me.bradley.ranks.Villager.onClick(Villager.java:65) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_65]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_65]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]
        ... 17 more
    Line 65:
    Code (Text):
    scheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    My whole Bukkit Scheduler:
    Code (Text):

                    BukkitScheduler scheduler = Bukkit.getServer().getScheduler();

                    scheduler.scheduleSyncDelayedTask((Plugin) this, new Runnable() {

                      public void run(){

                          Log.info("102");

                    e.setCancelled(true);

                    PermissionUser user = PermissionsEx.getUser(player);

                    user.addGroup("villager");

                    player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "You are now the villager rank!");

                    for (ItemStack i : player.getInventory().getContents()) {

                        if (i.getType() == Material.CHEST && i.hasItemMeta() && i.getItemMeta().hasDisplayName() && i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Villager Rank")) {

                            ItemStack item = i;

                            player.getInventory().remove(i);

                        }

                }

                      }

                    }, 1200L);
    Thanks for any help I get!
     
  2.  
  3. Dat formating though... also like said above replace 'this' with a instance of your main (the class that extends JavaPlugin)
     
  4. Ok, i have done what you have said but now it is saying I have a error on this line in my main class:
    Code (Text):
     
    public class Core extends JavaPlugin implements Listener{
    Do I need to extend and implement anything else?
     
  5. The error itself would be useful
     
  6. In the error it says that the runnable is in the "Villager" class. You cannot use "this" when in another class when needing a main class. You can pass the core class in when registering it, then store it in the Villager class and use it there. Alternatively you can use a static getter for the main class instance
     
  7. Ok, I have this in my villager class:
    Code (Text):
     scheduler.scheduleSyncDelayedTask((Plugin) new Core(), new Runnable() {
    And I have a error saying:
    Code (Text):
     
    [14:50:23 ERROR]: Could not pass event PlayerInteractEvent to EULARanks v1.0

    org.bukkit.event.EventException

        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:211) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:728) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_65]

        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_65]

        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit_server.jar:git-Bukkit-18fbb24]

        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_65]

    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!

        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at me.bradley.ranks.Core.<init>(Core.java:6) ~[?:?]

        at me.bradley.ranks.Villager.onClick(Villager.java:65) ~[?:?]

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_65]

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_65]

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_65]

        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_65]

        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        ... 17 more

    Caused by: java.lang.IllegalStateException: Initial initialization

        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at me.bradley.ranks.Core.<init>(Core.java:6) ~[?:?]

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_65]

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_65]

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_65]

        at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[?:1.8.0_65]

        at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_65]

        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:129) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:289) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:718) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.Bukkit.reload(Bukkit.java:544) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) ~[craftbukkit_server.jar:git-Bukkit-18fbb24]

        ... 9 more
    The line of code it is saying the error is on it this:
    Code (Text):
     public class Core extends JavaPlugin implements Listener{
    I don't know what is wrong.
     
  8. Do you have ANY idea of what you're doing?
     
  9. I have never really worked with runnable's so the answer is probably a no.
     
  10. Have you registered the event and show us all the code cause the error only states where the first 'error' is.

    EDIT: do you have to classes extending JAvaPlugin ?xD
     
  11. Ok, this is my main class:
    Code (Text):
     
    package me.bradley.ranks;



    import org.bukkit.event.Listener;

    import org.bukkit.plugin.java.JavaPlugin;



    public class Core extends JavaPlugin implements Listener{

       

        public void onEnable(){

            getServer().getPluginManager().registerEvents(new Villager(), this);

            getCommand("villager").setExecutor(new Villager());

        }

       

        public void onDisable(){

           

        }



    }
    This is my villager class. There are probably many things in there you all don't like but it all works apart from the scheduler:
    Code (Text):
     
    package me.bradley.ranks;



    import java.util.ArrayList;

    import java.util.List;



    import org.bukkit.Bukkit;

    import org.bukkit.ChatColor;

    import org.bukkit.Material;

    import org.bukkit.command.Command;

    import org.bukkit.command.CommandExecutor;

    import org.bukkit.command.CommandSender;

    import org.bukkit.craftbukkit.libs.jline.internal.Log;

    import org.bukkit.entity.Player;

    import org.bukkit.event.EventHandler;

    import org.bukkit.event.Listener;

    import org.bukkit.event.block.Action;

    import org.bukkit.event.player.PlayerInteractEvent;

    import org.bukkit.inventory.ItemStack;

    import org.bukkit.inventory.meta.ItemMeta;

    import org.bukkit.plugin.Plugin;

    import org.bukkit.scheduler.BukkitScheduler;



    import ru.tehkode.permissions.PermissionUser;

    import ru.tehkode.permissions.bukkit.PermissionsEx;



    public class Villager implements Listener, CommandExecutor{

       

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){

            Player player = (Player) sender;

            if(cmd.getName().equalsIgnoreCase("villager")){

                if(sender.isOp() || !(sender instanceof Player)){

                    if(args.length == 0){

                        player.sendMessage(ChatColor.RED + "Please specify a player to give it to!");

                    }

                    Player target = Bukkit.getServer().getPlayer(args [0]);

                    ItemStack v = new ItemStack(Material.DIAMOND, 1);

                    ItemMeta vMeta = v.getItemMeta();

                    vMeta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Villager Rank");

                    List<String> lore = new ArrayList<String>();

                    lore.add(ChatColor.WHITE + "Right click and wait 60 seconds for your rank!");

                    vMeta.setLore(lore);

                    v.setItemMeta(vMeta);

                    target.getInventory().addItem(v);

                }

            }

            returntrue;

        }

       

        @EventHandler

        public void onClick(PlayerInteractEvent e){

            Player player = e.getPlayer();

            ItemStack v = new ItemStack(Material.DIAMOND, 1);

            ItemMeta vMeta = v.getItemMeta();

            vMeta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Villager Rank");

            List<String> lore = new ArrayList<String>();

            lore.add(ChatColor.WHITE + "Right click and wait 60 seconds for your rank!");

            vMeta.setLore(lore);

            v.setItemMeta(vMeta);

            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){

                if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Villager Rank")) {

                    //if(player.getLocation().getX()==-487 && player.getLocation().getY()==68 && player.getLocation().getZ()==243){

                    player.sendMessage(ChatColor.RED + "The rank diamond has been activated!");

                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "enjin broadcast &b&lSomeone has activated the rank diamond! Go find them and kill them to get it!");

                    BukkitScheduler scheduler = Bukkit.getServer().getScheduler();

                    scheduler.scheduleSyncDelayedTask((Plugin) new Core(), new Runnable() {

                      public void run(){

                          Log.info("102");

                    e.setCancelled(true);

                    PermissionUser user = PermissionsEx.getUser(player);

                    user.addGroup("villager");

                    player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "You are now the villager rank!");

                    for (ItemStack i : player.getInventory().getContents()) {

                        if (i.getType() == Material.CHEST && i.hasItemMeta() && i.getItemMeta().hasDisplayName() && i.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Villager Rank")) {

                            ItemStack item = i;

                            player.getInventory().remove(i);

                        }

                }

                      }

                    }, 1200L);

                    //}

                    //player.sendMessage(ChatColor.RED + "Go to -487, 68, 243 to activate your diamond!");

                }

            }

        }



    }
     
  12. No I do not.
     
  13. You are creating a new instance of the main class, which is the proplem here, don't try to create a new instance, learn how to pass the instance of the main class *Check CratePlus src code for more info