Jump to content
Sign in to follow this  
fraronk

Help in scripting

Recommended Posts

Hello all, i'm new in scripting, with some friends we have a public DayZ server and we would like add some commands for admins, i found this script on internet:

Spoiler

class CustomMission: MissionServer
{	  
  	bool verify_admins = false; // true=verify presence of BI UID in admin list
	string cmd_prefix = "/"; // Must be special character
	ref TStringArray admins = {}; // Add your BI UID or SteamID

	bool IsPlayerAnAdmin(PlayerBase player) {
		bool found = false;
		for ( int i = 0; i < admins.Count(); ++i ) {
			if(player.GetIdentity().GetId() == admins[i] || player.GetIdentity().GetPlainId() == admins[i]) { found=true; break; }
		}
		return found;
	}

	void SendMessageToPlayer(PlayerBase player, string message) {
		Param1<string> param = new Param1<string>( message );
		GetGame().RPCSingleParam(player, ERPCs.RPC_USER_ACTION_MESSAGE, param, true, player.GetIdentity());
	}

	bool IsPlayer(string name) { 
		PlayerBase p;
		array<Man> players = new array<Man>; GetGame().GetPlayers(players);
		for ( int i = 0; i < players.Count(); ++i ) {
			p = players.Get(i);
			if(p.GetIdentity().GetName() == name) return true;
		}
		return false;
	}

	PlayerBase GetPlayer(string name) {
		PlayerBase p;
		array<Man> players = new array<Man>; GetGame().GetPlayers(players);
		for ( int i = 0; i < players.Count(); ++i ) {
			p = players.Get(i);
			if(p.GetIdentity().GetName() == name) return p;
		}
		return NULL;
	}

