Raised This Month: $32 Target: $400
 8% 

TF2 Weapon Ban Code [Ya gotta help me part 2]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ABNTGeneric
Junior Member
Join Date: Sep 2018
Old 09-17-2018 , 03:11   TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #1

EDIT: PART TWO WAS ACTUALLY NOT SOLVED. ALL THREE PARTS NEED SOLVING

Alright, I got a little problem with something I've seen done a-thousand times, but can't seem to execute myself. The snippet of code below is supposed to do 3 things, can you guess what they are? Because the game can't:
Code:
public Action OnPlayerSpawn(Handle event, const char[] name, bool dontBroadcast)
{
	int client = GetClientOfUserId(GetEventInt(event, "userid"));
	TFClassType class = TF2_GetPlayerClass(client);
	TFTeam team = TF2_GetClientTeam(client);
	
	if(class == TFClass_Scout)
	{
		int weaponone = GetPlayerWeaponSlot(client, 0);
		int weapontwo = GetPlayerWeaponSlot(client, 1);
		int weaponthree = GetPlayerWeaponSlot(client, 2);
		
		int oneindex = GetEntProp(weaponone, Prop_Send, "m_iItemDefinitionIndex");
		int twoindex = GetEntProp(weapontwo, Prop_Send, "m_iItemDefinitionIndex");
		int threeindex = GetEntProp(weaponthree, Prop_Send, "m_iItemDefinitionIndex");
		
		PrintToChat(client, "Scout: Current weapon indexes are %i, %i, and %i", oneindex, twoindex, threeindex);
		
		if(team == TFTeam_Blue)
		{
                        SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", weaponthree);
			TF2_RemoveWeaponSlot(client, 0);
			
			if(twoindex != 46 || 163 || 222 || 812 || 833 || 1121 || 1145)
			{
				TF2_RemoveWeaponSlot(client, 1);
			}
		}
	}
}
Thing Number One: If the player is a Blu Scout, switch their active weapon slot to melee (slot 2)
Thing Number Two: If the player is a Blu Scout, remove their primary weapon (slot 0)
Thing Number Three: If the weapon index doesn't match the indexes of any of these weapons (46, 163, 222, 812, 833, 1121, 1145), remove their secondary weapon (slot 1)

