Jump to content

1.19 Aggressive Passive Mobs


peuloom

Recommended Posts

Hello, people, sorry for bothering.

I'm trying to change every mob to be aggressive to the player, but I can't change their Attributes to add ATTACK_DAMAGE. Can someone help me?

Mob mob = (Mob) event.getEntity();

        if(!mob.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE)){
            mob.getAttributes().assignValues(new AttributeMap(AttributeSupplier.builder().add(Attributes.ATTACK_DAMAGE, 1).build()));
        }

        if(mob.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE)) {
            mob.setCustomName(Component.literal("Aggressive"));
            mob.goalSelector.addGoal(1, new MeleeAttackGoal((PathfinderMob) mob, 1.0, false));
            mob.goalSelector.addGoal(2, new LookAtPlayerGoal(mob, Player.class, 8.0F));
            mob.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(mob, Player.class, true));
        }

Thanks.

Link to comment
Share on other sites

Update, I've seen that I need "TransientAttributeModifiers" to do that, I've managed to change the AI properly, but always something "Pacific" attacks me, the game crashes. The .put to add Attack Damage seems to not work somehow. 

    public static Multimap<Attribute, AttributeModifier> getAttributeModifiers() {
        Multimap<Attribute, AttributeModifier> temp = LinkedHashMultimap.create();
        temp.put(Attributes.ATTACK_DAMAGE, new AttributeModifier("ATTACK_DAMAGE", 1, AttributeModifier.Operation.ADDITION));
        return temp;
    }

    @SubscribeEvent
    public static void onEntityJoinLevelEvent(EntityJoinLevelEvent event) {
        if(!(event.getEntity() instanceof PathfinderMob mob)) return;

        if(!mob.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE)){
            mob.setCustomName(Component.literal("Pacific"));
            mob.getAttributes().addTransientAttributeModifiers(getAttributeModifiers());
        }

        if(mob.getAttributes().hasAttribute(Attributes.ATTACK_DAMAGE)) {
            mob.setCustomName(Component.literal("Hostile"));
            mob.goalSelector.addGoal(1, new MeleeAttackGoal(mob, 1.0, false));
            mob.goalSelector.addGoal(2, new LookAtPlayerGoal(mob, Player.class, 8.0F));
            mob.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(mob, Player.class, true));
        }
    }

Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.