/*
	PostNuke Wasteland Itemsys
	Latest update: 03.02.2012
	Author: NeoPhoenix
*/
#if defined included_pnwl_item
	#endinput
#else
	#define included_pnwl_item
#endif

#include <a_samp>

//Dialoge
enum (+=1)
{
	dialog_SHOW_ITEM_LIST = 1,
	dialog_SHOW_ITEM_EFFEKT,
	dialog_SHOW_ITEM_GIVE_PLAYER,
	dialog_SHOW_ITEM_ACTIONS,
	dialog_SHOW_ITEM_GIVE_FAIL,
	dialog_SHOW_ITEM_GIVE_SUCCESS,
	dialog_SHOW_ITEM_GIVE_NUMBER,
	dialog_THROW_AWAY_COUNT,
	dialog_THROW_AWAY_OK,
	dialog_THROW_AWAY_AFTER,
	dialog_DESTROY_COUNT,
	dialog_DESTROY_OK,
	dialog_DESTROY_AFTER,
}

enum (+=1)
{
	ITEM_ACTION_INFO = 1,	//Aktionen (infos)
	ITEM_ACTION_USE,		//Aktionen (verwenden)
	ITEM_ACTION_GIVE,		//Aktionen (an spieler geben)
	ITEM_ACTION_THROW_AWAY,	//Aktionen (wegwerfen)
	ITEM_ACTION_HAND,		//Aktionen (benutzen)
	ITEM_ACTION_WEAR,		//Aktionen (anziehen)
	ITEM_ACTION_EQUIP,		//Aktionen (ausrüsten)
	ITEM_ACTION_DESTROY,	//Aktionen (zerstören)
}
//Defines
#define MAX_ITEM_ACTIONS 					(7)//Maximale mögliche Aktionen (Verzehr,wegwerfen,geben etc)
#define ITEMS_PER_PAGE 						(12)//Angezeigte Items pro Seite (im Lager)
#define MAX_ITEM_GIVE_RANGE 				(7.5)//Radius in der Spieler sein müssen den man ein Item geben will

enum (+=5)
{
	ITEM_SLOTS_R0 = 5,
	ITEM_SLOTS_R1,
	ITEM_SLOTS_R2,
	ITEM_SLOTS_R3,
	MAX_PLAYER_ITEMS = ITEM_SLOTS_R3,
}

item::GetBackpackSize(playerid)
{
	switch(PlayerEquipment[playerid][Rucksack])
	{
		case 1: return ITEM_SLOTS_R1;
		case 2: return ITEM_SLOTS_R2;
		case 3: return ITEM_SLOTS_R3;
	}
	return ITEM_SLOTS_R0;
}

#include "misc/item_list.inc"

enum e_item
{
	iAnzahl,
	iID,
}
new item_PlayerItem[MAX_PLAYERS][MAX_PLAYER_ITEMS][e_item];

enum c_item
{
	iPage,
	iSelected,
	iSlot[20],
	iAction[MAX_ITEM_ACTIONS],
	iPlayers[11],//GIVE ITEM
	iTAanzahl,//ITEM THROW AWAY
	iGive_ID,
}
new item_ItemCache[MAX_PLAYERS][c_item];

#include "libraries/lib_EX_container"
#include "libraries/lib_EX_iman"
#include "libraries/lib_EX_crafting"

stock item::SHOW_ITEM_LIST(playerid)
{
    /*if(item_ItemCache[playerid][iPage] < 0) item_ItemCache[playerid][iPage] = 0;
	for(new item=((item_ItemCache[playerid][iPage]-1)*ITEMS_PER_PAGE); item<(((item_ItemCache[playerid][iPage]-1)*ITEMS_PER_PAGE)+ITEMS_PER_PAGE); item++)
	{
		if(item >= MAX_PLAYER_ITEMS || item < 0)
		{
			if(loop == 0) format(string,sizeof(string),"{F1F7DC}-");
			else format(string,sizeof(string),"%s\n-",string);
			item_ItemCache[playerid][iSlot][loop] = -1;
		}
		else
		{
		    if(item_PlayerItem[playerid][item][iAnzahl] <= 0)
			{
				if(loop == 0) format(string,sizeof(string),"-");
				else format(string,sizeof(string),"%s\n-",string);
				item_ItemCache[playerid][iSlot][loop] = -1;
			}
		    else
		    {
		        if(loop == 0) format(string,sizeof(string),"%s (%d)",item_ItemInfo[item_PlayerItem[playerid][item][iID]][iName],item_PlayerItem[playerid][item][iAnzahl]);
				else format(string,sizeof(string),"%s\n%s (%d)",string,item_ItemInfo[item_PlayerItem[playerid][item][iID]][iName],item_PlayerItem[playerid][item][iAnzahl]);
		        item_ItemCache[playerid][iSlot][loop] = item;
		    }
		}
		loop++;
	}*/
	items_CreateSelectionMenu(playerid);
	return 1;
}


