So I am currently working on a breaking bad mod. And I have recently made a drug dealer that, on right click will trade drugs for money. To make him spawn however I require right clicking some sort of item. So I copied the code from the ItemBoat Class.
package com.camp.item;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityBoat;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import com.camp.creativetabs.CreativeTabsManager;
public class BoilingWater extends Item {
public BoilingWater(){
this.setUnlocalizedName("redP");
this.setCreativeTab(CreativeTabsManager.tabItem);
this.setMaxStackSize(64);
//this.setTextureName(StringLibrary.MODID + ":redp");
this.setTextureName("water_bucket");
}
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
float f = 1.0F;
float f1 = par3EntityPlayer.prevRotationPitch + (par3EntityPlayer.rotationPitch - par3EntityPlayer.prevRotationPitch) * f;
float f2 = par3EntityPlayer.prevRotationYaw + (par3EntityPlayer.rotationYaw - par3EntityPlayer.prevRotationYaw) * f;
double d0 = par3EntityPlayer.prevPosX + (par3EntityPlayer.posX - par3EntityPlayer.prevPosX) * (double)f;
double d1 = par3EntityPlayer.prevPosY + (par3EntityPlayer.posY - par3EntityPlayer.prevPosY) * (double)f + 1.62D - (double)par3EntityPlayer.yOffset;
double d2 = par3EntityPlayer.prevPosZ + (par3EntityPlayer.posZ - par3EntityPlayer.prevPosZ) * (double)f;
Vec3 vec3 = par2World.getWorldVec3Pool().getVecFromPool(d0, d1, d2);
float f3 = MathHelper.cos(-f2 * 0.017453292F - (float)Math.PI);
float f4 = MathHelper.sin(-f2 * 0.017453292F - (float)Math.PI);
float f5 = -MathHelper.cos(-f1 * 0.017453292F);
float f6 = MathHelper.sin(-f1 * 0.017453292F);
float f7 = f4 * f5;
float f8 = f3 * f5;
double d3 = 5.0D;
Vec3 vec31 = vec3.addVector((double)f7 * d3, (double)f6 * d3, (double)f8 * d3);
MovingObjectPosition movingobjectposition = par2World.rayTraceBlocks(vec3, vec31, true);
if (movingobjectposition == null)
{
return par1ItemStack;
}
else
{
Vec3 vec32 = par3EntityPlayer.getLook(f);
boolean flag = false;
float f9 = 1.0F;
List list = par2World.getEntitiesWithinAABBExcludingEntity(par3EntityPlayer, par3EntityPlayer.boundingBox.addCoord(vec32.xCoord * d3, vec32.yCoord * d3, vec32.zCoord * d3).expand((double)f9, (double)f9, (double)f9));
int i;
for (i = 0; i < list.size(); ++i)
{
Entity entity = (Entity)list.get(i);
if (entity.canBeCollidedWith())
{
float f10 = entity.getCollisionBorderSize();
AxisAlignedBB axisalignedbb = entity.boundingBox.expand((double)f10, (double)f10, (double)f10);
if (axisalignedbb.isVecInside(vec3))
{
flag = true;
}
}
}
if (flag)
{
return par1ItemStack;
}
else
{
if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
{
i = movingobjectposition.blockX;
int j = movingobjectposition.blockY;
int k = movingobjectposition.blockZ;
if (par2World.getBlock(i, j, k) == Blocks.snow_layer)
{
--j;
}
EntityBoat entityboat = new EntityBoat(par2World, (double)((float)i + 0.5F), (double)((float)j + 1.0F), (double)((float)k + 0.5F));
entityboat.rotationYaw = (float)(((MathHelper.floor_double((double)(par3EntityPlayer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3) - 1) * 90);
if (!par2World.getCollidingBoundingBoxes(entityboat, entityboat.boundingBox.expand(-0.1D, -0.1D, -0.1D)).isEmpty())
{
return par1ItemStack;
}
if (!par2World.isRemote)
{
par2World.spawnEntityInWorld(entityboat);
}
if (!par3EntityPlayer.capabilities.isCreativeMode)
{
--par1ItemStack.stackSize;
}
}
return par1ItemStack;
}
}
}
}
So That worked, however when I replace EntityBoat with EntityPig it does not work. Anyone else know how to make a item spawn a entity? I also would like to replace with my own custom entity. So here is the code for my custom entity and its manager.
/*
package com.camp.entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public class MrJavehMob extends EntityAnimal {
public MrJavehMob(World par1World) {
super(par1World);
// this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(5.0D);
this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(1.0d);//;
//this.targetTasks.addTask(0, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));
this.tasks.addTask(0, new EntityAIWander(this, 1.0D));;
// TODO Auto-generated constructor stub
}
protected String getHurtSound()
{
return "mob.wither.hurt";//;//;
}
@Override
public void dropFewItems(boolean recentlyHit, int lootLevel) {
int quantity = this.rand.nextInt(4) + 1;
for (int i = 0; i < quantity; i++) {
if (this.isBurning()) {
this.dropItem(Items.golden_apple, 1);
}
else {
Item drop = (Items.apple);
this.dropItem(drop, 1);
}
}
}
public boolean interact(EntityPlayer par1EntityPlayer)
{
ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();
if (itemstack != null && itemstack.getItem() == Items.bucket && !par1EntityPlayer.capabilities.isCreativeMode)
{
if (itemstack.stackSize-- == 1)
{
par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, new ItemStack(Items.milk_bucket));
}
else if (!par1EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Items.milk_bucket)))
{
par1EntityPlayer.dropPlayerItemWithRandomChoice(new ItemStack(Items.milk_bucket, 1, 0), false);
}
return true;
}
else
{
return super.interact(par1EntityPlayer);
}
}
@Override
public EntityAgeable createChild(EntityAgeable var1) {
// TODO Auto-generated method stub
return new MrJavehMob(worldObj);
}//end createChild
protected void applyEntityAttributes() {
super.applyEntityAttributes();
}//end applyEntityAttributes
public boolean isAIEnabled() {
return true;
}
}
*/
package com.camp.entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIPanic;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAITempt;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.passive.EntityAnimal;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import com.camp.item.ItemManager;
public class Jesse extends EntityAnimal{
public Jesse(World par1World) {
super(par1World); //par1World);//par1World);
// TODO Auto-generated constructor stub
this.setSize(1.0f, 1.0f);
//this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(60.0D);//;
this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(3.0d);//;
this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(200d);
this.tasks.addTask(1, new EntityAISwimming(this));
this.tasks.addTask(2, new EntityAITempt(this, 1.0d, ItemManager.methBag, false));
this.tasks.addTask(2, new EntityAIPanic(this, 0.7d));
this.tasks.addTask(3, new EntityAIWander(this, 1.0d));//;
}//end constructor
@Override
public EntityAgeable createChild(EntityAgeable var1) {
// TODO Auto-generated method stub
return new Jesse(worldObj);
}//end createChild
protected void applyEntityAttributes() {
super.applyEntityAttributes();
}//end applyEntityAttributes
public boolean isAIEnabled() {
return true;
}
public boolean interact(EntityPlayer par1EntityPlayer)
{
ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();
if (itemstack != null && itemstack.getItem() == ItemManager.methBag && !par1EntityPlayer.capabilities.isCreativeMode)
{
if (itemstack.stackSize-- == 1)
{
par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, new ItemStack(ItemManager.cashx100));
}
else if (!par1EntityPlayer.inventory.addItemStackToInventory(new ItemStack(ItemManager.cashx100)))
{
par1EntityPlayer.dropPlayerItemWithRandomChoice(new ItemStack(ItemManager.cashx100, 1, 0), false);
}
return true;
}
else
{
return super.interact(par1EntityPlayer);
}
}
public Jesse createChild1(EntityAgeable par1EntityAgeable)
{
return new Jesse(this.worldObj);
}
}
And The Manager:
package com.camp.entity;
import net.minecraft.entity.EntityList;
import cpw.mods.fml.common.registry.EntityRegistry;
public class EntityManager {
public static void mainRegistry() {
registerEntity();//;
}
public static void registerEntity() {
createEntity(Jesse.class, "Jesse", 0x0041F5, 0xF5B400);
createEntity(TinglePassive.class, "TinglePassive", 0x00FFFF, 0xFF00FF);//;
createEntity(Gus.class, "Gus", 0x045FFFF, 0xFF560FF);//;
}
public static void createEntity(Class entityClass, String entityName, int solidColor, int spotColor) {
int entityId = EntityRegistry.findGlobalUniqueEntityId();
EntityRegistry.registerGlobalEntityID(entityClass, entityName, entityId);
EntityList.entityEggs.put(Integer.valueOf(entityId), new EntityList.EntityEggInfo(entityId, solidColor, spotColor));
}
}