Skip to content

Commit 737c961

Browse files
committed
fix names & Bed.Head trouble
1 parent 456aeed commit 737c961

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

server/block/bed.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (Bed) SideClosed(cube.Pos, cube.Pos, *world.Tx) bool {
4242

4343
// BreakInfo ...
4444
func (b Bed) BreakInfo() BreakInfo {
45-
return newBreakInfo(0.2, alwaysHarvestable, nothingEffective, oneOf(b)).withBreakHandler(func(pos cube.Pos, w *world.Tx, _ item.User) {
46-
headSide, _, ok := b.head(pos, w)
45+
return newBreakInfo(0.2, alwaysHarvestable, nothingEffective, oneOf(b)).withBreakHandler(func(pos cube.Pos, tx *world.Tx, _ item.User) {
46+
headSide, _, ok := b.head(pos, tx)
4747
if !ok {
4848
return
4949
}
@@ -201,8 +201,8 @@ func (b Bed) DecodeNBT(data map[string]interface{}) interface{} {
201201
}
202202

203203
// head returns the head side of the bed. If neither side is a head side, the third return value is false.
204-
func (b Bed) head(pos cube.Pos, w *world.Tx) (Bed, cube.Pos, bool) {
205-
headSide, headPos, ok := b.side(pos, w)
204+
func (b Bed) head(pos cube.Pos, tx *world.Tx) (Bed, cube.Pos, bool) {
205+
headSide, headPos, ok := b.side(pos, tx)
206206
if !ok {
207207
return Bed{}, cube.Pos{}, false
208208
}
@@ -213,14 +213,14 @@ func (b Bed) head(pos cube.Pos, w *world.Tx) (Bed, cube.Pos, bool) {
213213
}
214214

215215
// side returns the other side of the bed. If the other side is not a bed, the third return value is false.
216-
func (b Bed) side(pos cube.Pos, w *world.Tx) (Bed, cube.Pos, bool) {
216+
func (b Bed) side(pos cube.Pos, tx *world.Tx) (Bed, cube.Pos, bool) {
217217
face := b.Facing.Face()
218218
if b.Head {
219219
face = face.Opposite()
220220
}
221221

222222
sidePos := pos.Side(face)
223-
o, ok := w.Block(sidePos).(Bed)
223+
o, ok := tx.Block(sidePos).(Bed)
224224
return o, sidePos, ok
225225
}
226226

@@ -237,7 +237,19 @@ func (Bed) CanRespawnOn() bool {
237237
return true
238238
}
239239

240-
func (Bed) RespawnOn(pos cube.Pos, u item.User, w *world.Tx) {}
240+
func (Bed) RespawnOn(pos cube.Pos, u item.User, tx *world.Tx) {}
241+
242+
// SleepOn called to set Bed.Head if succeed startSleeping called.
243+
func (b Bed) SleepOn(pos cube.Pos, sleeper world.Sleeper, tx *world.Tx, startSleeping func() bool) {
244+
head, headPos, ok := b.head(pos, tx)
245+
if !ok {
246+
return
247+
}
248+
if startSleeping() {
249+
head.Sleeper = sleeper.H()
250+
tx.SetBlock(headPos, head, nil)
251+
}
252+
}
241253

242254
// RespawnBlock represents a block using which player can set his spawn point.
243255
type RespawnBlock interface {
@@ -248,7 +260,7 @@ type RespawnBlock interface {
248260
}
249261

250262
// supportedFromBelow ...
251-
func supportedFromBelow(pos cube.Pos, w *world.Tx) bool {
263+
func supportedFromBelow(pos cube.Pos, tx *world.Tx) bool {
252264
below := pos.Side(cube.FaceDown)
253-
return w.Block(below).Model().FaceSolid(below, cube.FaceUp, w)
265+
return tx.Block(below).Model().FaceSolid(below, cube.FaceUp, tx)
254266
}

server/player/player.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,25 +1205,28 @@ func (p *Player) Sleep(pos cube.Pos) {
12051205
}
12061206

12071207
ctx, sendReminder := event.C(p), true
1208-
if p.Handler().HandleSleep(ctx, &sendReminder); ctx.Cancelled() {
1209-
return
1210-
}
12111208

1212-
b.Sleeper = p.H()
1213-
tx.SetBlock(pos, b, nil)
1209+
b.SleepOn(pos, p, p.tx, func() bool {
1210+
if p.Handler().HandleSleep(ctx, &sendReminder); ctx.Cancelled() {
1211+
return false
1212+
}
12141213

1215-
tx.World().SetRequiredSleepDuration(time.Second * 5)
1214+
p.sleeping = true
12161215

1217-
p.data.Pos = pos.Vec3Middle().Add(mgl64.Vec3{0, 0.5625})
1218-
p.sleeping = true
1219-
p.sleepPos = pos
1216+
tx.World().SetRequiredSleepDuration(time.Second * 5)
12201217

1221-
if sendReminder {
1222-
tx.BroadcastSleepingReminder(p)
1223-
}
1218+
p.data.Pos = pos.Vec3Middle().Add(mgl64.Vec3{0, 0.5625})
1219+
p.sleepPos = pos
1220+
1221+
if sendReminder {
1222+
tx.BroadcastSleepingReminder(p)
1223+
}
1224+
1225+
tx.BroadcastSleepingIndicator()
1226+
p.updateState()
1227+
return true
1228+
})
12241229

1225-
tx.BroadcastSleepingIndicator()
1226-
p.updateState()
12271230
}
12281231

12291232
// Wake forces the player out of bed if they are sleeping.

0 commit comments

Comments
 (0)