item::OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(GetPVarInt(playerid,"i_dialog") != 1) return 0;
	SetPVarInt(playerid,"i_dialog",0);
	DeletePVar(playerid,"i_dialog");
	switch(dialogid)
	{
		case dialog_NULL: return 1;
		case dialog_SHOW_ITEM_ACTIONS:
		{
			if(response)
			{
				new item	= item_ItemCache[playerid][iSelected],
					itemid	= item_PlayerItem[playerid][item][iID];
				switch(item_ItemCache[playerid][iAction][listitem])
				{
					case ITEM_ACTION_INFO:
					{//informationen
						format(string,sizeof(string),"{F1F7DC}Informationen von <%s>",item_ItemInfo[itemid][iName]);
						if(item_ItemInfo[itemid][iEffekt][0] != 0.0) format(string,sizeof(string),"%s\nHeilung: \t\t\t\t%0.2f HP",string,item_ItemInfo[itemid][iEffekt][0]);
						if(item_ItemInfo[itemid][iEffekt][1] != 0.0) format(string,sizeof(string),"%s\nRadioaktivität heilen: \t\t\t%0.1f Rad",string,item_ItemInfo[itemid][iEffekt][1]);
						if(item_ItemInfo[itemid][iEffekt][2] != 0.0) format(string,sizeof(string),"%s\nHunger: \t\t\t\t%0.0f Punkte",string,item_ItemInfo[itemid][iEffekt][2]);
						if(item_ItemInfo[itemid][iEffekt][3] != 0.0) format(string,sizeof(string),"%s\nDurst: \t\t\t\t\t%0.0f",string,item_ItemInfo[itemid][iEffekt][3]);
						if(item_ItemInfo[itemid][iEffekt][4] != 0.0) format(string,sizeof(string),"%s\nMüdigkeit: \t\t\t\t%0.0f Punkte",string,item_ItemInfo[itemid][iEffekt][4]);
						format(string,sizeof(string),"%s\nWert: \t\t\t\t\t%d Punkte\nGewicht: \t\t\t\t%0.2f kg",string,item_ItemInfo[itemid][iWert],item_ItemInfo[itemid][iGewicht]);
						item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_EFFEKT,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item - Effekte",string,"Zurück","Beenden");
					}
					case ITEM_ACTION_THROW_AWAY:
					{//item wegwerfen
						if(!(item_ItemInfo[itemid][iAction] & canThrowAway)) return 1;
						format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du wegwerfen?",item_ItemInfo[itemid][iName]);
						item::ShowPlayerDialog(playerid,dialog_THROW_AWAY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) wegwerfen",string,"Wegwerfen","Zurück");
					}
					case ITEM_ACTION_USE:
					{//item konsumieren
						if(!(item_ItemInfo[itemid][iAction] & canUse)) return 1;
						item::OnPlayerItemUse(playerid,item);
					}
					case ITEM_ACTION_GIVE:
					{//Item an spieler geben
						if(!(item_ItemInfo[itemid][iAction] & canTrade)) return 1;
						item::ShowPlayersInRangeToGive(playerid);
					}
					case ITEM_ACTION_HAND:
					{//Item benutzen
						if(!(item_ItemInfo[itemid][iAction] & canThrowAway)) return 1;
						item::OnPlayerItemToHand(playerid,item);
						return 1;
					}
					case ITEM_ACTION_WEAR:
					{//Kleidung anziehen
						if(!(item_ItemInfo[itemid][iAction] & canWear)) return 1;
						item::OnPlayerWearItem(playerid,item);
					}
					case ITEM_ACTION_EQUIP:
					{//Ausrüsten (equipment)
						if(!(item_ItemInfo[itemid][iProperties] & iEquip)) return 1;
						item::OnPlayerEquipItem(playerid,item);
					}
					case ITEM_ACTION_DESTROY:
					{
						format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du zerstören?",item_ItemInfo[itemid][iName]);
						item::ShowPlayerDialog(playerid,dialog_DESTROY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) zerstören",string,"Zerstören","Zurück");
					}
				}
				return 1;
			}
			else
			{
				item::SHOW_ITEM_LIST(playerid);
			}
			return 1;
		}
		case dialog_SHOW_ITEM_GIVE_FAIL:
		{
			if(response) item::SHOW_ITEM_ACTION_LIST(playerid);
			return 1;
		}
		case dialog_SHOW_ITEM_GIVE_SUCCESS:
		{
			if(response)
			{
				new Float:p[3];
				GetPlayerPos(playerid,p[0],p[1],p[2]);
				new give_id = item_ItemCache[playerid][iPlayers][listitem];
		
				if(give_id == -6)
				{//Itemlager
					if(!item::store_IsAtItemstorage(playerid))
					{
						item::ShowPlayersInRangeToGive(playerid);
						return 1;
					}
					item_ItemCache[playerid][iGive_ID] = 0;
					SetPVarInt(playerid,"give_item_to_thing",6);
					new item = item_ItemCache[playerid][iSelected],
						itemid = item_PlayerItem[playerid][item][iID];
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in das Lager packen?",item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) ins Lager packen",string,"Lagern","Zurück");
				}
				else if(give_id == -4)
				{//in einen ofen stecken
					new ofen = GetDialogOption(playerid,listitem);
					if(craft_GetRangeToFurnace(playerid,ofen) > AV_FURNACE_DIST)
					{
						item::ShowPlayersInRangeToGive(playerid);
						return 1;
					}
					item_ItemCache[playerid][iGive_ID] = ofen;
					SetPVarInt(playerid,"give_item_to_thing",4);
					new item = item_ItemCache[playerid][iSelected],
						itemid = item_PlayerItem[playerid][item][iID];
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in den Ofen (%d) stecken?",item_ItemInfo[itemid][iName],give_id);
					item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in den Ofen stecken",string,"Lagern","Zurück");
					return 1;
				}
				else if(give_id == -3)
				{//in eine craftingmaschine stecken
					new craft = GetDialogOption(playerid,listitem);
					if(craft_GetRangeToCraft(playerid,craft) > AV_CRAFT_DIST)
					{
						item::ShowPlayersInRangeToGive(playerid);
						return 1;
					}
					item_ItemCache[playerid][iGive_ID] = craft;
					SetPVarInt(playerid,"give_item_to_thing",3);
					new item = item_ItemCache[playerid][iSelected],
						itemid = item_PlayerItem[playerid][item][iID];
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in die Craftinmaschine (%d) stecken?",item_ItemInfo[itemid][iName],craft);
					item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in die Craftingmaschine stecken",string,"Lagern","Zurück");
					return 1;
				}
				else if(give_id == -2)
				{//item in einen container stecken
					new container = GetDialogOption(playerid,listitem);
					if(ContainerInfo[container][cont_Aktiv] == false || ContainerInfo[container][cont_Opened] == true || AV_CONT_RANGE < cont_GetPlayerRangeToContainer(playerid,container))
					{
						item::ShowPlayersInRangeToGive(playerid);
					}
					else
					{
						item_ItemCache[playerid][iGive_ID] = container;
						SetPVarInt(playerid,"give_item_to_thing",2);
						new item = item_ItemCache[playerid][iSelected],
							itemid = item_PlayerItem[playerid][item][iID];
						format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in Container %d lagern?",item_ItemInfo[itemid][iName],container);
						item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in Container lagern",string,"Lagern","Zurück");
					}
					return 1;
				}
				else
				{
					if(give_id >= 0 && IsPlayerConnected(give_id) && PlayerEvent[give_id][IstTot] == 0 && IsPlayerInRangeOfPoint(give_id,MAX_ITEM_GIVE_RANGE,p[0],p[1],p[2]))
					{
						if(IsPlayerFCNPC(give_id)) return 1;
						item_ItemCache[playerid][iGive_ID] = give_id;
						new item = item_ItemCache[playerid][iSelected],
							itemid = item_PlayerItem[playerid][item][iID];
						if(IsPlayerNPC(give_id)) format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du [NPC] %s (%d) geben?",item_ItemInfo[itemid][iName],GetName(give_id),give_id);
						else format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du %s %s (%d) geben?",item_ItemInfo[itemid][iName],PlayerInfo[give_id][Vorname],PlayerInfo[give_id][Nachname],give_id);
						item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) an Spieler geben",string,"Geben","Zurück");
						return 1;
					}
					item::ShowPlayersInRangeToGive(playerid);
				}
			}
			else
			{
				item::SHOW_ITEM_ACTION_LIST(playerid);
			}
			return 1;
		}
		case dialog_SHOW_ITEM_GIVE_NUMBER:
		{
			if(response)
			{
				new item		= item_ItemCache[playerid][iSelected],
					itemid		= item_PlayerItem[playerid][item][iID],
					anzahl		= strval(inputtext),
					give_id		= item_ItemCache[playerid][iGive_ID],
					thing_id	= give_id,//containerid, playerid usw
					to_thing	= 1;
				
				if(GetPVarInt(playerid,"give_item_to_thing") >= 1) to_thing = GetPVarInt(playerid,"give_item_to_thing");
				DeletePVar(playerid,"give_item_to_thing");
					
				if(anzahl <= 0 || anzahl > item_PlayerItem[playerid][item][iAnzahl])
				{
					if(to_thing >= 2)
					{
						switch(to_thing)
						{
							case 6:
							{
								format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du ins Lager packen?",item_ItemInfo[itemid][iName]);
								item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) einlagern",string,"Lagern","Zurück");
							}
							case 5:
							{
								format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du dem Tankwart geben?",item_ItemInfo[itemid][iName]);
								item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) dem Tankwart geben",string,"Zahlen","Zurück");
							}
							case 4:
							{
								format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in den Ofen (%d) stecken?",item_ItemInfo[itemid][iName],thing_id);
								item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in Ofen stecken",string,"Lagern","Zurück");						
							}
							case 3:
							{
								format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in die Craftingmaschine (%d) stecken?",item_ItemInfo[itemid][iName],thing_id);
								item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in Craftingmaschine stecken",string,"Lagern","Zurück");						
							}
							default://case 2:
							{
								format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du in Container %d lagern?",item_ItemInfo[itemid][iName],thing_id);
								item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in Container lagern",string,"Lagern","Zurück");						
							}
						}
						return 1;
					}
					if(IsPlayerFCNPC(give_id)) return 1;
					if(IsPlayerNPC(give_id)) format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du [NPC] %s (%d) geben?",item_ItemInfo[itemid][iName],GetName(give_id),give_id);
					else format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du %s %s (%d) geben?",item_ItemInfo[itemid][iName],PlayerInfo[give_id][Vorname],PlayerInfo[give_id][Nachname],give_id);
					item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_NUMBER,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) an Spieler geben",string,"Geben","Zurück");
					return 1;
				}
				
				new Float:p[3];
				GetPlayerPos(playerid,p[0],p[1],p[2]);
				
				new bool:is_valid = true;				
				if(to_thing >= 2) 
				{
					switch(to_thing)
					{
						case 6:
						{
							if(!item::store_IsAtItemstorage(playerid)) is_valid = false;
						}
						case 4:
						{//ofen
							if(give_id <= -1 || craft_GetRangeToFurnace(playerid,give_id) > AV_FURNACE_DIST) is_valid = false;
						}
						case 3:
						{//crafting
							if(give_id <= -1 || craft_GetRangeToCraft(playerid,give_id) > AV_CRAFT_DIST) is_valid = false;
						}
						case 2:
						{
							if(give_id <= -1 || ContainerInfo[give_id][cont_Aktiv] == false || cont_GetPlayerRangeToContainer(playerid,give_id) > AV_CONT_RANGE) is_valid = false;
						}
					}
				}
				else if(give_id == -1 || !IsPlayerConnected(give_id) || PlayerEvent[give_id][IstTot] >= 1 || !IsPlayerInRangeOfPoint(give_id,MAX_ITEM_GIVE_RANGE,p[0],p[1],p[2])) is_valid = false;
				
				if(is_valid == false)
				{
					item::ShowPlayersInRangeToGive(playerid);
					return 1;
				}
				else
				{
					new result = -1;
					if(to_thing >= 2)
					{
						switch(to_thing)
						{
							case 6://lager
							{
								result = item::store_ChangeItemCount(playerid,itemid,anzahl)==true?1:-3;
							}
							case 5://tankstelle
							{
								result = 1;
							}
							case 4://ofen
							{
								result = craft_AddItemToFurnace(playerid,itemid,anzahl);
							}
							case 3://crafting
							{
								result = craft_AddItemToCraft(playerid,itemid,anzahl);
							}
							case 2:
							{
								result = cont_AddContainerItem(thing_id,itemid,anzahl);// -1 = falsche itemid, -2 = falsche containerid, -3 = zu wenig platz, -4 = zu wenig items
							}
						}
					}
					else
					{
						result = item::GivePlayerItem(item_ItemCache[playerid][iGive_ID],itemid,anzahl);// -1 = falsche itemid, -2 = offline, -3 = zu wenig platz
					}
					if(result == -1)
					{//falsche itemid
						item::ShowPlayersInRangeToGive(playerid);
					}
					if(result == -2)
					{//offline/falsche container id
						if(to_thing >= 2) item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) in Container lagern","{F1F7DC}Falscher Container (res -2)","Zurück","");
						else item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) an Spieler geben","{F1F7DC}Der User ist offline","Zurück","");
					}
					else if(result == -3)
					{//zu wenig platz
						if(to_thing >= 2)
						{
							switch(to_thing)
							{
								case 4://ofen
								{
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) in den Ofen stecken","{F1F7DC}Im Ofen ist kein Platz mehr","Zurück","");
								}
								case 3://crafting
								{
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) in Craftingmaschine stecken","{F1F7DC}Du hast schon genug Items in der Craftingmaschine","Zurück","");
								}
								case 6:
								{//Lager
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) lagern","{F1F7DC}Beim lagern ist ein Fehler aufgetreten","Zurück","");
								}
								default://case 2:
								{
									format(string,sizeof(string),"{F1F7DC}Im Container %d ist zu wenig Platz",give_id);
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) in Container lagern",string,"Zurück","");
								}	
							}
							return 1;
						}
						else
						{
							format(string,sizeof(string),"{F1F7DC}%s %s (%d) hat zu wenig Platz in seinem Rucksack",PlayerInfo[give_id][Vorname],PlayerInfo[give_id][Nachname],give_id);
							item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) an Spieler geben",string,"Zurück","");
						}
					}
					else
					{
						new bool:give_success = false;
						if(to_thing >= 2)
						{
							give_success = true;
						}
						else
						{//an spieler geben
							if(IsPlayerFCNPC(give_id)) return 1;
							else if(!IsPlayerNPC(give_id)) 
							{
								if(item::GivePlayerItem(give_id,itemid,anzahl) == 1) give_success = true;
								else
								{//Item konnte nicht gegeben werden
									ShowPlayerDialog(give_id,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Tausch nicht möglich","{F1F7DC}Du konntest dem Spieler das Item nicht geben","Ok","");
									return 1;
								}
							}
							else give_success = true;
						}
						
						if(give_success == true && to_thing != 3 && to_thing != 4) item::RemovePlayerItems(playerid,itemid,anzahl);//Items abziehen, Ausnahme: Ofen/Craft
						
						if(to_thing >= 2)
						{
							switch(to_thing)
							{
								case 6://Lager
								{
									format(string,sizeof(string),"{F1F7DC}Du hast %d \"%s\" eingelagert.",anzahl,item_ItemInfo[itemid][iName]);
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) eingelagert",string,"Ok","");
								}
								case 5://Tankstelle
								{
									format(string,sizeof(string),"{F1F7DC}Du hast %d \"%s\" dem Tankwart gegeben.",anzahl,item_ItemInfo[itemid][iName]);
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) dem Tankwart gegeben",string,"Ok","");									
								}
								case 4://ofen
								{
									format(string,sizeof(string),"{F1F7DC}Du hast %d Items (%s) in den Ofen gesteckt.",anzahl,item_ItemInfo[itemid][iName]);
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) in den Ofen stecken",string,"Ok","");
									craft_UpdatePlayerFurnace(playerid);
								}
								case 3://crafting
								{
									format(string,sizeof(string),"{F1F7DC}Du hast %d Items (%s) in die Craftingmaschine gesteckt.",anzahl,item_ItemInfo[itemid][iName]);
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) in Craftingmaschine stecken",string,"Ok","");
									craft_UpdatePlayerCraft(playerid);
								}
								default://case 2:
								{
									format(string,sizeof(string),"{F1F7DC}Du hast %d Items (%s) in Container %d gelagert.",anzahl,item_ItemInfo[itemid][iName],thing_id);
									item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) in Container lagern",string,"Ok","");
								}
							}
							return 1;
						}
						else
						{
							if(IsPlayerFCNPC(give_id)) return 1;
							if(IsPlayerNPC(give_id)) format(string,sizeof(string),"{F1F7DC}Du hast [NPC] %s (%d) %d Items (%s) gegeben.",GetName(give_id),give_id,anzahl,item_ItemInfo[itemid][iName]);
							else format(string,sizeof(string),"{F1F7DC}Du hast %s %s (%d) %d Items (%s) gegeben.",PlayerInfo[give_id][Vorname],PlayerInfo[give_id][Nachname],give_id,anzahl,item_ItemInfo[itemid][iName]);
							item::ShowPlayerDialog(playerid,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) an Spieler geben",string,"Ok","");
							
							if(!IsPlayerNPC(give_id))
							{
								format(string,sizeof(string),"{F1F7DC}%s %s (%d) hat dir %d Items (%s) gegeben.",PlayerInfo[playerid][Vorname],PlayerInfo[playerid][Nachname],playerid,anzahl,item_ItemInfo[itemid][iName]);
								item::ShowPlayerDialog(give_id,dialog_NULL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) an Spieler geben",string,"Ok","");
								SetPVarInt(playerid,"npc_gespraech",0);
								SetPVarInt(playerid,"talk_to_npc",	0);
							}
							item::OnPlayerTradeItem(playerid,give_id,item,anzahl);
							return 1;
						}
					}
				}
			}
			else
			{
				DeletePVar(playerid,"give_item_to_thing");
				item::ShowPlayersInRangeToGive(playerid);
				return 1;
			}
			return 1;
		}
		case dialog_SHOW_ITEM_EFFEKT:
		{
			if(response) item::SHOW_ITEM_ACTION_LIST(playerid);
			return 1;
		}
		case dialog_THROW_AWAY_COUNT:
		{
			if(response)
			{
				new item 		= item_ItemCache[playerid][iSelected],
					itemid 		= item_PlayerItem[playerid][item][iID],
					max_count	= item::CountPlayerItems(playerid,itemid);
				if(strval(inputtext) <= 0 || strval(inputtext) > max_count)
				{
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du wegwerfen?",item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_THROW_AWAY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) wegwerfen",string,"Wegwerfen","Zurück");
				}
				else
				{
					item_ItemCache[playerid][iTAanzahl] = strval(inputtext);
					if(strval(inputtext) == max_count) format(string,sizeof(string),"{F1F7DC}Möchtest du wirklich ALLE \"%s\"s wegwerfen?",item_ItemInfo[itemid][iName]);
					else format(string,sizeof(string),"{F1F7DC}Möchtest du wirklich %d \"%s\"s wegwerfen?",strval(inputtext),item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_THROW_AWAY_OK,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) wegwerfen",string,"Wegwerfen","Zurück");
				}
			}
			else item::SHOW_ITEM_ACTION_LIST(playerid);
			return 1;
		}
		case dialog_THROW_AWAY_OK:
		{
			new item 	= item_ItemCache[playerid][iSelected],
				itemid 	= item_PlayerItem[playerid][item][iID];
			if(!(item_ItemInfo[itemid][iAction] & canThrowAway)) return 1;
			if(response)
			{
				if(item_ItemCache[playerid][iTAanzahl] > item::CountPlayerItems(playerid,itemid))
				{
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du wegwerfen?",item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_THROW_AWAY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) wegwerfen",string,"Wegwerfen","Zurück");			
					return 1;
				}
				item::OnPlayerThrowAway(playerid,item,item_ItemCache[playerid][iTAanzahl]);
				if(item_ItemCache[playerid][iTAanzahl] == item_PlayerItem[playerid][item][iAnzahl]) format(string,sizeof(string),"{F1F7DC}Du hast alle \"%s\"s weggeworfen.",item_ItemInfo[itemid][iName]);
				else format(string,sizeof(string),"{F1F7DC}Du hast %d \"%s\"s weggeworfen.",item_ItemCache[playerid][iTAanzahl],item_ItemInfo[itemid][iName]);
				item::ShowPlayerDialog(playerid,dialog_THROW_AWAY_AFTER,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) weggeworfen",string,"Zurück","Beenden");
			}
			else
			{
				format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du wegwerfen?",item_ItemInfo[item][iName]);
				item::ShowPlayerDialog(playerid,dialog_THROW_AWAY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) wegwerfen",string,"Wegwerfen","Zurück");
			}
			return 1;
		}
		case dialog_THROW_AWAY_AFTER:
		{
			if(response) item::SHOW_ITEM_LIST(playerid);
			return 1;
		}
		case dialog_DESTROY_COUNT:
		{
			if(response)
			{
				new item 		= item_ItemCache[playerid][iSelected],
					itemid 		= item_PlayerItem[playerid][item][iID],
					max_count	= item::CountPlayerItems(playerid,itemid);
				if(strval(inputtext) <= 0 || strval(inputtext) > max_count)
				{
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du zerstören?",item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_DESTROY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) zerstören",string,"Zerstören","Zurück");
				}
				else
				{
					item_ItemCache[playerid][iTAanzahl] = strval(inputtext);
					if(strval(inputtext) == max_count) format(string,sizeof(string),"{F1F7DC}Möchtest du wirklich ALLE \"%s\"s zerstören?",item_ItemInfo[itemid][iName]);
					else format(string,sizeof(string),"{F1F7DC}Möchtest du wirklich %d \"%s\"s zerstören?",strval(inputtext),item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_DESTROY_OK,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) zerstören",string,"Zerstören","Zurück");
				}
			}
			else item::SHOW_ITEM_ACTION_LIST(playerid);
			return 1;
		}
		case dialog_DESTROY_OK:
		{
			new item = item_ItemCache[playerid][iSelected],
				itemid = item_PlayerItem[playerid][item][iID];
			if(!(item_ItemInfo[itemid][iAction] & canDestroy)) return 1;
			if(response)
			{
				if(item_ItemCache[playerid][iTAanzahl] > item::CountPlayerItems(playerid,itemid))
				{
					format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du zerstören?",item_ItemInfo[itemid][iName]);
					item::ShowPlayerDialog(playerid,dialog_DESTROY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) zerstören",string,"Zerstören","Zurück");			
					return 1;
				}
				item::RemovePlayerItems(playerid,itemid,item_ItemCache[playerid][iTAanzahl]);
				if(item_ItemCache[playerid][iTAanzahl] == item_PlayerItem[playerid][item][iAnzahl]) format(string,sizeof(string),"{F1F7DC}Du hast alle \"%s\"s zerstört.",item_ItemInfo[itemid][iName]);
				else format(string,sizeof(string),"{F1F7DC}Du hast %d \"%s\"s zerstört.",item_ItemCache[playerid][iTAanzahl],item_ItemInfo[itemid][iName]);
				item::ShowPlayerDialog(playerid,dialog_DESTROY_AFTER,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item(s) zerstört",string,"Zurück","Beenden");
			}
			else
			{
				format(string,sizeof(string),"{F1F7DC}Wie viele \"%s\"s möchtest du zerstören?",item_ItemInfo[item][iName]);
				item::ShowPlayerDialog(playerid,dialog_DESTROY_COUNT,DIALOG_STYLE_INPUT,"{F1F7DC}Item(s) zerstören",string,"Zerstören","Zurück");
			}
			return 1;
		}
		case dialog_DESTROY_AFTER:
		{
			if(response) item::SHOW_ITEM_LIST(playerid);
			return 1;
		}
	}
	return 1;
}

