SyncDelayedTask dont work!

Discussion in 'Plugin Development' started by MrRedstone3000, Oct 17, 2014.

?

How can i fix that?

  1. Help

    0 vote(s)
    0.0%
  2. solve the problem

    0 vote(s)
    0.0%
Multiple votes are allowed.
Thread Status:
Not open for further replies.
  1. Offline

    MrRedstone3000

    I have a problem, if i try to make a cooldown with a SyncDelayedTask it looks fine but dont works.
    The class is not my main class. I am importing the class into my main class with: getServer().getPluginManager().registerEvents(new Fireman(), this);

    Here is the code from the class :


    package EventHandler;

    import java.util.ArrayList;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Fireball;
    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.plugin.Plugin;

    public class Fireman implements Listener {

    private Plugin plugin;


    ArrayList<String> codo = new ArrayList<>();



    @EventHandler
    public void onCompassKlick(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
    if(e.getPlayer().getItemInHand().getType() == Material.FLINT_AND_STEEL) {

    final Player p = e.getPlayer();

    if(!codo.contains(p.getName()))
    p.sendMessage("§8You are on Cooldown for 15 Secunds");
    p.playSound(p.getLocation(), Sound.WITHER_SHOOT, 10.0F, 2);
    Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    f.setIsIncendiary(false);
    f.setYield(0);
    codo.add(p.getName());
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){


    public void run() {
    p.sendMessage("test");
    codo.remove(p.getName());

    }
    }, 100);
    }
    }
    }
    }


    In the Console it says:

    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.3-b3020jnks]

    and

    at EventHandler.Fireman.onCompassKlick(Fireman.java:45) ~[?:?]
     
  2. Offline

    Watto

    Your plugin variable is null.

    private Plugin plugin;
    ^
    You aren't setting this.

    MrRedstone3000

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  3. Offline

    MrRedstone3000

    ohh....ok but what and how do i set it? (sorry i am a littel noobie)
     
  4. Offline

    Watto

    MrRedstone3000

    In your Fireman class:

    Code:java
    1. public Fireman(Plugin pl){
    2. this.plugin = pl;
    3. }


    In your Plugin class (Where you call the Fireman class)
    Code:java
    1. getServer().getPluginManager().registerEvents(new Fireman(this), this);


    Basically passing the plugin to the Fireman constructor and setting the plugin variable in the Fireman class
     
  5. Offline

    MrRedstone3000

    Thanks a lot!:D

    but my Arraylist isnt working... so i can still spam the fireballs :/
    if i click my item it shoots a fireball and it says "You are on Cooldown for 15 Secunds" whish is great and after 15 sekunds it says "test" wich is also great but i can spam it while cooldown...
    it should be all great

    my code right now:

    package EventHandler;




    import java.util.ArrayList;




    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Fireball;
    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.plugin.Plugin;

    public class Fireman implements Listener {

    private Plugin plugin;
    public Fireman(Plugin pl){
    this.plugin = pl;
    }

    ArrayList<String> codo = new ArrayList<>();



    @EventHandler
    public void onCompassKlick(PlayerInteractEvent e) {
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
    if(e.getPlayer().getItemInHand().getType() == Material.FLINT_AND_STEEL) {

    final Player p = e.getPlayer();

    if(!codo.contains(p.getName()));
    p.sendMessage("§8You are on Cooldown for 15 Secunds");
    p.playSound(p.getLocation(), Sound.WITHER_SHOOT, 10.0F, 2);
    Fireball f = e.getPlayer().launchProjectile(Fireball.class);
    f.setIsIncendiary(false);
    f.setYield(0);
    codo.add(p.getName());
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){


    public void run() {
    p.sendMessage("§8Cooldown over!");
    codo.remove(p.getName());

    }


    }, 20*15);


    }

    }
    }
    }
     
  6. Offline

    Watto

    Please wrap your code using the Code button when you post on topics it makes code easier to read

    Code:java
    1. Like this :3


    Also your problem is here:

    if(!codo.contains(p.getName()));

    You are ending the if statement with a semicolon so it's a useless statement.

    MrRedstone3000

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  7. Offline

    frontman31

    First of all, don't write your cooldown-system like that. In your MainClass you have to create a global HashMap with a String as key and a Long as Value. In your actual ListenerClass you have to write a couple of things in your method:
    1. initialize a Long variable (let's call it time) and set it equal to System.currentTimeMillis()
    2. then you have to check, if the HashMap of the MainClass contains the UUID of the Player
    3. initialize another Long variable (let's call it lastUsage) and set it equal to the Value you obtain by the HashMap
    4. another useful thing to know: a second in Java is represented by 1000, for instance 15 seconds is equal to 15 * 1000
    5. now you check if lastUsage + 15 * 100 > time; whether it's true, you return at this point (and maybe you print out to the Player that he is still on cooldown)
    6. after the if-statement you write your stuff and at the end you put the Player's UUID as Key and the time as Value in the HashMap of your MainClass

    To solve your actual problem with the ArrayList, don't create your ArrayList in the ListenerClass, put in the MainClass

    (Here's the code to prevent any misunderstandings)

    Code:java
    1. private <YourMainClass> plugin;
    2.  
    3. public <YourListener>(<YourMainClass> plugin) {
    4. this.plugin = plugin;
    5. }
    6.  
    7. @EventHandler
    8. public void <YourMethod>(<YourEvent> event) {
    9. Player p = event.getPlayer();
    10.  
    11. // Check the Item, etc..
    12.  
    13. // Initialize a Long variable for the current Time (as long)
    14. Long time = System.currentTimeMillis();
    15.  
    16. // Check if the HashMap contains the UUID
    17. if(plugin.cooldown.containsKey(p.getUniqueId().toString()) {
    18.  
    19. // Obtain the lastUsage by the HashMap
    20. Long lastUsage = plugin.cooldown.get(p.getUniqueId().toString());
    21.  
    22. if(lastUsage + 15 * 1000 > time) {
    23. // Player is still on cooldown
    24. return;
    25. }
    26. }
    27.  
    28. // Your stuff here
    29.  
    30. // Finally put the Player and the time in the HashMap
    31. plugin.cooldown.put(p.getUniqueId().toString(), time);
    32. }


    I hope, it will help.
     
  8. Offline

    fireblast709

    frontman31 Use long for the currentTimeMillis (note the lowercase l). While the Map can return null, a Long is preferred to be able to detect null values before any NullPointerExceptions, however currentTimeMillis will never return null, so no need to use the wrapper object.
     
  9. Offline

    MrRedstone3000

    Thanks a lot guys!
    :D

    One las question XD
    is it smarter to code everything into one(main) calss or making a lot diffrent classes?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  10. Offline

    Watto

    MrRedstone3000

    Much smarter to code in separate classes with logical names, as it allows you to effectively control your code and makes it easier to read :)
     
  11. Offline

    FerusGrim

    MrRedstone3000 Why store your UUIDs as Strings? As far as I can tell, there's never an instance when you're storing your UUIDs to a file anywhere, as cooldowns don't actually need to be saved. So why convert them to a String?

    It would save steps, and lessen confusion, to just keep them as UUIDs. Then, if you ever do have to save them to a config (for whatever reason), you can just call UUID#fromString when you load it, and use UUID#toString when you save it.

    Also, consider if a Runnable is even needed here. Unless you need a message displayed when the cooldown wears off (which you may not, in many instances), it may just be easier to save the Long value, and when the command is run again, check if they have a cooldown time stored, and if they do, if it's been long enough.
     
  12. Offline

    MrRedstone3000

    Ok i made a diffrent one like this but its not working :/

    Code:

    Code:
    package EventHandler;
     
    import java.util.HashMap;
     
    import org.bukkit.Material;
    import org.bukkit.Sound;
    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;
     
     
     
    public class Flash implements Listener {
       
        private HashMap<String, Long> cooldown = new HashMap<String, Long>();
       
        @EventHandler
       
        public void onFlash(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Long time = System.currentTimeMillis();
            if(this.cooldown.containsKey(p.getName())) {
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR){
            if(e.getPlayer().getItemInHand().getType() == Material.NETHER_STAR){
            Long lastUsage = this.cooldown.get(p.getName());
            if(lastUsage + 1000*15 > time) {
                p.sendMessage("§8You are still on Cooldown for " + lastUsage + " Sekunds");
                return;
            }
           
           
            p.getPlayer().setVelocity(p.getPlayer().getLocation().getDirection().multiply(4));
            p.sendMessage("§8You are on Cooldown for 15 Secunds");
            p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 10.0F, 2);   
            this.cooldown.put(p.getName(), time);
        }
       
            }
     
    }
    }
    }
     
  13. Offline

    frontman31

    fireblast709 Yeah sure, but I think this won't really matter at this point. Although, the use of primitive types is always appreciated.

    MrRedstone3000 I formatted your code a bit:
    Code:java
    1. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR){
    2. if(e.getPlayer().getItemInHand().getType() == Material.NETHER_STAR){
    3. long time = System.currentTimeMillis();
    4.  
    5. if(plugin.cooldown.containsKey(p.getName())) {
    6. long lastUsage = this.cooldown.get(p.getName());
    7.  
    8. if(lastUsage + 1000*15 > time) {
    9. p.sendMessage("§8You are still on Cooldown for " + lastUsage + " Sekunds");
    10. return;
    11. }
    12. }
    13.  
    14. p.getPlayer().setVelocity(p.getPlayer().getLocation().getDirection().multiply(4));
    15. p.sendMessage("§8You are on Cooldown for 15 Secunds");
    16. p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 10.0F, 2);
    17. this.cooldown.put(p.getName(), time);
    18. }
    19. }


    I think this should work.
    Oh, and by the way, you have to create the HashMap in the MainClass instead of the ListenerClass.
     
  14. Offline

    MrRedstone3000

    Ok thanks i try this :)

    Mh.. it still wont realy work...

    just to be shure my HashMap is "cooldown"
    and i dont need anything elese?
    The Error is : "cooldown cannot be resolved or is not a field" if i look at cooldown

    Here is the Code...

    Code:java
    1. package EventHandler;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.Sound;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.Plugin;
    13.  
    14.  
    15.  
    16. public class Flash implements Listener {
    17.  
    18.  
    19.  
    20. private Plugin plugin;
    21. public Flash(Plugin pl){
    22. this.plugin = pl;
    23. }
    24.  
    25.  
    26. @EventHandler
    27.  
    28. public void onFlash(PlayerInteractEvent e) {
    29. Player p = e.getPlayer();
    30.  
    31. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_AIR){
    32. if(e.getPlayer().getItemInHand().getType() == Material.NETHER_STAR){
    33. long time = System.currentTimeMillis();
    34.  
    35. if(plugin.cooldown.containsKey(p.getName())) {
    36. long lastUsage = this.cooldown.get(p.getName());
    37.  
    38. if(lastUsage + 1000*15 > time) {
    39. p.sendMessage("§8You are still on Cooldown for " + lastUsage + " Sekunds");
    40. return;
    41. }
    42. }
    43.  
    44. p.getPlayer().setVelocity(p.getPlayer().getLocation().getDirection().multiply(4));
    45. p.sendMessage("§8You are on Cooldown for 15 Secunds");
    46. p.playSound(p.getLocation(), Sound.FIREWORK_LAUNCH, 10.0F, 2);
    47. this.cooldown.put(p.getName(), time);
    48. }
    49. }
    50. }
    51. }


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

Share This Page