Raised This Month: $ Target: $400
 0% 

timed teamswitch-block


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 01-24-2011 , 11:57   timed teamswitch-block
Reply With Quote #1

Hey guys,

since the simple team-manager doesn't work for me, i wrote a little plugin myself. it works and i can switch a player to the other team, but i haven't figured out how to prevent the player from immediately switching back. Don't really know much about handles and events :<

This is what i got so far:
PHP Code:
/* Plugin Template generated by Pawn Studio */

#include <sourcemod>
#undef REQUIRE_PLUGIN
#include <adminmenu>

public Plugin:myinfo 
{
    
name "Switch Player",
    
author "MightyMalcolm",
    
description "Let's and admin switch a player's team",
    
version "1.1",
    
url "http://thestormhawks.eu/"
}

new 
bool:g_players[MAXPLAYERS+1];

public 
OnPluginStart()
{
    
RegAdminCmd("sm_switch",Command_SwitchPlayer,ADMFLAG_CUSTOM1,"Switches a player to the other team.");
    
RegAdminCmd("sm_switch_unlock",Command_Unlock,ADMFLAG_CUSTOM1,"Allows a players to switch teams again.");
    
RegAdminCmd("sm_switch_unlock_all",Command_UnlockAll,ADMFLAG_ROOT,"Allows all players to switch teams again.");
    
HookEvent("player_team"Event_PlayerTeamEventHookMode_Pre);
}

public 
Action:Command_SwitchPlayer(clientargs){
    
    new 
String:arg1[32];
    new 
String:name[MAX_NAME_LENGTH];
    new 
String:adminName[MAX_NAME_LENGTH];

    if(
GetCmdArgs() < 1){
        
ReplyToCommand(client,"[SM] Usage: sm_switch <player|#userid>");
        return 
Plugin_Handled;
    }
    
    
/* Get the first argument */
    
GetCmdArg(1arg1sizeof(arg1));

    
/* Try and find a matching player */
    
new target FindTarget(clientarg1truetrue);
    new 
team GetClientTeam(target);
    
    if (
target == -1)
    {
        
/* FindTarget() automatically replies with the failure reason. */
        
return Plugin_Handled;
    }
    
    
GetClientName(target,name,sizeof(name));
    
GetClientName(client,adminNamesizeof(adminName));
    
    
/* Higher and equally ranked admins are immune */
    
if (IsAdmin(target)){
        new 
level_client GetAdminImmunityLevel(GetUserAdmin(client));  
        new 
level_target GetAdminImmunityLevel(GetUserAdmin(target));
        if(
level_client <= level_target){
            
ReplyToCommand(client,"[SM] Cannot switch admins that have the same or a higher rank!");
            return 
Plugin_Handled;
        }
    }

    if(
target && IsValidEntity(target) && IsClientInGame(target))
    {    
        switch (
team)
        {
           case 
0:
           {
                  
ReplyToCommand(client,"[SM] Cannot switch a player from team Unassigned!");
                return 
Plugin_Handled;
           }
           case 
1:
           {
                
ReplyToCommand(client,"[SM] Cannot switch a player from team Spectator!");
                return 
Plugin_Handled;
           }
           case 
2:
           {
                
// Change the client's team
                
ChangeClientTeam(target3);
                
                
// make sure he doesn't switch back
                
g_players[target]=true;
                
CreateTimer(5*60.0BlockTimertarget);
                
                
PrintToChatAll("[SM] %s got switched to Humans by %s."nameadminName);
                return 
Plugin_Handled;
           }
           case 
3:
           {
                
// Change the client's team
                
ChangeClientTeam(target2);
                
                
// make sure he doesn't switch back
                
g_players[target]=true;
                
CreateTimer(5*60.0BlockTimertarget);
                
                
PrintToChatAll("[SM] %s got switched to Undead by %s."nameadminName);
                return 
Plugin_Handled;
           }
           default:
           {
                
ReplyToCommand(client"Invalid team index! Please try again!");
                return 
Plugin_Handled;
           }
        }
    }
    else
    {
        
ReplyToCommand (client"[SM] Invalid player!");
        return 
Plugin_Handled;
    }
    return 
Plugin_Handled;
}


public 
Action:Command_UnlockAll(clientargs){
    
    for(new 
0MAXPLAYERSi++){
        
g_players[i]=false;
    }
    
    
ReplyToCommand(client,"[SM] All players can now switch their teams!");
    return 
Plugin_Handled;
}