	override void OnEvent(EventType eventTypeId, Param params)  {
		super.OnEvent(eventTypeId,params);
		int i;
		PlayerBase player, temp_player;
		array<Man> players = new array<Man>; GetGame().GetPlayers(players);
		if(eventTypeId != ChatMessageEventTypeID) return; // Is chat message
		ChatMessageEventParams chat_params = ChatMessageEventParams.Cast( params );
		if(chat_params.param1 != 0 || chat_params.param2 == "") return; 
		player = GetPlayer(chat_params.param2);
		if(player == NULL) return;
		if(verify_admins && !IsPlayerAnAdmin(player)) { GetGame().AdminLog("[ADMCMD] (Unauthorized) " + player.GetIdentity().GetName() +" ("+player.GetIdentity().GetPlainId()+", "+player.GetIdentity().GetId()+") tried to execute "+ chat_params.param3); return; }
		string message = chat_params.param3, prefix, param0, command;
		TStringArray tokens = new TStringArray;
		message.Replace("` ", "&SPCESC!"); //Escape spaces before splitting
		message.Split(" ", tokens); int count = tokens.Count();

		for ( i = 0; i < count; ++i ) {
			message = tokens[i];
			message.Replace("&SPCESC!", " "); //.Replace doesn't work directly on TStringArray elements for some reason :(
			tokens[i] = message;
		}

		param0 = tokens.Get(0);
		param0.ParseStringEx(prefix); if(prefix != cmd_prefix) return;
		param0.ParseStringEx(command);
		GetGame().AdminLog("[ADMCMD] PLAYER: "+ player.GetIdentity().GetName() +" ("+player.GetIdentity().GetPlainId()+", "+player.GetIdentity().GetId()+") CMD: "+ command);
		switch(command) {
			case "spawn": {
				if(count != 2) { SendMessageToPlayer(player, "/spawn [object]"); return; }
				GetGame().CreateObject(tokens[1], player.GetPosition(), false, true );
				SendMessageToPlayer(player, "[ObjectSpawn] Object spawned: " + tokens[1]);
				break;
			}

			case "inv": {
				if(count != 2) { SendMessageToPlayer(player, "/inv [object]"); return; }
				player.GetInventory().CreateInInventory(tokens[1]);
				SendMessageToPlayer(player, "[ObjectSpawn] Object spawned in inventorz: " + tokens[1]);
				break;
			}

			case "weapon": {
				if(count != 2) { SendMessageToPlayer(player, "/weapon [weapon]"); return; }
				EntityAI weapon;
				switch(tokens[1]) {
					case "ump": {
						weapon = player.GetHumanInventory().CreateInHands("UMP45");
						player.GetInventory().CreateInInventory("Mag_UMP_25Rnd");
						break;
					}

					case "cz61": {
						weapon = player.GetHumanInventory().CreateInHands("CZ61");
						player.GetInventory().CreateInInventory("Mag_CZ61_20Rnd");
						break;
					}

					case "mp5": {
						weapon = player.GetHumanInventory().CreateInHands("MP5K");
						weapon.GetInventory().CreateAttachment("MP5k_StockBttstck");
						weapon.GetInventory().CreateAttachment("MP5_PRailHndgrd");
						player.GetInventory().CreateInInventory("Mag_MP5_30Rnd");
						break;
					}

					case "svd": {
						weapon = player.GetHumanInventory().CreateInHands("SVD");
						weapon.GetInventory().CreateAttachment("PSO11Optic");
						player.GetInventory().CreateInInventory("Mag_SVD_10Rnd");
						break;
					}

					case "mp133": {
						weapon = player.GetHumanInventory().CreateInHands("Mp133Shotgun");
						player.GetInventory().CreateInInventory("Ammo_12gaPellets");
						break;
					}

					case "mosin": {
						weapon = player.GetHumanInventory().CreateInHands("Mosin9130");
						weapon.GetInventory().CreateAttachment("PUScopeOptic");
						player.GetInventory().CreateInInventory("Ammo_762x54");
						break;
					}

					case "m4": {
						weapon = player.GetHumanInventory().CreateInHands("M4A1_Black");
						weapon.GetInventory().CreateAttachment("M4_RISHndgrd_Black");
						weapon.GetInventory().CreateAttachment("M4_MPBttstck_Black");
						weapon.GetInventory().CreateAttachment("ACOGOptic");
						//weapon.GetInventory().CreateAttachment("Suppressor");
						player.GetInventory().CreateInInventory("Mag_STANAGCoupled_30Rnd");
						break;
					}

					case "akm": {
						weapon = player.GetHumanInventory().CreateInHands("AKM");
						weapon.GetInventory().CreateAttachment("AK_RailHndgrd");
						weapon.GetInventory().CreateAttachment("AK_PlasticBttstck");
						player.GetInventory().CreateInInventory("Mag_AKM_30Rnd");
						break;
					}

					case "izh18": {
						weapon = player.GetHumanInventory().CreateInHands("Izh18");
						player.GetInventory().CreateInInventory("Ammo_762x39");
						break;
					}

					case "fnx": {
						weapon = player.GetHumanInventory().CreateInHands("FNX45");
						player.GetInventory().CreateInInventory("Mag_FNX45_15Rnd");
						break;
					}

					default: {
						SendMessageToPlayer(player, "[WeaponSpawner] " + tokens[1] + " not found");
						break;
					}
				}
				break;
			}

			case "strip": {
				if(count != 2) { SendMessageToPlayer(player, "/strip [player]"); return; }
				temp_player = GetPlayer(tokens[1]);
				if(temp_player == NULL) {
					SendMessageToPlayer(player, "[Strip] Can't find player called: '"+tokens[1]+"'");
				} else {
					temp_player.RemoveAllItems();
					SendMessageToPlayer(player, "[Strip] You stripped " + temp_player.GetIdentity().GetName());
					SendMessageToPlayer(temp_player, "You have been stripped by an admin");
				}
				break;
			}

			case "slap": {
				if(count != 2) { SendMessageToPlayer(player, "/slap [player]"); return; }
				temp_player = GetPlayer(tokens[1]);
				if(temp_player == NULL) {
					SendMessageToPlayer(player, "[Slap] Can't find player called: '"+tokens[1]+"'");
				} else {
					temp_player.SetPosition(temp_player.GetPosition() + "0 3 0");
					SendMessageToPlayer(player, "[Slap] You stripped " + temp_player.GetIdentity().GetName());
					SendMessageToPlayer(temp_player, "You have been slapped by an admin");
				}
				break;
			}
				
			case "topos": {
				if (count < 3) { SendMessageToPlayer(player, "/topos [x] [y] (player)"); return; }
				float ATL_Z = GetGame().SurfaceY(tokens[1].ToFloat(), tokens[2].ToFloat());
				vector reqpos = Vector(tokens[1].ToFloat(), ATL_Z, tokens[2].ToFloat());
				temp_player = player;
				if (count == 4) { 
					temp_player = GetPlayer(tokens[3]); 
					if (temp_player == NULL) {
						SendMessageToPlayer(player, "[Teleport] Can't find player called: '"+tokens[3]+"'"); return;
					} else {
						SendMessageToPlayer(temp_player, "[Teleport] You've been teleported to " + reqpos + " by admin " + player.GetIdentity().GetName());
					}
				}
				temp_player.SetPosition(reqpos);
				SendMessageToPlayer(player, "[Teleport] Target teleported to " + temp_player.GetPosition());
				break;
			}

			case "goto": {
				if(count != 2) { SendMessageToPlayer(player, "/goto [player]"); return; }
				temp_player = GetPlayer(tokens[1]);
				if(temp_player == NULL) {
					SendMessageToPlayer(player, "[Teleport] Can't find player called: '"+tokens[1]+"'");
				} else {
					player.SetPosition(temp_player.GetPosition());
					SendMessageToPlayer(player, "[Teleport] You teleported to " + temp_player.GetIdentity().GetName());
				}
				break;
			}

			case "allgoto": {
				PlayerBase allgoto_target;
				if(count != 2) { SendMessageToPlayer(player, "/allgoto [player]"); return; }
				allgoto_target = GetPlayer(tokens[1]);
				if(allgoto_target == NULL) {
					SendMessageToPlayer(player, "[Teleport] Can't find player called: '"+tokens[1]+"'");
				} else {
					SendMessageToPlayer(player, "[Teleport] You teleported everyone to your location");
					for (i = 0; i < players.Count(); i++) {
						temp_player = players.Get(i);
						SendMessageToPlayer(temp_player, "[Teleport] You have been teleported to player " + allgoto_target.GetIdentity().GetName());
						temp_player.SetPosition(allgoto_target.GetPosition());
					}
				}
				break;
			}

			case "here": {
				if(count != 2) { SendMessageToPlayer(player, "/here [player]"); return; }
				temp_player = GetPlayer(tokens[1]);
				if(temp_player == NULL) {
					SendMessageToPlayer(player, "[Teleport] Can't find player called: '"+tokens[1]+"'");
				} else {
					temp_player.SetPosition(player.GetPosition());
					SendMessageToPlayer(temp_player, "[Teleport] You have been teleported to admin " + player.GetIdentity().GetName());
					SendMessageToPlayer(player, "[Teleport] You teleported " + temp_player.GetIdentity().GetName() + " to your location");
				}
				break;
			}

			case "allhere": {
				SendMessageToPlayer(player, "[Teleport] You teleported everyone to your location");
				for (i = 0; i < players.Count(); i++) {
					temp_player = players.Get(i);
					SendMessageToPlayer(temp_player, "[Teleport] You have been teleported to admin " + player.GetIdentity().GetName());
					temp_player.SetPosition(player.GetPosition());
				}
				break;
			}

			case "time": {
				if(count != 3) { SendMessageToPlayer(player, "/time [hour] [minute]"); return; }
				GetGame().GetWorld().SetDate( 2018, 1, 7, tokens[1].ToInt(), tokens[2].ToInt());
				SendMessageToPlayer(player, "[Servertime] You have set the servertime to " + tokens[1] + ":"+tokens[2]);
				break;
			}
				
			case "day": {
				GetGame().GetWorld().SetDate( 2018, 1, 7, 12, 0);
				SendMessageToPlayer(player, "[Servertime] You have set the servertime to daytime");
				break;
			}

			case "night": {
				GetGame().GetWorld().SetDate( 2018, 1, 7, 24, 0);
				SendMessageToPlayer(player, "[Servertime] You have set the servertime to daytime");
				break;
			}

			case "rain": {
				if(count != 2) { SendMessageToPlayer(player, "/rain [value 0-100]"); return; }
				float rain = tokens[1].ToFloat() / 100;
				GetGame().GetWeather().GetRain().Set(rain, 2, 600);
				SendMessageToPlayer(player, "[Weather] You have set Rain to " + tokens[1] + "% ["+rain+"]");
				break;
			}

			case "fog": {
				if(count != 2) { SendMessageToPlayer(player, "/fog [value 0-100]"); return; }
				float fog = tokens[1].ToFloat() / 100;
				GetGame().GetWeather().GetFog().Set(fog, 2, 600);
				SendMessageToPlayer(player, "[Weather] You have set Fog to " + tokens[1] + "% ["+fog+"]");
				break;
			}

			case "overcast": {
				if(count != 2) { SendMessageToPlayer(player, "/overcast [value 0-100]"); return; }
				float overcast = tokens[1].ToFloat() / 100;
				GetGame().GetWeather().GetOvercast().Set(overcast, 2, 600);
				SendMessageToPlayer(player, "[Weather] You have set Overcast to " + tokens[1] + "% ["+overcast+"]");
				break;
			}

			case "kill": {
				if(count == 2) {
					temp_player = GetPlayer(tokens[1]);
					if(temp_player == NULL) {
						SendMessageToPlayer(player, "[Kill] Can't find player called: '"+tokens[1]+"'");
					} else {
						temp_player.SetHealth(0);
						SendMessageToPlayer(player, "[Kill] You killed " + temp_player.GetIdentity().GetName());
					}
				} else {
					player.SetHealth(0);
					SendMessageToPlayer(player, "[Kill] You killed yourself");
				}
				break;
			}

			case "killall": {
				SendMessageToPlayer(player, "[Kill] You killed everyone");
				for (i = 0; i < players.Count(); i++) {
					temp_player = players.Get(i);
					if(temp_player.GetIdentity().GetId() == player.GetIdentity().GetId()) continue;
					temp_player.SetHealth(0);
				}
				break;
			}

			case "pmall": {
				SendMessageToPlayer(player, "Sending '"+tokens[1]+"' to all players");
				for (i = 0; i < players.Count(); i++) {
					temp_player = players.Get(i);
					if(temp_player.GetIdentity().GetId() == player.GetIdentity().GetId()) continue;
					SendMessageToPlayer(temp_player, "MESSAGE FORM SERVER: "+tokens[1]);
				}
				break;
			}

			case "heal": {
				PlayerBase heal_target;
				if(count == 2) {
					heal_target = GetPlayer(tokens[1]);
					if(heal_target == NULL) {
						SendMessageToPlayer(player, "[Heal] Can't find player called: '"+tokens[1]+"'");
					} else {
						SendMessageToPlayer(player, "[Heal] You healed " + heal_target.GetIdentity().GetName());
					}
				} else {
					heal_target = player;
					SendMessageToPlayer(player, "[Heal] You healed yourself");
				}
				if(heal_target != NULL) {
					heal_target.SetHealth(heal_target.GetMaxHealth("", ""));
					heal_target.SetHealth("", "Blood", heal_target.GetMaxHealth("", "Blood"));
					heal_target.GetStatStamina().Set(1000);
					heal_target.GetStatEnergy().Set(1000);
					heal_target.GetStatWater().Set(1000);
				}
				break;
			}

			case "offroad": {
				SendMessageToPlayer(player, "[Offroad] Vehicled spawned");
				Car v;
				float playerAngle = MiscGameplayFunctions.GetHeadingAngle(player);
				vector posModifier = Vector(-(3 * Math.Sin(playerAngle)), 0, 3 * Math.Cos(playerAngle));
				v = Car.Cast(GetGame().CreateObject( "OffroadHatchback", player.GetPosition() + posModifier));
				
				v.GetInventory().CreateAttachment("SparkPlug");
				v.GetInventory().CreateAttachment("EngineBelt");
				v.GetInventory().CreateAttachment("CarBattery");
				v.GetInventory().CreateAttachment("HatchbackHood");
				v.GetInventory().CreateAttachment("HatchbackTrunk");
				v.GetInventory().CreateAttachment("HatchbackDoors_CoDriver");
				v.GetInventory().CreateAttachment("HatchbackWheel");
				v.GetInventory().CreateAttachment("HatchbackWheel");
				v.GetInventory().CreateAttachment("HatchbackWheel");
				v.GetInventory().CreateAttachment("HatchbackWheel");
				v.GetInventory().CreateAttachment("HatchbackWheel"); // spare
				break;
			}

			case "refuel": {
				ref array<Object> nearest_objects = new array<Object>;
				ref array<CargoBase> proxy_cargos = new array<CargoBase>;
				Car toBeFilled;
				vector position = player.GetPosition();
				GetGame().GetObjectsAtPosition ( position, 10, nearest_objects, proxy_cargos );
		
				for (i = 0; i < nearest_objects.Count(); i++) {
					if (nearest_objects[i].IsKindOf("CarScript")) {
						SendMessageToPlayer(player, "[Refuel] Found car: '"+nearest_objects[i]+"'");
						toBeFilled = Car.Cast(nearest_objects[i]);
						float fuelReq = toBeFilled.GetFluidCapacity( CarFluid.FUEL ) - (toBeFilled.GetFluidCapacity( CarFluid.FUEL ) * toBeFilled.GetFluidFraction( CarFluid.FUEL ));
						float oilReq = toBeFilled.GetFluidCapacity( CarFluid.OIL ) - (toBeFilled.GetFluidCapacity( CarFluid.OIL ) * toBeFilled.GetFluidFraction( CarFluid.OIL ));
						float coolantReq = toBeFilled.GetFluidCapacity( CarFluid.COOLANT ) - (toBeFilled.GetFluidCapacity( CarFluid.COOLANT ) * toBeFilled.GetFluidFraction( CarFluid.COOLANT ));
						float brakeReq = toBeFilled.GetFluidCapacity( CarFluid.BRAKE ) - (toBeFilled.GetFluidCapacity( CarFluid.BRAKE ) * toBeFilled.GetFluidFraction( CarFluid.BRAKE ));
						toBeFilled.Fill( CarFluid.FUEL, fuelReq );
						toBeFilled.Fill( CarFluid.OIL, oilReq );
						toBeFilled.Fill( CarFluid.COOLANT, coolantReq );
						toBeFilled.Fill( CarFluid.BRAKE, brakeReq );
						SendMessageToPlayer(player, "[Refuel] "+fuelReq+"L added, all fluids maxed");
					}
				}
				
				break;
			}

			default: {
				SendMessageToPlayer(player, "Unknown command: " + command);
				break;
			}
		}
	}

