I need help fixing my code to make mobs attack me when I right click on them.

Started by zommer500 on

Topic category: Help with modding (Java Edition)

Last seen on 02:03, 26. Jul 2022
Joined May 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I need help fixing my code to make mobs attack me when I right click on them.

Hey guys, I am trying to make passive mobs attack me when I right click on them, But I have bugs in my code when I export my mod.

The problem is a import function called net.minecraft.entity.EntityLiving is not working. There is bugs that say causes build errors and stuff like that.

I also need to know how the entityliving.setattacktarget() code work, but I don't have a way to fix this.

Here's the code:

import net.minecraft.entity.EntityLiving;

public class MobbattleProcedure {
    @Mod.EventBusSubscriber
    private static class GlobalTrigger {
        @SubscribeEvent
        public static void onRightClickEntity(PlayerInteractEvent.EntityInteract event) {
            EntityLiving entity = event.getTarget();
            PlayerEntity sourceentity = event.getPlayer();
            if (event.getHand() != sourceentity.getActiveHand()) {
                return;
            }
            entity.setattacktarget(sourceentity);
            double i = event.getPos().getX();
            double j = event.getPos().getY();
            double k = event.getPos().getZ();
            IWorld world = event.getWorld();
            Map<String, Object> dependencies = new HashMap<>();
            dependencies.put("x", i);
            dependencies.put("y", j);
            dependencies.put("z", k);
            dependencies.put("world", world);
            dependencies.put("entity", entity);
            dependencies.put("sourceentity", sourceentity);
            dependencies.put("event", event);
            executeProcedure(dependencies);
        }
    }
}