1
0

Merge pull request #1139 from Howaner/Hooks

Add hook HOOK_PLAYER_FOOD_LEVEL_CHANGE
This commit is contained in:
Mattes D 2014-07-02 16:33:24 +02:00
commit 9204c8a124
8 changed files with 94 additions and 28 deletions

View File

@ -0,0 +1,27 @@
return
{
HOOK_PLAYER_FOOD_LEVEL_CHANGE =
{
CalledWhen = "Called before the player food level changed. Plugin may override",
DefaultFnName = "OnPlayerFoodLevelChange", -- also used as pagename
Desc = [[
This hook is called before the food level changes.
The food level is not changed yet, plugins may choose
to refuse the change.
]],
Params =
{
{ Name = "Player", Type = "{{cPlayer}}", Notes = "The player who changes the food level." },
{ Name = "NewFoodLevel", Type = "number", Notes = "The new food level." },
},
Returns = [[
If the function returns false or no value, the next plugin's callback is called. Afterwards, the
server changes the food level of the player. If the function returns true, no
other callback is called for this event and the player's food level doesn't change.
]],
}, -- HOOK_PLAYER_FOOD_LEVEL_CHANGE
};

View File

@ -72,6 +72,7 @@ public:
virtual bool OnPlayerEating (cPlayer & a_Player) = 0; virtual bool OnPlayerEating (cPlayer & a_Player) = 0;
virtual bool OnPlayerFished (cPlayer & a_Player, const cItems & a_Reward) = 0; virtual bool OnPlayerFished (cPlayer & a_Player, const cItems & a_Reward) = 0;
virtual bool OnPlayerFishing (cPlayer & a_Player, cItems & a_Reward) = 0; virtual bool OnPlayerFishing (cPlayer & a_Player, cItems & a_Reward) = 0;
virtual bool OnPlayerFoodLevelChange (cPlayer & a_Player, int a_NewFoodLevel) = 0;
virtual bool OnPlayerJoined (cPlayer & a_Player) = 0; virtual bool OnPlayerJoined (cPlayer & a_Player) = 0;
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) = 0; virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) = 0;
virtual bool OnPlayerMoved (cPlayer & a_Player) = 0; virtual bool OnPlayerMoved (cPlayer & a_Player) = 0;

View File

