javastaticminecraftnon-staticminecraft-fabric

Retrieving the position of the PlayerEntity class (Fabric)


I am trying to make a very (at least I think) simple mod for Minecraft that calculates the coordinates of the nearest stronghold. For this, I need the player's position as well as its jaw. However, I am struggling to even retrieve these simple values.

Here's the code:

import net.minecraft.entity.player.PlayerEntity;

public class DebugScreenRetriever {
    
    public void RetrieveData(){
        double x = PlayerEntity.getX();
        double z = PlayerEntity.getZ();
        double yaw = PlayerEntity.getYaw();
    }
}

Keep in mind that I am very (VERY) new to java programming and have only programmed in python before.

The problem is that I am referencing a non-static method from a static context which I've read into and kind of understand but still am unsure how to resolve.

Preferably I would like this mod to work both singleplayer and multiplayer and to only be client-sided. This should theoretically be possible as all the information you need is on the F3 debug screen which every player can access. Therefore I would appreciate it if you point out any flaws I've made so far that would prevent me having it client-sided. Thanks!


Solution

  • You need to have an instance of the class to access the position. For example, to access the local player's instance you can use this:

    PlayerEntity player = MinecraftClient.getInstance().player;
    player.getX();
    player.getY();
    player.getYaw();
    

    Keep in mind that this only works on the client side.