Skip to content

Commit

Permalink
fix: logs and talkaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Sep 20, 2024
1 parent 8907987 commit 56822e5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
27 changes: 20 additions & 7 deletions data/scripts/talkactions/god/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ testLog:separator(" ")
testLog:groupType("god")
testLog:register()

-- @module ContainerTalkAction
-- @function Handles the "!testcontainer" TalkAction command for testing the ContainerIterator functionality.
--
-- This module defines a TalkAction that allows players to inspect their backpack containers.
-- It can optionally remove items from the container based on the provided parameter.
-- The command logs the total number of items and subcontainers found in the backpack.
-- This is primarily used to verify the correctness of changes made to the ContainerIterator.
local containerTalkAction = TalkAction("!testcontainer")

function containerTalkAction.onSay(player, words, param)
Expand All @@ -47,21 +54,27 @@ function containerTalkAction.onSay(player, words, param)
return true
end

local shouldRemove = (param and param:lower() == "remove") and true or false
local items = container:getItems(true)
local totalItems = 0
local totalSubContainers = 0
for i, item in pairs(items) do
if item then
local itemName = item:getName()

player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Found item: " .. itemName)
if shouldRemove and item then
item:remove()
end
-- Increment the item counter
totalItems = totalItems + 1

if item:getContainer() then
totalSubContainers = totalSubContainers + 1
else
totalItems = totalItems + 1
end
end

logger.error("[!container] - Player: {} executed the '!container' command. Total items found: {}.", player:getName(), totalItems)
local actionMessage = shouldRemove and "removed" or "have"
local playerMessage = actionMessage .. totalItems .. " items and " .. totalSubContainers .. " subcontainers from your backpack."

logger.info("[!testcontainer] - Player: {}, {} items from backpack: {}, subcontainers count: {}", player:getName(), actionMessage, totalItems, totalSubContainers)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, playerMessage)
return true
end

Expand Down
19 changes: 19 additions & 0 deletions src/lua/functions/items/item_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ int ItemFunctions::luaItemIsItem(lua_State* L) {
return 1;
}

int ItemFunctions::luaItemGetContainer(lua_State* L) {
// item:getContainer()
const auto &item = getUserdataShared<Item>(L, 1);
if (!item) {
lua_pushnil(L);
return 1;
}

const auto &container = item->getContainer();
if (!container) {
g_logger().trace("Item {} is not a container", item->getName());
pushBoolean(L, false);
return 1;
}

pushUserdata(L, container);
return 1;
}

int ItemFunctions::luaItemGetParent(lua_State* L) {
// item:getParent()
std::shared_ptr<Item> item = getUserdataShared<Item>(L, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/items/item_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ItemFunctions final : LuaScriptInterface {

registerMethod(L, "Item", "isItem", ItemFunctions::luaItemIsItem);

registerMethod(L, "Item", "getContainer", ItemFunctions::luaItemGetContainer);
registerMethod(L, "Item", "getParent", ItemFunctions::luaItemGetParent);
registerMethod(L, "Item", "getTopParent", ItemFunctions::luaItemGetTopParent);

Expand Down Expand Up @@ -101,6 +102,7 @@ class ItemFunctions final : LuaScriptInterface {

static int luaItemIsItem(lua_State* L);

static int luaItemGetContainer(lua_State* L);
static int luaItemGetParent(lua_State* L);
static int luaItemGetTopParent(lua_State* L);

Expand Down

0 comments on commit 56822e5

Please sign in to comment.