Jump to content

Recommended Posts

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

Posted

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.

Posted

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

Posted

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.

Posted

@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

Posted

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;

Posted

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;
    }

Posted

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

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