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 modding (Java Edition)

Last seen on 05:16, 19. Jan 2021
Joined May 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
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
Last seen on 04:35, 17. Jan 2021
Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
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);
    }