forked from lord-server/lord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ObjectState class. Closes lord-server#1515
- Loading branch information
Showing
3 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
mods/lord/Core/base_classes/src/base_classes/ObjectState.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
46 changes: 46 additions & 0 deletions
46
mods/lord/Core/base_classes/src/base_classes/ObjectState/Storage.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |