Solved .getLocation() is not a recognized method

Discussion in 'Plugin Development' started by Alias_Me, Jan 13, 2019.

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

    Alias_Me

    Hey, so I just started programming plugins yesterday with my limited knowledge of Java with the endgoal of making a Waypoint system.

    So I wanted to tackle teleportation first, and quickly stumbled upon the getLocation method, which seems to be essential to that.

    So I importet org.bukkit.Location and thought I was good to go, but my IDE (IntelliJ) can`t find that method, or anything from the .Location import really.

    Did I forget something?

    Here`s my Class:
    Code:
    package alias.teleport;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.Location;
    import org.bukkit.event.player.*;
    
    public class CommandClass implements CommandExecutor {
       @Override
       public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
          if(command.getName().equalsIgnoreCase("portal")) {
             System.out.println("geht"); //Old confirmation that the command is recognized
             //Save Old Location
             Location oldloc = sender.getLocation() //<-- IDE cant find this method
             return true;
          }
          return true;
       }
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Alias_Me You need to check if the sender is a Player, then cast to Player, then you can use getLocation
     
  3. Offline

    Alias_Me

    That works, thank you! Probably could have guessed that... :p

    What exactly does casting to a player do though? Like I know how to do it [Player <name> = (Player) sender;] but what exactly does that change? And how/when do I apply that?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Alias_Me It gets the player methods available.
    CommandSender is a base class for ConsoleCommamdSender and Player. Casting determines what type you are gonna use.
    And you apply it when you need player methods.
     
Thread Status:
Not open for further replies.

Share This Page