Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 6b4c865

Browse files
author
Dave Tucker
committed
Fix update sequencing issues
I noticed some weird things happening after I'd changed to using a newly generated world. Namely, the WorldStarted hook was being triggered *after* the updates for other containers had already been queued! To fix this I: - Made all ground drawing happen in addGround - Changed the update queue to be a more traditional strict queue - This has the added benefit that we can check the length easily - Removed unused hooks - WorldStarted and OnChunkGenerated Signed-off-by: Dave Tucker <dt@docker.com>
1 parent 7d4fae4 commit 6b4c865

File tree

3 files changed

+114
-128
lines changed

3 files changed

+114
-128
lines changed

Docker/container.lua

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,26 @@ end
200200
-- Container:addGround creates ground blocks
201201
-- necessary to display the container
202202
function Container:addGround()
203-
local y = GROUND_LEVEL
204-
205-
if GROUND_MIN_X > self.x - 2
206-
then
207-
OLD_GROUND_MIN_X = GROUND_MIN_X
208-
GROUND_MIN_X = self.x - 2
209-
for x= GROUND_MIN_X, OLD_GROUND_MIN_X
210-
do
211-
for z=GROUND_MIN_Z,GROUND_MAX_Z
212-
do
213-
setBlock(UpdateQueue,x,y,z,E_BLOCK_WOOL,E_META_WOOL_WHITE)
214-
end
215-
end
216-
end
203+
local y = GROUND_LEVEL
204+
local max_x = GROUND_MAX_X
205+
206+
if GROUND_MIN_X > self.x - 2
207+
then
208+
max_x = GROUND_MIN_X
209+
GROUND_MIN_X = self.x - 2
210+
min_x = GROUND_MIN_X
211+
end
212+
213+
local min_x = GROUND_MIN_X
214+
for x= min_x, max_x
215+
do
216+
for z=GROUND_MIN_Z,GROUND_MAX_Z
217+
do
218+
setBlock(UpdateQueue,x,y,z,E_BLOCK_WOOL,E_META_WOOL_WHITE)
219+
for sky=y+1,y+6
220+
do
221+
setBlock(UpdateQueue,x,sky,z,E_BLOCK_AIR,0)
222+
end
223+
end
224+
end
217225
end

Docker/docker.lua

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ function Initialize(Plugin)
3030

3131
-- Hooks
3232

33-
cPluginManager:AddHook(cPluginManager.HOOK_WORLD_STARTED, WorldStarted);
3433
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_JOINED, PlayerJoined);
3534
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_USING_BLOCK, PlayerUsingBlock);
36-
cPluginManager:AddHook(cPluginManager.HOOK_CHUNK_GENERATING, OnChunkGenerating);
3735
cPluginManager:AddHook(cPluginManager.HOOK_PLAYER_FOOD_LEVEL_CHANGE, OnPlayerFoodLevelChange);
3836
cPluginManager:AddHook(cPluginManager.HOOK_TAKE_DAMAGE, OnTakeDamage);
3937
cPluginManager:AddHook(cPluginManager.HOOK_WEATHER_CHANGING, OnWeatherChanging);
@@ -180,20 +178,6 @@ function updateContainer(id,name,imageRepo,imageTag,state)
180178
end
181179
end
182180

