-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
171 lines (139 loc) · 4.65 KB
/
utils.lua
File metadata and controls
171 lines (139 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
local Utils = {}
Utils.__index = Utils
function Utils:new()
local self = setmetatable({}, Utils)
self.compressedDataFile = "compressed_chest_data.txt"
if not fs.exists(self.compressedDataFile) then
fs.open(self.compressedDataFile, "w").close()
end
return self
end
function Utils:clearLineAt(line)
term.setCursorPos(1, line)
term.clearLine()
end
function Utils:saveAllChestData(data)
local file = fs.open(self.compressedDataFile, "w")
file.write(textutils.serializeJSON(data))
file.close()
end
function Utils:loadAllChestData()
if not fs.exists(self.compressedDataFile) then return {} end
local file = fs.open(self.compressedDataFile, "r")
local data = textutils.unserializeJSON(file.readAll()) or {}
file.close()
return data
end
function Utils:saveData(filename, data)
local file = fs.open(filename, "w")
file.write(textutils.serialize(data))
file.close()
end
function Utils:loadData(filename)
if not fs.exists(filename) then return nil end
local file = fs.open(filename, "r")
local data = textutils.unserialize(file.readAll())
file.close()
return data
end
function Utils:contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function Utils:getConnectedChests()
local chests = {}
for _, name in ipairs(peripheral.getNames()) do
local type = peripheral.getType(name)
if type == "minecraft:chest" or type == "minecraft:barrel" or type == "storagedrawers:basicdrawers" then
table.insert(chests, peripheral.wrap(name))
end
end
return chests
end
function Utils:getStorageInfo()
local chests = self:getConnectedChests()
local totalSlots = 0
local usedSlots = 0
local chestStates = {}
for _, chest in ipairs(chests) do
local chestName = peripheral.getName(chest)
local chestSize = chest.size()
local itemCount = #chest.list()
totalSlots = totalSlots + chestSize
usedSlots = usedSlots + itemCount
table.insert(chestStates, {
name = chestName,
connected = true,
itemCount = itemCount,
chestSize = chestSize,
percentFull = math.floor((itemCount / chestSize) * 100)
})
end
return {
totalSlots = totalSlots,
usedSlots = usedSlots,
chestCount = #chests,
chestStates = chestStates
}
end
Utils.cache = {
mostCommonItems = nil,
lastUpdated = 0,
cacheDuration = 30 -- Duración en segundos para actualizar la caché
}
function Utils:getMostCommonItems(limit)
local currentTime = os.epoch("utc") / 1000
if self.cache.mostCommonItems and (currentTime - self.cache.lastUpdated) < self.cache.cacheDuration then
return self.cache.mostCommonItems -- Devolver la caché si aún es válida
end
-- Recargar datos si la caché ha expirado
local itemCounts = {}
local chests = self:getConnectedChests()
for _, chest in pairs(chests) do
local items = chest.list()
for _, item in pairs(items) do
local itemName = item.name
itemCounts[itemName] = (itemCounts[itemName] or 0) + item.count
end
end
local mostCommonItems = {}
for name, count in pairs(itemCounts) do
table.insert(mostCommonItems, { name = name, count = count })
end
table.sort(mostCommonItems, function(a, b) return a.count > b.count end)
self.cache.mostCommonItems = mostCommonItems
self.cache.lastUpdated = currentTime -- Actualizar `lastUpdated`
return mostCommonItems
end
function Utils:showLoadingScreen()
term.clear()
local width, height = term.getSize()
local loadingText = "Inicializando el sistema..."
local animation = { "[-]", "[\\]", "[|]", "[/]" }
local animIndex = 1
while not _G.initializationComplete do
term.clear()
term.setCursorPos(1, 1)
local progressMessage = _G.loadingMessage or ""
-- Title
term.setCursorPos(math.floor((width - #loadingText) / 2) + 1, math.floor(height / 2) - 2)
term.write(loadingText)
-- Progress msg
term.setCursorPos(1, math.floor(height / 2))
term.clearLine()
term.setCursorPos(math.floor((width - #progressMessage) / 2) + 1, math.floor(height / 2))
term.write(progressMessage)
-- Loader
term.setCursorPos(math.floor(width / 2) - 1, math.floor(height / 2) + 2)
term.write(animation[animIndex])
animIndex = (animIndex % #animation) + 1
sleep(0.1)
end
term.clear()
term.setCursorPos(1, 1)
end
return Utils