Warp Delay

Discussion in 'Plugin Development' started by TheAnswerIsMinecraft, Dec 19, 2013.

Thread Status:
Not open for further replies.
  1. So i am coding a /hub command for a factions server and I am wondering how to add delay to the command like essentials warps.
    Code:java
    1. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String args[]){
    2. Player p = (Player)sender;
    3. if (cmd.getName().equalsIgnoreCase("hub")){
    4. p.teleport(this.hubloc);
    5. p.sendMessage(ChatColor.RED + "[DemonicPVP] " + ChatColor.YELLOW + "Teleported to hub!");
    6. }
    7. }
    8.  
     
  2. Offline

    se1by

  3. Offline

    ResultStatic

    @TheAnswerIsMinecraft here is a runnable.
    static Plugin plugin = Bukkit.getPluginManager().getPlugin("urpluginname");
    public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String args[]){
    final Player p = (Player)sender;
    if (cmd.getName().equalsIgnoreCase("hub")){
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { // for plugin u can put it in main and replace it with this.
    public void run() {
    p.teleport(this.hubloc); // stuff that happens when the timer is up
    p.sendMessage(ChatColor.RED + "[DemonicPVP] " + ChatColor.YELLOW + "Teleported to hub!");
    }
    }
    , 100L); //time in 20 ticks per second
    }
    }
    {
    }
    }
     
    TheAnswerIsMinecraft likes this.
  4. Thank you
     
  5. Wait what do you mean by for plugin u can put it in main and replace it?
     
  6. Offline

    ResultStatic

    so static Plugin plugin = Bukkit.getPluginManager().getPlugin("urpluginname"); is getting the plugin but in main you can do "this" instead of having plugin.
     
  7. ResultStatic ResultStatic
    Would i do it like this
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String args[]) {
    2. Player p = (Player) sender;
    3. if (cmd.getName().equalsIgnoreCase("hub")){
    4. if (p.hasPermission("dpvp.hub")){
    5. if (!p.hasPermission("dpvp.hub")){
    6. p.sendMessage(ChatColor.RED + "[DemonicPVP] You do not have access to that command!");
    7. }
    8. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plug, new Runnable() {
    9. p.teleport(this.hubloc);
    10. p.sendMessage(ChatColor.RED + "[DemonicPVP] " + ChatColor.YELLOW + "Teleported to hub!");
    11. }
    12. }
    13. , 100L);
    14. return false;
    15. }
    16. }

    because that returns errors

    EDIT: I got it working using: http://pastebin.com/Q2wgFhZi

    Anyone know how to make the teleportation cancel on player move? Thanks!

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

    ResultStatic

    TheAnswerIsMinecraft for that you are gonna want to have a runnable for 1-2 seconds checking the player cordinates. DONOT use player move event because that will check 20 times a seconds instead of once every couple second on every player on the server causing lag. if look at the essentials afk system it doesnt update right away because it checks every couple seconds.

    TheAnswerIsMinecraft i dont have time to finish this but it would be something like this. you check the player location with a runnable every few seconds. so check the move(Player p) and cancel the teleport if cancel contains the player. this could be totally wrong i didnt check or finish it but its a start
    Code:
    public void move(Player p) {
            Location location1 = p.getLocation().add(1, 1, 1);
            Location location2 = p.getLocation().subtract(1, 1, 1);
          if (p.getLocation().equals(location1));
          if (p.getLocation().equals(location2));
        cancel.add(p.getName();
       
        }
        static Plugin plug = Bukkit.getPluginManager().getPlugin("DemonicPVP");
        public static List<String> cancel = new ArrayList<String>();
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String args[]) {
                final Player p = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("hub")){
                if (p.hasPermission("dpvp.hub")){
                        if (!p.hasPermission("dpvp.hub")){
                                p.sendMessage(ChatColor.RED + "[DemonicPVP] You do not have access to that command!");
                        }
                        p.sendMessage(ChatColor.RED + "[DemonicPVP] " + ChatColor.YELLOW + "Teleportation commencing in 5 seconds!");
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                                private Location hubloc;{
                                        World Spawn = Bukkit.getWorld("Spawn");
                                this.hubloc = new Location(Spawn, 1604, 6, 1381);
                                this.hubloc.setYaw(180);
                             
                                move(p);
                                }
                                @Override
                                public void run() {
                                  if (cancel.contains(p.getName())) {
                                    p.sendMessage("teleport aborted!");
                                    return;
                                  p.teleport(this.hubloc);
                                  p.sendMessage(ChatColor.RED + "[DemonicPVP] " + ChatColor.YELLOW + "Teleported to hub!");
                }
        }
                        }             
                , 100L);
                        }
                }
             
                return false;
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page