Solved Scheduler Cooldown help

Discussion in 'Plugin Development' started by MisterPhiloe, Jan 18, 2014.

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

    MisterPhiloe

    SOLVED :D

    Someone?

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

    The_IcATaRiX

    Do you want a cooldown on the gun?

    -The_IcATaRiX
     
  3. Offline

    MisterPhiloe

    Yeah :D
     
  4. Offline

    The_IcATaRiX

    I did it for you, you just have to change some things if it isn't your main class, create Main as an instance etc. You should know that! :)

    Works for me (also change the cooldown to how you want it):

    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.Sound;
    8. import org.bukkit.entity.Firework;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13.  
    14. public class FireworkShootEvent {
    15.  
    16. private List<Player> cooldown = new ArrayList();
    17.  
    18. @EventHandler
    19. public void onShootDiamond(PlayerInteractEvent event){
    20. Action action = event.getAction();
    21. final Player player = event.getPlayer();
    22. Location loc = player.getLocation();
    23.  
    24. if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK ){
    25. if (player.getItemInHand().getType() == Material.DIAMOND_HOE){
    26. if (!this.cooldown.contains(player)){
    27. event.setCancelled(true);
    28. Player playerlaunching = (player);
    29. Firework arr = (Firework) playerlaunching.getWorld().spawn(playerlaunching.getLocation().add(0, 1, 0), Firework.class);
    30. arr.setVelocity(playerlaunching.getLocation().getDirection().normalize());
    31. loc.getWorld().playSound(loc, Sound.ITEM_PICKUP, 1, 0);
    32. this.cooldown.add(player);
    33. Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(),
    34. new Runnable()
    35. {
    36. public void run() {
    37. FireworkShootEvent.this.cooldown.remove(player);
    38. }
    39. }
    40. , 400L);
    41. }
    42. }
    43. }
    44. }
    45. }


    -The_IcATaRiX
     
  5. Offline

    MisterPhiloe

    Could you explain what getinstance() does because I very new to Java and BukkitAPI, and would i have to change by the Main.Instance of thing? sorry for asking maybe something really dumb :)
     
  6. Offline

    The_IcATaRiX

    I think this is a external class right? SO do you have a Main class?
     
  7. Offline

    MisterPhiloe

    Yup i have :)
     
  8. Offline

    The_IcATaRiX

    I think it connects the scheduler with the Main class, so if you would put a scheduler in your main class it would be "this"

    BTW: Did it work?
     
  9. Offline

    MisterPhiloe

    The_IcATaRiX
    But if I connect Main with the Main class it gives an eclipse fault under getinstance. is there something that has to be added in the main class? Couldn't check because i cant export it with errors
     
  10. Offline

    The_IcATaRiX

    @MasterPhiloe
    Can you post your main class please?
     
  11. Offline

    MisterPhiloe

  12. Offline

    The_IcATaRiX

    ahh i see it's

    Code:java
    1. pm.registerEvents(new FireworkShootEvent(), this);
    2.  
    3. //not (the registered event has to have the same name as the class.)
    4.  
    5. pm.registerEvents(new Playerinteract(), this);
     
  13. Offline

    MisterPhiloe

  14. Offline

    The_IcATaRiX

    Is your class called "Main" or "main"?
     
  15. Offline

    MisterPhiloe

    @The_IcATaRiX
    Main

    Anyone who can help?

    Okey so i figured out that i have to add something like this in the main class
    Code:java
    1. public static Main getInstance() {
    2. return getInstance();
    3. }


    But where and how? plz can anyone help me xD

    Bumpie dumbie

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

    The_IcATaRiX

    I got it for you

    Main
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.event.Listener;
    3. import org.bukkit.plugin.PluginManager;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin {
    7.  
    8. private static Main plugin;
    9.  
    10. public void onEnable() {
    11.  
    12. plugin = this;
    13. Bukkit.getLogger().info("Plugin has been enabled");
    14. PluginManager pm = getServer().getPluginManager();
    15. pm.registerEvents(new FireworkShootEvent(), getInstance());
    16. }
    17. public void onDisable() {
    18.  
    19. Bukkit.getLogger().info("Plugin has been disabled");
    20. }
    21. public static Main getInstance() {
    22. return plugin;
    23. }
    24. }
    25.  


    FireworkShootEvent class:
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.List;
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.Sound;
    9. import org.bukkit.entity.Firework;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15.  
    16. public class FireworkShootEvent implements Listener {
    17.  
    18. private List<Player> cooldown = new ArrayList();
    19.  
    20. @EventHandler
    21. public void onShootDiamond(PlayerInteractEvent event){
    22. Action action = event.getAction();
    23. final Player player = event.getPlayer();
    24. Location loc = player.getLocation();
    25.  
    26. if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK ){
    27. if (player.getItemInHand().getType() == Material.DIAMOND_HOE){
    28. if (!this.cooldown.contains(player)){
    29. event.setCancelled(true);
    30. Player playerlaunching = (player);
    31. Firework arr = (Firework) playerlaunching.getWorld().spawn(playerlaunching.getLocation().add(0, 1, 0), Firework.class);
    32. arr.setVelocity(playerlaunching.getLocation().getDirection().normalize());
    33. loc.getWorld().playSound(loc, Sound.ITEM_PICKUP, 1, 0);
    34. this.cooldown.add(player);
    35. Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(),
    36. new Runnable()
    37. {
    38. public void run() {
    39. FireworkShootEvent.this.cooldown.remove(player);
    40. }
    41. }
    42. , 400L);
    43. }
    44. }
    45. }
    46. }
    47. }


    Works for me.

    -The_IcATaRiX
     
  17. Offline

    MisterPhiloe

    @The_IcATaRiX
    I <3 you (no homo).
    Thanks man it works, Thank you so much for helping me ;)
     
  18. Offline

    The_IcATaRiX

    No problem. :)
     
Thread Status:
Not open for further replies.

Share This Page