我如何获得玩家正在看的方块的坐标?

问题描述 投票:1回答:1

我想获得玩家正在看的方块的坐标。我尝试用:

double x = player.getLookVec().getX();
double y = player.getLookVec().getY();
double z = player.getLookVec().getZ();

但是这些数字总是在0、0、0和1、1、1之间,所以我没有得到块的坐标。如何获得块的确切坐标?

更多代码:

@Mod.EventBusSubscriber (modid = FirstMod.MOD_ID, bus = Bus.FORGE)
public class RightClickBlock {  

    static String Player1 = null;
    static String Player2 = null;

    @SubscribeEvent 
    public static void on(FOVUpdateEvent event) 
    {
        LivingEntity player = event.getEntity();

        double x = player.getLookVec().getX();
        double y = player.getLookVec().getY();
        double z = player.getLookVec().getZ(); `
    }
}
java minecraft minecraft-forge
1个回答
0
投票

您只能在客户端上获得此消息,因此,如果要在服务器上使用它,则需要让客户端将数据包与此一起发送到服务器。无论如何,这是您的操作方式:

RayTraceResult lookingAt = Minecraft.getMinecraft().objectMouseOver;
if (lookingAt != null && lookingAt.typeOfHit == RayTraceResult.Type.BLOCK) {
    BlockPos pos = lookingAt.getBlockPos();
} else {
    // not looking at a block, or too far away from one to tell
}
© www.soinside.com 2019 - 2024. All rights reserved.