183-
--
184-
function WorldStarted(World)
185-
local y = GROUND_LEVEL
186-
-- just enough to fit one container
187-
-- then it should be dynamic
188-
for x = GROUND_MIN_X, GROUND_MAX_X
189-
do
190-
for z = GROUND_MIN_Z, GROUND_MAX_Z
191-
do
192-
setBlock(UpdateQueue,x,y,z,E_BLOCK_WOOL,E_META_WOOL_WHITE)
193-
end
194-
end
195-
end
196-
197181
--
198182
function PlayerJoined(Player)
199183
-- enable flying
@@ -245,17 +229,6 @@ function PlayerUsingBlock(Player, BlockX, BlockY, BlockZ, BlockFace, CursorX, Cu
245229
end
246230

247231

248-
function OnChunkGenerating(World, ChunkX, ChunkZ, ChunkDesc)
249-
-- override the built-in chunk generator
250-
-- to have it generate empty chunks only
251-
ChunkDesc:SetUseDefaultBiomes(false)
252-
ChunkDesc:SetUseDefaultComposition(false)
253-
ChunkDesc:SetUseDefaultFinish(false)
254-
ChunkDesc:SetUseDefaultHeight(false)
255-
return true
256-
end
257-
258-
259232
function DockerCommand(Split, Player)
260233
if table.getn(Split) > 0
261234
then

Docker/update.lua

Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -12,111 +12,116 @@ UPDATE_DIG = 1
1212
UPDATE_SIGN = 2
1313

1414
function NewUpdateQueue()
15-
queue = {first=nil, last=nil, current=nil}
15+
queue = {first=0, last=-1, current=nil}
1616

17-
-- queue.newUpdate creates an update operation and
18-
-- inserts it in the queue or returns an error
19-
-- in case of UPDATE_SIGN, line 1 to 4 should
20-
-- be present in meta parameter.
21-
-- the delay is optional and will make sure that
22-
-- the update is not triggered before given amount
23-
-- of ticks (0 by default)
24-
function queue:newUpdate(updateType, x, y, z, blockID, meta, delay)
25-
if updateType ~= UPDATE_SET and updateType ~= UPDATE_DIG and updateType ~= UPDATE_SIGN
26-
then
27-
return NewError(1,"NewUpdate: wrong update type")
28-
end
29-
30-
if delay == nil
31-
then
32-
delay = 0
33-
end
34-
35-
update = {op=updateType,next=nil,x=x,y=y,z=z,blockID=blockID,meta=meta, delay=delay}
17+
-- queue.newUpdate creates an update operation and
18+
-- inserts it in the queue or returns an error
19+
-- in case of UPDATE_SIGN, line 1 to 4 should
20+
-- be present in meta parameter.
21+
-- the delay is optional and will make sure that
22+
-- the update is not triggered before given amount
23+
-- of ticks (0 by default)
24+
function queue:newUpdate(updateType, x, y, z, blockID, meta, delay)
25+
if updateType ~= UPDATE_SET and updateType ~= UPDATE_DIG and updateType ~= UPDATE_SIGN
26+
then
27+
return NewError(1,"NewUpdate: wrong update type")
28+
end
3629

37-
-- update.exec executes update operation
38-
-- and returns an error if it fails
39-
function update:exec()
40-
if self.op == UPDATE_SET
41-
then
42-
cRoot:Get():GetDefaultWorld():SetBlock(self.x,self.y,self.z,self.blockID,self.meta)
43-
elseif self.op == UPDATE_DIG
44-
then
45-
cRoot:Get():GetDefaultWorld():DigBlock(self.x,self.y,self.z)
46-
elseif self.op == UPDATE_SIGN
47-
then
48-
cRoot:Get():GetDefaultWorld():SetSignLines(self.x,self.y,self.z,self.meta.line1,self.meta.line2,self.meta.line3,self.meta.line4)
49-
else
50-
return NewError(1,"update:exec: unknown update type: " .. tostring(self.op))
51-
end
52-
end
30+
if delay == nil
31+
then
32+
delay = 0
33+
end
5334

54-
if self.first == nil
55-
then
56-
self.first = update
57-
self.last = update
58-
else
59-
self.last.next = update
60-
self.last = update
61-
end
62-
end -- ends queue.newUpdate
35+
update = {op=updateType,x=x,y=y,z=z,blockID=blockID,meta=meta,delay=delay}
6336

64-
-- update triggers updates starting from
65-
-- the first one. It stops when the limit
66-
-- is reached, of if there are no more
67-
-- operations in the queue. It returns
68-
-- the amount of updates executed.
69-
-- When an update has a delay > 0, the delay
70-
-- is decremented, and the number of updates
71-
-- executed is not incremented.
72-
function queue:update(limit)
73-
n = 0
74-
self.current = self.first
37+
-- update.exec executes update operation
38+
-- and returns an error if it fails
39+
function update:exec()
40+
if self.op == UPDATE_SET
41+
then
42+
cRoot:Get():GetDefaultWorld():SetBlock(self.x,self.y,self.z,self.blockID,self.meta)
43+
elseif self.op == UPDATE_DIG
44+
then
45+
cRoot:Get():GetDefaultWorld():DigBlock(self.x,self.y,self.z)
46+
elseif self.op == UPDATE_SIGN
47+
then
48+
cRoot:Get():GetDefaultWorld():SetSignLines(self.x,self.y,self.z,self.meta.line1,self.meta.line2,self.meta.line3,self.meta.line4)
49+
else
50+
return NewError(1,"update:exec: unknown update type: " .. tostring(self.op))
51+
end
52+
end
7553

76-
while n < limit and self.current ~= nil
77-
do
78-
if self.current.delay == 0
79-
then
80-
err = self.current:exec()
81-
if err ~= nil
82-
then
83-
LOG("queue:update error: " .. err.message)
84-
break
85-
end
86-
87-
if self.current == self.first
88-
then
89-
self.first = self.current.next
90-
end
91-
n = n + 1
92-
else
93-
self.current.delay = self.current.delay - 1
94-
end
95-
self.current = self.current.next
96-
end
54+
self:push(update)
55+
end -- ends queue.newUpdate
9756

98-
return n
57+
-- update triggers updates starting from
58+
-- the first one. It stops when the limit
59+
-- is reached, of if there are no more
60+
-- operations in the queue. It returns
61+
-- the amount of updates executed.
62+
-- When an update has a delay > 0, the delay
63+
-- is decremented, and the number of updates
64+
-- executed is not incremented.
65+
function queue:update(limit)
66+
n = 0
67+
if self.current == nil
68+
then
69+
self.current = self:pop()
70+
end
71+
while n < limit and self.current ~= nil
72+
do
73+
if self.current.delay == 0
74+
then
75+
err = self.current:exec()
76+
if err ~= nil
77+
then
78+
LOG("queue:update error: " .. err.message)
79+
break
80+
end
81+
n = n + 1
82+
self.current = self:pop()
83+
else
84+
self.current.delay = self.current.delay - 1
85+
end
9986
end
87+
return n
88+
end
10089

101-
return queue
90+
function queue:push(value)
91+
local last = self.last + 1
92+
self.last = last
93+
self[last] = value
94+
end
95+
96+
function queue:pop()
97+
local first = self.first
98+
if first > self.last then return nil end
99+
local value = self[first]
100+
self[first] = nil -- to allow garbage collection
101+
self.first = first + 1
102+
return value
103+
end
104+
105+
return queue
102106
end
103107

104-
-- setBlock adds an update in given queue to
108+
-- setBlock adds an update in given queue to
105109
-- set a block at x,y,z coordinates
106110
function setBlock(queue,x,y,z,blockID,meta)
107-
queue:newUpdate(UPDATE_SET, x, y, z, blockID, meta)
111+
queue:newUpdate(UPDATE_SET, x, y, z, blockID, meta)
108112
end
109113

110-
-- setBlock adds an update in given queue to
114+
-- setBlock adds an update in given queue to
111115
-- remove a block at x,y,z coordinates
112116
function digBlock(queue,x,y,z)
113-
queue:newUpdate(UPDATE_DIG, x, y, z)
117+
queue:newUpdate(UPDATE_DIG, x, y, z)
114118
end
115119

116-
-- setBlock adds an update in given queue to
120+
-- setBlock adds an update in given queue to
117121
-- update a sign at x,y,z coordinates with
118122
-- 4 lines of text
119123
function updateSign(queue,x,y,z,line1,line2,line3,line4,delay)
120-
meta = {line1=line1,line2=line2,line3=line3,line4=line4}
121-
queue:newUpdate(UPDATE_SIGN, x, y, z, nil, meta, delay)
122-
end
124+
meta = {line1=line1,line2=line2,line3=line3,line4=line4}
125+
queue:newUpdate(UPDATE_SIGN, x, y, z, nil, meta, delay)
126+
end
127+

0 commit comments

Comments
 (0)