class arItems
{
	string SvenItems;
	string NewItems;
}

//The variables
array<arItems@> g_ItemsReplacer; 
array<Vector> g_OldItemPos;
const float Frame2Delay = 2 / 30;


void AddToArray(string SvenShit, string YourShit)
{
	arItems Items;
	arItems @pItems = cast<arItems>(Items);
	
	pItems.SvenItems = SvenShit;
	pItems.NewItems = YourShit;	
	
	g_ItemsReplacer.insertLast(pItems);
}

void ReplaceEntites()
{
	for (uint Index = 0; Index < g_ItemsReplacer.length(); Index++)
	{
		CBaseEntity@ Entity = null;
		arItems @pItems = cast<arItems>(g_ItemsReplacer[Index]);
		
		string SvenItemName = pItems.SvenItems;
		string NewItemName = pItems.NewItems;
	
		while( ( @Entity = g_EntityFuncs.FindEntityByClassname( Entity, SvenItemName ) ) !is null )	
		{
			Vector SvenEntityPos = Entity.pev.origin;
		
			//Check if the name matches first...
			if (Entity.pev.classname == SvenItemName)
			{
				//Remove that shit bruh
				g_EntityFuncs.Remove(Entity);
				//Add that shit mayne
				g_EntityFuncs.Create(NewItemName, SvenEntityPos, g_vecZero, false );
			}
			
		}
		
	}
}


string MatchEntityName(CBaseEntity@ pEntity)
{
	for (uint Index = 0; Index < g_ItemsReplacer.length(); Index++)
	{
		arItems @pItems = cast<arItems>(g_ItemsReplacer[Index]);
		
		if (pEntity.pev.classname == pItems.SvenItems)
			return pItems.NewItems;
	}

	return "";
}


void ReplaceEntityByPointer(EHandle Entity)
{  
	CBaseEntity@ pEntity = Entity.GetEntity();   

	//Failsafe
	if (pEntity is null)
		return;
		
	//The main variables...
	string MatchedName = MatchEntityName(pEntity);
	
	//Make sure we found a match first...
	if (MatchedName == "")
		return;

	//Else do the rest of the shit now...
	Vector SvenEntityPos = pEntity.pev.origin;
	g_OldItemPos.insertLast(SvenEntityPos);

	//Remove that shit bruh
	g_EntityFuncs.Remove(pEntity);
	
	//Check first for the old position before spawning in new shit. 
	for (uint Index = 0; Index < g_OldItemPos.length(); Index++)
	{		
		float Length = abs(SvenEntityPos.Length() - g_OldItemPos[Index].Length());

		//Be careful printing this shit out on maps with tons of spawned items
		//if (MatchedName != "")
		//	g_Log.PrintF("Item Name: " + pEntity.pev.classname + " Item Pos: " + Length + "\n");
		
		if (Length > 0.0 and Length < 20)
			return;
	}

	//Add that shit mayne
	CBaseEntity@ SpawnedItem = g_EntityFuncs.Create(MatchedName, SvenEntityPos, g_vecZero, false);
	g_OldItemPos.insertLast(SpawnedItem.pev.origin);
		
		
}

HookReturnCode EntitySpawned(CBaseEntity@ pEntity)
{
    g_Scheduler.SetTimeout("ReplaceEntityByPointer", Frame2Delay, EHandle(pEntity));

    return HOOK_CONTINUE;
}


void PluginInit()
{
	g_Module.ScriptInfo.SetAuthor( "Your mom" );
	g_Module.ScriptInfo.SetContactInfo( "Your mom" );

	//First Initalize the array...
	AddToArray("weapon_crowbar", "weapon_ins2kabar");
	AddToArray("weapon_pipewrench", "weapon_ins2kabar");
	AddToArray("weapon_9mmhandgun", "weapon_ins2beretta");
	AddToArray("weapon_357", "weapon_ins2python");
	AddToArray("weapon_eagle", "weapon_ins2deagle");
	AddToArray("weapon_shotgun", "weapon_cofbenelli");
	AddToArray("weapon_9mmAR", "weapon_cofmp5");
	AddToArray("weapon_uzi", "weapon_coftmp");
	AddToArray("weapon_uziakimbo", "weapon_coftmp");
	AddToArray("weapon_m16", "weapon_ins2m16a4");
	AddToArray("weapon_m249", "weapon_ins2m249");
	AddToArray("weapon_rpg", "weapon_ins2at4");
	AddToArray("weapon_handgrenade", "weapon_ins2mk2");
	AddToArray("weapon_sniperrifle", "weapon_ins2m40a1");
	AddToArray("ammo_357", "WEBLEY_MAG");
	AddToArray("ammo_556", "M16A4_MAG");
	AddToArray("ammo_762", "M40A1_MAG");
	AddToArray("ammo_9mmAR", "Parabellum9mm_MP5");
	AddToArray("ammo_9mmbox", "M249_MAG");
	AddToArray("ammo_9mmclip", "Parabellum9mm_MP5");
	AddToArray("ammo_ARgrenades", "M203_MAG");
	AddToArray("ammo_buckshot", "COFBENELLI_Mag");
	AddToArray("ammo_rpgclip", "weapon_ins2at4");
	
	g_Hooks.RegisterHook(Hooks::Game::EntityCreated, @EntitySpawned);
}


//Edit the entities here...
void MapStart()
{
	ReplaceEntites();
}
