Получение энтити

Версия Minecraft
1.12.2
1,470
19
189
Как получить энтити на которое направлено перекрестие игрока?
 
1,470
19
189

Eifel

Модератор
1,623
78
608
Если кинете тем, будет неплохо
Иногда можно потратить лишних 5 секунд и вбить в поисковик то, что ищешь. И найти ответ. Он для этого и создан:
L6qXVAL.png
 
5,018
47
783
На, лентяй. Код на 1.12.2
Эвент, при клике по воде моя капа увеличивается.

Знаешь как переиначить код на энтити или тебе это тоже рассказывать?)
Java:
public class EventHandThirstUpdate {
    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public void hadDrinken(PlayerInteractEvent.RightClickBlock event){

        final World world = (World)Minecraft.getMinecraft().world;
        EntityPlayer player = (EntityPlayer) event.getEntity();
        ItemStack is = player.getHeldItem(EnumHand.MAIN_HAND);
        RayTraceResult raytraceresult = this.rayTrace(world, player, true);
       
            if (raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK)
            {
                BlockPos blockpos = raytraceresult.getBlockPos();

                IPlayerCap capabilities = event.getEntity().getCapability(PlayerCapProvider.LEVEL_CAP, null);

                if (world.getBlockState(blockpos).getMaterial() == Material.WATER)
                {
                    if(is.isEmpty()){
                    capabilities.reduceWaterLevel(5);
       
                    player.world.playSound(player, blockpos, SoundEvents.ENTITY_GENERIC_DRINK, SoundCategory.NEUTRAL, 0.8F, 1.0F);
           

               
                    NetworkHandler.network.sendToServer(new HUDSyncMessageServer(capabilities.getWaterLevel()));

                }
                }
            }
    }
    protected RayTraceResult rayTrace(World worldIn, EntityPlayer playerIn, boolean useLiquids)
    {
        float f = playerIn.rotationPitch;
        float f1 = playerIn.rotationYaw;
        double d0 = playerIn.posX;
        double d1 = playerIn.posY + (double)playerIn.getEyeHeight();
        double d2 = playerIn.posZ;
        Vec3d vec3d = new Vec3d(d0, d1, d2);
        float f2 = MathHelper.cos(-f1 * 0.017453292F - (float)Math.PI);
        float f3 = MathHelper.sin(-f1 * 0.017453292F - (float)Math.PI);
        float f4 = -MathHelper.cos(-f * 0.017453292F);
        float f5 = MathHelper.sin(-f * 0.017453292F);
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        double d3 = 5.0D;
        if (playerIn instanceof net.minecraft.entity.player.EntityPlayerMP)
        {
            d3 = ((net.minecraft.entity.player.EntityPlayerMP)playerIn).interactionManager.getBlockReachDistance();
        }
        Vec3d vec3d1 = vec3d.addVector((double)f6 * d3, (double)f5 * d3, (double)f7 * d3);
        return worldIn.rayTraceBlocks(vec3d, vec3d1, useLiquids, !useLiquids, false);
    }
}
 
5,018
47
783
Ты что то не так делаешь. Получаешь энтити. Кастишь его в энтити-предмет. Потом проверяешь уже на энтити-предмет...
 
