Solved Creating a timer/delay before executing more commands.

Discussion in 'Plugin Development' started by CodeAX2, Feb 20, 2017.

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

    CodeAX2

    Hi there! I am trying to create a plugin in which when a player defeats a certain mob, there is a delay (say 2 seconds), and then they are teleported. I have most of what I want to have happen already coded in. The only thing I have no idea how to do is add the delay of 2 seconds before the commands and operations are run. Here is my code:

    Code:
        @EventHandler
        public void onBoss1Death(EntityDeathEvent e) {
            Location loc = e.getEntity().getLocation();
            if (e.getEntity() instanceof WitherSkeleton && loc.getX() <= -70 && loc.getX() >= -100 && loc.getY() <=206 && loc.getY() >= 190 && loc.getZ() >= 163 && loc.getZ() <= 180){
                e.getDrops().clear();
                Entity p = e.getEntity().getKiller();
                Location loc2 = new Location(e.getEntity().getWorld(), -88.5, 202.5, 186.5);
             
                //Timer goes here!!!
             
                p.teleport(loc2);
                Location signLoc = new Location(e.getEntity().getWorld(), -83, 202, 177);
                Sign sign = (Sign) signLoc.getBlock().getState();
               sign.setLine(0, "§4§l[Dungeon]");
               sign.setLine(1, "§eFight the");
               sign.setLine(2, "§eBoss!");
               sign.update();
            }
        }

    What could I do to add the timer?
     
  2. Offline

    Zombie_Striker

    @CodeAX2
    Use Schedulers.

    Also, the "1" in the method name tells me you most likely have other EDE event. You should only have one event per plugin. If you have other events, merge them.
     
  3. Offline

    CodeAX2

    I believe I set up my scheduler correctly and everything. Here is my code:

    Code:
               BukkitTask task = new Timer(this.plugin).runTaskLater(this.plugin, 40);
                p.teleport(loc2);
                Location signLoc = new Location(e.getEntity().getWorld(), -83, 202, 177);
                Sign sign = (Sign) signLoc.getBlock().getState();
               sign.setLine(0, "§4§l[Dungeon]");
               sign.setLine(1, "§eFight the");
               sign.setLine(2, "§eBoss!");
               sign.update();

    Anyways, there are no errors, just a warning about task not being used. So, the question is, how do I use the task so that the player teleport etc. runs after the 40 ticks?
     
  4. Offline

    Zombie_Striker

    @CodeAX2
    What should not bee used? What has the error?
     
  5. Offline

    CodeAX2

    On eclipse it is giving me a warning about task not being used, since it is a variable. So, my question is, how do I use it to give the 40 tick delay?
     
  6. @CodeAX2
    You're correctly using it, the error is somewhere else.

    All the eclipse warning is saying is that the "task" variable is never used, which is just an object which lets you track the task when it's waiting to be executed, but you don't need that here.
     
  7. Offline

    CodeAX2

    There isn't any error on eclipse, just the warning like you said. So then, since I don't need the task there, what should I put in order to actually execute the timer? And where should I put it? Also, to clarify further, I simply want a wait 2 seconds before continuing with the program type thing.
     
  8. Offline

    CodeAX2

    Hey everyone! I got it to work! For anyone out there that is having issues with it, here is my code:

    Code:
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                      public void run() {
                          p.teleport(loc2);
                        Location signLoc = new Location(e.getEntity().getWorld(), -83, 202, 177);
                        Sign sign = (Sign) signLoc.getBlock().getState();
                        sign.setLine(0, "§4§l[Dungeon]");
                        sign.setLine(1, "§eFight the");
                        sign.setLine(2, "§eBoss!");
                        sign.update();
                        Bukkit.getServer().broadcastMessage("This message is broadcast by the main thread");
                      }
                    }, 40L);
     
Thread Status:
Not open for further replies.

Share This Page