• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Compiling doPlayerAddItem on 7.6

Shawak

Intermediate OT User
Joined
Sep 11, 2008
Messages
1,979
Best answers
2
Reaction score
104
Location
Germany
Hello, I've a litle problem and i don't know how to solve it.
Always when i add 21 or more items to a player, and the have 2 bps (bp in bp) the function don't add the last (the 1 item) to the next backpack, it drops the item on the map.

Here is my source code:
Lua:
int LuaScriptInterface::luaDoPlayerAddItem(lua_State *L)
{
	//doPlayerAddItem(cid, itemid, <optional> count/subtype, <optional: default: 1> canDropOnMap)
	int32_t parameters = lua_gettop(L);

	bool canDropOnMap = true;
	if(parameters > 3){
		canDropOnMap = (popNumber(L) == 1);
	}

	uint32_t count = 0;
	if(parameters > 2){
		count = popNumber(L);
	}

	uint32_t itemId = (uint32_t)popNumber(L);
	uint32_t cid = popNumber(L);

	ScriptEnviroment* env = getScriptEnv();
	Player* player = env->getPlayerByUID(cid);
	if(!player){
		reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
		lua_pushnumber(L, LUA_ERROR);
		return 1;
	}

	const ItemType& it = Item::items[itemId];
	if(it.stackable && count > 100){
		int32_t subCount = count;
		while(subCount > 0){
			int32_t stackCount = std::min((int32_t)100, (int32_t)subCount);
			Item* newItem = Item::CreateItem(itemId, stackCount);

			if(!newItem){
				reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
				lua_pushnumber(L, LUA_ERROR);
				return 1;
			}

			ReturnValue ret = g_game.internalPlayerAddItem(player, newItem, canDropOnMap);

			if(ret != RET_NOERROR){
				delete newItem;
				reportErrorFunc("Could not add item");
				lua_pushnumber(L, LUA_ERROR);
				return 1;
			}

			subCount = subCount - stackCount;

			if(subCount == 0){
				if(newItem->getParent()){
					uint32_t uid = env->addThing((Thing*)newItem);
					lua_pushnumber(L, uid);
					return 1;
				}
				else{
					//stackable item stacked with existing object, newItem will be released
					lua_pushnumber(L, LUA_NULL);
					return 1;
				}
			}
		}
	}
	else{
		Item* newItem = Item::CreateItem(itemId, count);

		if(!newItem){
			reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
			lua_pushnumber(L, LUA_ERROR);
			return 1;
		}

		ReturnValue ret = g_game.internalPlayerAddItem(player, newItem, canDropOnMap);

		if(ret != RET_NOERROR){
			delete newItem;
			reportErrorFunc("Could not add item");
			lua_pushnumber(L, LUA_ERROR);
			return 1;
		}

		if(newItem->getParent()){
			uint32_t uid = env->addThing((Thing*)newItem);
			lua_pushnumber(L, uid);
			return 1;
		}
		else{
			//stackable item stacked with existing object, newItem will be released
			lua_pushnumber(L, LUA_NULL);
			return 1;
		}
	}

	return 0;
}
Please help me.

Regards,
Shawak
 
OP
Shawak

Shawak

Intermediate OT User
Joined
Sep 11, 2008
Messages
1,979
Best answers
2
Reaction score
104
Location
Germany
Solved can be closed.
 
Top