Adding a delay to an event

Discussion in 'Plugin Development' started by amitlin14, Aug 8, 2012.

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

    amitlin14

    Code:
        public void teleportDelayHunger(Player player, Player targetPlayer) {
            Location targetLocation = targetPlayer.getLocation();
            player.sendMessage(ChatColor.GRAY + "Teleporting...");
            player.teleport(targetLocation);
            }
    this is the event, I want to add a 5 seconds delay to it but when i do 'this.' it only shows me getClass()...

    help any1?
     
  2. Offline

    Malikk

  3. Offline

    davejavu

    amitlin14
    Code:java
    1. public void teleportDelayHunger(Player player, Player targetPlayer) {
    2. Location targetLocation = targetPlayer.getLocation();
    3. player.sendMessage(ChatColor.GRAY + "Teleporting...");
    4. getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    5. public void run() {
    6. player.teleport(targetLocation);
    7. }
    8. }, 10 x 20L);
    9.  


    All you'd need to change is the timing; 10 x 20L is 10 seconds, as 20 ticks is 1 second, and 10 x 1 = 10.
    By the way, sorry if there are errors, all off the top of my head.
     
  4. Offline

    amitlin14

    I know how to do a delayed task, but what im saying is that when i do this.getServer(), it tells me to create the method getServer()
     
  5. Offline

    davejavu

    amitlin14 If it's your main class: Did you extend JavaPlugin?
    If it's another class; use Bukkit.getServer()
     
  6. Offline

    amitlin14

    So will this work?

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
     
     
    public class TeleportHungerListener implements Listener {
     
        public void TeleportMenuHunger(Player player) {
            player.sendMessage(ChatColor.WHITE + "----" + ChatColor.GREEN + " Teleport " + ChatColor.WHITE + "----");
            player.sendMessage(ChatColor.AQUA + "/tp" + "\t" + ChatColor.WHITE + "Displays this menu");
            player.sendMessage(ChatColor.AQUA + "/tp <player>" + "\t" + ChatColor.WHITE + "Teleport to a player. Costs 3 hunger");
            player.sendMessage(ChatColor.AQUA + "/tphere" + "\t" + ChatColor.WHITE + "Teleport a player to you. Costs 3 hunger.");
        }
     
        public void TeleportMenuNoHunger(Player player) {
            player.sendMessage(ChatColor.WHITE + "----" + ChatColor.GREEN + " Teleport " + ChatColor.WHITE + "----");
            player.sendMessage(ChatColor.AQUA + "/tp" + "\t" + ChatColor.WHITE + "Displays this menu");
            player.sendMessage(ChatColor.AQUA + "/tp <player>" + "\t" + ChatColor.WHITE + "Teleport to a player.");
            player.sendMessage(ChatColor.AQUA + "/tphere" + "\t" + ChatColor.WHITE + "Teleport a player to you.");
        }
     
        public void teleportHunger(Player player, Player targetPlayer) {
            Location targetLocation = targetPlayer.getLocation();
            player.sendMessage(ChatColor.GRAY + "Teleporting...");
            player.teleport(targetLocation);
            player.setFoodLevel(player.getFoodLevel() - 3);
        }
     
        public void teleportNoHunger(Player player, Player targetPlayer) {
            Location targetLocation = targetPlayer.getLocation();
            player.sendMessage(ChatColor.GRAY + "Teleporting...");
            player.teleport(targetLocation);
        }
     
        public void teleportDelayHunger(final Player player, Player targetPlayer) {
            final Location targetLocation = targetPlayer.getLocation();
            player.sendMessage(ChatColor.GRAY + "Teleportation will commence in 5 seconds...");
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
     
                  public void run() {
                        player.sendMessage(ChatColor.GRAY + "Teleporting...");
                        player.teleport(targetLocation);
                        player.setFoodLevel(player.getFoodLevel() - 3);
                  }
                }, 100L);
        }
     
        public void teleportDelayNoHunger(final Player player, Player targetPlayer) {
            final Location targetLocation = targetPlayer.getLocation();
            player.sendMessage(ChatColor.GRAY + "Teleportation will commence in 5 seconds...");
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
     
                  public void run() {
                        player.sendMessage(ChatColor.GRAY + "Teleporting...");
                        player.teleport(targetLocation);
                        player.setFoodLevel(player.getFoodLevel() - 3);
                  }
                }, 100L);
        }
     
        public void teleportHereHunger(Player targetPlayer, Player player) {
            Location playerLocation = player.getLocation();
            targetPlayer.sendMessage(ChatColor.GRAY + "Teleporting...");
            targetPlayer.teleport(playerLocation);
            player.setFoodLevel(player.getFoodLevel() - 3);
        }
     
        public void teleportHereNoHunger(Player targetPlayer, Player player) {
            Location playerLocation = player.getLocation();
            targetPlayer.sendMessage(ChatColor.GRAY + "Teleporting...");
            targetPlayer.teleport(playerLocation);
        }
    }
     
  7. Offline

    davejavu

  8. Offline

    amitlin14

    and how can i use the listeners in another class that's in another package?
     
  9. Offline

    davejavu

  10. Offline

    amitlin14

    Ive read that, but ive got 2 classes with commands: the core(onenable, ondisable and config stuff) and the teleport class. I want to use teleportDelayHunger() in my teleport class but it just tells me to create method named teleportDelayHunger()...

    I dont know how to link them together...

    EDIT: my god... i found out just by playing with it a littlebit o.o

    i did extends TeleportHungerListener on my teleport class

    thanks for the help anyways!
     
Thread Status:
Not open for further replies.

Share This Page