/* * ===================================================================================== * * OpenMiner * * Copyright (C) 2018-2020 Unarelith, Quentin Bazin * Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) * * This file is part of OpenMiner. * * OpenMiner is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * OpenMiner is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with OpenMiner; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * ===================================================================================== */ #include #include "InventoryWidgetDef.hpp" void InventoryWidgetDef::serialize(sf::Packet &packet) const { WidgetDef::serialize(packet); packet << m_width << m_height << m_shiftDestination << m_offset << m_count << m_inventory << m_filter << m_isReadOnly; if (m_inventory == "player") packet << m_player << m_inventoryName; else if (m_inventory == "temp") packet << m_inventoryName; else if (m_inventory == "block") packet << m_blockPosition; } void InventoryWidgetDef::loadFromLuaTable(const sol::table &table) { WidgetDef::loadFromLuaTable(table); loadInventory(table); m_shiftDestination = table["shift_destination"].get_or(""); m_filter = table["filter"].get_or(""); m_isReadOnly = table["is_read_only"].get_or(false); sol::optional size = table["size"]; if (size != sol::nullopt) { m_width = size.value()["x"]; m_height = size.value()["y"]; } else gkError() << "For" << m_name << ": Inventory size must be defined"; } void InventoryWidgetDef::loadInventory(const sol::table &table) { sol::object inventoryObject = table["inventory"]; if (inventoryObject.valid() && inventoryObject.get_type() == sol::type::table) { sol::table inventoryTable = inventoryObject.as(); m_offset = inventoryTable["offset"].get_or(0); m_count = inventoryTable["count"].get(); m_inventory = inventoryTable["source"].get(); if (m_inventory == "player") { m_player = inventoryTable["player"].get(); m_inventoryName = inventoryTable["inventory_name"].get(); } else if (m_inventory == "temp") { m_inventoryName = inventoryTable["inventory_name"].get_or(""); } else if (m_inventory == "block") { sol::optional blockTable = inventoryTable["block"]; if (blockTable != sol::nullopt) { m_blockPosition.x = blockTable.value()["x"]; m_blockPosition.y = blockTable.value()["y"]; m_blockPosition.z = blockTable.value()["z"]; } else gkError() << "For" << m_name << ": Block position must be defined"; } else gkError() << "For" << m_name << ": Inventory source" << m_inventory << "is not valid"; } else gkError() << "For" << m_name << ": 'inventory' field must be a table"; }