Jump to content

[1.8] [SOLVED] Custom Potion Effect


DasNone

Recommended Posts

Hello everybody,

 

Just signed up but I have been lurking here for well over 2 years now. Now, my issue is following:

 

Whenever the potion is applied, it works as intended but as soon as it get cancelled out the functions won't get called (tested with the "System.out.println").

 

package net.dascraft.init;

import net.dascraft.Dascraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class DascraftEventHooks
{
@SubscribeEvent
public void onEntityUpdate(LivingUpdateEvent event)
{
	if (event.entityLiving.isPotionActive(Dascraft.protectionPotion))
	{
		if (event.entityLiving.worldObj.rand.nextInt(20) == 0)
		{
			if (event.entityLiving instanceof EntityPlayer)
			{
				EntityPlayer player = (EntityPlayer) event.entityLiving;
				player.capabilities.allowEdit = false;
				player.sendPlayerAbilities();
			}

			if (event.entityLiving.getActivePotionEffect(Dascraft.protectionPotion).getDuration() == 0)
			{
				event.entityLiving.removePotionEffect(Dascraft.protectionPotion.id);
				if (event.entityLiving instanceof EntityPlayer)
				{
					System.out.println("DOES IT WORK? YES IT DOES.");
					EntityPlayer player = (EntityPlayer) event.entityLiving;
					player.capabilities.allowEdit = true;
					player.sendPlayerAbilities();
					return;
				}
			}
		}
	}
}
}

 

I can't figure out for the life of me what's wrong with this code so I figured you might be able to help.

Link to comment
Share on other sites

I got it now, thank you diesieben07 for the fast reply and help. I didn't really take the route diesieben07 suggested going about it but the code below worked as well and because of that, issues may arise, only time will tell though.

 

Code for anyone interested:

package net.dascraft.init;

import net.dascraft.Dascraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.potion.PotionEffect;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class DascraftEventHooks
{
@SubscribeEvent
public void onEntityUpdate(LivingUpdateEvent event)
{
	if (event.entityLiving.isPotionActive(Dascraft.protectionPotion))
	{
		if (event.entityLiving.worldObj.rand.nextInt(20) == 0)
		{
			if (event.entityLiving instanceof EntityPlayer)
			{
				EntityPlayer player = (EntityPlayer) event.entityLiving;
				player.capabilities.allowEdit = false;
				player.sendPlayerAbilities();
			}
		}

		PotionEffect effect = event.entityLiving.getActivePotionEffect(Dascraft.protectionPotion);

		if (effect.onUpdate(event.entityLiving) && effect.getDuration() == 1)
		{
			System.out.println("DOES IT WORK? YES IT DOES.");
			EntityPlayer player = (EntityPlayer) event.entityLiving;
			player.capabilities.allowEdit = true;
			player.sendPlayerAbilities();
			return;
		}
	}
}
}

Link to comment
Share on other sites

You should seriously consider switching that out, as your event handler needs to be run every tick for every living entity and not only when the entity has your potion effect.

 

Oh right, I'll get on it right away. I will come back for status later on.

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.

Announcements



×
×
  • Create New...

Important Information

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