public 
Action:Command_Unlock(client,args){
    
    new 
String:arg1[32];
    new 
String:name[MAX_NAME_LENGTH];
    
    if(
GetCmdArgs() < 1){
        
ReplyToCommand(client,"[SM] Usage: sm_switch_unlock <player|#userid>");
        return 
Plugin_Handled;
    }
    
    
/* Get the first argument */
    
GetCmdArg(1arg1sizeof(arg1));

    
/* Try and find a matching player */
    
new target FindTarget(clientarg1truetrue);
    
    if (
target == -1)
    {
        
/* FindTarget() automatically replies with the failure reason. */
        
return Plugin_Handled;
    }
    
    
/* Higher and equally ranked admins are immune */
    
if (IsAdmin(target)){
        new 
level_client GetAdminImmunityLevel(GetUserAdmin(client));  
        new 
level_target GetAdminImmunityLevel(GetUserAdmin(target));
        if(
level_client <= level_target){
            
ReplyToCommand(client,"[SM] Cannot unlock admins that have the same or a higher rank!");
            return 
Plugin_Handled;
        }
    }
    
    
GetClientName(target,name,sizeof(name));
    
    
g_players[target]=false;
    
ReplyToCommand(client,"[SM] Player %s can now switch his team!");
    return 
Plugin_Handled;
}

public 
Action:Event_PlayerTeam(Handle:event, const String:name[], bool:dontBroadcast){

    
// if (g_players[client] == true) --> block
    
if(g_players[GetEventInt(event,"userid")]){
        
ReplyToCommand (GetEventInt(event,"userid"), "[SM] You are not allowed to switch team for 5 minutes!");
        
        return 
Plugin_Handled;
    }
    return 
Plugin_Continue;
}

public 
Action:BlockTimer(Handle:timeranytarget){
    
/*    TODO
    // Target disconnected or something, dont affect other players
    target = GetClientOfUserId(target);
    if(!target || !IsClientInGame(target)){
        return Plugin_Handled;
    } */

    // allows the player to switch team again
    
g_players[target]=false;
    return 
Plugin_Handled;
}


/*
 * Copied from
 * http://forums.alliedmods.net/showthread.php?t=74531&highlight=ping+kick
 */
public bool:IsAdmin(client){
    
    new 
AdminId:admin GetUserAdmin(client);
    
decl String:name[MAX_NAME_LENGTH];

    
GetClientName(clientnamesizeof(name));
    
//LogAction(0, -1, "Checking for valid AdminID: Found: %d for client %s", _:admin, name);
    
    
if(admin == INVALID_ADMIN_ID)
    {
        
//LogAction(0, -1, "Client %s has an invalid Admin ID.", name);
        
return false;
    }

    
//LogAction(0, -1, "Client %s has a valid Admin ID and is immune.", name);
    
return true;

All it does for the moment, is suppressing the messags "Player xyz joined team blabla", but i can still switch my team. also, as i commented in the code, i need to parse the actual target as a paramater for i don't want to lock teams for everyone.
and i almost forgot to mention the tag mismatch marked in the code as well.

pls help, guys! =)

Last edited by MightyMalcolm; 01-24-2011 at 17:58. Reason: code updated
MightyMalcolm is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 01-24-2011 , 12:15   Re: timed teamswitch-block
Reply With Quote #2

Thats very bad, put the HookEvent into OnPluginStart, the function returns nothing so remove the "new:Handle lock = ". Oh yeah and PHP instead of CODE tags please
__________________
Silvers is offline
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 01-24-2011 , 12:22   Re: timed teamswitch-block
Reply With Quote #3

Quote:
Originally Posted by Silvers View Post
put the HookEvent into OnPluginStart
how would that help? your reply still doesn't tell me how to block a specific player for a specific time!
(at least i changed to php-code^^)
MightyMalcolm is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 01-24-2011 , 12:55   Re: timed teamswitch-block
Reply With Quote #4

Quote:
Originally Posted by MightyMalcolm View Post
how would that help? your reply still doesn't tell me how to block a specific player for a specific time!
(at least i changed to php-code^^)
Because it should throw an error if you hook the event multiple times, you should only hook from OnPluginStart or when you know it's not been hooked / being re-hooked.


I'd recommend looking here for blocking the team switch: https://forums.alliedmods.net/showthread.php?t=78070


I think you should store a global bool if the player is blocked from switching or not. Then allow the command or not... sm_switchplayer - Is this from another plugin? Are you trying to stop another plugin from switching players for a specified amount of time, if so then I think you have to edit the source of that.



PHP Code:
#include <sourcemod>
#include <sdktools>
#pragma semicolon 1

new g_bBlockSwitch[MAXPLAYERS+1];

//TODO: in case i want to unlock before 5 minutes have passed
// new bool:KillAllTimers=false;

public OnPluginStart(){
    
RegAdminCmd("sm_switchplayer",Command_SwitchPlayer,ADMFLAG_CUSTOM1,"Switches a player to the other team.");
    
HookEvent("player_team"Event_PlayerTeamEventHookMode_Pre); //tag mismatch here
}

