Skip to content

Commit

Permalink
fix names & Bed.Head trouble
Browse files Browse the repository at this point in the history
  • Loading branch information
FDUTCH committed Jan 13, 2025
1 parent 456aeed commit 737c961
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
30 changes: 21 additions & 9 deletions server/block/bed.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (Bed) SideClosed(cube.Pos, cube.Pos, *world.Tx) bool {

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

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

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

sidePos := pos.Side(face)
o, ok := w.Block(sidePos).(Bed)
o, ok := tx.Block(sidePos).(Bed)
return o, sidePos, ok
}

Expand All @@ -237,7 +237,19 @@ func (Bed) CanRespawnOn() bool {
return true
}

func (Bed) RespawnOn(pos cube.Pos, u item.User, w *world.Tx) {}
func (Bed) RespawnOn(pos cube.Pos, u item.User, tx *world.Tx) {}

// SleepOn called to set Bed.Head if succeed startSleeping called.
func (b Bed) SleepOn(pos cube.Pos, sleeper world.Sleeper, tx *world.Tx, startSleeping func() bool) {
head, headPos, ok := b.head(pos, tx)
if !ok {
return
}
if startSleeping() {
head.Sleeper = sleeper.H()
tx.SetBlock(headPos, head, nil)
}
}

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

// supportedFromBelow ...
func supportedFromBelow(pos cube.Pos, w *world.Tx) bool {
func supportedFromBelow(pos cube.Pos, tx *world.Tx) bool {
below := pos.Side(cube.FaceDown)
return w.Block(below).Model().FaceSolid(below, cube.FaceUp, w)
return tx.Block(below).Model().FaceSolid(below, cube.FaceUp, tx)
}
31 changes: 17 additions & 14 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,25 +1205,28 @@ func (p *Player) Sleep(pos cube.Pos) {
}

ctx, sendReminder := event.C(p), true
if p.Handler().HandleSleep(ctx, &sendReminder); ctx.Cancelled() {
return
}

b.Sleeper = p.H()
tx.SetBlock(pos, b, nil)
b.SleepOn(pos, p, p.tx, func() bool {
if p.Handler().HandleSleep(ctx, &sendReminder); ctx.Cancelled() {
return false
}

tx.World().SetRequiredSleepDuration(time.Second * 5)
p.sleeping = true

p.data.Pos = pos.Vec3Middle().Add(mgl64.Vec3{0, 0.5625})
p.sleeping = true
p.sleepPos = pos
tx.World().SetRequiredSleepDuration(time.Second * 5)

if sendReminder {
tx.BroadcastSleepingReminder(p)
}
p.data.Pos = pos.Vec3Middle().Add(mgl64.Vec3{0, 0.5625})
p.sleepPos = pos

if sendReminder {
tx.BroadcastSleepingReminder(p)
}

tx.BroadcastSleepingIndicator()
p.updateState()
return true
})

tx.BroadcastSleepingIndicator()
p.updateState()
}

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

0 comments on commit 737c961

Please sign in to comment.