microwaver
本帖最后由 microwaver 于 2018-8-13 19:37 编辑

比如我想生成一个实体,然后让这个实体表现出就像玩家按下左键一样的效果(如果瞄准一个实体就攻击,如果瞄准一个方块就挖掘),应该怎么写呢?
我已经随着这个实体生成了一个fakeplayer,但是没发现fakeplayer有能帮助选择目标的方法啊..

(18/8/13 19:36)更新:我从EntityRenderer类下的getMouseOver方法找到了Minecraft客户端计算瞄准物体的代码,当我把它改到我的mod里时,也能在单机下运行.不过我总觉得这样可能有点隐患啊....

耗子
  1. public RayTraceResult rayTrace(World worldIn, EntityPlayer playerIn, boolean useLiquids)
  2.     {
  3.         float f = playerIn.rotationPitch;
  4.         float f1 = playerIn.rotationYaw;
  5.         double d0 = playerIn.posX;
  6.         double d1 = playerIn.posY + (double)playerIn.getEyeHeight();
  7.         double d2 = playerIn.posZ;
  8.         Vec3d vec3d = new Vec3d(d0, d1, d2);
  9.         float f2 = MathHelper.cos(-f1 * 0.017453292F - (float)Math.PI);
  10.         float f3 = MathHelper.sin(-f1 * 0.017453292F - (float)Math.PI);
  11.         float f4 = -MathHelper.cos(-f * 0.017453292F);
  12.         float f5 = MathHelper.sin(-f * 0.017453292F);
  13.         float f6 = f3 * f4;
  14.         float f7 = f2 * f4;
  15.         double d3 = playerIn.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue();
  16.         Vec3d vec3d1 = vec3d.add((double)f6 * d3, (double)f5 * d3, (double)f7 * d3);
  17.         return worldIn.rayTraceBlocks(vec3d, vec3d1, useLiquids, !useLiquids, false);
  18.     }
复制代码

这个是获取指向的方块的方向,来自Item类。

如果我的答案有帮助的话,请给我最高的评分

berry64
怕不是影分身插件/mod

如果是插件的话可以以这样的思路写

  1. double x,y,z;
  2. x = -cos(pitch) * sin(yaw);
  3. y = -sin(pitch);
  4. z =  cos(pitch) * cos(yaw);
  5. Vector3 v3 = new Vector3(x,y,z);
  6. Location loc = p.getEyeLocation().clone();
  7. for(int i = 0; i < reachdistance; i++){
  8.    if(player.getLocation().getWorld().getBlockAt(loc) != null){
  9.           player.getLocation().getWorld().getBlockAt(loc).setType(Material.AIR) // 这里破坏可以用时间
  10.     }
  11.     //实体的懒得写了自己看看吧
  12.    loc.add(v3);
  13. }
复制代码

第一页 上一页 下一页 最后一页