[HL2:DM] Remove Weapons on Spawn

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

[HL2:DM] Remove Weapons on Spawn

Postby Painkiller » Wed Jan 26, 2022 5:05 pm

Hello Sp Team and Community,

I have a plugin that should remove all weapons and ammunition from the server.
Except for the 357 and crossbow.
But it just does not work.
Maybe someone can help

Syntax: Select all

from entities.hooks import EntityCondition, EntityPreHook
from entities.helpers import index_from_pointer
from filters.weapons import WeaponClassIter, WeaponIter
from weapons.entity import Weapon

def remove_idle_weapons():
for w in WeaponIter.iterator():
if w.get_property_int('m_hOwnerEntity') in [-1, 0]:
w.call_input('Kill')

@EntityPreHook(EntityCondition.is_human_player, 'bump_weapon')
@EntityPreHook(EntityCondition.is_bot_player, 'bump_weapon')
def pre_pickup(args):
weapon = Weapon(index_from_pointer(args[1]))
if weapon.classname in WeaponClassIter() and not weapon.classname in ['weapon_357', 'weapon_crossbow']:
remove_idle_weapons()
return False



Greeting Painkiller
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [HL2:DM] Remove Weapons on Spawn

Postby VinciT » Mon Jan 31, 2022 3:45 pm

Try this:

Syntax: Select all

# ../remove_weapons/remove_weapons.py

# Source.Python
from events import Event
from filters.entities import EntityIter
from filters.weapons import WeaponClassIter
from listeners import OnNetworkedEntityCreated
from players.entity import Player


# Players will spawn with these weapons.
player_weapons = {'weapon_357', 'weapon_crossbow'}
# Weapons that can spawn on the map.
map_weapons = {'weapon_357', 'weapon_crossbow'}


# Set containing weapon names which will be removed.
banned_weapons = {weapon.name for weapon in WeaponClassIter()}.difference(
map_weapons)

# Dictionary that defines what ammo a weapon uses.
weapon_ammo = {
'weapon_357': ('item_ammo_357', 'item_ammo_357_large'),
'weapon_ar2': (
'item_ammo_ar2', 'item_ammo_ar2_large', 'item_ammo_ar2_altfire'
),
'weapon_crossbow': 'item_ammo_crossbow',
'weapon_pistol': ('item_ammo_pistol', 'item_ammo_pistol_large'),
'weapon_smg1': (
'item_ammo_smg1', 'item_ammo_smg1_large', 'item_ammo_smg1_grenade'
),
'weapon_shotgun': 'item_box_buckshot',
'weapon_rpg': 'item_rpg_round',
}

# Set containing names of ammo entities which will be removed.
banned_ammo = {'item_item_crate'}

# Now let's ban some ammo.
for weapon_name in banned_weapons:
# Get the name of the ammo entity.
ammo = weapon_ammo.get(weapon_name, None)

# Does this weapon even use ammo?
if ammo is None:
# Nope, skip it.
continue

# Are there multiple ammo entities for this weapon?
if isinstance(ammo, tuple):
banned_ammo.update(ammo)

# Or just one?
else:
banned_ammo.add(ammo)


# Combine both the weapons and ammo into a single set.
banned_entities = banned_weapons.union(banned_ammo)


class PlayerRW(Player):
"""Extended Player class."""
caching = True

def adjust_weapons(self):
"""Gives the player weapons specified in the 'player_weapons' set."""
# First let's remove all of their weapons.
for weapon in self.weapons():
weapon.remove()

# And then we shall give them the specified weapons.
for weapon_name in player_weapons:
self.give_named_item(weapon_name)


def load():
"""Called when the plugin gets loaded."""
for entity in EntityIter():
if entity.classname in banned_entities:
entity.delay(0, entity.remove)


@Event('player_spawn')
def player_spawn(event):
"""Called when a player spawns."""
player = PlayerRW.from_userid(event['userid'])
player.delay(0, player.adjust_weapons)


@OnNetworkedEntityCreated
def on_networked_entity_created(entity):
"""Called when a networked entity gets created."""
if entity.classname in banned_entities:
# We need to delay the next check by a single frame because the
# 'owner_handle' property hasn't been set yet.
entity.delay(0, delayed_check, (entity,))


def delayed_check(entity):
"""Removes the entity if it doesn't belong to anyone."""
if entity.owner_handle == -1:
entity.remove()
Last edited by VinciT on Thu Feb 10, 2022 3:37 pm, edited 1 time in total.
ImageImageImageImageImage
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Remove Weapons on Spawn

Postby Painkiller » Mon Jan 31, 2022 3:54 pm

Thank you, even with ammunition that is perfect.

Also works without problem.

Greeting Painkiller
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [HL2:DM] Remove Weapons on Spawn

Postby VinciT » Mon Jan 31, 2022 4:01 pm

Sweet, I'm glad it's working properly.
ImageImageImageImageImage
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Remove Weapons on Spawn

Postby Painkiller » Wed Feb 09, 2022 2:22 pm

Another option would be very interesting!

At the moment you can add the exceptions which is good but the problem is that you spawn with all these weapons.
Which is not so good.

Greets Painkiller
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [HL2:DM] Remove Weapons on Spawn

Postby VinciT » Thu Feb 10, 2022 3:47 pm

I've updated the plugin to separate weapons the player spawns with and the ones that can spawn on the map.
ImageImageImageImageImage
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Remove Weapons on Spawn

Postby Painkiller » Thu Feb 10, 2022 4:00 pm

Thank you that works great. Thank you very much.

Edit: I'll just add these missing weapons below ?

Code: Select all

   
    'weapon_shotgun': 'item_box_buckshot',
    'weapon_rpg': 'item_rpg_round',
    'weapon_slam': '',
    'weapon_frag': '',
    'weapon_crowbar': '',
    'weapon_stunstick': '',
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [HL2:DM] Remove Weapons on Spawn

Postby VinciT » Thu Feb 10, 2022 4:14 pm

Painkiller wrote:Edit: I'll just add these missing weapons below ?

Code: Select all

   
    'weapon_shotgun': 'item_box_buckshot',
    'weapon_rpg': 'item_rpg_round',
    'weapon_slam': '',
    'weapon_frag': '',
    'weapon_crowbar': '',
    'weapon_stunstick': '',
There's no need, the weapon_ammo dictionary is only used for weapons that have ammo items.
ImageImageImageImageImage
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Remove Weapons on Spawn

Postby Painkiller » Thu Feb 10, 2022 4:25 pm

Ok, I have tried to add something like a combine_ball ammunition so
"item_ammo_ar2_altfire"
As this does not exist on some maps.
The problem is when I have 3 max I have to shoot three times.
"item_ammo_ar2_altfire" three times.

It will probably do the same with smg1 grenades and rpg ammo.

But i think it doesn't matter i can put it in as many times as i want.

Syntax: Select all

# Players will spawn with these weapons.
player_weapons = {'weapon_crowbar', 'weapon_physcannon', 'item_ammo_ar2_altfire', 'item_ammo_ar2_altfire', 'item_ammo_ar2_altfire'}

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 25 guests