stock item::SHOW_ITEM_ACTION_LIST(playerid,slot=-1)
{
	mui_DestroySelectionMenu(playerid);
	if(slot != -1 && slot < MAX_PLAYER_ITEMS && slot >= 0) item_ItemCache[playerid][iSelected] = slot;
	new item = item_ItemCache[playerid][iSelected],
		loop=0,
		itemid = item_PlayerItem[playerid][item][iID];
    format(string,sizeof(string),"{F1F7DC}Informationen");
	item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_INFO;
    loop++;

	if(item_ItemInfo[itemid][iAction] & canUse)
	{
	    format(string,sizeof(string),"%s\nItem verwenden",string);
	    item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_USE;
		loop++;
	}
	if(item_ItemInfo[itemid][iAction] & canTrade)
	{
	    format(string,sizeof(string),"%s\nAn Spieler geben",string);
	    item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_GIVE;
		loop++;
	}
	if(item_ItemInfo[itemid][iAction] & canThrowAway)
	{
		format(string,sizeof(string),"%s\nIn die Hand nehmen",string);
		item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_HAND;
		loop++;
	}		
	if(item_ItemInfo[itemid][iAction] & canWear)
	{
		if(PlayerEquip[playerid][item_ItemInfo[itemid][iWearInfo][1]][eItemID] == itemid) format(string,sizeof(string),"%s\nKleidung ausziehen",string);
		else format(string,sizeof(string),"%s\nKleidung anziehen",string);
		item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_WEAR;
		loop++;	
	}
	if(item_ItemInfo[itemid][iProperties] & iEquip)
	{
		format(string,sizeof(string),"%s\nEquipment verwenden",string);
		item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_EQUIP;
		loop++;
	}
	if(item_ItemInfo[itemid][iAction] & canThrowAway)
	{
		format(string,sizeof(string),"%s\nWegwerfen",string);
		item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_THROW_AWAY;
		loop++;
	}
	if(item_ItemInfo[itemid][iAction] & canDestroy)
	{
		format(string,sizeof(string),"%s\nZerstören {C85135}(!)",string);
		item_ItemCache[playerid][iAction][loop] = ITEM_ACTION_DESTROY;
		loop++;	
	}
	
	format(query,sizeof(query),"{F1F7DC}Item \"%s\" (%dx) verwalten",item_ItemInfo[itemid][iName],item_PlayerItem[playerid][item][iAnzahl]);
	item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_ACTIONS,DIALOG_STYLE_LIST,query,string,"Weiter","Zurück");
	return 1;
}

