I want to make a mod that converts one creature to another when the player right-clicks the creature with a specific item, similar to the process of converting a zombie villager to a villager. However, I don't want the creature to have to have a potion effect to convert. I am using an event handler (EntityInteractEvent) that is called whenever the player right-clicks an entity. Using the code I have below, when I right-click a cow with a cookie, the cow should be replaced by a sheep. However, without the "isPotionActive()" requirement, the cow does disappear, but two sheep spawn. One of the sheep is a real sheep and the other one is a ghost entity that doesn't move or take damage but can fall. I tested each line of code, expecting the problem to be in the "convert to other mob" section (see code), but the problem was within the "isPotionActive()" line. When I added the potion effect requirement, the ghost entity did not spawn, but when I removed the requirement for the potion effect, the ghost entity spawned. I tried to work around this problem by adding the potion effect to the entity before checking for the potion effect (see the commented out "addPotionEffect()" line), but the ghost entity still spawned. If it helps, I also noticed that when I put the effect on the cow, then exit and re-enter the world and use the item on the cow, the ghost entity spawns.
Does anyone know how to convert an entity to another one without using a potion?
Here is my source code (note that both entities, the item, and the potion effect can be changed easily):
public class CureEventHandler
{
//called when the player right-clicks on an entity
@SubscribeEvent
public void itemInteractionForEntity(EntityInteractEvent event)
{
ItemStack heldItem = event.entityPlayer.getHeldItem();
//if the target exists...
if (event.target != null)
{
//((EntityLivingBase) event.target).addPotionEffect(new PotionEffect(18, 100, 255, false));
//if held item is correct and the mob has the correct effect
if (heldItem != null && heldItem.getItem() == Items.cookie && ((EntityLivingBase) event.target).isPotionActive(Potion.weakness))
{
if (event.target instanceof EntityCow)
{
EntityCow entitycow = (EntityCow)event.target;
//if the player isn't in creative mode, subtract one from the stack
if (!event.entityPlayer.capabilities.isCreativeMode)
{
--heldItem.stackSize;
}
//convert to other mob
//the new entity
EntitySheep entitysheep = new EntitySheep(entitycow.worldObj);
//new entity copies original entity location
entitysheep.copyLocationAndAnglesFrom(entitycow);
//original entity spawns new entity into the world
entitycow.worldObj.spawnEntityInWorld(entitysheep);
//new entity removes the old entity
entitysheep.worldObj.removeEntity(entitycow);
}
}
}
}
}
Only spawn entities on the server (i.e. when the world object is NOT remote) - the client side version is generated automatically, so when you explicitly spawn one on the client, you end up with a 'ghost' entity.
Does anyone know how to convert an entity to another one without using a potion?
Here is my source code (note that both entities, the item, and the potion effect can be changed easily):
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class CureEventHandler
{
//called when the player right-clicks on an entity
@SubscribeEvent
public void itemInteractionForEntity(EntityInteractEvent event)
{
ItemStack heldItem = event.entityPlayer.getHeldItem();
//if the target exists...
if (event.target != null)
{
//((EntityLivingBase) event.target).addPotionEffect(new PotionEffect(18, 100, 255, false));
//if held item is correct and the mob has the correct effect
if (heldItem != null && heldItem.getItem() == Items.cookie && ((EntityLivingBase) event.target).isPotionActive(Potion.weakness))
{
if (event.target instanceof EntityCow)
{
EntityCow entitycow = (EntityCow)event.target;
//if the player isn't in creative mode, subtract one from the stack
if (!event.entityPlayer.capabilities.isCreativeMode)
{
--heldItem.stackSize;
}
//convert to other mob
//the new entity
EntitySheep entitysheep = new EntitySheep(entitycow.worldObj);
//new entity copies original entity location
entitysheep.copyLocationAndAnglesFrom(entitycow);
//original entity spawns new entity into the world
entitycow.worldObj.spawnEntityInWorld(entitysheep);
//new entity removes the old entity
entitysheep.worldObj.removeEntity(entitycow);
}
}
}
}
}