Jump to content

[1.12] Give potion effect to looking entity


JimiIT92

Recommended Posts

I'm trying to create a wand that when right clicked will give a potion effect to the entity the player is looking at. But for some reason the effect is not applied :/ Here's the code i'm using right now

 
	package com.mw.items;
	import com.mw.MW;
import com.mw.core.MWTabs;
import com.mw.utils.EnumWands;
import com.mw.utils.Utils;
	import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
	public class ItemWand extends Item{
	    private EnumWands type;
    
    public ItemWand(EnumWands typeIn) {
        this.setCreativeTab(MWTabs.TOOLS);
        this.maxStackSize = 1;
        this.setMaxDamage(256);
        this.type = typeIn;
    }
    
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
        if(!worldIn.isRemote) {
            if(this.type == EnumWands.CORRUPTER || this.type == EnumWands.HEALER) {
                Entity pointed = Utils.getLookingEntity();
                if(pointed != null && pointed instanceof EntityLiving) {
                    Potion potion;
                    int effect = MW.RANDOM.nextInt(5);
                    if(this.type == EnumWands.CORRUPTER) {
                        switch(effect) {
                        case 0:
                        default:
                            potion = MobEffects.POISON;
                            break;
                        case 1:
                            potion = MobEffects.BLINDNESS;
                            break;
                        case 2:
                            potion = MobEffects.LEVITATION;
                            break;
                        case 3:
                            potion = MobEffects.SLOWNESS;
                            break;
                        case 4:
                            potion = MobEffects.WEAKNESS;
                            break;
                        }
                        worldIn.playSound((EntityPlayer)null, playerIn.posX, playerIn.posY, playerIn.posZ, SoundEvents.ENTITY_ENDERMITE_AMBIENT, SoundCategory.NEUTRAL, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
                        
                    } else {
                        switch(effect) {
                        case 0:
                        default:
                            potion = MobEffects.REGENERATION;
                            break;
                        case 1:
                            potion = MobEffects.RESISTANCE;
                            break;
                        case 2:
                            potion = MobEffects.ABSORPTION;
                            break;
                        case 3:
                            potion = MobEffects.INSTANT_HEALTH;
                            break;
                        case 4:
                            potion = MobEffects.STRENGTH;
                            break;
                        }
                        worldIn.playSound((EntityPlayer)null, playerIn.posX, playerIn.posY, playerIn.posZ, SoundEvents.ENTITY_PLAYER_LEVELUP, SoundCategory.NEUTRAL, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
                    }
                    
                    ((EntityLiving)pointed).addPotionEffect(new PotionEffect(potion, 30 * 20));
                    
                    ItemStack itemstack = playerIn.getHeldItem(handIn);
	                    if (!playerIn.capabilities.isCreativeMode)
                    {
                        itemstack.damageItem(1, playerIn);
                    }
                    
                    Utils.setCooldown(playerIn, this, 3);
                }
            }
            
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }
}


I'm sure the code got called because the wand goes in cooldown and the sound is played, it's just the effect doesn't apply to the mob (and both effect and entity are corrects). How can i fix this? :)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

  • 1 month later...

Are you sure you're getting the right Entity? It seems like trying to get the mob you're looking at on the server side is a bit of a stretch.

 

Rather than that I'd try to get an Minecraft.getMinecraft().objectMouseOver which is a raytraceresult. Then use a packet to tell your server to add a potion effect to it. That seems like a more reasonable and foolproof idea, since the server doesn't know your exact position at all times, it just updates it with a client package every x ticks. 

 

I'm not an expert, but that's what I'd attempt. hopefully that can get you going.

 

Edit: If you're sure this should work, the least you can do is implement a System.out.Println Message with the entity and the effect, just so you can be sure that whatever you think should happen is occuring.

Edited by oldcheese
Link to comment
Share on other sites

@oldcheese If it can help, this is the cod ei'm using to get the entity the player is looking at

 
	Entity entity = Minecraft.getMinecraft().getRenderViewEntity();
        Entity pointedEntity = null;
        if (entity != null)
        {
            if (Minecraft.getMinecraft().world != null)
            {
                Minecraft.getMinecraft().mcProfiler.startSection("pick");
                Minecraft.getMinecraft().pointedEntity = null;
                double d0 = (double)Minecraft.getMinecraft().playerController.getBlockReachDistance() * 2;
                Minecraft.getMinecraft().objectMouseOver = entity.rayTrace(d0, 20);
                Vec3d vec3d = entity.getPositionEyes(20);
                boolean flag = false;
                int i = 3;
                double d1 = d0;
	                if (Minecraft.getMinecraft().playerController.extendedReach())
                {
                    //d1 = 6.0D;
                    d1 = 14.0D;
                    d0 = d1;
                }
                else
                {
                    if (d0 > 3.0D)
                    {
                        flag = true;
                    }
                }
	                if (Minecraft.getMinecraft().objectMouseOver != null)
                {
                    d1 = Minecraft.getMinecraft().objectMouseOver.hitVec.distanceTo(vec3d);
                }
	                Vec3d vec3d1 = entity.getLook(1.0F);
                Vec3d vec3d2 = vec3d.addVector(vec3d1.x * d0, vec3d1.y * d0, vec3d1.z * d0);
                pointedEntity = null;
                Vec3d vec3d3 = null;
                float f = 1.0F;
                List<Entity> list = Minecraft.getMinecraft().world.getEntitiesInAABBexcluding(entity, entity.getEntityBoundingBox().expand(vec3d1.x * d0, vec3d1.y * d0, vec3d1.z * d0).grow(1.0D, 1.0D, 1.0D), Predicates.and(EntitySelectors.NOT_SPECTATING, new Predicate<Entity>()
                {
                    public boolean apply(@Nullable Entity p_apply_1_)
                    {
                        return p_apply_1_ != null && p_apply_1_.canBeCollidedWith();
                    }
                }));
                double d2 = d1;
	                for (int j = 0; j < list.size(); ++j)
                {
                    Entity entity1 = list.get(j);
                    AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow((double)entity1.getCollisionBorderSize());
                    RayTraceResult raytraceresult = axisalignedbb.calculateIntercept(vec3d, vec3d2);
	                    if (axisalignedbb.contains(vec3d))
                    {
                        if (d2 >= 0.0D)
                        {
                            pointedEntity = entity1;
                            vec3d3 = raytraceresult == null ? vec3d : raytraceresult.hitVec;
                            d2 = 0.0D;
                        }
                    }
                    else if (raytraceresult != null)
                    {
                        double d3 = vec3d.distanceTo(raytraceresult.hitVec);
	                        if (d3 < d2 || d2 == 0.0D)
                        {
                            if (entity1.getLowestRidingEntity() == entity.getLowestRidingEntity() && !entity1.canRiderInteract())
                            {
                                if (d2 == 0.0D)
                                {
                                    pointedEntity = entity1;
                                    vec3d3 = raytraceresult.hitVec;
                                }
                            }
                            else
                            {
                                pointedEntity = entity1;
                                vec3d3 = raytraceresult.hitVec;
                                d2 = d3;
                            }
                        }
                    }
                }
	                if (pointedEntity != null && flag && vec3d.distanceTo(vec3d3) > 3.0D)
                {
                    pointedEntity = null;
                    Minecraft.getMinecraft().objectMouseOver = new RayTraceResult(RayTraceResult.Type.MISS, vec3d3, (EnumFacing)null, new BlockPos(vec3d3));
                }
	                if (pointedEntity != null && (d2 < d1 || Minecraft.getMinecraft().objectMouseOver == null))
                {
                    Minecraft.getMinecraft().objectMouseOver = new RayTraceResult(pointedEntity, vec3d3);
	                    if (pointedEntity instanceof EntityLivingBase || pointedEntity instanceof EntityItemFrame)
                    {
                        Minecraft.getMinecraft().pointedEntity = pointedEntity;
                    }
                }
	                Minecraft.getMinecraft().mcProfiler.endSection();
            }
        }
        return pointedEntity;


I've already tried to see if the entity i'm looking is correct by printing the entity's class and the effect choosen and both works as well (if i point a Cow it prints EntityCow). I will try to see if using packets will solve the problem :)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.