gpt4 book ai didi

java - 如何获取玩家正在查看的方 block 的坐标?

转载 作者:行者123 更新时间:2023-12-03 23:27:15 24 4
gpt4 key购买 nike

我想获得玩家正在查看的方块的坐标。我尝试这样做:

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
{

@SubscribeEvent
public static void on(FOVUpdateEvent event)

{
if(player.getHeldItemMainhand().getItem() == Items.BEDROCK)
{
LivingEntity player = event.getEntity();
World worldIn = player.world;

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

worldIn.setBlockState(new BlockPos(x, y, z) , Blocks.BEDROCK.getDefaultState());
}
}
}

最佳答案

我注意到(至少在 1.16 中)Minecraft.getInstance().objectMouseOver.getHitVec()如果玩家从南面、东面或上方看方块,则返回空气。如果数字太大,它会给出旁边的块。此方法使用玩家位置,如果双人结束于 .0 , 并决定什么时候位置需要 -1 .

Minecraft instance = Minecraft.getInstance();

if(instance.objectMouseOver.getType() != RayTraceResult.Type.BLOCK){return;}

Vector3d blockVector = instance.objectMouseOver.getHitVec();

double bX = blockVector.getX(); double bY = blockVector.getY(); double bZ = blockVector.getZ();
double pX = instance.player.getPosX(); double pY = instance.player.getPosY(); double pZ = instance.player.getPosZ();

if(bX == Math.floor(bX) && bX <= pX){bX--;}
if(bY == Math.floor(bY) && bY <= pY+1){bY--;} // +1 on Y to get y from player eyes instead of feet
if(bZ == Math.floor(bZ) && bZ <= pZ){bZ--;}

BlockState block = instance.world.getBlockState(new BlockPos(bX, bY, bZ));

关于java - 如何获取玩家正在查看的方 block 的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61717481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com