stock bool:item::HasPlayerItem(playerid,itemid)
{
	for(new i=0; i<MAX_PLAYER_ITEMS; i++)
	{
		if(!item_PlayerItem[playerid][i][iID]) 			return false;
	    if(item_PlayerItem[playerid][i][iID] == itemid) return true;
	}
	return false;
}

stock item::CountPlayerUsedSlots(playerid)
{
	new count = 0;
	for(new i=0; i<MAX_PLAYER_ITEMS; i++)
	{
		if(!item_PlayerItem[playerid][i][iID]) break;
	    count++;
	}
	return count;
}

stock item::CountPlayerItems(playerid,itemid)
{
	new count = 0;
	for(new i=0; i<MAX_PLAYER_ITEMS; i++)
	{
		if(!item_PlayerItem[playerid][i][iID]) break;
	    if(item_PlayerItem[playerid][i][iID] == itemid) count += item_PlayerItem[playerid][i][iAnzahl];
	}
	return count;
}

stock item::FindByObjectID(objectid)
{
	for(new item; item<MAX_ITEMS; item++)
	{
		if(item_ItemInfo[item][iObjectID] == objectid)
		{
			return item;
		}
	}
	return -1;
}

stock item::CountPlayerFreeSlots(playerid)
{
	new count = 0;
	for(new i=0; i<item::GetBackpackSize(playerid); i++) if(!item_PlayerItem[playerid][i][iID]) count++;
	return count;
}