public 
Action:Command_SwitchPlayer(clientargs){
    
// Block switching on this client
    
if( g_bBlockSwitch[client] )
        return 
Plugin_Handled

    g_bBlockSwitch
[client] = true;
    
CreateTimer(30.0BlockTimerGetClientUserId(client));
    
// TODO: how can i pass the target player as a parameter? i tried a few things, but none worked

    
ReplyToCommand(client,"[SM] Player %s got switchted!",targetName);
    return 
Plugin_Handled;
}

public 
Action:BlockTimer(Handle:timerany:client){
    
// Client disconnected or something, dont affect other players
    
client GetClientOfUserId(client);
    if( !
client || !IsClientInGame(client) )
        return 
Plugin_Handled;

    
g_bBlockSwitch[client] = false;
    
    
// if(KillAllTimers){
        // return Plugin_Stop;
    // } 

    
return Plugin_Handled;
}

public 
Action:Event_PlayerTeam(Handle:event, const String:name[], bool:dontBroadcast){
    if (
GetEventBool(event,"disconnect")){
        return 
Plugin_Continue;
    }
    
//TODO: check if it really is the client we want to block
    
return Plugin_Handled;

This won't work, but thats more what I'm thinking.
__________________

Last edited by Silvers; 01-24-2011 at 15:45. Reason: Fixed link
Silvers is offline
zeroibis
Veteran Member
Join Date: Jun 2007
Old 01-24-2011 , 13:18   Re: timed teamswitch-block
Reply With Quote #5

The most simple thing to do would be to create an array containing the players who are not allowed to switch and check it when they try to. Then after a set time has expired remove the given user from the list. It would also be ideal to store steam id and not ent id in this list to prevent errors or the user just retrying to get around it.
__________________
zeroibis is offline
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 01-24-2011 , 15:40   Re: timed teamswitch-block
Reply With Quote #6

@ silvers: thx, got the idea and working on it. i do not want to interfere with an already existing plugin. i only used "sm_switchplayer" because it sounded quite intuitive.
by the way: great link xD directly sends me back to this page^^

@zeroibis: yupp i'm trying to do that, but i cannot use SteamIDs for the game does not support them. yes you heard correct: it does NOT support them, which makes it especially difficult to perm-ban hackers. but that is another (unsolvable) issue.

thx for the effort guys. i'm on my way to get this done =)
MightyMalcolm is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 01-24-2011 , 15:44   Re: timed teamswitch-block
Reply With Quote #7

Ahh sorry meant to correct that lol: https://forums.alliedmods.net/showthread.php?t=78070

What game may I ask?
__________________
Silvers is offline
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 01-24-2011 , 16:35   Re: timed teamswitch-block
Reply With Quote #8

Dark Messiah Might and Magic the only source engine game (i know) that has not been developed by valve or clever modders. you have to love it if you want to play it, believe me^^ (but then it loves you back)
MightyMalcolm is offline
MightyMalcolm
Senior Member
Join Date: Jan 2010
Old 01-24-2011 , 17:43   Re: timed teamswitch-block
Reply With Quote #9

EDIT:
i got the main changes you told me in my plugin, but the block is not working at all. i suppose my event-hook is not properly made, because everything it does, is suppressing all messages about ppl switching teams. everybody can still switch teams, it's just not displayed. this my hook:

PHP Code:
new bool:g_players[MAXPLAYERS+1];

public 
OnPluginStart()
{
    
// ...
    
HookEvent("player_team"Event_PlayerTeamEventHookMode_Pre);
}

public 
Action:Event_PlayerTeam(Handle:event, const String:name[], bool:dontBroadcast){

    
// if (g_players[client] == true) --> block
    
if(g_players[GetEventInt(event,"userid")]){
        
ReplyToCommand (GetEventInt(event,"userid"), "[SM] You are not allowed to switch team for 5 minutes!");
        
        return 
Plugin_Handled;
    }
    return 
Plugin_Continue;

i also edited my first post to show the full code

Last edited by MightyMalcolm; 01-24-2011 at 17:57.
MightyMalcolm is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 01-24-2011 , 19:07   Re: timed teamswitch-block
Reply With Quote #10

PHP Code:
new bool:g_bPlayers[MAXPLAYERS+1];

public 
OnPluginStart()
{
    
RegConsoleCmd("jointeam"JoinTeam)
}

public 
OnClientConnected(client)
{
    
g_bPlayers[client] = false;
}

public 
Action:JoinTeam(clientargs)
{
    if( 
g_bPlayers[client] )
    {
        
PrintToChat(client"[SM] Please wait before changing team.");
        return 
Plugin_Handled
    
}

    
g_bPlayers[client] = true;
    
CreateTimer(30.0tmrResetSwitchGetClientUserId(client)); // 30 seconds timeout before they can switch again.

    
return Plugin_Continue
}

public 
Action:tmrResetSwitch(Handle:timerany:client)
{
    
client GetClientOfUserId(client);
    if( 
client )
        
g_bPlayers[client] = false;

That should work
__________________
Silvers 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:06.


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