3,005
192
592
Кастишь его в энтити-предмет. Потом проверяешь уже на энтити-предмет.
Какой код пишет максик можно понять по его советам..
~~~
По теме:
Вот измененный, готовый код.
Код:
    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public void hadDrinken(PlayerInteractEvent.RightClickBlock event) {

        final World world = Minecraft.getMinecraft().world;
        EntityPlayer player = (EntityPlayer) event.getEntity();
        ItemStack is = player.getHeldItem(EnumHand.MAIN_HAND);
        RayTraceResult rtr = rayTrace(world, player, true);

        if (rtr.typeOfHit == RayTraceResult.Type.ENTITY) {
            Entity lookedEntity = rtr.entityHit;
            if (lookedEntity != null && lookedEntity instanceof EntityItem) {
                EntityItem item = (EntityItem) lookedEntity;
                //TODO ur code.
            }
        }
    }

    protected RayTraceResult rayTrace(World worldIn, EntityPlayer playerIn, boolean useLiquids) {
        float f = playerIn.rotationPitch;
        float f1 = playerIn.rotationYaw;
        double d0 = playerIn.posX;
        double d1 = playerIn.posY + playerIn.getEyeHeight();
        double d2 = playerIn.posZ;
        Vec3d vec3d = new Vec3d(d0, d1, d2);
        float f2 = MathHelper.cos(-f1 * 0.017453292F - (float) Math.PI);
        float f3 = MathHelper.sin(-f1 * 0.017453292F - (float) Math.PI);
        float f4 = -MathHelper.cos(-f * 0.017453292F);
        float f5 = MathHelper.sin(-f * 0.017453292F);
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        double d3 = 5.0D;
        if (playerIn instanceof net.minecraft.entity.player.EntityPlayerMP)
            d3 = ((net.minecraft.entity.player.EntityPlayerMP) playerIn).interactionManager.getBlockReachDistance();
        Vec3d vec3d1 = vec3d.addVector(f6 * d3, f5 * d3, f7 * d3);
        return worldIn.rayTraceBlocks(vec3d, vec3d1, useLiquids, !useLiquids, false);
    }
 
5,018
47
783
1,470
19
189
Какой код пишет максик можно понять по его советам..
~~~
По теме:
Вот измененный, готовый код.
Код:
    @SideOnly(Side.CLIENT)
    @SubscribeEvent
    public void hadDrinken(PlayerInteractEvent.RightClickBlock event) {

        final World world = Minecraft.getMinecraft().world;
        EntityPlayer player = (EntityPlayer) event.getEntity();
        ItemStack is = player.getHeldItem(EnumHand.MAIN_HAND);
        RayTraceResult rtr = rayTrace(world, player, true);

        if (rtr.typeOfHit == RayTraceResult.Type.ENTITY) {
            Entity lookedEntity = rtr.entityHit;
            if (lookedEntity != null && lookedEntity instanceof EntityItem) {
                EntityItem item = (EntityItem) lookedEntity;
                //TODO ur code.
            }
        }
    }

    protected RayTraceResult rayTrace(World worldIn, EntityPlayer playerIn, boolean useLiquids) {
        float f = playerIn.rotationPitch;
        float f1 = playerIn.rotationYaw;
        double d0 = playerIn.posX;
        double d1 = playerIn.posY + playerIn.getEyeHeight();
        double d2 = playerIn.posZ;
        Vec3d vec3d = new Vec3d(d0, d1, d2);
        float f2 = MathHelper.cos(-f1 * 0.017453292F - (float) Math.PI);
        float f3 = MathHelper.sin(-f1 * 0.017453292F - (float) Math.PI);
        float f4 = -MathHelper.cos(-f * 0.017453292F);
        float f5 = MathHelper.sin(-f * 0.017453292F);
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        double d3 = 5.0D;
        if (playerIn instanceof net.minecraft.entity.player.EntityPlayerMP)
            d3 = ((net.minecraft.entity.player.EntityPlayerMP) playerIn).interactionManager.getBlockReachDistance();
        Vec3d vec3d1 = vec3d.addVector(f6 * d3, f5 * d3, f7 * d3);
        return worldIn.rayTraceBlocks(vec3d, vec3d1, useLiquids, !useLiquids, false);
    }
rtr = null почему-то
 
1,990
18
105
Какой код пишет максик можно понять по его советам..
>тут же совет уровня Максика
Браво.

Ответ, если что, в EntityRenderer#getMouseOver.
Кроме того, что нужно вызвать ваш rayTraceBlocks для блоков (кажется, это очевидно из названия), нужно ещё и достать список сущностей и индивидуально их прорейкастить. Выбирается в итоге тот объект (блок или сущность), до которого дистанция луча наименьшая. Все подробности внутри указанного метода, его можно хоть скопипастить.
 
3,005
192
592
Сверху