How to make a wait timer?

Discussion in 'Plugin Development' started by qwwupp, Aug 4, 2013.

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

    qwwupp

    I'm pretty much brand new to bukkit, and I'm making a small plugin. I need to know how to make the plugin wait for 30 seconds, then do something. I've read the article on Scheduler Programming, but I haven't been able to firmly grasp it. Any help with this would be exceptional. I'm trying to put the timer on line 59 of BlockListener.java



    BlockListener.java
    Code:java
    1. package com.gmail.nomfruitsalad;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.block.BlockFace;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.block.BlockBreakEvent;
    11. import org.bukkit.event.block.BlockPhysicsEvent;
    12. import org.bukkit.event.block.BlockPlaceEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.plugin.Plugin;
    16.  
    17. public class BlockListener implements Listener {
    18.  
    19.  
    20. @EventHandler
    21. public void onBlockPhysics(BlockPhysicsEvent event) {
    22. Block block = event.getBlock();
    23.  
    24. if ((block.getType() == Material.SAND) || (block.getType() == Material.GRAVEL)) {
    25. Block above = block.getRelative(BlockFace.UP);
    26. event.setCancelled(true);
    27. }
    28. }
    29.  
    30. private LeagueOfMinecraft plugin;
    31.  
    32. @EventHandler
    33. public void onBlockPlace(BlockPlaceEvent event) {
    34. GlobalMethods gm = new GlobalMethods();
    35. final Block block = event.getBlock();
    36. Player p = event.getPlayer();
    37. Inventory pInv = p.getInventory();
    38.  
    39. if (block.getType() == Material.COBBLESTONE) {
    40. event.setCancelled(true);
    41. }
    42. }
    43. @EventHandler
    44. public void onBlockBreak(BlockBreakEvent event) {
    45. GlobalMethods gm = new GlobalMethods();
    46. final Block block = event.getBlock();
    47. Player p = event.getPlayer();
    48. Inventory pInv = p.getInventory();
    49. ExperienceManager expMan = new ExperienceManager(p);
    50. //Below is the if where I want to implement the timer
    51. if (block.getType() == Material.IRON_ORE) {
    52. ItemStack ironi = new ItemStack(Material.IRON_INGOT, 2);
    53. pInv.addItem(ironi);
    54. expMan.changeExp(4);
    55. event.setCancelled(true);
    56. block.setType(Material.COBBLESTONE);
    57. p.sendMessage(ChatColor.GREEN + "+2" + ChatColor.WHITE + " Iron Ingots");
    58. p.sendMessage(ChatColor.GREEN + "+4" + ChatColor.DARK_GREEN + " XP");
    59. //right here I want it to wait 30 seconds
    60. //then block.setType(Material.IRON_ORE);
    61.  
    62. }
    63.  
    64. else if (block.getType() == Material.GOLD_ORE) {
    65. expMan.changeExp(8);
    66. event.setCancelled(true);
    67. block.setType(Material.COBBLESTONE);
    68. p.sendMessage(ChatColor.GREEN + "+8" + ChatColor.DARK_GREEN + " XP");
    69.  
    70. }
    71. else if (block.getType() == Material.LOG) {
    72. int ran = gm.random(13, 4);
    73. ItemStack plank = new ItemStack(Material.WOOD, ran);
    74. pInv.addItem(plank);
    75. event.setCancelled(true);
    76. block.setType(Material.COBBLESTONE);
    77. p.sendMessage(ChatColor.GREEN + "+" + ran + ChatColor.WHITE + " Wooden Planks");
    78. }
    79. else if (block.getType() == Material.COBBLESTONE) {
    80. event.setCancelled(true);
    81. }
    82.  
    83. }
    84. }




    LeagueOfMinecraft.java
    Code:java
    1. package com.gmail.nomfruitsalad;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. import org.bukkit.plugin.PluginManager;
    5.  
    6. public class LeagueOfMinecraft extends JavaPlugin {
    7. private final BlockListener bListen = new BlockListener();
    8.  
    9. @Override
    10. public void onDisable() {
    11. getLogger().info("League of Minecraft Shutting Down...");
    12. }
    13.  
    14. @Override
    15. public void onEnable() {
    16. PluginManager pm = getServer().getPluginManager();
    17. pm.registerEvents(bListen, this);
    18. getLogger().info("League of Minecraft Starting Up...");
    19. }
    20. }
    21.  


    bump please help, this is a major point in my plugin, and my work has pretty much came to a screeching halt.

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

    Mycrowut

    qwwupp

    Okay so you will need to make a new public void.
    Just copy and paste what I have below:
    Code:java
    1. public void Timer(final Player player) {
    2. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    3. public void run() {
    4. //code that you want to run in 30 seconds
    5. }
    6. }
    7. },600L); //20 = 1 second | 20*30 = 600 (The "L" also needs to be here)
    8. }


    You will need to change "final Player player" to something such as a location or a block which will get passed through to this function.

    You can use this function by using:
    Code:java
    1. Timer(player);

    But replace "player" with block or whatever you're using.

    This can be a bit confusing but if you need help don't be afraid to ask but please do a lot of trial and error before asking questions.

    Also:
    [​IMG]
     
    qwwupp and Blir like this.
  3. Offline

    qwwupp


    Alright, so I've done that, and like you suggested, I did a bit of troubleshooting, but it keeps coming up with a java.lang.NullPointerException on line 35 (the line I have the Timer at). I think it may be due to me referencing the method from a separate class, but I couldn't see how that would relate to anything "null". Thanks for helping me, I appreciate it a lot.

    The full error is


    The timer code is in my main class for the plugin (LeagueOfMinecraft, where the error is occuring), starting on line 34:

    Code:java
    1. package com.gmail.nomfruitsalad;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.BlockBreakEvent;
    10. import org.bukkit.inventory.Inventory;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.PluginManager;
    15.  
    16. public class LeagueOfMinecraft extends JavaPlugin {
    17. private final Listen xListen = new Listen();
    18.  
    19. @Override
    20. public void onDisable() {
    21. getLogger().info("League of Minecraft Shutting Down...");
    22. }
    23.  
    24. @Override
    25. public void onEnable() {
    26. PluginManager pm = getServer().getPluginManager();
    27. pm.registerEvents(xListen, this);
    28. getLogger().info("League of Minecraft Starting Up...");
    29.  
    30. }
    31.  
    32. Plugin LeagueOfMinecraft;
    33.  
    34. public void Timer(final Block block) {
    35. LeagueOfMinecraft.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    36. public void run() {
    37. //code that you want to run in 30 seconds
    38. block.setType(Material.IRON_ORE);
    39. }
    40. },60L); //20 = 1 second | (doing 60L for testing purposes)
    41. }
    42.  
    43.  
    44. }



    ...and I call the Timer in the Listen class, on line 35:
    Code:java
    1. package com.gmail.nomfruitsalad;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.BlockBreakEvent;
    10. import org.bukkit.inventory.Inventory;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class Listen implements Listener{
    14.  
    15. @EventHandler
    16. public void onBlockBreak(BlockBreakEvent event) {
    17. //declare var + gm
    18. final Block block = event.getBlock();
    19. Player p = event.getPlayer();
    20. Inventory pInv = p.getInventory();
    21. ExperienceManager expMan = new ExperienceManager(p);
    22. LeagueOfMinecraft lm = new LeagueOfMinecraft();
    23.  
    24.  
    25. //begining of if statements (probably should've use a switch or something)
    26. if (block.getType() == Material.IRON_ORE) {
    27. ItemStack ironi = new ItemStack(Material.IRON_INGOT, 2);
    28. pInv.addItem(ironi);
    29. expMan.changeExp(4);
    30. event.setCancelled(true);
    31. block.setType(Material.COBBLESTONE);
    32. p.sendMessage(ChatColor.GREEN + "+2" + ChatColor.WHITE + " Iron Ingots");
    33. p.sendMessage(ChatColor.GREEN + "+4" + ChatColor.DARK_GREEN + " XP");
    34. //call timer
    35. lm.Timer(block);
    36. //then block.setType(Material.IRON_ORE);
    37. }
    38. }}


    What exactly is causing the error (eclipse doesn't see anything wrong)?
     
  4. Offline

    Mycrowut

    qwwupp
    PHP:
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    It seems there is something with your plugin manager that is wrong. I would put the timer function directly into your other class to make things easy - even though this may not be the best coding practice.

    -Mycro
     
  5. Offline

    qwwupp

    Mycrowut
    Alright, I did that, but it's still giving me the NullPointerException:

    It's happening with the timer in my Listen class :
    Code:java
    1. package com.gmail.nomfruitsalad;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockBreakEvent;
    9. import org.bukkit.inventory.Inventory;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.Plugin;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Listen extends JavaPlugin implements Listener {
    15.  
    16.  
    17. Plugin plugin;
    18.  
    19. public void Timer(final Block block) {
    20. //Listen.
    21. getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    22. public void run() {
    23. //code that you want to run in 30 seconds
    24. block.setType(Material.IRON_ORE);
    25. }
    26. },60L);
    27. } //20 = 1 second | (60 for testing purposes)
    28.  
    29. @EventHandler
    30. public void onBlockBreak(BlockBreakEvent event) {
    31. //declare var + gm
    32. final Block block = event.getBlock();
    33. Player p = event.getPlayer();
    34. Inventory pInv = p.getInventory();
    35. ExperienceManager expMan = new ExperienceManager(p);
    36. // Uneeded.. for now
    37. //LeagueOfMinecraft lm = new LeagueOfMinecraft();
    38.  
    39.  
    40.  
    41. //if statements
    42. if (block.getType() == Material.IRON_ORE) {
    43. ItemStack ironi = new ItemStack(Material.IRON_INGOT, 2);
    44. pInv.addItem(ironi);
    45. expMan.changeExp(4);
    46. event.setCancelled(true);
    47. block.setType(Material.COBBLESTONE);
    48. // p.sendMessage(ChatColor.GREEN + "+2" + ChatColor.WHITE + " Iron Ingots");
    49. //p.sendMessage(ChatColor.GREEN + "+4" + ChatColor.DARK_GREEN + " XP");
    50. Timer(block);
    51. //right here I want it to wait 30 seconds
    52. //then block.setType(Material.IRON_ORE);
    53. }
    54. }}


    Here's where I use the plugin manager (in the LeagueOfMinecraft class):


    Code:
    package com.gmail.nomfruitsalad;
     
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.PluginManager;
     
    public class LeagueOfMinecraft extends JavaPlugin {
        private final Listen xListen = new Listen();
       
        @Override
        public void onDisable() {
            getLogger().info("League of Minecraft Shutting Down...");
        }
       
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(xListen, this);
            getLogger().info("League of Minecraft Starting Up...");
           
        }
       
       
       
       
        }
    If it would make it easier to help, I could send you the entire eclipse project, I'm just so frustrated with this, every time I think I've got it working, it doesn't.
     
  6. Offline

    xTrollxDudex

    qwwupp
    You need to initialize the plugin variable and you can't have 2 classes that extend JavaPlugin.
     
  7. Offline

    qwwupp

    xTrollxDudex
    Sorry if this is completely obvious, but how would I initialize the variable? I've changed it like you said so that only one class extends JavaPlugin.
    The class that extends JavaPlugin (after cutting and pasting the timer code into here):

    Code:
    package com.gmail.nomfruitsalad;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
     
    public class LeagueOfMinecraft extends JavaPlugin {
        private final Listen xListen = new Listen();
     
     
        @Override
        public void onDisable() {
            getLogger().info("League of Minecraft Shutting Down...");
        }
     
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(xListen, this);
            getLogger().info("League of Minecraft Starting Up...");
         
        }
     
        Plugin plugin;
     
        public void Timer(final Block block) {
            //Listen.
            getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
            public void run() {
            //code that you want to run in 30 seconds
                block.setType(Material.IRON_ORE);
            }
            },60L);
            } //20 = 1 second | (60 for testing purposes)
     
     
        }
     
  8. Offline

    xTrollxDudex

    qwwupp
    That is correct but if you want the timer in listen,

    In your listener up this:
    PHP:
    LeagueOfMinecraft plugin;
    public 
    Listen(LeagueOfMinecraft plugin){
    this.plugin plugin;
    }
    What this does is when you register your listener, you would pass an instance of the main class to the listener.
    Register like this
    PHP:
    getServer().getPluginManager().registerEvents(new Listen(this), this);
     
  9. Offline

    qwwupp

    xTrollxDudex

    Ah, I see, thanks for the help, but getServer() requires the class to extend JavaPlugin (the reason I was extending it in my listener class in the first place), so I've no choice but to put it in the LeagueOfMinecraft class.
     
  10. Offline

    xTrollxDudex

    qwwupp
    You register in the main class
     
Thread Status:
Not open for further replies.

Share This Page