Create a traction procedure

Started by RedInventor on

Topic category: Help with Minecraft modding (Java Edition)

Joined Mar 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Create a traction procedure

I am working on a procedure where a player with an item that has a custom NBT tag to attract nearby items to come to the player. 

Is this possible with the procedure or does it need to be code in?

 

P.S. If more detail needs to be provided, let me know.

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I think it has to be coded…
Sun, 05/31/2020 - 21:18

I think it has to be coded in here is a snippet I made for you,

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

Keep in mind you can change the NBT to whatever you like :D.

Joined Mar 2016
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I went with an enchant…
Mon, 06/01/2020 - 07:16

I went with an enchant instead, but I am kind of lost for how to get the item to move.

Enchants.java

public class Enchants {
    public static final DeferredRegister<Enchantment> ENCHANTMENTS = new DeferredRegister<>(ForgeRegistries.ENCHANTMENTS, "minerology");
    public static final RegistryObject<Enchantment> MAGNETIC = ENCHANTMENTS.register("magnetic", () -> new MagneticEnchant(Rarity.RARE, EnchantmentType.DIGGER, new EquipmentSlotType[]{EquipmentSlotType.MAINHAND}));
}

MagneticEnchant.java

public class MagneticEnchant extends Enchantment {
    public MagneticEnchant(Rarity rarityIn, EnchantmentType typeIn, EquipmentSlotType[] slots) {
        super(rarityIn, typeIn, slots);
    }
    public int getMaxLevel() {
        return 5;
    }
    public int getMinLevel() {
        return 1;
    }
    public int getMinEnchantability(int enchantmentLevel) {
        return enchantmentLevel * 10;
    }
    public getMaxEnchantability(int enchantmentLevel) {
        return this.getMinEnchantability(enchantmentLevel) + 15;
    }
    public static class MagneticEquipped {
            PlayerEntity playerIn = event.player;
            World worldIn = playerIn.world;
            int x = (int) playerIn.getPosX();
            int y = (int) playerIn.getPosY();
            int x = (int) playerIn.getPosZ();
            ItemStack hand = playerIn.getItemStackFromSlot(EquipmentSlotType.HAND);
            int enchantLevel = EnchantmentHelper.getEnchantmentLevel(EnchantmentInit.MAGNETIC.get(), playerIn.getItemStackFromSlotType.HAND));
            int radius = 5 * enchantLevel;
            int radSq = radius * radius;
            AxisAlignedBB area = new AxisAlignedBB(player.getPosition().add(-radius, -radius, -radius), player.getPosition().add(1 + radius, 1 + radius, 1 + radius));
            List<EntityItem> items = world.getEntitiesWithinAABB(EntityItem.class, area, EntitySelectors.IS_ALIVE);
            if(EnchantmentHelper.getEnchantmentLevel(EnchantmentInit.MAGNETIC.get(), playerIn.getJeldItemMainhand()) > 0) {
                    int itemCount = 0;
                    for (EntityItem item : items){
                        if(item.getPositionVector().squareDistanceTo(traceResult.hitvec) <= radSq){
                            item.setPosition(player.posX, player.posY, player.posZ);
                            item.setPickupDelay(0);
                            itemCount++;
                        }
                    }
                }
            

Joined Sep 2019
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
That is what the nifty code…
Mon, 06/01/2020 - 13:06

That is what the nifty code I provided with you was for,

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

You can play with the speed and the bounding box, or you increase the speed and bounding box depending on your enchantment level🤔. This code will work best in a tick method by the way.