Hi I made it that you can give a mob an item and he will give it back but I also made it that it says something and a different entity spawns aswell but it does those things twice. It says the chat thing twice and it spawns the entity twice please help!
Code:
public boolean interact(EntityPlayer par1EntityPlayer)
{
ItemStack itemstack = par1EntityPlayer.inventory.getCurrentItem();
if (itemstack != null && itemstack.itemID == MajorasMaskmod.ItemMoonTear.itemID && !par1EntityPlayer.capabilities.isCreativeMode)
{
if (itemstack.stackSize-- == 1)
{
par1EntityPlayer.inventory.setInventorySlotContents(par1EntityPlayer.inventory.currentItem, new ItemStack(MajorasMaskmod.ItemLandTitleDeed));
par1EntityPlayer.addChatMessage("Bussiness Scrub: AH! That stone! You must give it to me!");
setDead();
EntityBScrub2 entitybscrub2 = new EntityBScrub2(worldObj);
entitybscrub2.setLocationAndAngles(posX, posY, posZ, rotationYaw, rotationPitch);
entitybscrub2.setHealth(this.getHealth());
entitybscrub2.renderYawOffset = renderYawOffset;
worldObj.spawnEntityInWorld(entitybscrub2);
}
else if (!par1EntityPlayer.inventory.addItemStackToInventory(new ItemStack(MajorasMaskmod.ItemLandTitleDeed)))
{
par1EntityPlayer.dropPlayerItem(new ItemStack(MajorasMaskmod.ItemLandTitleDeed.itemID, 1, 0));
}
return true;
}
else
{
return super.interact(par1EntityPlayer);
}
}
}
Yeah, everything does things twice, block, items, entities, ect. This is because it gets called once for the client, and once for the server since both run an instance of the game. Most of the time, you don't need the client to run things that have to do with the world, you would just let the server do the work and then the client would just sync with it. This checks for if its the server running the method or the client:if(!this.worldObj.isRemote){
I don't work much with entities, so is interact() a method you made that you call? Or a method in the superclass? It might just be called twice from the other methods. If it still doesn't work, use debounce. Like this:
a=true;
if (a){ //will only run once
a=false;
//do stuff
}
Rollback Post to RevisionRollBack
Click the green "up" arrow if you received an answer, even if it's one you don't like.
Code:
Click the green "up" arrow if you received an answer, even if it's one you don't like.
I keep getting a error for public poolean interact(EntityPlayer par1EntityPlayer)
code:
a=true;
if (a){ //will only run once
a=false;
//do stuff
}
Click the green "up" arrow if you received an answer, even if it's one you don't like.