Page 1 of 1

Weapon Cleaner

Posted: Sun Nov 06, 2016 1:32 am
by decompile
Hey!

Im requesting a small plugin which should do following:

If a player equips a weapon from the ground, it should be dropped and instantly removed.

Same shoulda work if a weapon has been dropped from the player on the ground, it should be removed instantly too. (Thinking of you spawn with an usp, and you drop it on the ground)

Thanks

Re: Weapon Cleaner

Posted: Sun Nov 06, 2016 9:15 am
by Ayuto

Syntax: Select all

from entities.hooks import EntityPreHook
from entities.hooks import EntityCondition

import memory

from entities.entity import Entity
from entities.entity import BaseEntity

@EntityPreHook(EntityCondition.is_player, 'bump_weapon')
def pre_bump_weapon(args):
player = memory.make_object(Entity, args[0])
weapon = memory.make_object(Entity, args[1])

# Player received weapon through GiveNamedItem (e.g. on spawn)
# Maybe there is a better solution for this.
if player.origin == weapon.origin:
return

weapon.remove()
return False

@EntityPreHook(EntityCondition.is_player, 'drop_weapon')
def pre_drop_weapon(args):
weapon_ptr = args[1]
if not weapon_ptr:
return

weapon = memory.make_object(BaseEntity, weapon_ptr)
weapon.remove()
return False

Re: Weapon Cleaner

Posted: Sun Nov 06, 2016 9:20 am
by iPlayer
I've had weird random crashes when removing an entity in a pre-hook without preventing function from further executing. Invincible told me it happened because function may try to continue operating on a removed entity. So I'd

Syntax: Select all

return False
in both hooks after removing it.

Re: Weapon Cleaner

Posted: Sun Nov 06, 2016 9:54 am
by Ayuto
Yes, that a good point. Thank you!

Re: Weapon Cleaner

Posted: Sun Nov 06, 2016 2:35 pm
by iPlayer
After a second thought... DropWeapon function actually does some important stuff. It actually removes weapon inthandle from player's properties. To be certain, from m_hMyWeapons array, that is used, for instance, here. If we prevent this function from execution, I suspect that weapon's inthandle won't be removed and further attempts to iter over player's weapons would raise. Player.weapon_indexes itself will raise.

So my suggestion: replace second hook with a PostHook. I can confirm that weapon pointer will be valid, no need to do workarounds, at least for Windows, CS:S. But! In some cases (read weapon_knife) DropWeapon will remove the weapon on its own. So you'll also have to be ready for NULL pointer. In case of a post-hook on this function the following lines

Syntax: Select all

if not weapon_ptr:
return
are indeed needed.