Skip to content

Commit

Permalink
Add reactivating brain sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
2No2Name committed Jan 17, 2025
1 parent 588a34b commit 919c74c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,31 @@ public static void disableSensor(LivingEntity brainedEntity, SensorType<?> senso
if (sensor instanceof SensorAccessor sensorAccessor) {
//Disable the sensor by setting the maximum last sense time, which will make it count down almost forever
// Removing the whole sensor could be an issue, since it may be serialized and used in a future version.
sensorAccessor.setLastSenseTime(Long.MAX_VALUE);

//Instead of setting to Long.MAX_VALUE, we want to be able to recover the random offset of the sensor:
long lastSenseTime = sensorAccessor.getLastSenseTime();
int senseInterval = sensorAccessor.getSenseInterval(); //Usual values: 20,40,80,200

long maxMultipleOfSenseInterval = Long.MAX_VALUE - (Long.MAX_VALUE % senseInterval);
maxMultipleOfSenseInterval -= senseInterval;
maxMultipleOfSenseInterval += lastSenseTime;

sensorAccessor.setLastSenseTime(maxMultipleOfSenseInterval);
}
}

public static void enableSensor(LivingEntity brainedEntity, SensorType<?> sensorType) {
Brain<?> brain = brainedEntity.getBrain();
Sensor<?> sensor = ((BrainAccessor<?>) brain).getSensors().get(sensorType);
if (sensor instanceof SensorAccessor sensorAccessor) {
long lastSenseTime = sensorAccessor.getLastSenseTime();
int senseInterval = sensorAccessor.getSenseInterval();

//Recover the random offset of the sensor:
if (lastSenseTime > senseInterval) {
lastSenseTime = lastSenseTime % senseInterval;
}
sensorAccessor.setLastSenseTime(lastSenseTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
@Mixin(Sensor.class)
public interface SensorAccessor {

@Accessor("lastSenseTime")
long getLastSenseTime();

@Accessor("senseInterval")
int getSenseInterval();

@Accessor("lastSenseTime")
void setLastSenseTime(long lastSenseTime);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.jellysquid.mods.lithium.mixin.ai.useless_sensors.goat_item_sensor;

import me.jellysquid.mods.lithium.common.ai.brain.SensorHelper;
import net.minecraft.SharedConstants;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.sensor.SensorType;
import net.minecraft.entity.passive.GoatEntity;
import net.minecraft.world.World;
Expand All @@ -28,6 +30,10 @@ protected GoatEntityMixin(EntityType<? extends LivingEntity> entityType, World w
at = @At("RETURN")
)
private void disableItemSensor(CallbackInfo ci) {
SensorHelper.disableSensor(this, SensorType.NEAREST_ITEMS);
if (!this.getBrain().hasMemoryModule(MemoryModuleType.NEAREST_VISIBLE_WANTED_ITEM)) {
SensorHelper.disableSensor(this, SensorType.NEAREST_ITEMS);
} else if (SharedConstants.isDevelopment) {
throw new IllegalStateException("Goat Entity has a nearest visible wanted item memory module! The mixin.ai.useless_sensors.goat_item_sensor should probably be removed permanently!");
}
}
}

0 comments on commit 919c74c

Please sign in to comment.