Skip to content

Commit

Permalink
world/loader.go: Fixed crash when changing worlds (after dying).
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Dec 23, 2024
1 parent 20a1e6d commit c507072
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions server/world/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package world

import (
"github.com/go-gl/mathgl/mgl64"
"maps"
"math"
"sync"
)
Expand Down Expand Up @@ -45,7 +46,15 @@ func (l *Loader) ChangeWorld(tx *Tx, new *World) {
l.mu.Lock()
defer l.mu.Unlock()

l.reset(tx)
loaded := maps.Clone(l.loaded)
l.w.Exec(func(tx *Tx) {
for pos := range loaded {
tx.World().removeViewer(tx, pos, l)
}
})
clear(l.loaded)
delete(l.w.viewers, l)

l.world(new)
}

Expand Down Expand Up @@ -117,28 +126,14 @@ func (l *Loader) Close(tx *Tx) {
l.mu.Lock()
defer l.mu.Unlock()

l.reset(tx)
l.closed = true
l.viewer = nil
}

// Reset clears all chunks loaded by the Loader and repopulates the loading queue so that they can all be loaded again.
func (l *Loader) Reset(tx *Tx) {
l.mu.Lock()
defer l.mu.Unlock()

l.reset(tx)
l.w.addWorldViewer(l)
l.populateLoadQueue()
}

// reset clears the Loader so that it may be used as if it was created again with NewLoader.
func (l *Loader) reset(tx *Tx) {
for pos := range l.loaded {
l.w.removeViewer(tx, pos, l)
tx.World().removeViewer(tx, pos, l)
}
l.loaded = map[ChunkPos]*Column{}
delete(l.w.viewers, l)

l.closed = true
l.viewer = nil
}

// world sets the loader's world, adds them to the world's viewer list, then starts populating the load queue.
Expand Down

0 comments on commit c507072

Please sign in to comment.