[OLD] Environment Controller and Events (Mods) - TheDeathlyCow/thermoo GitHub Wiki
here.
⚠️ This page is outdated as of Thermoo v2.0.0, however, it is left up for posterity The new page can be foundNow that we understand how temperature and wetness work, we can now move on to how the environment can affect the player through the EnvironmentController
and the events.
Environment Controller
The EnvironmentController
is an interface that provides configuration and values for wetness change per tick, passive temperature change per tick, and block heating. The implementation of this interface can be accessed through the EnvironmentManager
. A default implementation is provided by Thermoo which allows for configuration in the Thermoo Config. However, you may of course use your own implementation. If you want the events to use your own controller, you can update the default controller through the EnvironmentManager
.
Details on the default implementation of the environment controller can be found here
A very important note is that the Environment Controller does nothing if no listeners are set up for its events. See below for details on that.
Events
Thermoo comes with several built-in events that can be used by mods to add their own passive temperature and soaking changes. However, it is important to note that listeners must declare that an initial change should be applied before Thermoo will apply it; and that if any listeners wish to apply the change, it will be applied once and only once. The first point is important because it means that features such as passive freezing will only be activated if requested by a mod. If another mod or datapack wishes to implement its own version of such a system through Thermoo, then Thermoo's default environmental controller will not interfere.
Thermoo has the following events, for more details on each please refer to the javadoc:
LivingEntityEnvironmentEvents.TICK_IN_HEATED_LOCATION
LivingEntityEnvironmentEvents.TICK_HEAT_EFFECTS
LivingEntityEnvironmentEvents.TICK_IN_WET_LOCATION
PlayerEnvironmentEvents.TICK_BIOME_TEMPERATURE_CHANGE
Examples
Example: Enable the ticking of wetness
Important: Your mod MUST implement a listener like this for ALL events if the changes are to be applied by Thermoo. If multiple mods set up listeners to apply the initial change, and they do so correctly, then the change will only be applied once for each invocation of the event. An example of this done for all events can be found in the test mod
// Add listener to mod initializer
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
LivingEntityEnvironmentEvents.TICK_IN_WET_LOCATION.register(
(controller, entity, result) -> {
// Tells Thermoo that the initial change should be applied. If this is never called, then the initial change will not be applied
result.applyInitialChange(entity);
}
);
}
}
The Default implementation
Thermoo provides a default implementation of the Environment Controller, to allow for its many many different values to be tweaked by mod pack authors and players. Detailed information on each part of the config can be found on the config page
Extending the Controller
The EnvironmentController implements a decorator pattern that allows for mods to apply their own changes to it dynamically. If you want to add your own functionality to the environment controller, you must extend EnvironmentControllerDecorator
and write your changes there. Then you register your decorator using EnvironmentManager#addController
, with the constructor passed as the argument. For example, EnvironmentManager.INSTANCE.addController(MyControllerDecorator::new)
UML Class diagram of Environment contoller decorator:
@GameTest
s
Example: Mocking Environment Controller for You can create custom environment controllers for use in tests, to prevent some mechanics (such as passive freezing) from interfering with your other tests, but still retain other functionality, like so:
@BeforeBatch(batchId = "hotFloorTests")
public void mockController(ServerWorld serverWorld) {
// Decorates the current controller with a new anonymously-implemented mock controller
EnvironmentManager.INSTANCE.addController(
controller ->
new EnvironmentControllerDecorator(controller) {
@Override
public int getLocalTemperatureChange(World world, BlockPos pos) {
return 0;
}
@Override
public int getHeatAtLocation(World world, BlockPos pos) {
return 0;
}
@Override
public int getOnFireWarmthRate(LivingEntity entity) {
return 0;
}
}
);
}
@AfterBatch(batchId = "hotFloorTests")
public void resetController(ServerWorld serverWorld) {
// Removes the last controller added (in our case, the mock controller)
EnvironmentManager.INSTANCE.peelController();
}
That's it for the mod-specific guide. However, I'd also strongly recommend checking out parts of the Datapack guide too, particularly Temperature Effects and Tags, as they are likely to be highly relevant to your mod as well.