Skip to content

Commit

Permalink
player/player.go: Handle block breaking server-side.
Browse files Browse the repository at this point in the history
Resolves #960. Also fixes weird breaking bugs because `p.breaking` was never set to false(!)
  • Loading branch information
Sandertv committed Jan 5, 2025
1 parent c209ad4 commit e9ed5f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
24 changes: 13 additions & 11 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ type playerData struct {

breaking bool
breakingPos cube.Pos
breakingFace cube.Face
lastBreakDuration time.Duration

breakParticleCounter uint32
breakCounter uint32

hunger *hungerManager

Expand Down Expand Up @@ -1730,7 +1731,7 @@ func (p *Player) StartBreaking(pos cube.Pos, face cube.Face) {
punchable.Punch(pos, face, p.tx, p)
}

p.breaking = true
p.breaking, p.breakingFace = true, face
p.SwingArm()

if p.GameMode().CreativeInventory() {
Expand Down Expand Up @@ -1786,7 +1787,7 @@ func (p *Player) AbortBreaking() {
if !p.breaking {
return
}
p.breaking, p.breakParticleCounter = true, 0
p.breaking, p.breakCounter = false, 0
for _, viewer := range p.viewers() {
viewer.ViewBlockAction(p.breakingPos, block.StopCrackAction{})
}
Expand All @@ -1800,19 +1801,17 @@ func (p *Player) ContinueBreaking(face cube.Face) {
return
}
pos := p.breakingPos

p.SwingArm()

b := p.tx.Block(pos)
p.tx.AddParticle(pos.Vec3(), particle.PunchBlock{Block: b, Face: face})

if p.breakParticleCounter += 1; p.breakParticleCounter%5 == 0 {
if p.breakCounter++; p.breakCounter%5 == 0 {
p.SwingArm()

// We send this sound only every so often. Vanilla doesn't send it every tick while breaking
// either. Every 5 ticks seems accurate.
p.tx.PlaySound(pos.Vec3(), sound.BlockBreaking{Block: p.tx.Block(pos)})
p.tx.PlaySound(pos.Vec3(), sound.BlockBreaking{Block: b})
}
breakTime := p.breakTime(pos)
if breakTime != p.lastBreakDuration {
if breakTime := p.breakTime(pos); breakTime != p.lastBreakDuration {
for _, viewer := range p.viewers() {
viewer.ViewBlockAction(pos, block.ContinueCrackAction{BreakTime: breakTime})
}
Expand Down Expand Up @@ -2348,7 +2347,7 @@ func (p *Player) Tick(tx *world.Tx, current int64) {
p.tickFood()
p.tickAirSupply()

if p.Position()[1] < float64(p.tx.Range()[0]) && p.GameMode().AllowsTakingDamage() && current%10 == 0 {
if p.Position()[1] < float64(p.tx.Range()[0]) {
p.Hurt(4, entity.VoidDamageSource{})
}
if p.insideOfSolid() {
Expand Down Expand Up @@ -2380,6 +2379,9 @@ func (p *Player) Tick(tx *world.Tx, current int64) {
c.ContinueCharge(p, tx, p.useContext(), p.useDuration())
}
}
if p.breaking {
p.ContinueBreaking(p.breakingFace)
}

for it, ti := range p.cooldowns {
if time.Now().After(ti) {
Expand Down
17 changes: 3 additions & 14 deletions server/session/handler_player_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,16 @@ func handlePlayerAction(action int32, face int32, pos protocol.BlockPos, entityR
defer s.swingingArm.Store(false)
c.FinishBreaking()
case protocol.PlayerActionCrackBreak:
s.swingingArm.Store(true)
defer s.swingingArm.Store(false)

newPos := cube.Pos{int(pos[0]), int(pos[1]), int(pos[2])}

// Sometimes no new position will be sent using a StartBreak action, so we need to detect a change in the
// block to be broken by comparing positions.
if newPos != s.breakingPos {
s.breakingPos = newPos
c.StartBreaking(newPos, cube.Face(face))
return nil
}
c.ContinueBreaking(cube.Face(face))
// Don't do anything for this action. It is no longer used. Block
// cracking is done fully server-side.
case protocol.PlayerActionStartItemUseOn:
// TODO: Properly utilize these actions.
case protocol.PlayerActionStopItemUseOn:
c.ReleaseItem()
case protocol.PlayerActionStartBuildingBlock:
// Don't do anything for this action.
case protocol.PlayerActionCreativePlayerDestroyBlock:
// Don't do anything for this action.
// Don't do anything for this action.
case protocol.PlayerActionMissedSwing:
s.swingingArm.Store(true)
defer s.swingingArm.Store(false)
Expand Down

0 comments on commit e9ed5f2

Please sign in to comment.