gpt4 book ai didi

java - 如何用java记录Minecraft玩家的视线位置

转载 作者:行者123 更新时间:2023-12-02 13:09:04 28 4
gpt4 key购买 nike

我知道这个标题很令人困惑。我正在用java制作一个我的世界模组。我制作了一个元素,当你右键单击时,它会爆炸并产生闪电。目前,它会在玩家所在位置爆炸并产生闪电。我怎样才能让它在玩家看到的位置爆炸?我的元素类别目前如下所示:

package com.mod.elliotmod;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ItemMyTNT extends Item {

public ItemMyTNT(String name, int stackSize) {
GameRegistry.registerItem(this, name);
setUnlocalizedName(ElliotMod.MODID + "_" + name);
setCreativeTab(CreativeTabs.tabMisc);
setMaxStackSize(stackSize);
}

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {

world.spawnEntityInWorld(new EntityLightningBolt(world, player.posX, player.posY, player.posZ));
world.createExplosion(player, player.posX, player.posY, player.posZ, 5.0F, true);

return item;
}
}

编辑:这是我现在拥有的:

package com.mod.elliotmod;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ItemMyTNT extends Item {

public ItemMyTNT(String name, int stackSize) {
GameRegistry.registerItem(this, name);
setUnlocalizedName(ElliotMod.MODID + "_" + name);
setCreativeTab(CreativeTabs.tabMisc);
setMaxStackSize(stackSize);
}

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {

double x = player.getLookVec().xCoord;
double y = player.getLookVec().yCoord;
double z = player.getLookVec().zCoord;

world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
world.createExplosion(player, x, y, z, 5.0F, true);

return item;
}
}

我在实体播放器类中找到了这个。可能会有所帮助

        Vec3 vec3 = new Vec3(((double)this.rand.nextFloat() - 0.5D) * 0.1D, Math.random() * 0.1D + 0.1D, 0.0D);
vec3 = vec3.rotatePitch(-this.rotationPitch * (float)Math.PI / 180.0F);
vec3 = vec3.rotateYaw(-this.rotationYaw * (float)Math.PI / 180.0F);
double d0 = (double)(-this.rand.nextFloat()) * 0.6D - 0.3D;
Vec3 vec31 = new Vec3(((double)this.rand.nextFloat() - 0.5D) * 0.3D, d0, 0.6D);
vec31 = vec31.rotatePitch(-this.rotationPitch * (float)Math.PI / 180.0F);
vec31 = vec31.rotateYaw(-this.rotationYaw * (float)Math.PI / 180.0F);
vec31 = vec31.addVector(this.posX, this.posY + (double)this.getEyeHeight(), this.posZ);

我使用了这个类,我的代码看起来像这样,但不起作用:

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {
Random rand = new Random();
Vec3 vec3 = new Vec3(((double)rand.nextFloat() - 0.5D) * 0.1D, Math.random() * 0.1D + 0.1D, 0.0D);
vec3 = vec3.rotatePitch(-player.rotationPitch * (float)Math.PI / 180.0F);
vec3 = vec3.rotateYaw(-player.rotationYaw * (float)Math.PI / 180.0F);
double d0 = (double)(rand.nextFloat()) * 0.6D - 0.3D;
Vec3 vec31 = new Vec3(((double)rand.nextFloat() - 0.5D) * 0.3D, d0, 0.6D);
vec31 = vec31.rotatePitch(-player.rotationPitch * (float)Math.PI / 180.0F);
vec31 = vec31.rotateYaw(-player.rotationYaw * (float)Math.PI / 180.0F);
vec31 = vec31.addVector(player.posX, player.posY + (double)player.getEyeHeight(), player.posZ);

MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vec3, vec31, false, true, false);
if (movingobjectposition == null) {
//nothing was under the player's gaze
return item;
}
if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
int ix = movingobjectposition.getBlockPos().getX();
int iy = movingobjectposition.getBlockPos().getY();
int iz = movingobjectposition.getBlockPos().getZ();
if (!world.isBlockNormalCube(movingobjectposition.getBlockPos(), true)) {
--iy;
}
world.spawnEntityInWorld(new EntityLightningBolt(world, ix, iy, iz));
}
if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
double ix = movingobjectposition.hitVec.xCoord;
double iy = movingobjectposition.hitVec.yCoord;
double iz = movingobjectposition.hitVec.zCoord;
world.spawnEntityInWorld(new EntityLightningBolt(world, ix, iy, iz));
}

return item;


}

最佳答案

因此,您的第一次尝试(使用玩家的位置)只正确了一半,但没有到达玩家正在寻找的地方。

您正确地确定需要使用播放器的 LookVec 但是您没有正确实现它。实体的 LookVec 是一个标准化 vector ,它不是一个位置。

所以你的第二次尝试仍然只正确了一半(并且在 (0,0,0) 的 1 block 内产生闪电。

因此,要纠正错误,您需要将这两个值相加:

public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {

double x = player.posX + player.getLookVec().xCoord;
double y = player.posY + player.getLookVec().yCoord;
double z = player.posZ + player.getLookVec().zCoord;

world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
world.createExplosion(player, x, y, z, 5.0F, true);

return item;
}

但这仍然只是部分正确,因为它没有获得玩家正在查看的 block :它在玩家的脸前面创建了闪电 1 block (好吧,实际上是他们的脚,我们没有添加玩家的眼睛高度)。

唯一的方法就是进行一些光线追踪。

注释:

  • world.rayTraceBlocks_do_do 的名称可能会有所不同,具体取决于您的 Forge 版本。我从 old project 中提取了这段代码并且需要一些按摩。

  • vec3:玩家眼睛位置的 vector

  • vec31:玩家的注视方向(按照上面的示例)

    MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do_do(vec3, vec31, false, true, false);
    if (movingobjectposition == null) {
    //nothing was under the player's gaze
    return item;
    }
    if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
    int ix = movingobjectposition.blockX;
    int iy = movingobjectposition.blockY;
    int iz = movingobjectposition.blockZ;
    if (!world.getBlock(ix, iy, iz).isNormalCube()) {
    --iy;
    }
    //spawn lightning (ix, iy, iz)
    }
    if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
    double ix = movingobjectposition.hitVec.xCoord;
    double iy = movingobjectposition.hitVec.yCoord;
    double iz = movingobjectposition.hitVec.zCoord;
    //spawn lightning (ix, iy, iz)
    }

关于java - 如何用java记录Minecraft玩家的视线位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44031862/

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