Posted July 8, 201411 yr 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
July 8, 201411 yr 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 , 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.
July 8, 201411 yr I think you can select all EntityItems near the player (world.getEntitiesWithinAABBExcludingEntity) and move them. Take look at handleMaterialAcceleration function in the EntityItem class.
July 9, 201411 yr 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.
July 9, 201411 yr Once you have found a way to get the item entities, you can set their velocity so they fly toward the player.
July 9, 201411 yr 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 Toastrackenigma
July 9, 201411 yr 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;
July 9, 201411 yr Have fun 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; }
July 9, 201411 yr Have fun 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.
July 9, 201411 yr Author @MultiMote Thank you so much that worked! Now my magnet sucks items towards 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.