Cuberite Forum
I'd like to write a plugin to do x but there is no hook for it - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Cuberite (https://forum.cuberite.org/forum-4.html)
+--- Forum: Development (https://forum.cuberite.org/forum-13.html)
+--- Thread: I'd like to write a plugin to do x but there is no hook for it (/thread-1635.html)

Pages: 1 2


I'd like to write a plugin to do x but there is no hook for it - lost_RD - 10-27-2014

Specifically, I want to modify the void cPlayer::HandleFood(void) function (https://github.com/mc-server/MCServer/blob/master/src/Entities/Player.cpp#L1934) in order to disable natural regen to emulate the Ultra Hardcore game mode.

Having said that, while digging around the code to find the part that I would want to modify, I also found that one could also lower or raise the threshold at which natural regen occurs as well as extending the time between food tick timer loops. I'd prefer to be able to change these things with a plugin rather than having to recompile every time I change for testing purposes (because what else are plugins for?) and since I am proficient with Lua I'm now wondering how to go about this. Is there a tutorial/guide somewhere?

Firstly, does this new functionality require a new hook? HOOK_PLAYER_FOOD_TICK perhaps? Its function could be:

Code:
function OnPlayerFoodTick(Player, FoodTickTimer)

The only issue with this approach is it doesn't give the chance to modify the hunger threshold for doing food ticks (m_FoodLevel >= 18 by default). Should this whole plugin be done instead with HOOK_TICK? That way I could just replace cplayer::HandleFood with some Lua:

Code:
Player:HandleFood()

    foodThreshold = 18
    foodTickCycle = 80
    foodExhaustion = 3

    if IsGameModeCreative() then
        // Hunger is disabled for Creative
        return
    end

    // Apply food exhaustion that has accumulated:
    if Player.FoodExhaustionLevel > 4 then
        Player.FoodExhaustionLevel = FoodExhaustionLevel - 4
        if Player.FoodSaturationLevel > 0 then
            Player.FoodSaturationLevel = math.max(Player.FoodSaturationLevel - 1, 0)
        else
            Player:SetFoodLevel(Player.FoodLevel - 1)
        end
    end

    // Heal or damage, based on the food level, using the m_FoodTickTimer:
    if Player.FoodLevel >= foodThreshold  or Player.FoodLevel <= 0 then
    {
        Player.FoodTickTimer = Player.FoodTickTimer + 1
        if Player.FoodTickTimer >= foodTickCycle then
            Player.FoodTickTimer = 0;

            if Player.FoodLevel >= foodThreshold and Player.GetHealth() < Player.GetMaxHealth() then
                // Regenerate health from food, incur x pts of food exhaustion:
                Player:Heal(1)
                Player:AddFoodExhaustion(foodExhaustion)
            elseif Player.FoodLevel <= 0 and Player.Health > 1 then
                // Damage from starving
                Player:TakeDamage(dtStarving, nullptr, 1, 1, 0)
            end
        end
    else
        Player.FoodTickTimer = 0
    end
end

Anyway, I'm super excited about being able to write plugins in Lua because I'm very comfortable with Lua, I'm just not sure how high the barrier for entry to this project is and am looking for guidance on the matter.

I'm new to the project as of today having seen reference to it in /r/admincraft and am already very impressed by everything I've seen! Except the lack of tutorials I guess Wink


RE: I'd like to write a plugin to do x but there is no hook for it - Seadragon91 - 10-27-2014

There is a hook here
http://mc-server.xoft.cz/LuaAPI/OnPlayerFoodLevelChange.html
that will get called if the food level of the player changes.


RE: I'd like to write a plugin to do x but there is no hook for it - NiLSPACE - 10-27-2014

Hello and welcome to the forum Smile

You might want to look into the OnPlayerFoodLevelChange hook. If you have any questions about the API please ask them. If you're looking for a tutorial then we have one here. It should at least help you in the beginning Smile


RE: I'd like to write a plugin to do x but there is no hook for it - lost_RD - 10-27-2014

The aim is to prevent health from regenerating - the food level changing is not the hook I want, the health level changing is what I want. Unless I'm missing something about how the health regen code works and how it relates to the food level change hook?


RE: I'd like to write a plugin to do x but there is no hook for it - NiLSPACE - 10-27-2014

I thought we had a hook for that, but apparently not. This should be added. You could make an issue for it on github, but instead of "OnPlayerFoodTick" it should probably be called something like "OnEntityHealthChange".


RE: I'd like to write a plugin to do x but there is no hook for it - Howaner - 10-27-2014

Or HOOK_PLAYER_HEALTH_REGENERATION ?


RE: I'd like to write a plugin to do x but there is no hook for it - xoft - 10-27-2014

I think the new hook should only be about the player health regeneration, since that's the only case not already covered. Any entities losing health (combat, fall damage, ...) is already covered in the OnTakeDamage hook.


RE: I'd like to write a plugin to do x but there is no hook for it - xoft - 10-27-2014

Also, I think I'd prefer a hook-based solution to one where all the params could be settable. There are hundreds of params that could be set like that and adding access to all of them would be wasteful. Also, that would need some place to initialize the param for each entity, and to specify a default and... you get the point.


RE: I'd like to write a plugin to do x but there is no hook for it - lost_RD - 10-27-2014

And potions would not be covered by HOOK_PLAYER_HEALTH_REGENERATION because they are already covered by HOOK_ENTITY_ADD_EFFECT, right?

(10-27-2014, 03:43 AM)xoft Wrote: Also, I think I'd prefer a hook-based solution to one where all the params could be settable. There are hundreds of params that could be set like that and adding access to all of them would be wasteful. Also, that would need some place to initialize the param for each entity, and to specify a default and... you get the point.

So basically it's a lot of work to expose the inner working to the plugin API? Or is the main issue that it would add more bloat than the average player needs? Either way, fair enough, I don't have any real plans that require access to these params. A super customised server is already made possible by this project being open-source so no complaints from me!


RE: I'd like to write a plugin to do x but there is no hook for it - xoft - 10-27-2014

OnEntityAddEffect is only fired for when the effect is added to the player (start the countdown, add particles etc.) There's no exact hook that is fired when the effect is actually being carried out. Good point on pointing that out, even non-players can gain health from potions, so we might want to consider the OnEntityHealthRegeneration hook, that would be fired for all instances of an entity gaining health, with a parameter telling what the source of the regeneration is - whether it's food-based, or potion-based. Can we think of any other source?