@ -715,6 +715,26 @@ bool cPluginLua::OnPlayerEating(cPlayer & a_Player)
bool cPluginLua::OnPlayerFoodLevelChange(cPlayer & a_Player, int a_NewFoodLevel)
{
cCSLock Lock(m_CriticalSection);
bool res = false;
cLuaRefs & Refs = m_HookMap[cPluginManager::HOOK_PLAYER_FOOD_LEVEL_CHANGE];
for (cLuaRefs::iterator itr = Refs.begin(), end = Refs.end(); itr != end; ++itr)
{
m_LuaState.Call((int)(**itr), &a_Player, a_NewFoodLevel, cLuaState::Return, res);
if (res)
{
return true;
}
}
return false;
}
bool cPluginLua::OnPlayerFished(cPlayer & a_Player, const cItems & a_Reward) bool cPluginLua::OnPlayerFished(cPlayer & a_Player, const cItems & a_Reward)
{ {
cCSLock Lock(m_CriticalSection); cCSLock Lock(m_CriticalSection);

View File

@ -95,6 +95,7 @@ public:
virtual bool OnPlayerEating (cPlayer & a_Player) override; virtual bool OnPlayerEating (cPlayer & a_Player) override;
virtual bool OnPlayerFished (cPlayer & a_Player, const cItems & a_Reward) override; virtual bool OnPlayerFished (cPlayer & a_Player, const cItems & a_Reward) override;
virtual bool OnPlayerFishing (cPlayer & a_Player, cItems & a_Reward) override; virtual bool OnPlayerFishing (cPlayer & a_Player, cItems & a_Reward) override;
virtual bool OnPlayerFoodLevelChange (cPlayer & a_Player, int a_NewFoodLevel) override;
virtual bool OnPlayerJoined (cPlayer & a_Player) override; virtual bool OnPlayerJoined (cPlayer & a_Player) override;
virtual bool OnPlayerMoved (cPlayer & a_Player) override; virtual bool OnPlayerMoved (cPlayer & a_Player) override;
virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override; virtual bool OnPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status) override;

View File

@ -695,6 +695,25 @@ bool cPluginManager::CallHookPlayerEating(cPlayer & a_Player)
bool cPluginManager::CallHookPlayerFoodLevelChange(cPlayer & a_Player, int a_NewFoodLevel)
{
FIND_HOOK(HOOK_PLAYER_FOOD_LEVEL_CHANGE);
VERIFY_HOOK;
for (PluginList::iterator itr = Plugins->second.begin(); itr != Plugins->second.end(); ++itr)
{
if ((*itr)->OnPlayerFoodLevelChange(a_Player, a_NewFoodLevel))
{
return true;
}
}
return false;
}
bool cPluginManager::CallHookPlayerFished(cPlayer & a_Player, const cItems a_Reward) bool cPluginManager::CallHookPlayerFished(cPlayer & a_Player, const cItems a_Reward)
{ {
FIND_HOOK(HOOK_PLAYER_FISHED); FIND_HOOK(HOOK_PLAYER_FISHED);

View File

@ -87,6 +87,7 @@ public: // tolua_export
HOOK_PLAYER_EATING, HOOK_PLAYER_EATING,
HOOK_PLAYER_FISHED, HOOK_PLAYER_FISHED,
HOOK_PLAYER_FISHING, HOOK_PLAYER_FISHING,
HOOK_PLAYER_FOOD_LEVEL_CHANGE,
HOOK_PLAYER_JOINED, HOOK_PLAYER_JOINED,
HOOK_PLAYER_LEFT_CLICK, HOOK_PLAYER_LEFT_CLICK,
HOOK_PLAYER_MOVING, HOOK_PLAYER_MOVING,
@ -188,6 +189,7 @@ public: // tolua_export
bool CallHookPlayerEating (cPlayer & a_Player); bool CallHookPlayerEating (cPlayer & a_Player);
bool CallHookPlayerFished (cPlayer & a_Player, const cItems a_Reward); bool CallHookPlayerFished (cPlayer & a_Player, const cItems a_Reward);
bool CallHookPlayerFishing (cPlayer & a_Player, cItems a_Reward); bool CallHookPlayerFishing (cPlayer & a_Player, cItems a_Reward);
bool CallHookPlayerFoodLevelChange (cPlayer & a_Player, int a_NewFoodLevel);
bool CallHookPlayerJoined (cPlayer & a_Player); bool CallHookPlayerJoined (cPlayer & a_Player);
bool CallHookPlayerMoving (cPlayer & a_Player); bool CallHookPlayerMoving (cPlayer & a_Player);
bool CallHookPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status); bool CallHookPlayerLeftClick (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, char a_Status);

View File

@ -1223,9 +1223,7 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, e
{ {
// A plugin won't let us eat, abort (send the proper packets to the client, too): // A plugin won't let us eat, abort (send the proper packets to the client, too):
m_Player->AbortEating(); m_Player->AbortEating();
return;
} }
return;
} }
else else
{ {

View File

@ -37,9 +37,9 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName)
: super(etPlayer, 0.6, 1.8) : super(etPlayer, 0.6, 1.8)
, m_bVisible(true) , m_bVisible(true)
, m_FoodLevel(MAX_FOOD_LEVEL) , m_FoodLevel(MAX_FOOD_LEVEL)
, m_FoodSaturationLevel(5) , m_FoodSaturationLevel(5.0)
, m_FoodTickTimer(0) , m_FoodTickTimer(0)
, m_FoodExhaustionLevel(0) , m_FoodExhaustionLevel(0.0)
, m_FoodPoisonedTicksRemaining(0) , m_FoodPoisonedTicksRemaining(0)
, m_LastJumpHeight(0) , m_LastJumpHeight(0)
, m_LastGroundHeight(0) , m_LastGroundHeight(0)
@ -521,7 +521,15 @@ void cPlayer::Heal(int a_Health)
void cPlayer::SetFoodLevel(int a_FoodLevel) void cPlayer::SetFoodLevel(int a_FoodLevel)
{ {
m_FoodLevel = std::max(0, std::min(a_FoodLevel, (int)MAX_FOOD_LEVEL)); int FoodLevel = std::max(0, std::min(a_FoodLevel, (int)MAX_FOOD_LEVEL));
if (cRoot::Get()->GetPluginManager()->CallHookPlayerFoodLevelChange(*this, FoodLevel))
{
m_FoodSaturationLevel = 5.0;
return;
}
m_FoodLevel = FoodLevel;
SendHealth(); SendHealth();
} }
@ -572,10 +580,8 @@ bool cPlayer::Feed(int a_Food, double a_Saturation)
return false; return false;
} }
m_FoodLevel = std::min(a_Food + m_FoodLevel, (int)MAX_FOOD_LEVEL); SetFoodSaturationLevel(m_FoodSaturationLevel + a_Saturation);
m_FoodSaturationLevel = std::min(m_FoodSaturationLevel + a_Saturation, (double)m_FoodLevel); SetFoodLevel(m_FoodLevel + a_Food);
SendHealth();
return true; return true;
} }
@ -969,7 +975,7 @@ void cPlayer::Respawn(void)
// Reset food level: // Reset food level:
m_FoodLevel = MAX_FOOD_LEVEL; m_FoodLevel = MAX_FOOD_LEVEL;
m_FoodSaturationLevel = 5; m_FoodSaturationLevel = 5.0;
// Reset Experience // Reset Experience
m_CurrentXp = 0; m_CurrentXp = 0;
@ -1902,9 +1908,6 @@ void cPlayer::HandleFood(void)
return; return;
} }
// Remember the food level before processing, for later comparison
int LastFoodLevel = m_FoodLevel;
// Heal or damage, based on the food level, using the m_FoodTickTimer: // Heal or damage, based on the food level, using the m_FoodTickTimer:
if ((m_FoodLevel > 17) || (m_FoodLevel <= 0)) if ((m_FoodLevel > 17) || (m_FoodLevel <= 0))
{ {
@ -1917,7 +1920,7 @@ void cPlayer::HandleFood(void)
{ {
// Regenerate health from food, incur 3 pts of food exhaustion: // Regenerate health from food, incur 3 pts of food exhaustion:
Heal(1); Heal(1);
m_FoodExhaustionLevel += 3; m_FoodExhaustionLevel += 3.0;
} }
else if ((m_FoodLevel <= 0) && (m_Health > 1)) else if ((m_FoodLevel <= 0) && (m_Health > 1))
{ {
@ -1939,24 +1942,19 @@ void cPlayer::HandleFood(void)
} }
// Apply food exhaustion that has accumulated: // Apply food exhaustion that has accumulated:
if (m_FoodExhaustionLevel >= 4) if (m_FoodExhaustionLevel >= 4.0)
{ {
m_FoodExhaustionLevel -= 4; m_FoodExhaustionLevel -= 4.0;
if (m_FoodSaturationLevel >= 1) if (m_FoodSaturationLevel >= 1.0)
{ {
m_FoodSaturationLevel -= 1; m_FoodSaturationLevel -= 1.0;
} }
else else
{ {
m_FoodLevel = std::max(m_FoodLevel - 1, 0); SetFoodLevel(m_FoodLevel - 1);
} }
} }
if (m_FoodLevel != LastFoodLevel)
{
SendHealth();
}
} }