Cooldown error in the Listener

Discussion in 'Plugin Development' started by oran10majar, Jun 12, 2014.

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

    oran10majar

    I try to make cooldown in the listener, its worked, but now it no longer works...
    And its work only in the main class.

    Someone knows why?

    -------------------------------------------------Code--------------------------------------------------

    this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){

    @Override
    public void run() {

    /*Msg*/

    }

    }, 0L, 20L);
     
  2. oran10majar Why's this async? And chances are you've cut out the useful parts of that. And you probably didn't pass an instance of your main class when doing this outside the main class. And we have code/syntax tags here.
     
  3. Offline

    oran10majar



    Its worked... Now its no longer work in the listener and its working only in the main class.

    I do "extends JavaPlugin implements Listener" and all the things I need to do. And its worked, but now its don't work.
     
  4. oran10majar Only extend JavaPlugin in your main class - pass an instance to the Listener class.
     
  5. Offline

    Konkz

    If he wants to have everything in one class then let it be,

    @OP - Here is an example how to make a delayed task, stole it off Bukkit's wiki:
    PHP:
    BukkitScheduler scheduler Bukkit.getServer().getScheduler();
            
    scheduler.scheduleSyncDelayedTask(this, new Runnable() {
     
                public 
    void run() {
                    
    // Well... cooldown is over
                
    }
            }, 
    20L); //20L = 20 Ticks = 1 Second
     
  6. Konkz
    Quite happy for him to do solely in the main class, but that doesn't seem to be what he wants to do ;)
     
  7. Offline

    oran10majar



    I know how to do that! I need only to know why its don't working.

    (Its working in the main class, but its doesn't work in the listener class) My code:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void punch(final Player player, final Player p){
    3.  
    4. if(player.getLocation().getWorld().getName().equals("lobby") || player.getLocation().getWorld().getName().equals("AC-LOBBY")){
    5. if(p.isOp() || p.hasPermission("hub.all") || p.hasPermission("hub.vip") || p.hasPermission("hub.vip1")){
    6.  
    7. if(getConfig().getBoolean(p.getName()+"-HideP") == false){
    8. if(getConfig().getBoolean(player.getName()+"-HideP") == false){
    9.  
    10. if(getConfig().getBoolean(player.getName()+"-Punch") == true){
    11. p.sendMessage(pr+"This player already punched!");
    12. }else{
    13. if(getConfig().getBoolean(p.getName()+"-punchok") == false){
    14. p.sendMessage(pr+"You can't do this so fastly!");
    15. }else{
    16. getConfig().set(player.getName()+"-Punch", true);
    17. saveConfig();
    18.  
    19. player.setVelocity(player.getLocation().getDirection().multiply(0).setY(2.5));
    20. ParticleEffect.LARGE_EXPLODE.display(player.getLocation(), 15, 0, 0, 0, 10, 1);
    21. player.playSound(player.getLocation(), Sound.EXPLODE, 1, 1);
    22. p.playSound(player.getLocation(), Sound.EXPLODE, 1, 1);
    23.  
    24. player.sendMessage(pr+"You punched by "+ChatColor.YELLOW+p.getName()+ChatColor.GRAY+"!");
    25. p.sendMessage(pr+"You punched "+ChatColor.YELLOW+player.getName()+ChatColor.GRAY+"!");
    26.  
    27. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    28.  
    29. @Override
    30. public void run() {
    31. if(getConfig().getBoolean(player.getName()+"-Punch") == true){
    32. ParticleEffect.LAVA.display(player.getLocation().add(0, 0.5, 0), 15, 0, 0, 0, 10, 3);
    33.  
    34. if(!player.getLocation().add(0, -1, 0).getBlock().getType().equals(Material.AIR)){
    35. getConfig().set(player.getName()+"-Punch", false);
    36. saveConfig();
    37. }
    38. }
    39.  
    40. }
    41.  
    42. }, 0L, 2L);
    43.  
    44. getConfig().set(p.getName()+"-punchcd", 15);
    45. getConfig().set(p.getName()+"-punchok", false);
    46. saveConfig();
    47.  
    48. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
    49.  
    50. @Override
    51. public void run() {
    52. int sec = getConfig().getInt(p.getName()+"-punchcd");
    53.  
    54. if(sec == 0){
    55. getConfig().set(p.getName()+"-punchcd", null);
    56. getConfig().set(p.getName()+"-punchok", true);
    57. saveConfig();
    58. }
    59.  
    60. if(sec > 0){
    61. getConfig().set(p.getName()+"-punchcd", sec-1);
    62. saveConfig();
    63. }
    64.  
    65. }
    66.  
    67. }, 0L, 20L);
    68.  
    69.  
    70. }
    71. }
    72. }else{
    73. p.sendMessage(pr+"You can't punch this player now. The player on Hide Players mode.");
    74. }
    75. }
    76. }else{
    77. p.sendMessage(pr+"Only VIPs can punch players!");
    78. }
    79. }
    80. }
     
  8. oran10majar 3rd time lucky? Pass an instance of your main class. If you're unaware of how to do that, google "java pass an instance of a class"
     
    Konkz likes this.
  9. Offline

    Konkz

    Do a repeating task and take away from sec.

    Code:
    repeatingTask(){
    int secs = config.getInt("my.int"); //lets say its going to be 10
    if (secs == 1) {
      Bukkit.broadcastMessage("Secs is 1");
    } else if (secs == 0) {
    Bukkit.broadcastMessage("Secs is 0!");
    this.cancel
    }
    }, 0L, 20L);
    Something like that. Also, you should never extends JavaPlugin in more then one class - only your main class extends JavaPlugin.
     
  10. Offline

    fireblast709

    Just to arbitrarily suggest stuff, why not use a Map<String, Long> for cooldowns rather than schedulers?
     
    AdamQpzm likes this.
  11. Offline

    oran10majar


    I have 30000 lines of code. I need 10 classes to do this. I don't want 1 class.
     
  12. Offline

    Konkz

    If you could, please quote where you've been told to use one class.

    What pass an instance of your main class is not making a new class or combining them but, if you like, having a bridge that data can drive across.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page