Solved Teleport Requesting

Discussion in 'Plugin Development' started by Astrophylite, Sep 11, 2015.

Thread Status:
Not open for further replies.
  1. Hey all,

    I'm back again, this time with hopefully a more simple thing. I have everything set up, but I am just wondering how I would actually make the two players get teleported. I have already searched Google for something and decompiled some plugins, but none of the code is easy enough for me to implement into my code. I have set up the whole command stuff, but I cannot work out how to get the player to get teleported.
    Here is the code:
    Code:
    package me.zircon.zirconessentials.commands.teleport.request;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.zircon.zirconessentials.miscellaneous.Utils;
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class RequestMain implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        HashMap<Player, Player> tpRequests = new HashMap<Player, Player>();
           
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only ingame players can use /" + label + "!");
                return false;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("tprequest")) {
                if(!sender.hasPermission("zirconessentials.teleport.ask")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to request to teleport to other players!");
                    return false;
                } else {
                    if(args.length == 0 || args.length > 1) {
                        sender.sendMessage(ChatColor.RED + "Usage: /" + label + " <player>");
                        return false;
                    } else {
                        final Player target = Bukkit.getServer().getPlayer(args[0]);
                        if(target == null) {
                            p.sendMessage(ChatColor.RED + "That player cannot be found!");
                            return false;
                        } else {
                            int delay = settings.getConfig().getInt("request-time-out");
                            target.sendMessage(ChatColor.AQUA + p.getDisplayName() + " wants to teleport to you.");
                            target.sendMessage(ChatColor.AQUA + "To accept it, type: " + ChatColor.RED + "/tpaccept");
                            target.sendMessage(ChatColor.AQUA + "To deny it, type: " + ChatColor.RED + "/tpdeny");
                            target.sendMessage(ChatColor.AQUA + "The request will expire in " + delay + " seconds!");
                            tpRequests.put(p, target);
                            Bukkit.getScheduler().scheduleSyncDelayedTask(Utils.instance(), new Runnable() {
                                public void run() {
                                    tpRequests.remove(p, target);
                                }
                            }, delay*20);
                        }
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("tpaccept")) {
                if(!sender.hasPermission("zirconessentials.teleport.accept")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to accept teleport requests!");
                    return false;
                } else {
                   
                }
            }
            if(cmd.getName().equalsIgnoreCase("tpdeny")) {
                if(!sender.hasPermission("zirconessentials.teleport.deny")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to deny teleport requests!");
                    return false;
                } else {
                   
                }
            }
            return false;
        }
    }
    
     
  2. Offline

    Halmerson

    @_zircon_
    Not gonna lie, I didn't understand your whole message because I didn't see you before post, but if you trying to teleport a player, you need to create a location, then save it in a hashmap, then teleport that player to the locatipn. Again, I'm sorry if this isn't what you wanted, but for future reference, not everyone sees your other posts
     
  3. @Halmerson Thank you! Don't worry about that part, I was mainly talking to the people who helped me like every time I post :p

    EDIT: @Halmerson I am trying to get the checking working, but it just keeps returning saying that I have no available teleport requests? Here is the code:
    Code:
    package me.zircon.zirconessentials.commands.teleport.request;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.zircon.zirconessentials.miscellaneous.Utils;
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class TeleportRequest implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        public static Player target;
        public static Player p;
        public static HashMap<Player, Player> tpRequests = new HashMap<Player, Player>();
           
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only ingame players can use /" + label + "!");
                return false;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("tprequest")) {
                if(!sender.hasPermission("zirconessentials.teleport.ask")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to request to teleport to other players!");
                    return false;
                } else {
                    if(args.length == 0 || args.length > 1) {
                        sender.sendMessage(ChatColor.RED + "Usage: /" + label + " <player>");
                        return false;
                    } else {
                        final Player target = Bukkit.getServer().getPlayer(args[0]);
                        if(target == null) {
                            p.sendMessage(ChatColor.RED + "That player cannot be found!");
                            return false;
                        } else {
                            int delay = settings.getConfig().getInt("request-time-out");
                            p.sendMessage(ChatColor.GREEN + "Request sent!");
                            target.sendMessage(ChatColor.AQUA + p.getDisplayName() + " wants to teleport to you.");
                            target.sendMessage(ChatColor.AQUA + "To accept it, type: " + ChatColor.RED + "/tpaccept");
                            target.sendMessage(ChatColor.AQUA + "To deny it, type: " + ChatColor.RED + "/tpdeny");
                            target.sendMessage(ChatColor.AQUA + "The request will expire in " + delay + " seconds!");
                            tpRequests.put(p, target);
                            Bukkit.getScheduler().scheduleSyncDelayedTask(Utils.instance(), new Runnable() {
                                public void run() {
                                    tpRequests.remove(p, target);
                                    p.sendMessage(ChatColor.RED + "Your teleport request has expired!");
                                }
                            }, delay*20);
                        }
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("tpaccept")) {
                if(!p.hasPermission("zirconessentials.teleport.accept")) {
                    p.sendMessage(ChatColor.RED + "You do not have permission to accept teleport requests!");
                    return false;
                } else {
                    if(tpRequests.containsKey(target)) {
                        World w = Bukkit.getServer().getWorld(p.getLocation().getWorld().getName());
                        double x = p.getLocation().getX();
                        double y = p.getLocation().getY();
                        double z = p.getLocation().getZ();
                        target.teleport(new Location(w, x, y, z));
                        target.sendMessage(ChatColor.GREEN + "You were teleported to " + p.getDisplayName());
                        p.sendMessage(ChatColor.GREEN + "The teleport request to " + target.getName() + " was accepted!");
                        return true;
                    } else {
                        p.sendMessage(ChatColor.RED + "You have no pending teleport requests!");
                        return false;
                    }
                }
            }
            return false;
        }
    }
    
     
    Last edited: Sep 11, 2015
  4. Offline

    Gamesareme

    @_zircon_ Why is the target static? That is wrong, and it also means that only one person will every be able to teleport at once.


    If I was you when a player types tpaccept I would use a for loop, and run through all the players in the hashmap, grave the player they have after them, and then see if this player is the same as the player accepting the request. If he is the same then you can grab the target with out having to use that static. Also you really should use the player unique id rather than the player object, because what you have can cause memory leeks.

    This code has not been changed to use the player unique id. YOU must do this.

    Code:
    for(Player player : playershash.keySet()){
                if(playershash.get(player) == null){ //Replace null with the player who is accepting the request.
                    Player accepter = (Player) sender;
                    Player personWantingTelleport = playershash.get(player);
                    personWantingTelleport.teleport(accepter.getLocation());
                }
            }
    I have not tested this, but it is to give you an idea of how to get the sender and the accepter with out using improper static.
     
    Last edited: Sep 11, 2015
  5. Offline

    RoboticPlayer

    Instead of doing
    Code:
    World w = Bukkit.getServer().getWorld(p.getLocation().getWorld().getName());
                        double x = p.getLocation().getX();
                        double y = p.getLocation().getY();
                        double z = p.getLocation().getZ();
                        target.teleport(new Location(w, x, y, z));
    You can (much more easily) do
    Code:
    target.teleport(p.getLocation());
     
  6. -sigh-
    Still not working.
    Updated code:
    Code:
    package me.zircon.zirconessentials.commands.teleport.request;
    
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.zircon.zirconessentials.miscellaneous.Utils;
    import me.zircon.zirconessentials.other.SettingsManager;
    
    public class TeleportRequest implements CommandExecutor {
    
        SettingsManager settings = SettingsManager.getInstance();
        Player target;
        Player p;
        HashMap<UUID, UUID> tpRequests = new HashMap<UUID, UUID>();
           
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only ingame players can use /" + label + "!");
                return false;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("tprequest")) {
                if(!sender.hasPermission("zirconessentials.teleport.ask")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to request to teleport to other players!");
                    return false;
                } else {
                    if(args.length == 0 || args.length > 1) {
                        sender.sendMessage(ChatColor.RED + "Usage: /" + label + " <player>");
                        return false;
                    } else {
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if(target == null) {
                            p.sendMessage(ChatColor.RED + "That player cannot be found!");
                            return false;
                        } else {
                            Player owner = Bukkit.getPlayer("_Zircon_");
                            int delay = settings.getConfig().getInt("request-time-out");
                            p.sendMessage(ChatColor.GREEN + "Request sent!");
                            target.sendMessage(ChatColor.AQUA + p.getDisplayName() + " wants to teleport to you.");
                            target.sendMessage(ChatColor.AQUA + "To accept it, type: " + ChatColor.RED + "/tpaccept");
                            target.sendMessage(ChatColor.AQUA + "To deny it, type: " + ChatColor.RED + "/tpdeny");
                            target.sendMessage(ChatColor.AQUA + "The request will expire in " + delay + " seconds!");
                            tpRequests.put(p.getUniqueId(), target.getUniqueId());
                            owner.sendMessage(ChatColor.GREEN + "The new list of players is:\n" + tpRequests.toString());
                            Bukkit.getScheduler().scheduleSyncDelayedTask(Utils.instance(), new Runnable() {
                                public void run() {
                                    tpRequests.remove(p.getUniqueId(), target.getUniqueId());
                                    p.sendMessage(ChatColor.RED + "Your teleport request has expired!");
                                }
                            }, delay*20);
                        }
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("tpaccept")) {
                if(!sender.hasPermission("zirconessentials.teleport.accept")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to accept teleport requests!");
                    return false;
                } else {
                    if(tpRequests.containsKey(p.getUniqueId())) {
                        target.teleport(p.getLocation());
                        target.sendMessage(ChatColor.GREEN + "You were teleported to " + p.getDisplayName());
                        p.sendMessage(ChatColor.GREEN + "The teleport request to " + target.getName() + " was accepted!");
                        tpRequests.remove(p.getUniqueId(), target.getUniqueId());
                        return true;
                    } else {
                        p.sendMessage(ChatColor.RED + "You have no pending teleport requests!");
                        return false;
                    }
                }
            }
            return false;
        }
    }
    
     
  7. Offline

    SyTeck

    The problem is when you add and get the player from the HashMap.

    You are currently adding the sender (requester) as the key and the target as value, while checking if the target is a key.
    This is where you're problems are:
    Code:
    tpRequests.put(p.getUniqueId(), target.getUniqueId());
    
    if(tpRequests.containsKey(p.getUniqueId())) {
     
  8. -snip-

    EDIT: I changed it back to my old code, but it is erroring on line 67 and line 67 is:
    Code:
    if(tpRequests.containsKey(target.getUniqueId())) {
    The Code:
    Code:
    package me.zircon.zirconessentials.commands.teleport.request;
    import java.util.HashMap;
    import java.util.UUID;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import me.zircon.zirconessentials.miscellaneous.Utils;
    import me.zircon.zirconessentials.other.SettingsManager;
    public class TeleportRequest implements CommandExecutor {
        SettingsManager settings = SettingsManager.getInstance();
        Player target;
        Player p;
        HashMap<UUID, UUID> tpRequests = new HashMap<UUID, UUID>();
          
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "Only ingame players can use /" + label + "!");
                return false;
            }
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("tprequest")) {
                if(!sender.hasPermission("zirconessentials.teleport.ask")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to request to teleport to other players!");
                    return false;
                } else {
                    if(args.length == 0 || args.length > 1) {
                        sender.sendMessage(ChatColor.RED + "Usage: /" + label + " <player>");
                        return false;
                    } else {
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        if(target == null) {
                            p.sendMessage(ChatColor.RED + "That player cannot be found!");
                            return false;
                        } else {
                            Player owner = Bukkit.getPlayer("_Zircon_");
                            int delay = settings.getConfig().getInt("request-time-out");
                            p.sendMessage(ChatColor.GREEN + "Request sent!");
                            target.sendMessage(ChatColor.AQUA + p.getDisplayName() + " wants to teleport to you.");
                            target.sendMessage(ChatColor.AQUA + "To accept it, type: " + ChatColor.RED + "/tpaccept");
                            target.sendMessage(ChatColor.AQUA + "To deny it, type: " + ChatColor.RED + "/tpdeny");
                            target.sendMessage(ChatColor.AQUA + "The request will expire in " + delay + " seconds!");
                            tpRequests.put(p.getUniqueId(), target.getUniqueId());
                            owner.sendMessage(ChatColor.GREEN + "The new list of players is:\n" + tpRequests.toString());
                            Bukkit.getScheduler().scheduleSyncDelayedTask(Utils.instance(), new Runnable() {
                                public void run() {
                                    tpRequests.remove(p.getUniqueId(), target.getUniqueId());
                                    p.sendMessage(ChatColor.RED + "Your teleport request has expired!");
                                }
                            }, delay*20);
                        }
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("tpaccept")) {
                if(!sender.hasPermission("zirconessentials.teleport.accept")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to accept teleport requests!");
                    return false;
                } else {
                    if(tpRequests.containsKey(target.getUniqueId())) {
                        target.teleport(p.getLocation());
                        target.sendMessage(ChatColor.GREEN + "You were teleported to " + p.getDisplayName());
                        p.sendMessage(ChatColor.GREEN + "The teleport request to " + target.getName() + " was accepted!");
                        tpRequests.remove(p.getUniqueId(), target.getUniqueId());
                        return true;
                    } else {
                        p.sendMessage(ChatColor.RED + "You have no pending teleport requests!");
                        return false;
                    }
                }
            }
            return false;
        }
    }
     
    Last edited: Sep 12, 2015
  9. Offline

    SyTeck

    @_zircon_, you have to tag me next time so I can see that you posted something :p

    Can you show me the error message?
     
  10. @SyTeck Sorry, I have been working on other stuff and haven't found the time to respond! I am just appending some changes to the plugin, then I will post the error code.
     
  11. Offline

    Zombie_Striker

    I would suggest not hard coding your name into your plugin. Even though (I'm assuming) this is just for your server, you should have the owner stored in a config instead of hard coding the name into the plugin.

    I think you need to store the p's instance and the target instance IN the delayed task.
    This is in /tpaccept, but you don't have a target field that is accessible to this part of your method. If you're using an IDE, this should come up as red/underlined.

    Also found under tpaccept. You put in Sender(Player1), and the target (Player2) when the player1 sends /tparequest and then removes it by removing the Sender (PLAYER2), and the target(PLAYER1) when player2 sends /tparequest. This would mean that the key Player1 NEVER gets removed unless player1 accepts a tp request.

    I can tell at this point you copied and pasted a few sections of your code without making the changes needed. Please go through your code and revise your code (I would suggest making comments above each line about what each line does. That way you can see were logic does not work.)
     
  12. @Zombie_Striker
    Thanks! However, my mind just exploded about your explanation for the /tpaccept, but I managed to work it out xD!

    EDIT: Oh and the thing about hardcoding my name into the plugin is for testing purposes, it will get removed later on :p
    EDIT2: I have made the target field, I just don't think I posted the updated version ;)
    EDIT3: Just saying, I haven't copy and pasted all of this, it's just this was made before I got my head around the good ways of formatting and all that stuffs.

    Okay, I've got this working now. I had to create a whole new object xD
    I can't remember who I got the code from but +1 to them :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page