	void SetRandomHealth(EntityAI itemEnt)
	{
		int rndHlt = Math.RandomInt(40,100);
		itemEnt.SetHealth("","",rndHlt);
	}

	override PlayerBase CreateCharacter(PlayerIdentity identity, vector pos, ParamsReadContext ctx, string characterName)
	{
		Entity playerEnt;
		playerEnt = GetGame().CreatePlayer(identity, characterName, pos, 0, "NONE");//Creates random player
		Class.CastTo(m_player, playerEnt);
		
		GetGame().SelectPlayer(identity, m_player);

		m_player.SetHealth(heal_target.GetMaxHealth("", ""));
	    m_player.SetHealth("", "Blood", heal_target.GetMaxHealth("", "Blood"));
		m_player.GetStatStamina().Set(1000);
		m_player.GetStatEnergy().Set(1000);
		m_player.GetStatWater().Set(1000);
		
		return m_player;
	}
	
	override void StartingEquipSetup(PlayerBase player, bool clothesChosen)
	{
/*
		player.RemoveAllItems();

		EntityAI item = player.GetInventory().CreateInInventory(topsArray.GetRandomElement());
		EntityAI item2 = player.GetInventory().CreateInInventory(pantsArray.GetRandomElement());
		EntityAI item3 = player.GetInventory().CreateInInventory(shoesArray.GetRandomElement());
*/
		EntityAI itemEnt;
		ItemBase itemBs;
		
		itemEnt = player.GetInventory().CreateInInventory("Rag");
		itemBs = ItemBase.Cast(itemEnt);
		itemBs.SetQuantity(4);
		SetRandomHealth(itemEnt);

		itemEnt = player.GetInventory().CreateInInventory("RoadFlare");
		itemBs = ItemBase.Cast(itemEnt);
	}
};

 

