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

Entity AI | Sensors

GamerCoder edited this page Jan 20, 2023 · 3 revisions

Sensors are a built-in way for entities to perform actions when memories are present. This guide will show you how to set up your own.

Create a Sensor

How to create a basic sensor, without applying it.

import me.gamercoder215.mobchip.ai.memories.Memory;
import me.gamercoder215.mobchip.ai.memories.EntityMemory;
import me.gamercoder215.mobchip.bukkit.BukkitBrain;

public class MySensor extends CustomSensor<Drowned> { // A Sensor for a Bukkit Drowned

    public MySensor(MyPlugin plugin) {
        super(Drowned.class, new NamespacedKey(plugin, "mysensor"), 40); // Input Class, unique NamespacedKey for Sensor, and scan rate for memories every tick
    }

    @Override
    public List<Memory<?>> required() {
        return ImmutableList.of(EntityMemory.HOME); // Requires EntityMemory.HOME
    }

    @Override
    public void run(World w, LivingEntity en) {
        System.out.println("Home was found!"); // Run Action Here
    }

}

Register Sensor

In order for a Sensor to work properly, it has to be registered in the Minecraft Registry. Methods available in BukkitBrain will allow you to do this.

import me.gamercoder215.mobchip.bukkit.BukkitBrain;

public class MyPlugin extends JavaPlugin {
    
    @Override
    public void onEnable() {
        MySensor sensor = new MySensor(this);
        BukkitBrain.registerSensor(sensor);

        getLogger().info("Done!");
    }
} 

Add a Sensor

This will show you how to add a custom sensor to the Entity's Senses. The sensor must be registered.

import me.gamercoder215.mobchip.EntityBrain;
import me.gamercoder215.mobchip.bukkit.BukkitBrain;

public class MyPlugin extends JavaPlugin {
  
    public void addSensor(Drowned d) {
        EntityBrain brain = BukkitBrain.getBrain(d);

        EntitySenses senses = brain.getSenses();

        MySensor sensor = new MySensor(this);
        senses.addSensor(sensor); // Make sure sensor is registered in onEnable() before adding!
    }
}