Solved plugin.getServer().getScheduler() Failing to work

Discussion in 'Plugin Development' started by TheMintyMate, Mar 25, 2014.

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

    TheMintyMate

    Code Withdrawn
     
  2. Offline

    McKiller5252

    Try this:

    Code:java
    1.  
    2. this.getServer().getScheduler().scheduleSyncTaskLater(plugin, new BukkitRunnable() {
    3. public void run() { onCooldown.remove(name);}},20);
    4. }
    5.  
     
  3. Offline

    TheMintyMate

    Ive tried your fix, however later in the line at "(plugin, new BukkitRunnable....." it also still complains. Do you have any suggestions of how to fix this?
    Thanks,
    Minty :)
     
  4. Offline

    amhokies

    TheMintyMate
    There aren't any variables with the name "plugin", so you can't access something that's not there. The class you're doing the method in is an instance of Plugin, so you can just use the 'this' keyword.
     
  5. Offline

    Barinade

    just remove "plugin."
    as long as you extend JavaPlugin you directly inherit methods from JavaPlugin (such as getServer())
     
  6. Offline

    DrEinsteinium

    amhokies
    I agree with this. Look at the usage for the this keyword in Java. It allows you to reference the instance of the class you're currently working with and it's a really clever keyword to use in situations like this.

    Edit: Or like above, don't even use the this keyword... It's not required
     
  7. Offline

    TheMintyMate

    TheMintyMate
    hm. I tried changing it to "this". However, "scheduleSyncTaskLater" is now underlined, and compiles the error " The method scheduleSyncTaskLater(Main, new BukkitRunnable(){}, int) is undefined for the type BukkitScheduler". I have also attempted to remove plugin intirely, but this made no difference. What do I do next? Thanks :)
     
  8. Offline

    amhokies

    It's probably due to the fact that there is no method with that name in the BukkitScheduler class -_-

    http://jd.bukkit.org/rb/apidocs/org/bukkit/scheduler/BukkitScheduler.html
     
  9. Offline

    TheMintyMate

    amhokies
    So what should I do to deal with the issue?
    Thanks :)
    Edit: Oh I see :p Ok, I think I know where to go from here...... Thanks :)
     
  10. Offline

    amhokies

    A little bit of common sense goes a long way. If the method you're trying to use doesn't exist... try a new method. The page I linked has all the methods in the BukkitScheduler class, so look for what you need, and use it.
     
  11. Offline

    Barinade

    int scheduleSyncDelayedTask(Plugin plugin, Runnable task)
    Schedules a once off task to occur as soon as possible.
     
  12. Offline

    TheMintyMate

    Ok, I have now updated the code to the following:

    Code Withdrawn

    However I now have the issue that there is no delay....... Whats wrong now?
    Thanks,
    Minty :)
     
  13. Offline

    Barinade

    The crappy naming is bothering me, ffs.
    the "20" is one second, if you want 20 seconds, set it to 400
     
  14. Offline

    amhokies

    TheMintyMate

    this.getServer().getScheduler().scheduleSyncDelayedTask(null ...

    You need to pass in your main plugin class instance. That's where the 'this' keyword comes into play like before.
     
  15. Offline

    TheMintyMate

    amhokies
    What do you mean "main class instance"? Currently its in the following tree:
    ♣Main
    ☼onEnable/onDisable
    publicvoid onPlayerInteractBlock(PlayerInteractEvent event)
    ♦ Detect item use
    ♦ Strike lighting at location of players crosshair within 8 blocks
    ♦Obtain player name, add to 'cooldown' array (as a string)
    ♦Run schedule of 2 seconds, and then remove player from cool down.

    Edit: happy to supply the current code (latest)
     
  16. Offline

    cast

  17. Offline

    TheMintyMate

    cast
    amhokies
    Barinade
    DrEinsteinium
    McKiller5252
    Thank-you all for your help. I have now come to realise that I need to check if the player is in the cool down, before initialising the effect. Because I didn't include a check in the if statement, no matter how many times the player is entered into the array, they can still use the effect. So now I have added a check in the if statement to see if they are in the cool down. They can only use the effect if they are not in the Array.
    Thanks again,
    Minty :)

    Here is a mini explanation of how you could do a cool down for any of those who come across this post in need for a cool down:

    Code:java
    1.  
    2. ArrayList <String> onCooldown = new ArrayList<String>();
    3.  

    Here we create an array to store the names of those who are 'cooling down'.
    ------------------------------------------------------------------------------------------------------------------
    Code:text
    1.  
    2. if ( WHATEVER YOU NEED HERE && onCooldown.isEmpty()) {
    3. }
    4.  


    This checks that their is no player in the array. Please note that if one player is in the array, NO player will have access through the if statement. This may be an issue if you need multiple users to be able to use the effect, without waiting for others cooldowns to stop. This hasn't been the said issue for me.
    Edit: To fix this replace onCooldown.isEmpty() with !onCooldown.contains(getName())
    ------------------------------------------------------------------------------------------------------------------
    Inside the If statement:
    Code:java
    1.  
    2. //WHATEVER YOU WANT THE PLAYER TO BE ABLE TO DO
    3. final String name = player.getName();
    4. onCooldown.add(name);
    5. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
    6. @Override
    7. public void run() {
    8.  
    9. onCooldown.remove(name);
    10. }
    11.  
    12. } ,40);
    13.  

    This adds the player to the array (once the effect for the player has been activated). It then counts down before removing the player from the array. To change how long the cool down lasts, just change the int at the end (40) to what ever number you need. 40 lasts roughly 2 seconds.
    ------------------------------------------------------------------------------------------------------------------
    You can also add an else statement to tell the player that there is a cool down...
    Code:java
    1.  
    2. else if (WHAT EVER YOU NEED HERE && onCooldown.contains(getName()) ) {
    3. player.sendMessage("Cooling Down.....");
    4. }
    5.  


    Hope this helps, feel free to contact me for any more information TheMintyMate

    @cast
    @amhokies
    @Barinade
    @DrEinsteinium
    @McKiller5252
    Just a quick question to throw to the community; is their anyway to disable the sound of thunder? :)
    - Otherwise, this thread is now Closed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
    McKiller5252 likes this.
Thread Status:
Not open for further replies.

Share This Page