Solved Cooldowns

Discussion in 'Plugin Development' started by TheHolySheep_NL, Mar 23, 2014.

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

    TheHolySheep_NL

    Hi
    I am working on a Donator plugin where donators can use special items.
    i am trying to add cooldowns.
    It puts a player in a Cooldowns and it says:
    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Donators" + ChatColor.WHITE + "] " + ChatColor.RED + "You have to wait 10 seconds!");

    But it doesnt remove the player from the Cooldown after the 10 seconds.
    and i get an massive error in the console.
    Maybe the "return true;" causes the error because i get a red line under it when i try to use it.

    (i just started making bukkit plugin so..)

    - Sheep

    Main Class:

    Code:
    package me.theholysheepnl.Donator;
     
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Main extends JavaPlugin implements Listener{
     
     
    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(new AxeListener(),this);
        Bukkit.getServer().getPluginManager().registerEvents(new SwordListener(),this);
    }
     
    }
    
    AxeListener Class:

    Code:
    package me.theholysheepnl.Donator;
     
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class AxeListener implements Listener {
     
    ArrayList<Player> cooldown = new ArrayList<Player>();
     
     
    @EventHandler(priority = EventPriority.NORMAL)
    public void onClick(PlayerInteractEvent event){
    final Player p = (Player) event.getPlayer();
    if(cooldown.contains(p)) {
    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Donators" + ChatColor.WHITE + "] " + ChatColor.RED + "You have to wait 10 seconds!");
    return true;
    }
    if(p.getItemInHand().getType() == Material.DIAMOND_AXE){
    if(p.hasPermission("skill.axe"))
    if((event.getAction() == Action.RIGHT_CLICK_AIR) || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Donators" + ChatColor.WHITE + "] " + ChatColor.GREEN + "You actived your" + ChatColor.GOLD + " EnderPearl Skill" + ChatColor.GREEN + "!");
    p.launchProjectile(EnderPearl.class);
    p.playSound(p.getLocation(), Sound.PORTAL, 1, 1);
    cooldown.add(p);
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(null, new Runnable() {
    public void run() {
    cooldown.remove(p);
    }
    }, 200);
    return true;
    }
    }
     
    }
    }
     
  2. Offline

    jboy44

    Mattkx4 likes this.
  3. Offline

    xXSniperzzXx_SD

    TheHolySheep_NL

    Please use the Code block... And chances are you're not in your class that extends JavaPlugin, therefor "this" is null
     
  4. Offline

    KevyPorter

    Quick Google searches can be life savers sometimes:
    Normal Cooldown

    Advanced Cooldown With Time Remaining
     
  5. Offline

    TheHolySheep_NL

    jboy44 what is "the stacktrace" ?
    Sorry i just started making plugins :/

    - Sheep
     
  6. Offline

    infopaste

    I haven't really looked into your problem. Just though I should share what Lib I use for cool-downs.


    The following is a made by Comphenix, scroll down on this post. Its somewhere in the comments.
    Code:java
    1. public class Cooldowns {
    2. private static Table<String, String, Long> cooldowns = HashBasedTable.create();
    3.  
    4. /**
    5.   * Retrieve the number of milliseconds left until a given cooldown expires.
    6.   * <p>
    7.   * Check for a negative value to determine if a given cooldown has expired. <br>
    8.   * Cooldowns that have never been defined will return {@link Long#MIN_VALUE}.
    9.   * @param player - the player.
    10.   * @param key - cooldown to locate.
    11.   * @return Number of milliseconds until the cooldown expires.
    12.   */
    13. public static long getCooldown(Player player, String key) {
    14. return calculateRemainder(cooldowns.get(player.getName(), key));
    15. }
    16.  
    17. /**
    18.   * Update a cooldown for the specified player.
    19.   * @param player - the player.
    20.   * @param key - cooldown to update.
    21.   * @param delay - number of milliseconds until the cooldown will expire again.
    22.   * @return The previous number of milliseconds until expiration.
    23.   */
    24. public static long setCooldown(Player player, String key, long delay) {
    25. return calculateRemainder(
    26. cooldowns.put(player.getName(), key, System.currentTimeMillis() + delay));
    27. }
    28.  
    29. /**
    30.   * Determine if a given cooldown has expired. If it has, refresh the cooldown. If not, do nothing.
    31.   * @param player - the player.
    32.   * @param key - cooldown to update.
    33.   * @param delay - number of milliseconds until the cooldown will expire again.
    34.   * @return TRUE if the cooldown was expired/unset and has now been reset, FALSE otherwise.
    35.   */
    36. public static boolean tryCooldown(Player player, String key, long delay) {
    37. if (getCooldown(player, key) <= 0) {
    38. setCooldown(player, key, delay);
    39. return true;
    40. }
    41. return false;
    42. }
    43.  
    44. private static long calculateRemainder(Long expireTime) {
    45. return expireTime != null ? expireTime - System.currentTimeMillis() : Long.MIN_VALUE;
    46. }
    47. }


    Make a class called Cooldowns (very import you call it Cooldowns) and paste that code into it.

    Here is an example of using it:

    Code:
    if (Cooldowns.tryCooldown(player, "MagicHand", 15000)) {
        // Do what you need to do here. You don't have to set the cooldown when you're done.
    } else {
        // Cooldown hasn't expired yet
        player.sendMessage("You have " + (Cooldowns.getCooldown(player, "MagicHand") / 1000) + " seconds left.");
    }
    Not sure if this is what your looking for, but it's a helpful piece of code.
     
  7. Offline

    nlthijs48

    TheHolySheep_NL You need to change the "return true;" to "return;" because the method is of type void, so you can't return a boolean.
     
  8. Offline

    TheHolySheep_NL

    Hi , nlthijs48

    if i change it to return;
    and i use the special item i get this massive error:

    - sheep

    Code:
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-g
    bafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:161) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-g
    bafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :604) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(Craf
    tScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnk
    s]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(
    CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b297
    5jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncR
    epeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-
    gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncD
    elayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gb
    afd450-b2975jnks]
            at me.theholysheepnl.Donator.AxeListener.onClick(AxeListener.java:37) ~[
    ?:?]
            at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            ... 15 more
     
  9. Offline

    jpjunho

  10. Offline

    nlthijs48

    TheHolySheep_NL In the following line you register the Runnable with an argument null, this should instead be (a reference to) your main plugin class (extends JavaPlugin). That is also the line in the code where the stacktrace points to.
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(null, new Runnable()
     
  11. Offline

    TheHolySheep_NL

    jpjunho
    i did but i get a error:

    - Sheep

    Code:
    [18:31:10 ERROR]: Could not pass event PlayerInteractEvent to Donator v0.2
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-g
    bafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInt
    eractManager.java:374) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jn
    ks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(Craf
    tScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnk
    s]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(
    CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b297
    5jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncR
    epeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-
    gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncD
    elayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gb
    afd450-b2975jnks]
            at me.theholysheepnl.Donator.AxeListener.onClick(AxeListener.java:36) ~[
    ?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            ... 15 more
    >
    @nlthijs48

    is this what you mean?

    Code:
    package me.theholysheepnl.Donator;
     
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
     
    public class AxeListener implements Listener {
       
        ArrayList<Player> cooldown = new ArrayList<Player>();
           
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void onClick(PlayerInteractEvent event){
            final Player p = (Player) event.getPlayer();
            if(cooldown.contains(p)) {
                p.sendMessage(ChatColor.GOLD + "[TSG] " + ChatColor.RED + "You have to wait 10 seconds!");
               
            }
           
              if(p.getItemInHand().getType() == Material.DIAMOND_AXE){
                if(p.hasPermission("skill.axe"))
                    if((event.getAction() == Action.RIGHT_CLICK_AIR) || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                      p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Donators" + ChatColor.WHITE + "] " + ChatColor.GREEN + "You actived your" + ChatColor.GOLD + " EnderPearl Skill" + ChatColor.GREEN + "!");   
                        p.launchProjectile(EnderPearl.class);
                        p.playSound(p.getLocation(), Sound.PORTAL, 1, 1);               
                        cooldown.add(p);
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(null, new Runnable() {
                            public void run() {
                            cooldown.remove(p);
                            }
                        }, 200);
                    }
              }
        }
    }
    
    when i use this i get the error:

    Code:
    [18:40:33 ERROR]: Could not pass event PlayerInteractEvent to Donator v0.2
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-g
    bafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:161) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-g
    bafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :604) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(Craf
    tScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnk
    s]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimer(
    CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b297
    5jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncR
    epeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-
    gbafd450-b2975jnks]
            at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.scheduleSyncD
    elayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gb
    afd450-b2975jnks]
            at me.theholysheepnl.Donator.AxeListener.onClick(AxeListener.java:38) ~[
    ?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-1-gbafd450-b2975jnks]
            ... 15 more
    >
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  12. Offline

    nlthijs48

    TheHolySheep_NL Did you change something?
    The error is in this part:
    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(null, new Runnable() {
    2. public void run() {
    3. cooldown.remove(p);
    4. }
    5. }, 200);

    The "null" should be a reference to your main class of your plugin, as I can see in your code you don't have that here. You need to get the main plugin class by your constructor of this class.
     
  13. Offline

    Opacification

  14. Offline

    TheHolySheep_NL

    hi, nlthijs48
    i am fearly new to making bukkit plugins :/
    so i dont know how to do this: "You need to get the main plugin class by your constructor of this class."

    - Sheep
     
  15. Offline

    nlthijs48

    TheHolySheep_NL In your AxeListener class add the following code after the declaration of the arraylist "cooldown":
    Code:java
    1. JavaPlugin plugin;
    2.  
    3. public AxeListener(JavaPlugin plugin) {
    4. this.plugin = plugin;
    5. }

    Then in you main class you need to give "this" as a parameter when you create a instance of this class (Eclipse will highlight the line in your onEnable() method where you register this listener with red).
    Now you can change the "null" you have in your AxeListener with "plugin" and then the task will be scheduled normally and without errors.
     
  16. Offline

    TheHolySheep_NL

    nlthijs48
    did you mean this?
    The errors aren't fixed :(

    - Sheep

    Main
    Code:
    package me.theholysheepnl.Donator;
     
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Main extends JavaPlugin implements Listener{
     
     
    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(new AxeListener(this),this);
        Bukkit.getServer().getPluginManager().registerEvents(new SwordListener(),this);
    }
     
    }
    
    AxeListener

    Code:
    package me.theholysheepnl.Donator;
     
     
    import java.util.ArrayList;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.EnderPearl;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class AxeListener implements Listener {
       
       
        ArrayList<Player> cooldown = new ArrayList<Player>();
       
        JavaPlugin plugin;
       
        public AxeListener(JavaPlugin plugin) {
            this.plugin = plugin;
        }
       
       
           
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void onClick(PlayerInteractEvent event){
            final Player p = (Player) event.getPlayer();
            if(cooldown.contains(p)) {
                p.sendMessage(ChatColor.GOLD + "[TSG] " + ChatColor.RED + "You have to wait 10 seconds!");
               
            }
           
              if(p.getItemInHand().getType() == Material.DIAMOND_AXE){
                if(p.hasPermission("skill.axe"))
                    if((event.getAction() == Action.RIGHT_CLICK_AIR) || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                      p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Donators" + ChatColor.WHITE + "] " + ChatColor.GREEN + "You actived your" + ChatColor.GOLD + " EnderPearl Skill" + ChatColor.GREEN + "!");   
                        p.launchProjectile(EnderPearl.class);
                        p.playSound(p.getLocation(), Sound.PORTAL, 1, 1);               
                        cooldown.add(p);
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                            cooldown.remove(p);
                            }
                        }, 200);
                    }
              }
        }
    }
    
     
  17. Offline

    nlthijs48

    TheHolySheep_NL Yes that is what I meant, do you have another error now? Can you post that again?
     
  18. Offline

    TheHolySheep_NL

    nlthijs48
    the error is fixed and i get the wait message but i can use it again (within the cooldown).

    - Sheep
     
  19. Offline

    nlthijs48

    TheHolySheep_NL Use the following code to fix it, you don't want to execute more code if the contains(p) is true:
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onClick(PlayerInteractEvent event) {
    3. final Player p = (Player) event.getPlayer();
    4. if((event.getAction() == Action.RIGHT_CLICK_AIR) || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if(p.getItemInHand().getType() == Material.DIAMOND_AXE) {
    6. if(p.hasPermission("skill.axe")) {
    7. if(cooldown.contains(p)) {
    8. p.sendMessage(ChatColor.GOLD + "[TSG] " + ChatColor.RED + "You have to wait 10 seconds!");
    9. return;
    10. }
    11. p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GREEN + "Donators" + ChatColor.WHITE + "] " + ChatColor.GREEN + "You actived your" + ChatColor.GOLD + " EnderPearl Skill" + ChatColor.GREEN + "!");
    12. p.launchProjectile(EnderPearl.class);
    13. p.playSound(p.getLocation(), Sound.PORTAL, 1, 1);
    14. cooldown.add(p);
    15. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    16. public void run() {
    17. cooldown.remove(p);
    18. }
    19. }, 200);
    20. } else {
    21. p.sendMessage("You don't have permission");
    22. }
    23. }
    24. }
    25. }
     
  20. Offline

    TheHolySheep_NL

    nlthijs48
    It works!!!!
    Thank you very Much!!!

    - Sheep
     
  21. Offline

    nlthijs48

  22. Offline

    _BrodieC

    Can someone here help me with my cooldowns?? im confused because there are no errors in eclipse but it just doesnt work when i try use it.
    public class Iron implements Listener {

    public Main main;

    public Iron(Main main) {
    this.main = main;
    }
    ArrayList<Player> resistcooldown = new ArrayList<Player>();
    @EventHandler
    public void riposte(PlayerInteractEvent e) {
    final Player p = e.getPlayer();
    if(resistcooldown.contains(p) && e.getAction() == Action.RIGHT_CLICK_AIR)
    if(Armour.getKit(p).equals(ClassType.IRON)) {
    if(utilPlayer.hasSwords(p)) {
    p.sendMessage( ChatColor.DARK_AQUA + "Templar> " +ChatColor.GRAY + "You can not use " + ChatColor.AQUA + "Resist");
    }else if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    if(Armour.getKit(p).equals(ClassType.IRON)) {
    if(utilPlayer.hasSwords(p)) {

    p.sendMessage(ChatColor.DARK_AQUA + "Tempar> " + ChatColor.GRAY + "You have used" + ChatColor.AQUA + " Resist");
    p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 100,3));
    resistcooldown.add(p);
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    public void run() {
    resistcooldown.remove(p);
    p.sendMessage(ChatColor.RED + "Templar> " + ChatColor.GRAY + "Recharged " + ChatColor.AQUA + "Resist");
    }
    }, 200L);
    }
    }
    }
    }
    }
     
  23. Offline

    nlthijs48

    _BrodieC Did you register your events in the onEnable() method of your main class?
     
Thread Status:
Not open for further replies.

Share This Page