Skip to content

Commit

Permalink
Add base_classes.ObjectState.Storage. Relates to #1515
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 authored and Doloment committed Aug 20, 2024
1 parent 759c229 commit 2b0f9f5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
6 changes: 5 additions & 1 deletion mods/lord/Core/base_classes/src/base_classes/ObjectState.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
local Storage = require("base_classes.ObjectState.Storage")


--- @class base_classes.ObjectState
local ObjectState = {
--- @type table
state = nil,
}

--- @param state_table table Table of all state entries
--- @return ObjectState
--- @return base_classes.ObjectState
function ObjectState:new(state_table)
local class = self
self = {}
Expand All @@ -24,4 +27,5 @@ function ObjectState:remove_state_entry(entry_name)
return self
end


return ObjectState
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--- @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 object:get_luaentity().object_state
end

--- Obtains `ObjectState` from meta or object properties depending on whether `object` is a player or not
--- @param object ObjectRef a player or an entity to apply `ObjectState` to
--- @param state_table table
function Storage.set_state_of(object, state_table)
if not object then
return
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()
entity.object_state = state_table
end
end


return Storage
41 changes: 0 additions & 41 deletions mods/lord/Core/builtin/src/object_state.lua

This file was deleted.

7 changes: 6 additions & 1 deletion util/mt-ide-helper/classes/ObjectRef/Entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ function Entity:set_velocity(vel) end
--* Fifth column: subject viewed from above
--* Sixth column: subject viewed from below
--* `get_entity_name()` (**Deprecated**: Will be removed in a future version, use the field `self.name` instead)
--* `get_luaentity()`


--- Returns the object's associated luaentity table, if there is one
--- Otherwise returns nil (e.g. for players)
--- @return LuaEntity|nil
function Entity:get_luaentity() end

0 comments on commit 2b0f9f5

Please sign in to comment.