(IDEA) Entity can pick up items

Started by ImSeyo on

Topic category: Feature requests and ideas for MCreator

Last seen on 08:55, 4. Oct 2024
Joined Oct 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
(IDEA) Entity can pick up items
Sat, 07/06/2024 - 08:15 (edited)

When creating my mod, I needed a function so that my essence could pick up items. I searched the whole Internet. As it turned out, MCreator entities cannot do this. Then I had to dig through the entity code and create my own code in AI and goals. Of course, I'm not a programmer and just used Chat GPT. After a bunch of attempts, the code was able to compile correctly. I really hope that you will add this to MCreator so that you can also interact with variables, or so that the entity can pick up certain items. If you see ENTITY_NAMEEntity in the code, then you should replace ENTITY_NAME with the name of your entity, and the last Entity should remain. For example, to have ZombieEntity instead of ENTITY_NAMEEntity.
Here is the code itself.

        this.goalSelector.addGoal(1, new PickUpItemGoal(this));
   }

   static class PickUpItemGoal extends Goal {
       private final ENTITY_NAMEEntity entity;

       public PickUpItemGoal(ENTITY_NAMEEntity entity) {
           this.entity = entity;
       }

       @Override
       public boolean canUse() {
           List<ItemEntity> items = this.entity.level().getEntitiesOfClass(ItemEntity.class, this.entity.getBoundingBox().inflate(1.0D, 1.0D, 1.0D));
           return !items.isEmpty();
       }

       @Override
       public void tick() {
           List<ItemEntity> items = this.entity.level().getEntitiesOfClass(ItemEntity.class, this.entity.getBoundingBox().inflate(1.0D, 1.0D, 1.0D));
           for (ItemEntity item : items) {
               if (!item.hasPickUpDelay() && item.isAlive()) {
                   ItemStack itemStack = item.getItem().copy();
                   for (int i = 0; i < this.entity.inventory.getSlots(); i++) {
                       ItemStack remaining = this.entity.inventory.insertItem(i, itemStack, false);
                       if (remaining.isEmpty()) {
                           item.remove(RemovalReason.DISCARDED);
                           break;
                       } else {
                           itemStack = remaining;
                       }
                   }
               }
           }
       }

Edited by ImSeyo on Sat, 07/06/2024 - 08:15