Skip to content

Commit

Permalink
Add ObjectState class. Closes #1515
Browse files Browse the repository at this point in the history
  • Loading branch information
Doloment authored and alek13 committed Aug 22, 2024
1 parent 93f0525 commit 7f21d6a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mods/lord/Core/base_classes/src/base_classes.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
local Event = require("base_classes.Event")
local Form = require("base_classes.Form")
local Event = require("base_classes.Event")
local Form = require("base_classes.Form")
local ObjectState = require("base_classes.ObjectState")


base_classes = {} -- luacheck: ignore unused global variable base_classes

local function register_api()
_G.base_classes = {
--- @type base_classes.Event
Event = Event,
Event = Event,
--- @type base_classes.Form
Form = Form,
Form = Form,
--- @type base_classes.ObjectState
ObjectState = ObjectState,
}
end

Expand Down
53 changes: 53 additions & 0 deletions mods/lord/Core/base_classes/src/base_classes/ObjectState.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local Storage = require("base_classes.ObjectState.Storage")


--- @class base_classes.ObjectState
local ObjectState = {
--- @private
--- @type table<string,any>
state_table = nil,
}

--- @param object table Table of all state entries
--- @return base_classes.ObjectState
function ObjectState:new(object)
local class = self

self = setmetatable({}, { __index = class })
self:load(object)

return self
end

--- @param name string
--- @return any
function ObjectState:get_entry(name)
return self.state_table[name]
end

--- @param name string
--- @param value any
--- @return base_classes.ObjectState
function ObjectState:set_entry(name, value)
self.state_table[name] = value

return self
end
--- @param name string
--- @return base_classes.ObjectState
function ObjectState:del_entry(name)
return self:set_entry(name, nil)
end

--- @param object ObjectRef
--- @return boolean
function ObjectState:save(object)
return Storage.set_state_of(object, self.state_table)
end

--- @param object Player|Entity
function ObjectState:load(object)
self.state_table = Storage.get_state_of(object)
end

return ObjectState
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--- @static
--- @class base_classes.ObjectState.Storage
local Storage = {}

--- Obtains `ObjectState` from meta or object properties depending on whether `object` is a player or not
--- @param object Player|Entity
--- @return table
function Storage.get_state_of(object)
if not object then
return
end

return object:is_player()
and minetest.deserialize(object:get_meta():get("object_state") or "return {}")
or object:get_luaentity().object_state or {}
end

--- Obtains `ObjectState` from meta or object properties depending on whether `object` is a player or not
---
--- @param object Player|Entity a player or an entity to apply `ObjectState` to
--- @param state_table table
---
--- @return boolean success or not
function Storage.set_state_of(object, state_table)
if not object then
return false
end

local state_string = minetest.serialize(state_table)

if object:is_player() then
local meta = object:get_meta()
meta:set_string("object_state", state_string)
else
local entity = object:get_luaentity()
if not entity then
return false
end
entity.object_state = state_table
end

return true
end


return Storage

0 comments on commit 7f21d6a

Please sign in to comment.