I want to create a simulation, where a human-looking mob (e. g. villager) collects food and if it fails to collect enough fast enough, dies of hunger.
My plugin will be implement the food collection algorithm. I’d like to reuse the health management code (i. e. ideally I won’t implement the code, which decides, whether the mob died from hunger or not).
Are there any mobs (preferably human-looking), which
a) die, if they don’t get enough food in time and
b) which I can re-use in my Sponge plugins
b) You can reuse every entity for your plugin(human looking: villager, fake player, zombie, zombie pigman, witch)
a) You have probably played Minecraft, so you know there aren’t NPCs that even have a hunger bar.
Well, villagers won’t reproduce without enough food, and have an inventory that can be filled with food items. They even collect crops and share food by themselves. But they won’t die.
As far as I know, no mobs or players die from hunger (unless hardcore got harder recently). So no matter what you’ll need to do some kind of health management.
See: http://minecraft.gamepedia.com/Hunger On Easy mode, starvation damage will not lower you below half your maximum health, while on Normal mode, it can reduce you to a single hit point. On Hard mode, starvation can kill. Interestingly, the Protection enchantment on armor reduces starvation damage.
In my plugin class (Bitbucket) I create a command, which spawns a villager:
class AlmaEconSimPlugin {
@Inject private var logger: Logger? = null
@Inject private var game: Game? = null
@Listener
fun onPreInit(event: GamePreInitializationEvent) {
val gm = game
if (gm == null) {
return;
}
[...]
gm.commandManager.register(
this,
createSpawnMortal(),
"spm"
)
}
[...]
private fun createSpawnMortal(): CommandCallable? {
return CommandSpec
.builder()
.description(Text.of("Spawns a villager, which dies of hunger, if he or she doesn't eat enough"))
.executor(SpawnMortalCommand()).build()
}
[...]
}
Because there is no ChangeTimeEvent(which would be fired every tick except the gamerule doDaylightCircle is disabled) I would use SendCommandEvent. However there can be other plugins that change the time automatically. Don’t know how to catch these. You could compare every tick the current time with one tick before, but that doesn’t seen to be good IMO.
You can also use getWorldTime store the time, and periodically check the current value against the stored value. You will be able to detect the change of day this way.