Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Entity Brain | Attributes

GamerCoder edited this page Feb 24, 2023 · 3 revisions

Attributes are used to store numerical data on an entity for an entity's functions, such as attack knockback, movement speed, and more. This guide will show you how to make your own, and apply them to entities.

Get & Modify Attribute Instance

Want to let passive mobs attack people? They need Attack Damage and Attack Knockback attributes. MobChip will allow you to do that!

The attribute will be added automatically if it does not exist.

import me.gamercoder215.mobchip.EntityBrain;
import me.gamercoder215.mobchip.ai.attribute.AttributeInstance;
import me.gamercoder215.mobchip.ai.attribute.EntityAttribute;
import me.gamercoder215.mobchip.BukkitBrain;

public class MyPlugin extends JavaPlugin {
    
    public void addAttack(Cow c) {
        EntityBrain brain = BukkitBrain.getBrain(c);
        
        // Extends Bukkit AttributeInstance
        AttributeInstance attack = brain.getAttributeInstance(EntityAttribute.GENERIC_ATTACK_DAMAGE);
        attack.setBaseValue(3.0D);
        attack.addModifier(new AttributeModifier(UUID.randomUUID(), "modifier-1", 5.0D, AttributeModifier.Operation.MULTIPLY_SCALAR_1)); // Supports Bukkit Attribute Modifiers

        AttributeInstance knockback = brain.getAttributeInstance(EntityAttribute.GENERIC_ATTACK_KNOCKBACK);
        knockback.setBaseValue(1.0D);
    }
}

Create a Custom Attribute

This will allow you to create your own Attribute that is natively stored in the entity and can be modified with Vanilla Minecraft Commands.

import me.gamercoder215.mobchip.EntityBrain;
import me.gamercoder215.mobchip.ai.attribute.AttributeInstance;
import me.gamercoder215.mobchip.ai.attribute.EntityAttribute;
import me.gamercoder215.mobchip.BukkitBrain;

public class MyPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        NamespacedKey key = new NamespacedKey(this, "my_attribute");
        EntityAttribute.builder()
            .setKey(key);
            .setMinValue(-1024.0D); // The minimum value of the attribute
            .setMaxValue(Double.MAX_VALUE); // The maximum value of the attribute
            .setDefaultValue(0D); // The default value of the attribute
            .setClientSide(false); // Whether functionality related to the attribute should only be available to the client
            .build(); // Build registers the attribute automatically, will throw errors if key is already registered

        EntityAttribute myAttribute = EntityAttribute.getAttribute(key); // Fetchable after registry; will return null if not registered
    }
}