Delayed / Reapeting | Tasks [How Do I Do This From Different Classes]

Discussion in 'Spigot Plugin Development' started by UnthinkableR, May 20, 2015.

  1. Hello I Am Trying to make cool downs for my interact event and...

    CODE Im Using:
    Code (Text):
    public class ThrowItem implements Listener{
    ArrayList<String> CoolDown = new ArrayList<String>();
    public static Main plugin;

        @EventHandler
        public void PlayerThrowItemEvent(PlayerInteractEvent e){
            if((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)){
                if(!CoolDown.contains(e.getPlayer().getName())){
            Player p = e.getPlayer();
            String id = p.getName();
            ItemStack i = p.getItemInHand();
            ItemStack AIR = new ItemStack(Material.AIR);
            if(i.getType() == Material.IRON_AXE){          
       
           //DO STUFF();        
        Bukkit.getServer().getPluginManager().callEvent(new PlayerThrowItemEvent(p, p.getLocation(), i));
                CoolDown.add(id);
               
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                      public void run() {
                          CoolDown.remove(id);
                      }
                    }, 60);
                }
              }else{
                 
              }
            }
        }
       
        @EventHandler
        public void PlayerQuitEvent(PlayerQuitEvent e){
            if(CoolDown.contains(e.getPlayer().getName())){
            CoolDown.remove(e.getPlayer().getName());
            }
        }
    And Why This Error?
    PHP:
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:226) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.PlayerInteractManager.interact(PlayerInt
    eractManager.java:463) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java
    :734) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_31]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_31]
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot.jar
    :git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:7
    10) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    68) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    51) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :554) [spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.
    jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.validate(Craf
    tScheduler.java:397) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.runTaskTimer(
    CraftScheduler.java:123) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.scheduleSyncR
    epeatingTask(CraftScheduler.java:119) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at org.bukkit.craftbukkit.v1_8_R2.scheduler.CraftScheduler.scheduleSyncD
    elayedTask(CraftScheduler.java:102) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            at be.UnthinkableR.KitPvP.Events.ThrowItem.PlayerThrowItemEvent(ThrowIte
    m.java:40) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _31]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _31]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_31]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-26dfd01-5cb9b70]
            ... 17 more
    >
    private static That ^^^^^;
    And I Don't Think That This Will Work If That Didn't Work
    Code (Text):
    public class SupplyDrop implements Listener{
        private static Main plugin;
        private static int timeinseconds = 120;
        static FileConfiguration sdConfig = plugin.getSupplyDropConfig();
        static int SupplyDropTask = 0;
       
       
        public static void StartCountDown(){
            SupplyDropTask = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
            {
                int timer = 4;
             
                @Override
                public void run()
                {
                    if(timer > 0)
                    {
                        for(Player p : Bukkit.getOnlinePlayers()){
                            Methods.sendMessage(p, plugin.getMessagesConfig().getString("SupplyDropCountDown"));
                        }
                    }
                    else
                    {
                         SupplyDrop.DropItem();
                         for(Player p : Bukkit.getOnlinePlayers()){
                             Methods.sendMessage(p, plugin.getMessagesConfig().getString("SupplyDropMSG"));
                         }
                     
                        Bukkit.getScheduler().cancelTask(SupplyDropTask);
                    }
                 
                    timer--;
                }
            }, 0L, 20L);
        }
       
        public static void DropItem(){
           
        }
    }
     
  2. Line 40: Bukkit.getScheduler().cancelTask(SupplyDropTask);
     
  3. Why is nearly everything static...
     
  4. Well you would want to make a getCooldown, and pass the instance of the listener class to another class where you register the Delayed or Repeating task by putting it in the constructor. Don't misuse statics.
     
  5. @UnthinkableR
    • Variables and methods should be lowerCamelCase.
    • Remove the static keyword and create a constructor with a plugin instance as first parameter which sets the plugin field.
     
  6. As for the error this was never initialized:
    Code (Text):
    public static Main plugin;
     
  7. I learned from the best :D
     
  8. The main can be null.
     
  9. It can, but it can't be used
     
  10. Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot.




    SupplyDropTask = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()