(Those weapon indexes are Bonk!, Mad Milk, and anything else that isn't a pistol)

Simple right? That's what I thought. Here's what happens when I test it:

Problem Number One: Though it does remove weapon slot 0, and does switch the player's active weapon to slot 1, it doesn't do it immediately. Instead of spawning the Scout with weapon slot 1 active, it spawns him with weapon slot 0 active, then switches to weapon slot 1. Since there's no weapon in slot 0, you have blank hands and a t-posing Scout for the remainder of the switch time, every time you hit a resupply locker.

Problem Number Two: Though the game detects the weapon indexes correctly (tested with that PrintToChat line), it does diddly jack to the if statement. So the game knows what weapon the Scout has equipped in slot 1, but removes slot 1 regardless of whether the weapon index matches any of the given exceptions. Now what's that all about?

Problem Number Three: is a hypothetical issue. What if I decide that the Wrap Assassin is just too good, and needs to be banned? If I used the same method as Thing Number Three (given I can make it work), then Scout has the possibility of spawning with no weapons at all! That's a Texas sized problem if you ask me! How can I make it so a Scout with the Wrap Assassin equipped can spawn and have that sucker replaced with the default bat?

I should also mention real quick that OnPlayerSpawn is actually hooked to post_inventory_application, so if that's part of the problem, tell me, but I'd like this to update when you hit a resupply locker or manually select a loadout.

If possible, I would like to contain this all in a plugin, not in a server config file. This is part of a larger plugin, and I like clean, all-in-one packages.

Last edited by ABNTGeneric; 09-18-2018 at 02:27.
ABNTGeneric is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-17-2018 , 03:33   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #2

To much to read lol.

But what is this ment to do?

Code:
if(twoindex != 46 || 163 || 222 || 812 || 833 || 1121 || 1145)
__________________
Neuro Toxin is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 09-17-2018 , 04:00   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #3

Your main problem, as pointed out by Neuro Toxin, is this line:

PHP Code:
if(twoindex != 46 || 163 || 222 || 812 || 833 || 1121 || 1145
That if statement isn't doing what you think it's doing.

What you're trying to do is this:

PHP Code:
if(twoindex != 46 || twoindex != 163 || twoindex != 222 || twoindex != 812 || twoindex != 833 || twoindex != 1121 || twoindex != 1145
Explanation:

PHP Code:
// If twoindex doesn't equal 46 OR if 163 OR if 222 OR if 812 OR if 833 OR if 1121 OR if 1145
if(twoindex != 46 || 163 || 222 || 812 || 833 || 1121 || 1145)

// If twoindex doesn't equal 46 OR if twoindex doesn't equal 163 OR if twoindex doesn't equal 222 OR if twoindex doesn't equal 812 OR if twoindex doesn't equal 833 OR if twoindex doesn't equal 1121 OR if twoindex doesn't equal 1145
if(twoindex != 46 || twoindex != 163 || twoindex != 222 || twoindex != 812 || twoindex != 833 || twoindex != 1121 || twoindex != 1145
__________________
Psyk0tik is offline
mug1wara
AlliedModders Donor
Join Date: Jun 2018
Old 09-17-2018 , 13:57   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #4

You did one "|" too much

(If I'm correct)

It's either that, or I believe that "|" is just a true / false.
mug1wara is offline
ABNTGeneric
Junior Member
Join Date: Sep 2018
Old 09-17-2018 , 23:11   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #5

Quote:
Originally Posted by Crasher_3637 View Post
Your main problem, as pointed out by Neuro Toxin, is this line:

PHP Code:
if(twoindex != 46 || 163 || 222 || 812 || 833 || 1121 || 1145
That if statement isn't doing what you think it's doing.

What you're trying to do is this:

PHP Code:
if(twoindex != 46 || twoindex != 163 || twoindex != 222 || twoindex != 812 || twoindex != 833 || twoindex != 1121 || twoindex != 1145
Explanation:

PHP Code:
// If twoindex doesn't equal 46 OR if 163 OR if 222 OR if 812 OR if 833 OR if 1121 OR if 1145
if(twoindex != 46 || 163 || 222 || 812 || 833 || 1121 || 1145)

// If twoindex doesn't equal 46 OR if twoindex doesn't equal 163 OR if twoindex doesn't equal 222 OR if twoindex doesn't equal 812 OR if twoindex doesn't equal 833 OR if twoindex doesn't equal 1121 OR if twoindex doesn't equal 1145
if(twoindex != 46 || twoindex != 163 || twoindex != 222 || twoindex != 812 || twoindex != 833 || twoindex != 1121 || twoindex != 1145
This solved Problem 2, thank you very much. I don't suppose you have a fix for the other problems? It's fine if you don't, the whitelist is at least functional now, though clunky and unprofessional.
ABNTGeneric is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 09-18-2018 , 00:10   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #6

Quote:
Originally Posted by ABNTGeneric View Post
This solved Problem 2, thank you very much. I don't suppose you have a fix for the other problems? It's fine if you don't, the whitelist is at least functional now, though clunky and unprofessional.
Yeah, sorry, I don't. I'm not familiar with the functions for TF2, so I don't want to be giving you the wrong information.
__________________
Psyk0tik is offline
ABNTGeneric
Junior Member
Join Date: Sep 2018
Old 09-18-2018 , 02:30   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #7

Quote:
Originally Posted by Crasher_3637 View Post
Yeah, sorry, I don't. I'm not familiar with the functions for TF2, so I don't want to be giving you the wrong information.
Y'know how I just said it worked? Well stupidly, that was before I tested it. Even though the game understands what weapon you're holding, and can check that you're holding the incorrect weapon, and can even carry out tasks only under those conditions, I still cannot get it to correctly remove weapon slot 1. This is such an inexplicable problem, I think it almost deserves a topic of its own.
I'm gonna do more research on this hooey.

Last edited by ABNTGeneric; 09-18-2018 at 02:31.
ABNTGeneric is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 09-18-2018 , 03:32   Re: TF2 Weapon Ban Code [Ya gotta help me part 2]
Reply With Quote #8

Quote:
Originally Posted by ABNTGeneric View Post
The snippet of code below is supposed to do 3 things, can you guess what they are? Because the game can't:
The game doesn't do any guesswork, it just runs code. :v

Joking aside, here's some answers:

Thing Number Three / Problem Number Two still doesn't do what you've described as wanting to do: It returns true if your secondary item isn't X or isn't Y. Since the statements are disjoint (if item is X (= false), then item can't be Y (= true)), it'll still always occur (false or true = true). You'll want to && the statements together, so the item is removed if all the statements are true (i.e., no match occurred).

My brain hurt trying to parse that statement out mentally. I'd use a switch myself:

Code:
switch (twoindex) {
    case 46, 163, 222, 812, 833, 1121, 1145: {
        // it's whitelisted, do nothing
    }
    default: {
        TF2_RemoveWeaponSlot(client, 1);
    }
}
Problem Number One is an issue with setting m_hActiveWeapon. Switching a weapon requires a bit more than that.
I don't think SourceMod has a function to do this exact thing for you, but luckily the Melee Only plugin has code for this. Copy over SetActiveWeapon and the things that refer to g_hSDKWeaponSwitch and use it in place of your SetEntPropEnt call. This particular call uses the same gamedata that SDKHooks does, so you don't have to pack your own.

Problem Number Three has a simple enough solution; spawn your own copy of the bat and equip it. There's multiple ways to go about this; see my implementation here, Give Bots Weapons's CreateWeapon function, or TF2Items' OnGiveNamedItem forward.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 09-18-2018 at 03:50.
nosoop is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:56.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode