Exemplo n.º 1
0
int32_t NpcScriptInterface::luaCloseShopWindow(lua_State* L)
{
	//closeShopWindow(cid)
	ScriptEnvironment* env = getScriptEnv();

	Player* player = g_game.getPlayerByID(popNumber(L));

	if (!player) {
		reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
		pushBoolean(L, false);
		return 1;
	}

	Npc* npc = env->getNpc();

	if (!npc) {
		reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
		pushBoolean(L, false);
		return 1;
	}

	int32_t buyCallback;
	int32_t sellCallback;

	Npc* merchant = player->getShopOwner(buyCallback, sellCallback);

	//Check if we actually have a shop window with this player.
	if (merchant == npc) {
		player->sendCloseShop();

		if (buyCallback != -1) {
			luaL_unref(L, LUA_REGISTRYINDEX, buyCallback);
		}

		if (sellCallback != -1) {
			luaL_unref(L, LUA_REGISTRYINDEX, sellCallback);
		}

		player->setShopOwner(NULL, -1, -1);
		npc->removeShopPlayer(player);
	}

	pushBoolean(L, true);
	return 1;
}
Exemplo n.º 2
0
int NpcScriptInterface::luaNpcCloseShopWindow(lua_State* L)
{
    // npc:closeShopWindow(player)
    Player* player = getPlayer(L, 2);
    if (!player) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }

    Npc* npc = getUserdata<Npc>(L, 1);
    if (!npc) {
        reportErrorFunc(getErrorDesc(LUA_ERROR_CREATURE_NOT_FOUND));
        pushBoolean(L, false);
        return 1;
    }

    int32_t buyCallback;
    int32_t sellCallback;

    Npc* merchant = player->getShopOwner(buyCallback, sellCallback);
    if (merchant == npc) {
        player->sendCloseShop();
        if (buyCallback != -1) {
            luaL_unref(L, LUA_REGISTRYINDEX, buyCallback);
        }

        if (sellCallback != -1) {
            luaL_unref(L, LUA_REGISTRYINDEX, sellCallback);
        }

        player->setShopOwner(nullptr, -1, -1);
        npc->removeShopPlayer(player);
    }

    pushBoolean(L, true);
    return 1;
}