Hi all, I'm pretty new to Minecraft development, but I'd like to think I have a solid Java background. I'm having some trouble producing an item (a magic wand) that can apply affects to friendly mobs when right-clicked. Here's my current attempt, loosely based on the code from using a bucket on a cow:
@Override
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
if (par3EntityPlayer.isSneaking())
{
// Don't open inventory on the client. This code needs to refer to the server.
if (par2World.isRemote)
return par1ItemStack;
par3EntityPlayer.addChatMessage("Open inventory");
} else {
// Not sneaking. Use the wand.
// Second condition should be whether the player is currently capable of using the wand (to be defined later)
if (par3EntityPlayer.capabilities.isCreativeMode || true)
{
// Whether the active spell is a duration (held) spell or a single cast
if (activeSpell.hasDuration())
{
par3EntityPlayer.addChatMessage("Use wand (duration)");
par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
}
else
{
par3EntityPlayer.addChatMessage("Use wand (instant)");
//////////////////////////////////////////////////////
// Here is the code I'm having trouble with.
MovingObjectPosition mop = this.getMovingObjectPositionFromPlayer(par2World, par3EntityPlayer, false);
//MovingObjectPosition mop = par3EntityPlayer.rayTrace(75.0D, 1.0F);
if (mop == null)
return par1ItemStack;
if (mop.typeOfHit == EnumMovingObjectType.ENTITY)
par3EntityPlayer.addChatMessage("Hit an entity"); // perform actual right-click effect here
else if (mop.typeOfHit == EnumMovingObjectType.TILE)
par3EntityPlayer.addChatMessage("Hit a tile");
else
par3EntityPlayer.addChatMessage("Something strange happened");
System.out.println(mop.entityHit);
return par1ItemStack;
}
}
}
return par1ItemStack;
}
When I run this, it runs as I would expect, with the exception that it invariably displays "Hit a tile", even when I'm clicking on a chicken or a pig. The System.out.println call a bit later always displays that entityHit is null, as well. I started trying to mess with the rayTrace method, but getMovingObjectPositionFromPlayer seems like it would be more reliable. I just don't really understand what the MovingObjectPosition class is yet, or what to do with it. Isn't getMovingObjectPositionFromPlayer supposed to return the block or entity the player's cursor is under?
Thanks in advance for any and all help!