stock item::GivePlayerItem(playerid,itemid,anzahl,bool:_save=true,bool:_display=true)// -1 = falsche itemid, -2 = offline, -3 = zu wenig platz, -4 = zu wenig items
{
	if(itemid < 0 || itemid >= MAX_ITEMS) 	return -1;
	if(anzahl < 0) 							return -4;
	if(anzahl == 0) 						return  1;
	if(!IsPlayerConnected(playerid)) 		return -2;	
	new tmp_required = anzahl;
	for(new i=0; i<item::GetBackpackSize(playerid); i++)
	{
		if(item_PlayerItem[playerid][i][iID] == itemid) tmp_required -= (item_ItemInfo[itemid][iStacksize]-item_PlayerItem[playerid][i][iAnzahl]);
		if(item_PlayerItem[playerid][i][iID] == 0)		tmp_required -= item_ItemInfo[itemid][iStacksize];
		if(tmp_required <= 0) break;
	}
	if(tmp_required >= 1) return -3;//Zu wenig Platz	
	new bool:had_item = false;
	if(!had_item)
	{
		for(new i; i<MAX_PLAYER_ITEMS; i++)
		{
			if(item_PlayerItem[playerid][i][iID] == itemid)
			{
				had_item = true;
				break;
			}
		}
	}	
	tmp_required = anzahl;	
	for(new i=0; i<item::GetBackpackSize(playerid); i++)
	{
		if(item_PlayerItem[playerid][i][iID] == itemid || item_PlayerItem[playerid][i][iID] == 0)
		{
			new size_left 	= (item_ItemInfo[itemid][iStacksize]-item_PlayerItem[playerid][i][iAnzahl]),
				store		= size_left>=tmp_required?tmp_required:size_left;
			item_PlayerItem[playerid][i][iID] 		= itemid;
			item_PlayerItem[playerid][i][iAnzahl] 	+= store;	
			tmp_required -= store;
			if(tmp_required <= 0) break;
		}
	}	
	if(_save)
	{
		if(had_item) 	format(string,sizeof(string),"UPDATE pnwl_user_item SET anzahl=anzahl+%d WHERE userid='%d' AND itemid='%d'",anzahl,PlayerInfo[playerid][PLAYER_ID],itemid);
		else 			format(string,sizeof(string),"INSERT INTO pnwl_user_item (userid,anzahl,itemid) VALUES ('%d','%d','%d')",PlayerInfo[playerid][PLAYER_ID],anzahl,itemid);
		db_query_once(SQL_DB,string);
	}	
	if(_display == true)
	{	
		new item_mess[144];
		format(item_mess,144,#FORMAT_CHAT_INFO"Du hast das Item \"%s\" %d mal erhalten.",item_ItemInfo[itemid][iName],anzahl);
		SendClientMessage(playerid,GRUEN,item_mess);
	}
	PlayerCache[playerid][item_Gewicht] += (float(anzahl) * item_ItemInfo[itemid][iGewicht]);
	if(PlayerCache[playerid][item_Gewicht] > PlayerEvent[playerid][MaxWeight])
	{
		PlayerEvent[playerid][CanRun] = false;
		format(string,sizeof(string),#FORMAT_CHAT_INFO_N"Du trägst zu viele Items (%0.2fkg)",PlayerCache[playerid][item_Gewicht]);
		SendClientMessage(playerid,-1,string);
	}
	return 1;
}

stock item::RemovePlayerItems(playerid,item_id,anzahl=-1,opt_item=-1,bool:_save=true)//-2 = nicht connected, -1 = falsche itemid, X = anzahl entfernt
{
	if(!IsPlayerConnected(playerid))		return -2;
	if(item_id < 0 || item_id >= MAX_ITEMS) return -1;
	if(anzahl == -1)	anzahl = item::CountPlayerItems(playerid,item_id);
	if(anzahl < 0)		return 0;
	if(anzahl == 0) 	return 0;
	new remove_left 	= anzahl,
		remove_this		= 0,
		bool:items_left = false,
		anzahl_removed	= 0,
		Float:old_weight= PlayerCache[playerid][item_Gewicht];
	if(opt_item >= 0 && opt_item < MAX_PLAYER_ITEMS)
	{
		if(item_PlayerItem[playerid][opt_item][iID] != item_id)
		{
			remove_this = item_PlayerItem[playerid][opt_item][iAnzahl]>=remove_left?(remove_left):(item_PlayerItem[playerid][opt_item][iAnzahl]);
			remove_left -= remove_this;
			item_PlayerItem[playerid][opt_item][iAnzahl] 	-= remove_this;
			anzahl_removed									+= remove_this;
			PlayerCache[playerid][item_Gewicht] 			-= (float(remove_this)*item_ItemInfo[item_id][iGewicht]);
			if(item_PlayerItem[playerid][opt_item][iAnzahl] == 0) item_PlayerItem[playerid][opt_item][iID] = 0;
			else if(remove_left == 0) items_left = true;
			item::MigrateSlot(playerid,opt_item);
		}
	}
	if(remove_left >= 1)
	{
		for(new i=(MAX_PLAYER_ITEMS-1); i>=0; i--)
		{
			if(item_PlayerItem[playerid][i][iID] != item_id) continue;
			remove_this 	= item_PlayerItem[playerid][i][iAnzahl]>=remove_left?(remove_left):(item_PlayerItem[playerid][i][iAnzahl]);
			remove_left 	-= remove_this;
			anzahl_removed	+= remove_this;
			item_PlayerItem[playerid][i][iAnzahl]	-= remove_this;
			PlayerCache[playerid][item_Gewicht]		-= (float(remove_this)*item_ItemInfo[i][iGewicht]);
			if(item_PlayerItem[playerid][i][iAnzahl] == 0)
			{
				item_PlayerItem[playerid][i][iID] = 0;
				item::MigrateSlot(playerid,i);
			}
			if(!remove_left)
			{
				if(item_PlayerItem[playerid][i][iAnzahl] == 0) items_left = true;
				break;
			}
		}
	}	
	if(_save)
	{
		if(!items_left) items_left = item::HasPlayerItem(playerid,item_id);
		if(!items_left)
		{
			//Kleidung checken
			if(item_ItemInfo[item_id][iAction] & canWear)
			{
				new wearSlot = item_ItemInfo[item_id][iWearInfo][0];
				if(PlayerEquip[playerid][wearSlot][eItemID] == item_id)
				{
					equip::RemoveSlot(playerid,wearSlot);
					format(string,sizeof(string),#FORMAT_INFO_N"Das Kleidungsstück \"%s\" wurde entfernt.",item_ItemInfo[item_id][iName]);
					SendClientMessage(playerid,-1,string);
				}
			}
			//Equipment checken
			if(item_ItemInfo[item_id][iProperties] & iEquip)
			{
				if(PlayerCache[playerid][equip_fist_item] == item_id)
				{
					if(PlayerCurrentWeaponSlot[playerid] == 1) PlayerCurrentWeaponSlot[playerid] = 0, equip::RemoveSlot(playerid,EQUIPMENT_SLOT);
					PlayerCache[playerid][equip_fist_item] = 0;
					format(string,sizeof(string),#FORMAT_INFO_N"Der Ausrüstungsgegenstand \"%s\" wurde entfernt.",item_ItemInfo[item_id][iName]);
					SendClientMessage(playerid,-1,string);
				}
			}
			
			//MySQL save
			format(string,sizeof(string),"DELETE FROM pnwl_user_item WHERE itemid>='1' AND userid='%d' AND itemid='%d'",PlayerInfo[playerid][PLAYER_ID],item_id);
		}
		else format(string,sizeof(string),"UPDATE pnwl_user_item SET anzahl=anzahl-%d WHERE userid='%d' AND itemid='%d'",anzahl,PlayerInfo[playerid][PLAYER_ID],item_id);
		db_query_once(SQL_DB,string);
	}
	craft_UpdatePlayerFurnace(playerid);
	if(old_weight > PlayerEvent[playerid][MaxWeight] && PlayerCache[playerid][item_Gewicht] < PlayerEvent[playerid][MaxWeight])
	{
		PlayerEvent[playerid][CanRun] = true;
		format(string,sizeof(string),#FORMAT_CHAT_INFO"Du trägst nun genug (%0.2fkg)",PlayerCache[playerid][item_Gewicht]);
		SendClientMessage(playerid,-1,string);
	}
	return anzahl_removed;
}

stock item::MigrateSlot(playerid,dest)//0 - nicht möglich, 1 = aufgefüllt, 2 = migriert
{
	if(dest < 0 || dest >= MAX_PLAYER_ITEMS) return 0;
	new bool:refill = item_PlayerItem[dest][dest][iAnzahl]>=1?true:false,
		refill_left	= refill==true?(item_ItemInfo[item_PlayerItem[playerid][dest][iID]][iStacksize]-item_PlayerItem[playerid][dest][iAnzahl]):0;
	for(new item=(MAX_PLAYER_ITEMS-1); item>dest; item--)
	{
		if(item_PlayerItem[playerid][item][iAnzahl] >= 1)
		{
			if(refill)
			{//Auffüllen
				if(item_PlayerItem[playerid][item][iID] != item_PlayerItem[playerid][dest][iID]) continue;
				new count = item_PlayerItem[playerid][item][iAnzahl]>=refill_left?refill_left:item_PlayerItem[playerid][item][iAnzahl];
				item_PlayerItem[playerid][dest][iAnzahl] += count;
				if((item_PlayerItem[playerid][item][iAnzahl] -= count) == 0) item_PlayerItem[playerid][item][iID] = 0;
				if(!refill_left) return 1;
			}
			else
			{//Migrieren
				item_PlayerItem[playerid][dest][iAnzahl] 	= item_PlayerItem[playerid][item][iAnzahl];
				item_PlayerItem[playerid][dest][iID] 		= item_PlayerItem[playerid][item][iID];
				item_PlayerItem[playerid][item][iAnzahl]	= 0;
				item_PlayerItem[playerid][item][iID] 		= 0;
				if(item_PlayerItem[playerid][dest][iAnzahl] < item_ItemInfo[item_PlayerItem[playerid][dest][iID]][iStacksize]) item::MigrateSlot(playerid,dest);
				return 2;
			}
		}
	}
	return 0;
}

stock item::ShowPlayersInRangeToGive(playerid,slot=-1)
{
	if(slot != -1 && slot < MAX_PLAYER_ITEMS && slot >= 0) item_ItemCache[playerid][iSelected] = slot;
    new Float:p[3],players_in_range;
    GetPlayerPos(playerid,p[0],p[1],p[2]);
	format(string,sizeof(string),"{F1F7DC}");
    for(new d=0; d<=__SERVER[HIGHEST_PLAYER_ID]; d++)
    {
        if(!IsPlayerConnected(d) || d == playerid || IsPlayerFCNPC(d) || !IsPlayerInRangeOfPoint(d,MAX_ITEM_GIVE_RANGE,p[0],p[1],p[2]) || PlayerEvent[d][IstTot] >= 1) continue;
		
		if(players_in_range >= 1) 
		{
			if(IsPlayerNPC(d)) format(string,sizeof(string),"%s\n[NPC] %s (%d)",string,GetName(d),d);
			else format(string,sizeof(string),"%s\n%s (ID %d)",string,PlayerInfo[d][Vorname],PlayerInfo[d][Nachname],d);
		}
		else
		{
			if(npc_list:npc::IdByName(GetName(d)) == NPC_NONE) continue;//Invalid NPC or RNPC (bot walking around just for decoration)
			if(IsPlayerNPC(d)) format(string,sizeof(string),"[NPC] %s (%d)",GetName(d),d);
			else format(string,sizeof(string),"%s %s (%d)",PlayerInfo[d][Vorname],PlayerInfo[d][Nachname],d);			
		}
		item_ItemCache[playerid][iPlayers][players_in_range] = d;
		
		if(++players_in_range == 10)
		{
			item_ItemCache[playerid][iPlayers][players_in_range] = -1;
			format(string,sizeof(string),"%s\n... weitere Möglichkeiten",string);
			break;
		}
    }
	
	//@lib_item->lib_EX_container
	//including containers to deposit items
	if(players_in_range < 10)
	{
		for(new container; container<MAX_CONTAINERS; container++)
		{
			if(cont_GetPlayerRangeToContainer(playerid,container) < AV_CONT_RANGE)
			{
				item_ItemCache[playerid][iPlayers][players_in_range] 	= -2;
				PlayerOption[playerid][dialog][players_in_range] 		= container;
				if(players_in_range >= 1) format(string,sizeof(string),"%s\n[CONT] Container %d",string,container);
				else format(string,sizeof(string),"[CONT] Container %d",container);

				if(++players_in_range == 10)
				{
					item_ItemCache[playerid][iPlayers][players_in_range] = -1;
					format(string,sizeof(string),"%s\n... weitere Möglichkeiten",string);
					break;
				}
			}
		}
		if(players_in_range < 10)
		{
			new craft = craft_GetNearestCraft(playerid);
			if(craft != -1)
			{
				item_ItemCache[playerid][iPlayers][players_in_range] 	= -3;
				PlayerOption[playerid][dialog][players_in_range] 		= craft;
				if(players_in_range >= 1) format(string,sizeof(string),"%s\n[CRAFT] Crafting (%d)",string,craft);
				else format(string,sizeof(string),"[CRAFT] Crafting (%d)",craft);
				if(++players_in_range == 10)
				{
					item_ItemCache[playerid][iPlayers][players_in_range] = -1;
					format(string,sizeof(string),"%s\n... weitere Möglichkeiten",string);
				}					
			}
		}
		if(players_in_range < 10)
		{//Noch Platz
			new ofen = craft_GetNearestFurnace(playerid);
			if(ofen != -1)
			{
				item_ItemCache[playerid][iPlayers][players_in_range] 	= -4;
				PlayerOption[playerid][dialog][players_in_range] 		= ofen;
				if(players_in_range >= 1) format(string,sizeof(string),"%s\n[CRAFT] Ofen (%d)",string,ofen);
				else format(string,sizeof(string),"[CRAFT] Ofen (%d)",ofen);
				if(++players_in_range == 10)
				{
					item_ItemCache[playerid][iPlayers][players_in_range] = -1;
					format(string,sizeof(string),"%s\n... weitere Möglichkeiten",string);
				}
			}
		}
		if(players_in_range < 10)
		{//Noch Platz
			new bool:result = false;
			if(PlayerEvent[playerid][InteractHouse] != -1)
			{
				new hid = PlayerEvent[playerid][InteractHouse];
				if(HouseInfo[hid][hUpgrade][5] >= 1)
				{
					switch(HouseInfo[hid][hInterior])
					{
						case 2: if(IsPlayerInRangeOfPoint(playerid,3.0,801,-2727.6999,100.9000)) result = true;
						default: if(IsPlayerInRangeOfPoint(playerid,3.0,801,-2727.6999,100.9000)) result = true;
					}
				}
			}
			else
			{
			}
			if(result)
			{
				item_ItemCache[playerid][iPlayers][players_in_range] 	= -6;
				PlayerOption[playerid][dialog][players_in_range] 		= 0;
				if(players_in_range >= 1) format(string,sizeof(string),"%s\nItemlager",string);
				else format(string,sizeof(string),"Itemlager");
				if(++players_in_range == 10)
				{
					item_ItemCache[playerid][iPlayers][players_in_range] = -1;
					format(string,sizeof(string),"%s\n... weitere Möglichkeiten",string);
				}
			}
		}
	}	
	
    if(players_in_range == 0)
    {
        item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_FAIL,DIALOG_STYLE_MSGBOX,"{F1F7DC}Item an Spieler geben","{F1F7DC}Es sind zur Zeit keine Spieler in der Umgebung, an denen\ndu dieses Item geben kannst. Um ein item an einen anderen\nMitspieler zu übergeben ist es notwendig, dass dieser in deiner\nNähe steht.","Zurück","Beenden");
    }
    else
    {
        item::ShowPlayerDialog(playerid,dialog_SHOW_ITEM_GIVE_SUCCESS,DIALOG_STYLE_LIST,"{F1F7DC}Item an Spieler geben",string,"Geben","Zurück");
    }
	return 1;
}

forward Float:item::GetPlayerWeight(playerid);
stock Float:item::GetPlayerWeight(playerid)
{
	new Float:weight = 0.0;
	for(new slot=0; slot<MAX_PLAYER_ITEMS; slot++) if(item_PlayerItem[playerid][slot][iAnzahl]) weight += (float(item_PlayerItem[playerid][slot][iAnzahl]) * item_ItemInfo[item_PlayerItem[playerid][slot][iID]][iGewicht]);
	return weight;
}

stock item::ResetPlayerItems(playerid,bool:_save=true)
{
	for(new slot=0; slot<MAX_PLAYER_ITEMS; slot++)
	{
		item_PlayerItem[playerid][slot][iID] 		= 0;
		item_PlayerItem[playerid][slot][iAnzahl] 	= 0;
	}
	if(_save == true)
	{
		format(string,sizeof(string),"DELETE FROM pnwl_user_item WHERE itemid>='0' AND userid='%d'",PlayerInfo[playerid][PLAYER_ID]);
		db_query_once(SQL_DB,string);
	}
	PlayerCache[playerid][item_Gewicht] = 0.0;
	return 1;
}

stock item::ShowPlayerDialog(playerid,dialogid,dialogstyle,header[],info[],but1[],but2[])
{
	SetPVarInt(playerid,"i_dialog",1);
    ShowPlayerDialog(playerid,dialogid,dialogstyle,header,info,but1,but2);
    return 1;
}

stock item::OnPlayerItemLibInit(playerid)
{
	PlayerCache[playerid][item_Gewicht] = 0.0;
	format(query,sizeof(query),"SELECT * FROM pnwl_user_item WHERE userid='%d'",PlayerInfo[playerid][PLAYER_ID]);
	SQL_RES = db_query(SQL_DB,query);
	if(db_num_rows(SQL_RES) >= 1)
	{
		new item_cache[2];
		do
		{
			db_get_field_assoc(SQL_RES,"itemid",string,sizeof(string));
			item_cache[0] = strval(string);
			if(item_cache[0] <= 0 || item_cache[0] > MAX_ITEMS) continue;
			db_get_field_assoc(SQL_RES,"anzahl",string,sizeof(string));
			item_cache[1] = strval(string);
			if(item_cache[1] >= 1)
			{
				switch(item::GivePlayerItem(playerid,item_cache[0],item_cache[1],false,false))
				{
					case 1:		continue;
					case -3:	break;//Zu wenig Platz
				}
			}
		}
		while(db_next_row(SQL_RES));
	}
	db_free_result(SQL_RES);
	PlayerCache[playerid][item_Gewicht] = item::GetPlayerWeight(playerid);
	if(PlayerCache[playerid][item_Gewicht] > PlayerEvent[playerid][MaxWeight])
	{
		PlayerEvent[playerid][CanRun] = false;
		format(string,sizeof(string),#FORMAT_CHAT_INFO_N"Du trägst zu viele Items (%0.2fkg)",PlayerCache[playerid][item_Gewicht]);
		SendClientMessage(playerid,-1,string);
	}
	return 1;
}

stock item::ConvertNameToFunc(itemid)
{
	new strx[MAX_ITEM_NAME];
	for(new i; i<strlen(item_ItemInfo[itemid][iName]); i++)
	{
		strx[i] = item_ItemInfo[itemid][iName][i];
		if(strx[i] >= 'a' && strx[i] <= 'z') continue;
		if(strx[i] >= 'A' && strx[i] <= 'Z') continue;
		if(strx[i] >= '0' && strx[i] <= '9') continue;
		strx[i] = '_';
	}
	return strx;
}

stock item::StopPlayerInteract(playerid,bool:_remove=false)
{
	if(GetPVarInt(playerid,"holding_f_iFAct") >= 1)
	{
		if(_remove)
		{
			if(PlayerCache[playerid][holding_item] >= 1 && PlayerCurrentWeaponSlot[playerid] == 0)
			{
				PlayerCache[playerid][holding_item] = 0;
				equip::RemoveSlot(playerid,ITEM_ACTION_SLOT);
			}
		}
		DeletePVar(playerid,"holding_f_iFAct");
		DeletePVar(playerid,"holding_f_iFAct_id");
		DeletePVar(playerid,"holding_f_iFAct_item");
		DeletePVar(playerid,"holding_f_iFAct_time");
		ClearAnimations(playerid);
		UpdateStatusProgressBar(playerid);
		return 1;
	}
	return 0;
}
		
stock item::RemovePlayerHoldingItem(playerid,bool:_inventory=false)
{
	new itemid = PlayerCache[playerid][holding_item];
	if(itemid >= 1)
	{
		if(_inventory) equip::RemoveSlot(playerid,ITEM_ACTION_SLOT);
		else
		{
			if(item_ItemInfo[itemid][iSize] == iSize_carry) ApplyAnimation(playerid,"CARRY","putdwn",5.0,0,0,0,0,0);
			else ApplyAnimation(playerid,"BOMBER","BOM_PLANT_IN",5.0,1,0,0,0,450);
			SetTimerEx("pevent_DropItem",500,false,"d",playerid);
		}
	}
	return 1;
}

stock item::SetPlayerHoldingItem(playerid,itemid)
{
	printf("setplayerholdingitem %d",itemid);
	SetPVarInt(playerid,"_guns_event",1);
	equip::RemoveSlot(playerid,ITEM_ACTION_SLOT);
	switch(PlayerCurrentWeaponSlot[playerid])
	{
		case 2,3:
		{//Waffe
			if(GetPlayerWeapon(playerid) != 0)
			{//Waffe
				new data[2];
				GetPlayerWeaponData(playerid,GetWeaponSlot(GetPlayerWeapon(playerid)),data[0],data[1]);
				PlayerWeapon[playerid][PlayerCurrentWeaponSlot[playerid]][wMagAmmo] = data[1];				
			}
		}
	}
	ResetPlayerWeapons(playerid);
	SetPlayerArmedWeapon(playerid,0);
	PlayerTextDrawHide(playerid,WeaponAmmo[playerid]);
	PlayerCache[playerid][current_weapon_id]	= 0;
	PlayerCurrentWeaponSlot[playerid]			= 0;
	PlayerCache[playerid][holding_item]			= itemid;
	equip::RemoveSlot(playerid,ITEM_ACTION_SLOT);
	if(item_ItemInfo[itemid][iSize] == iSize_carry) SetPlayerSpecialAction(playerid,SPECIAL_ACTION_CARRY);
	equip::UseEquipment(playerid,item_ItemInfo[itemid][iObjectID],item_ItemInfo[itemid][iHoldBoneID],item_ItemInfo[itemid][iHoldAttachInfo][0],item_ItemInfo[itemid][iHoldAttachInfo][1],item_ItemInfo[itemid][iHoldAttachInfo][2],item_ItemInfo[itemid][iHoldAttachInfo][3],item_ItemInfo[itemid][iHoldAttachInfo][4],item_ItemInfo[itemid][iHoldAttachInfo][5],item_ItemInfo[itemid][iHoldAttachInfo][6],item_ItemInfo[itemid][iHoldAttachInfo][7],item_ItemInfo[itemid][iHoldAttachInfo][8],.oslot=ITEM_ACTION_SLOT);
	DeletePVar(playerid,"_guns_event");	
	return 1;
}

#include "libraries/lib_EX_item_storage.inc"