From 1baf82db329526309c2d05581d3bc9520f03927b Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 26 Oct 2018 19:07:35 +0300 Subject: [PATCH] [Client] Avoid PlayerSpellbook packet spam in some mods --- apps/openmw/mwscript/statsextensions.cpp | 56 ++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/apps/openmw/mwscript/statsextensions.cpp b/apps/openmw/mwscript/statsextensions.cpp index fd688fd4b..d8901a86c 100644 --- a/apps/openmw/mwscript/statsextensions.cpp +++ b/apps/openmw/mwscript/statsextensions.cpp @@ -470,18 +470,24 @@ namespace MWScript // make sure a spell with this ID actually exists. MWBase::Environment::get().getWorld()->getStore().get().find (id); - ptr.getClass().getCreatureStats (ptr).getSpells().add (id); - /* - Start of tes3mp addition + Start of tes3mp change (major) + + Only add the spell if the target doesn't already have it - Send an ID_PLAYER_SPELLBOOK packet every time a player gains a spell - through a script + Send an ID_PLAYER_SPELLBOOK packet every time a player gains a spell here */ - if (ptr == MWMechanics::getPlayer()) - mwmp::Main::get().getLocalPlayer()->sendSpellChange(id, mwmp::SpellbookChanges::ADD); + MWMechanics::Spells &spells = ptr.getClass().getCreatureStats(ptr).getSpells(); + + if (!spells.hasSpell(id)) + { + spells.add(id); + + if (mwmp::Main::get().getLocalPlayer()->isLoggedIn() && ptr == MWMechanics::getPlayer()) + mwmp::Main::get().getLocalPlayer()->sendSpellChange(id, mwmp::SpellbookChanges::ADD); + } /* - End of tes3mp addition + End of tes3mp change (major) */ } }; @@ -498,26 +504,32 @@ namespace MWScript std::string id = runtime.getStringLiteral (runtime[0].mInteger); runtime.pop(); - ptr.getClass().getCreatureStats (ptr).getSpells().remove (id); + /* + Start of tes3mp change (major) + + Only remove the spell if the target has it - MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager(); + Send an ID_PLAYER_SPELLBOOK packet every time a player loses a spell here + */ + MWMechanics::Spells &spells = ptr.getClass().getCreatureStats(ptr).getSpells(); - if (ptr == MWMechanics::getPlayer() && - id == wm->getSelectedSpell()) + if (spells.hasSpell(id)) { - wm->unsetSelectedSpell(); - } + ptr.getClass().getCreatureStats(ptr).getSpells().remove(id); - /* - Start of tes3mp addition + if (ptr == MWMechanics::getPlayer()) + { + MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager(); - Send an ID_PLAYER_SPELLBOOK packet every time a player loses a spell - through a script - */ - if (ptr == MWMechanics::getPlayer()) - mwmp::Main::get().getLocalPlayer()->sendSpellChange(id, mwmp::SpellbookChanges::REMOVE); + if (id == wm->getSelectedSpell()) + wm->unsetSelectedSpell(); + + if (mwmp::Main::get().getLocalPlayer()->isLoggedIn()) + mwmp::Main::get().getLocalPlayer()->sendSpellChange(id, mwmp::SpellbookChanges::REMOVE); + } + } /* - End of tes3mp addition + End of tes3mp change (major) */ } };