Skip to content

Commit

Permalink
Add basic things attributes modification talkaction (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski authored Aug 25, 2021
1 parent e1feac2 commit 89ef7b0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
48 changes: 48 additions & 0 deletions data/talkactions/scripts/attributes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function onSay(player, words, param)
if not player:getGroup():getAccess() then
return true
end

local position = player:getPosition()
position:getNextPosition(player:getDirection())

local tile = Tile(position)
if not tile then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no tile in front of you.")
return false
end

local thing = tile:getTopVisibleThing(player)
if not thing then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is an empty tile in front of you.")
return false
end

local separatorPos = param:find(',')
if not separatorPos then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Usage: %s attribute, value.", words))
return false
end

local attribute = string.trim(param:sub(0, separatorPos - 1))
local value = string.trim(param:sub(separatorPos + 1))

if thing:isItem() then
local attributeId = Game.getItemAttributeByName(attribute)
if attributeId == ITEM_ATTRIBUTE_NONE then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Invalid attribute name.")
return false
end

if not thing:setAttribute(attribute, value) then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Could not set attribute.")
return false
end

player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, string.format("Attribute %s set to: %s", attribute, thing:getAttribute(attributeId)))
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
else
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Thing in front of you is not supported.")
return false
end
end
1 change: 1 addition & 0 deletions data/talkactions/talkactions.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<talkactions>
<!-- commands -->
<talkaction words="/attr" separator=" " script="attributes.lua" />
<talkaction words="/ban" separator=" " script="ban.lua" />
<talkaction words="/ipban" separator=" " script="ipban.lua" />
<talkaction words="/unban" separator=" " script="unban.lua" />
Expand Down
8 changes: 8 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Game", "getWorldType", LuaScriptInterface::luaGameGetWorldType);
registerMethod("Game", "setWorldType", LuaScriptInterface::luaGameSetWorldType);

registerMethod("Game", "getItemAttributeByName", LuaScriptInterface::luaGameGetItemAttributeByName);
registerMethod("Game", "getReturnMessage", LuaScriptInterface::luaGameGetReturnMessage);

registerMethod("Game", "createItem", LuaScriptInterface::luaGameCreateItem);
Expand Down Expand Up @@ -4379,6 +4380,13 @@ int LuaScriptInterface::luaGameGetReturnMessage(lua_State* L)
return 1;
}

int LuaScriptInterface::luaGameGetItemAttributeByName(lua_State* L)
{
// Game.getItemAttributeByName(name)
lua_pushnumber(L, stringToItemAttribute(getString(L, 1)));
return 1;
}

int LuaScriptInterface::luaGameCreateItem(lua_State* L)
{
// Game.createItem(itemId[, count[, position]])
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ class LuaScriptInterface
static int luaGameGetWorldType(lua_State* L);
static int luaGameSetWorldType(lua_State* L);

static int luaGameGetItemAttributeByName(lua_State* L);
static int luaGameGetReturnMessage(lua_State* L);

static int luaGameCreateItem(lua_State* L);
Expand Down

0 comments on commit 89ef7b0

Please sign in to comment.