Solved Trying to use player.(whatever) without command or event

Discussion in 'Spigot Plugin Development' started by Weber623, Jan 12, 2020.

Thread Status:
Not open for further replies.
  1. I am fairly new to coding, and I made a loop that constantly runs a specific command. It is in its own class and is under public void run() { (code) }. I would like to use the player function, but I am not sure how to get it to register without using a Player player = (Player) sender; or a Player player = event.getPlayer(); and I can't use either because I don't have an event running. It's probably something simple that I couldn't figure out but I couldn't find anything about this. Any help would be appreciated :)
  2. Use
    Code (Java):
    • Useful Useful x 1
  3. Well, you would certainly help us by posting actual code since what you say is not really helpful...
    There are multiple ways of getting a player, the best is dependent on what your circumstances are. Are you in a player-based event? Use event.getPlayer(). Are you in a Command? Use
    Code (Java):
    if (commandSender instanceof Player) {
        Player player = (Player) commandSender;
        // ...
    Do you want to retrieve the Player based on his UUID or name? Use
    Code (Java):
    Do you want to get every online player?
    Code (Text):
    As you see, there is not enough context to exactly know what you want
  4. He’s using a runnable. He makes it obvious when he says “It’s in its own class and is in a public void run()”.
  5. Ah, I see. Examples are only partially valid then
  6. Code (Java):
        public void run(){

            for (Entity entity : player.getWorld().getEntities()) {
                if (entity.getLocation() == player.getEyeLocation()) {
                player.sendMessage("You're looking at an " + entity.getType() + "!");

    Here is the code. I am trying to make something that when a player looks at another player, it executes a command, which right now is just sending a message. The problem is that it won't let me use player because it can't be resolved.
  7. So would I do something like this
    Code (Java):
    Player player = (Player) Bukkit.getOnlinePlayers();
  8. Well, there are, again, multiple Solution strategies. The reason player can‘t be resolved is because you haven‘t defined it anywhere. Depending on what you exactly want (do you want to send this message for every person on the server or just specific people) you could (as @Weber623 said) get all players and then do a double loop where you iterate over every player and get every entity he is looking at. Or you could define it for every player. Then you would have a Field
    Code (Java):
    Player player;
    In the class your runnable is inside. You could then set that player (but be sure to check if it’s not null in your run-method) on for example a player login event. Or you could set it whenever a certain command is executed. There are a lot of possibilities that you could think of.
    • Informative Informative x 1
  9. Strahan


    A lot of problems there. Doing this in a general runnable like that, without being in an event tied to the player, means you have to loop both the entities and the online players (inefficient). Also you are looping all entities (wildly inefficient). You'd be better off looping nearby entities to cut down on the load. I'd do this in a PlayerMoveEvent and only if From/To's block coords haven't changed.

    Also getEyeLocation() doesn't mean what you think it means. It isn't at what they are looking, it's where their eye is.

    For example, even though my character is looking into the distance, this code:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      Player p = (Player)sender;
      p.sendMessage("Your Location: " + p.getLocation().getX() + " / " + p.getLocation().getY() + " / " + p.getLocation().getZ());
      p.sendMessage("Eye Location: " + p.getEyeLocation().getX() + " / " + p.getEyeLocation().getY() + " / " + p.getEyeLocation().getZ());
      return true;
    results in:
    • Useful Useful x 1
  10. Thanks very much for the help. Would something like p.getLineOfSight work for what I wanted?
  11. That returns a List<Block> so no.
    You need to ray trace in the direction the player is looking.
    That's not an easy task for a beginner.

    This is pretty spoonfeedy but i wrote you an example.

    Btw dont copy just broken code.

    Here is what you should do before continuing:
    1) Go here
    2) Watch everything in 'Java Programming Tutorial'
    3) Watch everything in 'Bukkit Coding Tutorial 1.13' or 'Bukkit Coding Tutorial' if you live in 2014
    4) Watch at least 2-3 Vids from 'Advanced Bukkit Coding'
    5) Come back and ask questions
    #11 7smile7, Jan 13, 2020
    Last edited: Jan 13, 2020
    • Winner Winner x 1
  12. Thanks so much for the help. I know I’m kind of a noob at this stuff and I don’t expect to be written a perfect code for me. I didn’t realize doing this would be that difficult. I’ll be sure to watch those tutorials and better learn Java. Once again thank you and I appreciate the help
Thread Status:
Not open for further replies.