but it doesn't woork completly.
we've have notice on testing that in the execution of any command, thake the following as example:

case "spawn": {
      if(count != 2) { SendMessageToPlayer(player, "/spawn [object]"); return; }
      GetGame().CreateObject(tokens[1], player.GetPosition(), false, true );
      SendMessageToPlayer(player, "[ObjectSpawn] Object spawned: " + tokens[1]);
      break;
      }

the variable player is not set properly, cause is not the player that write the command, but it's always the first player that joined the server

Do you guys think that the problem is the names of players?

Should i change in all this code instead using names should i use IDs?

bool IsPlayer(string name) { 
		PlayerBase p;
		array<Man> players = new array<Man>; GetGame().GetPlayers(players);
		for ( int i = 0; i < players.Count(); ++i ) {
			p = players.Get(i);
			if(p.GetIdentity().GetName() == name) return true;
		}
		return false;
	}

	PlayerBase GetPlayer(string name) {
		PlayerBase p;
		array<Man> players = new array<Man>; GetGame().GetPlayers(players);
		for ( int i = 0; i < players.Count(); ++i ) {
			p = players.Get(i);
			if(p.GetIdentity().GetName() == name) return p;
		}
		return NULL;
	}

	override void OnEvent(EventType eventTypeId, Param params)  {
		super.OnEvent(eventTypeId,params);
		int i;
		PlayerBase player, temp_player;
		array<Man> players = new array<Man>; GetGame().GetPlayers(players);
		if(eventTypeId != ChatMessageEventTypeID) return; // Is chat message
		ChatMessageEventParams chat_params = ChatMessageEventParams.Cast( params );
		if(chat_params.param1 != 0 || chat_params.param2 == "") return; 
		player = GetPlayer(chat_params.param2);
		if(player == NULL) return;
		if(verify_admins && !IsPlayerAnAdmin(player)) { GetGame().AdminLog("[ADMCMD] (Unauthorized) " + player.GetIdentity().GetName() +" ("+player.GetIdentity().GetPlainId()+", "+player.GetIdentity().GetId()+") tried to execute "+ chat_params.param3); return; }
		string message = chat_params.param3, prefix, param0, command;
		TStringArray tokens = new TStringArray;
		message.Replace("` ", "&SPCESC!"); //Escape spaces before splitting
		message.Split(" ", tokens); int count = tokens.Count();

		for ( i = 0; i < count; ++i ) {
			message = tokens[i];
			message.Replace("&SPCESC!", " "); //.Replace doesn't work directly on TStringArray elements for some reason :(
			tokens[i] = message;
		}

 

Share this post


Link to post
Share on other sites

Are you talking about the target player or the executing player not being recognized correctly?

 

When targeting a specific player you have to include his survivor index, if the target has not set his name via startup parameters. eg. /kill Survivor (1)

Share this post


Link to post
Share on other sites

i'm talking about the executing player, when we test it on our server i type the command like /spawn apple, and the apple spawn to another player, so the executing player is taken wrong, is there any way for debug it or know why it's happen

Share this post


Link to post
Share on other sites

Thhe executing player is searched by a simple name check. Set your in-game name via launch parameters to something that is not survivor and you are good to go

Share this post


Link to post
Share on other sites
45 minutes ago, philippj said:

Thhe executing player is searched by a simple name check. Set your in-game name via launch parameters to something that is not survivor and you are good to go

is there any code for set as default name instead Survivor the Steam ID or something like it?

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×