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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Reach Out To Rapid Digital: What sapp Info: +1 41 4 80 7 14 85 Email INFO: rap iddi gita lrecov ery @ exe cs. com Hello, my name is Jayson, and I’m 35 years old from the United Kingdom. My family and I recently endured an incredibly challenging experience that I wouldn’t wish on anyone. We became victims of a cryptocurrency investment fraud scheme that saw us lose a staggering $807,000 in USDT and Bitcoins. The fraudsters had created a convincing facade, and we were lured into investing, only to discover later that the platform was a complete scam. We were left devastated, not just financially, but emotionally, as we had trusted these people and believed in the legitimacy of the investment. After the initial shock wore off, we desperately searched for ways to recover the lost funds. It seemed like an impossible task, and we felt as though there was no hope. That’s when, by sheer luck, we stumbled across a post about Rapid Digital Recovery, a cryptocurrency and funds recovery organization with a proven track record in cybersecurity and fraud recovery. We decided to reach out to them, and from the first interaction, we were impressed with their professionalism and transparency. They explained the recovery process in detail and reassured us that they had the skills and expertise to track down the perpetrators and recover our funds. This gave us a renewed sense of hope, something we hadn’t felt in months. What truly stood out during our experience with Rapid Digital Recovery was their dedication to the recovery process. The team went above and beyond, using sophisticated tracking tools and cyber forensics to gather critical information. Within a matter of weeks, they had successfully located the funds and traced the scam back to the fraudsters responsible. They worked with the authorities to ensure the criminals were held accountable for their actions. To our relief, the team at Rapid Digital Recovery was able to recover every single penny we had lost. The funds were returned in full, and the sense of closure we felt was invaluable. We couldn’t have imagined such a positive outcome in the early stages of our recovery journey, and we are deeply grateful for the work they did. If you ever find yourself in a similar situation, I highly recommend contacting Rapid Digital Recovery. Their expertise, transparency, and dedication to their clients make them the go-to choice for anyone seeking to recover lost cryptocurrency or funds. They truly gave us back our financial future.  
    • This is my first time modding anything, so maybe just skill issue. I'm using Forge 54.0.12 and Temurin 21.0.5+11-LTS I wanted to create a custom keybind and to check whether it works I'd like to send a chat message. I tried using Minecraft.getInstance().player.sendSystemMessage(Component.literal("test")); but IntelliJ couldnt resolve sendSystemMessage(...). Since I saw people using it in earlier versions, I tried the same thing with 1.20.6(- 50.1.0), where it works fine, now I can't figure out if this is intentional and whether there are other options for sending chat messages. On that note, is there more documentation than https://docs.minecraftforge.net/en/1.21.x/? It seems very incomplete compared to something like the Oracle Java docs
    • Hi, i'm having this error and I wanna fix it. we try: -Reload drivers -Eliminate .minecraft -Eliminate Java -Restart launcher -Verify if minecraft is using gpu -Mods  in .minecraft is empty -Install the latest and recomended version of forge idk what i have to do, help me pls. the lastest log is: https://mclo.gs/WAMao8x  
    • Read the FAQ, Rule #2. (https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/)  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.