Solved Get location from another class

Discussion in 'Plugin Development' started by Jaaakee224, Feb 23, 2014.

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


    I would like to pull the location from another class, to be able to teleport.

    AFK Class (Only Command Methods Shown)
    1. Player player = (Player)sender;
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    4. if (sender instanceof Player){
    6. Player p = (Player)sender;
    7. if (cmd.getName().equalsIgnoreCase("setafk")) {
    8. if(sender.hasPermission("setafk.allow")) {
    9. getConfig().set("", p.getLocation().getWorld().getName());
    10. getConfig().set("hub.x", Double.valueOf(p.getLocation().getX()));
    11. getConfig().set("hub.y", Double.valueOf(p.getLocation().getY()));
    12. getConfig().set("hub.z", Double.valueOf(p.getLocation().getZ()));
    13. getConfig().set("hub.yaw", Float.valueOf(p.getLocation().getYaw()));
    14. getConfig().set("hub.pitch", Float.valueOf(p.getLocation().getPitch()));
    15. saveConfig();
    16. p.sendMessage(ChatColor.GREEN + "AFK Pool location set!");
    17. return true;
    18. }else{
    19. p.sendMessage(ChatColor.RED + "You do not have permission to do that!");
    20. }
    21. }
    23. if (cmd.getName().equalsIgnoreCase("afk")) {
    24. if (getConfig().getConfigurationSection("hub") == null) {
    25. p.sendMessage(ChatColor.RED + "The AFK pool location has not yet been set!");
    26. return true;
    27. }
    28. World w = Bukkit.getServer().getWorld(getConfig().getString(""));
    29. double x = getConfig().getDouble("hub.x");
    30. double y = getConfig().getDouble("hub.y");
    31. double z = getConfig().getDouble("hub.z");
    32. float pitch = (float) getConfig().getDouble("hub.pitch");
    33. float yaw = (float) getConfig().getDouble("hub.yaw");
    34. Location l = new Location(w, x, y, z);
    35. l.setPitch(pitch);
    36. l.setYaw(yaw);
    37. p.teleport(l);
    38. p.sendMessage(ChatColor.GREEN + "You're AFK and have been sent to the pool.");
    39. }
    40. } else {
    41. sender.sendMessage("You must be a player!");
    42. }
    43. return false;
    44. }

    IdleTimer Class (I want to send the player, when he is AFK to the location in the AFK Class, this is not the full class)
    1. @Override
    2. public void run() {
    3. for (Player player : this.plugin.getServer().getOnlinePlayers()) {
    4. if (this.plugin.getLocationMap().containsKey(player.getName())) {
    5. if (this.plugin.getLocationMap().get(player.getName()).equals(player.getLocation()) && !AFK.isPlayerAfk(player.getName())) {
    6. long idleTime = this.plugin.getCfg().idleTime() * 20;
    7. player.setPlayerTime(-idleTime, true);
    8. //this.plugin.becomeAfk(player.getName());
    9. this.plugin.afk(player.getName(), false);
    10. }

    Any help? Thanks! :)
  2. Offline


    If I understand your question correctly, you'd need to associate each location with a player via a global HashMap. Use the player's name as a key and the location as a value. Then just call map.get(playerName) when you need to teleport them.
  3. Offline


    ShadyPotato Can you show me an example please? This is my first time working with this problem :/


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


    Get the set location from the config by doing: getConfig().get("String path");
    Do this for all set locations, then:
    int x = getConfig().getInt("String Path");
    int y = getConfig().getInt("String Path");
    int z = getConfig().getInt("String Path");

    p.teleport(new Location(p.getWorld(), x, y, z));
    Do this for all set variables and you should be done!
  5. Offline


    I think I have it, but let me explain a little more just to show if what you said is correct.

    So I have a main class called AFK and this is part of the code:
    1. World w = Bukkit.getServer().getWorld(getConfig().getString(""));
    2. double x = getConfig().getDouble("hub.x");
    3. double y = getConfig().getDouble("hub.y");
    4. double z = getConfig().getDouble("hub.z");
    5. float pitch = (float) getConfig().getDouble("hub.pitch");
    6. float yaw = (float) getConfig().getDouble("hub.yaw");
    7. Location l = new Location(w, x, y, z);
    8. l.setPitch(pitch);
    9. l.setYaw(yaw);
    10. p.teleport(l);

    Here, it tells me that the variable "l" is the location I have in the configuration, and if I use player.teleport(l) anywhere in the AFK class it would be able to teleport to the location given.

    Is there anyway I can pull the variable off of the AFK class to put it into another class, but not remove it from the main class?
  6. Offline


    In your main class, that extends JavaPlugin, declare your Location.
    1. public static Location loc;

    In your second class do this
    1. import static PathToMainClass.*;

    This'll allow you to use the variable in both classes.
  7. Offline


    Didn't really read the above posts, but anyway. Make a private Location spawn, then in a constructor get the spawn location from the config and you can then do p.teleport<variable here, I'd call it spawn> this is so you don't load the data from the config each time it is needed.
  8. Offline


    Change to this:
    1. int x = getConfig().getInt("kits.spawn.X");
    2. int y = getConfig().getInt("kits.spawn.Y");
    3. int z = getConfig().getInt("kits.spawn.Z");
    5. p.teleport(new Location(p.getWorld(), x, y, z));

    And add the relevant variables like yaw and pitch etc....
    That should work :) Also change the path to the configs.
  9. Offline


  10. Offline


  11. Offline


    Just saying, wouldn't a double be better since a coordinate is a LONGGGGGGGGGGGGGGGGG decimal number? You can always do double and then round it as an int.
  12. Offline


    Yea, but I though that I would make it a little easier as not many people know what double means where as Integer is more well known.
  13. Offline


    I'd say for teleporting players using an integer and double makes no difference as the player gets teleported to middle of block either way. It is only useful to use a double for teleporting a player if you want them to be at the exact place in the block, which in 99.9% of times is unnecessary.

    Double or integer, no difference but easier to use.
  14. Offline



    I didn't even know it teleported. you to the middle of the block.
Thread Status:
Not open for further replies.

Share This Page