Wait Certain Number of ticks

Discussion in 'Plugin Development' started by AXCoding, Jul 19, 2014.

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

    AXCoding

    Hi, I'm making a plugin in which a player is shot up into the sky when they step on a redstone block, then after 1.5 seconds the console issues the command /spawn %player%. I can't seem to figure out how to do the 1.5 second wait before doing the bukkit.dispatchCommand().
    Main Class:
    Code:java
    1. package me.JasonBourne685.playerShooter;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5.  
    6. import org.bukkit.plugin.PluginDescriptionFile;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class playerShooter extends JavaPlugin{
    10. public final Logger logger = Logger.getLogger("Minecraft");
    11. public static playerShooter plugin;
    12. @Override
    13. public void onDisable() {
    14. PluginDescriptionFile pdfFile = this.getDescription();
    15. this.logger.info(pdfFile.getName() + " has been DISABLED!");
    16. }
    17.  
    18. @Override
    19. public void onEnable() {
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. getServer().getPluginManager().registerEvents(new playerShooterListener(), this);
    22. getConfig().options().copyDefaults(true);
    23. saveConfig();
    24. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been ENABLED!");
    25. }
    26. }


    Listener:
    Code:java
    1. package me.JasonBourne685.playerShooter;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Effect;
    5. import org.bukkit.Location;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerMoveEvent;
    9. import org.bukkit.util.Vector;
    10.  
    11. public class playerShooterListener implements Listener{
    12.  
    13. @SuppressWarnings("deprecation")
    14. @EventHandler
    15. public void playerShot(PlayerMoveEvent e){
    16. if(e.getPlayer().hasPermission("playershot.shoot")){
    17. Location blockUnder = e.getPlayer().getLocation().add(0.0D, -1.0D, 0.0D);
    18. if(blockUnder.getBlock().getTypeId() == 152){
    19.  
    20. e.getPlayer().setVelocity(new Vector(0.1D, 4.0D, 0.0D));
    21. e.getPlayer().getWorld().playEffect(e.getPlayer().getLocation(), Effect.POTION_BREAK, 1);
    22. }
    23.  
    24. }
    25. }
    26. }
    27.  
     
  2. Offline

    mythbusterma

    Create a BukkitRunnable and then call runTaskDelayed() (or whatever that method is called) on it.
     
  3. Offline

    jeussa

    For this you'll need to use the bukkit scheduler.

    This is the way I always do it:

    First we need to add a variable 'instance' to our mainclass, and give it a value at the 'onLoad()' method. Here's an example:
    Code:java
    1. //Change 'MainClass' to the name of your main-class
    2. public static MainClass instance;
    3.  
    4. public void onLoad(){
    5. instance = this;
    6. }



    Then we create a method inside the main-class so we can easily schedule tasks.

    There are four types of tasks you can schedule like this one. I always try to only use the Sync ones.
    - AsyncDelayedTask
    - SyncDelayedTask
    - AsyncRepeatingTask
    - SyncRepeatingTask

    Code:java
    1. //Returns with the task ID
    2. public static int scheduleSyncDelayedTask(Runnable runnable, int delay){
    3. return Bukkit.getScheduler().scheduleSyncDelayedTask(instance, runnable, delay);
    4. }



    Now we can simply create a delayed task wherever we want. Use the following:

    Code:java
    1. MainClass.scheduleSyncDelayedTask(new Runnable(){
    2. public void run(){
    3. //TODO add your code in here (the task which will be performed later)
    4. }, delayInTicks);


    Please note that 1 tick is 20 seconds. So if you want a delay of 1,5 seconds you do 1,5*20 = 30 ticks

    Also if you have any variables inside a method which should take part of the delayed task, place 'final' in front of them.

    Example:
    Code:java
    1. final String message = "Hi dere";
    2. //TODO schedule task in here

    You can now use the variable 'message' because bukkit will now remember instead of deleting after the method has been performed
     
  4. Offline

    mythbusterma

    jeussa

    Why force him to use all that unnecessary code, the unnecessay singleton, and the unnecessary method? Furthermore you don't use a sync method "whenever you can," you use a synchronous call whenever it is necessary.

    Much simpler code:

    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run () {
    4. // do stuff
    5. }
    6. }.runTaskLater(Plugin plugin, int delay);


    Where delay is in ticks.
     
    Zupsub likes this.
  5. Corrected.
     
  6. Offline

    AXCoding

    jeussa Thanks for the help... but I'm still having some problems... Could you quickly run through my code..

    MainClass -playerShooter:
    Code:java
    1. package me.JasonBourne685.playerShooter;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5.  
    6.  
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class playerShooter extends JavaPlugin{
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14. public static playerShooter plugin;
    15. @Override
    16. public void onDisable() {
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + " has been DISABLED!");
    19. }
    20.  
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. getServer().getPluginManager().registerEvents(new playerShooterListener(), this);
    25. getConfig().options().copyDefaults(true);
    26. saveConfig();
    27. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been ENABLED!");
    28. }
    29. public static playerShooter instance;
    30.  
    31. public void onLoad(){
    32. instance = this;
    33. }
    34. public static int scheduleSyncDelayedTaks(Runnable runnable, int delay){
    35. return Bukkit.getScheduler().scheduleSyncDelayedTask(instance, runnable, delay);
    36. }
    37.  
    38.  
    39. }
    40.  


    Listener Class:
    Code:java
    1. package me.JasonBourne685.playerShooter;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Effect;
    5. import org.bukkit.Location;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerMoveEvent;
    9. import org.bukkit.util.Vector;
    10.  
    11. public class playerShooterListener implements Listener{
    12.  
    13. @SuppressWarnings("deprecation")
    14. @EventHandler
    15. public void playerShot(PlayerMoveEvent e){
    16. if(e.getPlayer().hasPermission("playershot.shoot")){
    17. Location blockUnder = e.getPlayer().getLocation().add(0.0D, -1.0D, 0.0D);
    18. if(blockUnder.getBlock().getTypeId() == 152){
    19.  
    20. e.getPlayer().setVelocity(new Vector(0.1D, 4.0D, 0.0D));
    21. e.getPlayer().getWorld().playEffect(e.getPlayer().getLocation(), Effect.POTION_BREAK, 1);
    22. playerShooter.scheduleSyncDelayedTask(new Runnable(){
    23. public void run(){
    24.  
    25. }, delayInTicks);
    26. }
    27. }
    28.  
    29. }
    30. }
    31. }
    32.  



    My listener class is showing the following errors - Line 25 Syntax Error enter } to complete class body
    Line 29 Syntax error on token } delete this token
     
  7. Offline

    mythbusterma

    AXCoding

    See my post above on why jessua's code was incorrect.

    Also: it specifically says "delete this token," why don't you just delete the bracket like it says to.
     
  8. Offline

    AXCoding

    mythbusterma because it throws even more errors when I tried to delete it...

    A question with your code, when I implement the code in my listener, it throws an Error for "Plugin plugin"
    Error : Multiple markers at this line
    - Syntax error on token "plugin", delete this token
    - Plugin cannot be resolved to a variable
    Code:java
    1. package me.JasonBourne685.playerShooter;
    2.  
    3. import org.bukkit.Effect;
    4. import org.bukkit.Location;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerMoveEvent;
    8. import org.bukkit.util.Vector;
    9.  
    10. public class playerShooterListener implements Listener{
    11.  
    12. @SuppressWarnings("deprecation")
    13. @EventHandler
    14. public void playerShot(PlayerMoveEvent e){
    15. if(e.getPlayer().hasPermission("playershot.shoot")){
    16. Location blockUnder = e.getPlayer().getLocation().add(0.0D, -1.0D, 0.0D);
    17. if(blockUnder.getBlock().getTypeId() == 152){
    18. e.getPlayer().setVelocity(new Vector(0.1D, 4.0D, 0.0D));
    19. e.getPlayer().getWorld().playEffect(e.getPlayer().getLocation(), Effect.POTION_BREAK, 1);
    20. new BukkitRunnable() {
    21. @Override
    22. public void run () {
    23. // do stuff
    24. }
    25. }.runTaskLater(Plugin plugin, 30);
    26. }
    27. }
    28. }
    29. }
    30.  
     
  9. Offline

    mythbusterma

    AXCoding

    That's because "Plugin plugin" is a place holder, you need to replace it with a reference to your main class.
     
  10. Offline

    AXCoding

    mythbusterma I'm sorry for being so bad at this :( ...I just started.. How do I reference my main class?
     
  11. Offline

    mythbusterma

    AXCoding

    The most common way of doing so is by passing an instance of your main class into a constructor for your listener (or other auxiliary classes).

    I.e.
    Code:java
    1. private JavaPlugin parent;
    2.  
    3. public ListenerClass(JavaPlugin parent) {
    4. this.parent = parent;
    5. }


    Then you can use "parent" to represent your main class anywhere in your auxiliary class.
     
  12. Offline

    AXCoding

  13. Offline

    jeussa

    mythbusterma
    Your code does not show how to add the 'plugin' variable.

    AXCoding
    At the code you showed containing my method you called the variable 'plugin' and used the name 'instance' in the method.
     
  14. Offline

    mythbusterma

    jeussa
    Because one could reasonably assume that, if you know Java, you would know how to replace the place-holder.
    If you look at my later posts, I did tell him how to utilize constructors to obtain an instance (in this case, the correct way of obtaining an instance of it).
     
Thread Status:
Not open for further replies.

Share This Page