Jump to content

Recommended Posts

Posted

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! :)

I'd really like to think of myself as a newb, not a noob.  If you ever suspect I'm acting noobish, please call me out on it.

Posted

Thanks for your response, diesieben07.  I'm actually not worried about how to accomplish anything with the Entity object once I have it; I'm just not able to get a working reference to an Entity in the first place.  The *getMovingObjectPositionFromPlayer* that I've seen demonstrated in the vanilla class ItemBucket is never returning an Entity that I clicked, but rather always returning a tile.

I'd really like to think of myself as a newb, not a noob.  If you ever suspect I'm acting noobish, please call me out on it.

Posted

Ah, okay.  I apologize for misunderstanding you.  If I write a method subscribed to EntityInteractEvent in the item class, do I need to add a check to make sure that the player currently has the item equipped, or will it only fire if that item is equipped because it's in the class definition for that item?

I'd really like to think of myself as a newb, not a noob.  If you ever suspect I'm acting noobish, please call me out on it.

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.