Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi,

I've got an item in my mod which I want (while right-click is held) to begin pulling items within 8 blocks towards the player. So far I have this code:

...

@Override

public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player) {

System.out.println("Hello World!");

return itemstack;

}

That prints out Hello World to the terminal while I am pressing right click, however I have no idea where to start with actually pulling items towards the player.

Thanks in advance :)

Toastrackenigma

 

P.S. I'm modding Minecraft 1.7.10

So I suggest pulling out your shovel and heavy digging equipment.

 

What in vanilla Minecraft does this? The player naturally does this. So that means we need to look into all player related code. So try EntityPlayer. Start digging through to see if you can find the code that pulls the items in. Then you have your starting point. And remember, base your code off of the vanilla code, don't directly copy/paste (unless of course its 1/2/3/4 lines :P, still see if you can find a better way. That's always fun).

We all stuff up sometimes... But I seem to be at the bottom of that pot.

I think you can select all EntityItems near the player (world.getEntitiesWithinAABBExcludingEntity) and move them. Take look at handleMaterialAcceleration function in the EntityItem class.

  • Author

Thanks for the help, but after a bit more research I've decided that moving items is too hard for my third day modding!

After thinking about it, removing the items from the world and then adding them to the player's inventory would achieve the same goal as moving them towards the player.

Do you guys think that would be easier than actually moving them towards the player? If so, do you have any pointers or advice that you can give me to achieve this?

Thanks again :)

Toastrackenigma

 

P.S. @MultiMote handleMaterialAcceleration is in the world.java class and only handles the movement of items when they are in water.

  • Author

@MultiMote The code was way to complex :)

@ArcaneFractal That sounds like it could actually work... does anyone know the method used to set the velocity? So far I've got

        @Override

public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player) {

world.getEntitiesWithinAABBExcludingEntity(player, player.boundingBox.expand(8D, 8D, 8D));

return itemstack;

}

Pretty sure that should select the items, I think that the world.getEntites part should return a list of entites around the player in a 8x8x8 radius. For the velocity I'm imagining something along the lines of setItemVelocity(), setItemSpeed(), ItemVelocity(), etc.

I could be completely wrong :P

Toastrackenigma

The method World.getEntitiesWithinAABBExcludingEntity returns a List. You have to save it in a variable.

 

List<Entity> = world.getEntitiesWithinAABBExcludingEntity(EntityItem.class, player.boundingBox.expand(8D, 8D, 8D));

 

and set its velocity to the difference between its position and the player's (I'm not writing the for loop)

 

double factor = 0.02d;

entity.motionX += (player.posX - entity.posX) * factor;

entity.motionY += (player.posY - entity.posY) * factor;

entity.motionZ += (player.posZ - entity.posZ) * factor;

Have fun  ;D

 

public ItemStack onItemRightClick(ItemStack is, World world, EntityPlayer ep)
    {
            double radius = 5;
            List<EntityItem> items = world.getEntitiesWithinAABB(EntityItem.class, ep.boundingBox.expand(radius, radius, radius));
            for(EntityItem it : items){
                double distX = ep.posX - it.posX;
                double distZ = ep.posZ - it.posZ;
                double distY = it.posY+1.5D - ep.posY;
                double dir = Math.atan2(distZ, distX);
                double speed = 1F / it.getDistanceToEntity(ep) * 0.5;

                if (distY<0)
                {
                    it.motionY += speed;
                }

                it.motionX = Math.cos(dir) * speed;
                it.motionZ = Math.sin(dir) * speed;
            }

        return is;
    }

Have fun  ;D

 

public ItemStack onItemRightClick(ItemStack is, World world, EntityPlayer ep)
    {
            double radius = 5;
            List<EntityItem> items = world.getEntitiesWithinAABB(EntityItem.class, ep.boundingBox.expand(radius, radius, radius));
            for(EntityItem it : items){
                double distX = ep.posX - it.posX;
                double distZ = ep.posZ - it.posZ;
                double distY = it.posY+1.5D - ep.posY;
                double dir = Math.atan2(distZ, distX);
                double speed = 1F / it.getDistanceToEntity(ep) * 0.5;

                if (distY<0)
                {
                    it.motionY += speed;
                }

                it.motionX = Math.cos(dir) * speed;
                it.motionZ = Math.sin(dir) * speed;
            }

        return is;
    }

 

You should probably use the MathHelper class.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.