Jump to content

[1.10.2] [Solved] Having trouble igniting or potion-effecting mobs


richard

Recommended Posts

I've got a "wand" item that, on right-click, I wish to be able to inflict various things on mobs: set on fire or apply a potion effect to them. My two attempts at doing this are below.

 

Apply a poison potion effect:

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(@Nonnull ItemStack itemstack, World world, EntityPlayer player, EnumHand hand) {
        RayTraceResult target = Minecraft.getMinecraft().objectMouseOver;
        if (target.typeOfHit == RayTraceResult.Type.ENTITY) {
            if (target.entityHit instanceof EntityLivingBase) {
                EntityLivingBase entitylivingbase = (EntityLivingBase) target.entityHit;
                Potion potion = Potion.REGISTRY.getObject(new ResourceLocation("poison"));
                PotionEffect potioneffect = new PotionEffect(potion, 50, 1);
                if (potion.isInstant()) {
                    potion.affectEntity(null, null, entitylivingbase, potioneffect.getAmplifier(), 0.5D);
                } else {
                    entitylivingbase.addPotionEffect(potioneffect);
                }
            }
        }
        return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
    }

The result of the above is that the potion effect doesn't appear to be applied at all.

 

Changing the code to instead set the entity on fire:

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(@Nonnull ItemStack itemstack, World world, EntityPlayer player, EnumHand hand) {
        RayTraceResult target = Minecraft.getMinecraft().objectMouseOver;
        if (target.typeOfHit == RayTraceResult.Type.ENTITY) {
            target.entityHit.setFire(4);
        }
        return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
    }

This results in a brief flash of fire effect on the entity, but it does not remain on fire for the full 4 seconds that I specify. I know that I've invoked the setFire method correctly because if I do so in a hitEntity() override like so:

    public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
        target.setFire(4);
        return true;
    }

The entity stays on fire!

Link to comment
Share on other sites

Minecraft.getMinecraft().objectMouseOver;

Is client side only, meaning since the server doesn't know about this it can't do it. Meaning it is reset next tick for the client.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.