How do I make an entity fly towards the player when right-clicking in that general direction?

Started by FinchyMcFinch on

Topic category: Help with Minecraft modding (Java Edition)

Active 4 years ago
Joined May 2020
Points:
639

User statistics:

  • Modifications: 0
  • Forum topics: 2
  • Wiki pages: 0
  • MCreator plugins: 0
  • Comments: 1
How do I make an entity fly towards the player when right-clicking in that general direction?
Tue, 05/19/2020 - 10:05 (edited)

I'm attempting to create a Slime Rancher mod for 1.14.4, and I'm on the Vacpack currently. The Vacpack should let the user suck items and entities towards them and then deposit those things in their inventory, but I'm not sure how to do that. Can anyone help me out?

Edited by FinchyMcFinch on Tue, 05/19/2020 - 10:05
Active 4 years ago
Joined Sep 2019
Points:
988

User statistics:

  • Modifications: 1
  • Forum topics: 14
  • Wiki pages: 0
  • MCreator plugins: 0
  • Comments: 507
Is the Vacpack an item or…
Mon, 06/01/2020 - 00:57

Is the Vacpack an item or armor. If armor put the following snippet in onAmorTick(), 

Keep in mind the following snippet is only for items but you can do similar with entities

public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn)
    {
        ItemStack itemstack = playerIn.getHeldItem(handIn);
        if (itemstack.hasTag() && itemstack.getTag().getBoolean("magnetized") == true) {
            List<ItemEntity> itemEntities = playerIn.world.getEntitiesWithinAABB(ItemEntity.class, playerIn.getBoundingBox().grow(10));
            Iterator iterator = itemEntities.iterator();
            while (iterator.hasNext()) {
                ItemEntity item = (ItemEntity) iterator.next();
                double distX = playerIn.getPosX() - item.getPosX();
                double distZ = playerIn.getPosZ() - item.getPosZ();
                double distY = item.getPosY() + 1.5D - item.getPosY();
                double dir = Math.atan2(distZ, distX);
                double speed = 1F / item.getDistance(playerIn) * 0.5;
                if (distY < 0) {
                    item.setMotion(item.getMotion().x, item.getMotion().y + speed, item.getMotion().z);
                }
                item.setMotion(Math.cos(dir) * speed, item.getMotion().y, item.getMotion().z);
                item.setMotion(item.getMotion().x, item.getMotion().y, Math.sin(dir) * speed);
            }
        }
        return ActionResult.resultSuccess(itemstack);
    }