Skip to content

Commit 240c152

Browse files
committed
fix time flicker
1 parent aada061 commit 240c152

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

server/session/world.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package session
22

33
import (
44
"fmt"
5-
"github.com/df-mc/dragonfly/server/entity/effect"
65
"image/color"
76
"math/rand/v2"
87
"strings"
98
"time"
109

10+
"github.com/df-mc/dragonfly/server/entity/effect"
11+
1112
"github.com/df-mc/dragonfly/server/block"
1213
"github.com/df-mc/dragonfly/server/block/cube"
1314
"github.com/df-mc/dragonfly/server/entity"
@@ -227,8 +228,9 @@ func entityOffset(e world.Entity) mgl64.Vec3 {
227228
}
228229

229230
// ViewTime ...
230-
func (s *Session) ViewTime(time int) {
231+
func (s *Session) ViewTime(time int, doDayLightCycle bool) {
231232
s.writePacket(&packet.SetTime{Time: int32(time)})
233+
s.sendGameRules([]protocol.GameRule{{Name: "dodaylightcycle", Value: doDayLightCycle}})
232234
}
233235

234236
// ViewEntityTeleport ...

server/world/viewer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package world
22

33
import (
4+
"time"
5+
46
"github.com/df-mc/dragonfly/server/block/cube"
57
"github.com/df-mc/dragonfly/server/world/chunk"
68
"github.com/go-gl/mathgl/mgl64"
79
"github.com/google/uuid"
8-
"time"
910
)
1011

1112
// Viewer is a viewer in the world. It can view changes that are made in the world, such as the addition of
@@ -38,7 +39,7 @@ type Viewer interface {
3839
ViewChunk(pos ChunkPos, dim Dimension, blockEntities map[cube.Pos]Block, c *chunk.Chunk)
3940
// ViewTime views the time of the world. It is called every time the time is changed or otherwise every
4041
// second.
41-
ViewTime(t int)
42+
ViewTime(t int, doDayLightCycle bool)
4243
// ViewEntityItems views the items currently held by an Entity that is able to equip items.
4344
ViewEntityItems(e Entity)
4445
// ViewEntityArmour views the items currently equipped as armour by the Entity.
@@ -86,7 +87,7 @@ func (NopViewer) ViewEntityMovement(Entity, mgl64.Vec3, cube.Rotation, bool)
8687
func (NopViewer) ViewEntityVelocity(Entity, mgl64.Vec3) {}
8788
func (NopViewer) ViewEntityTeleport(Entity, mgl64.Vec3) {}
8889
func (NopViewer) ViewChunk(ChunkPos, Dimension, map[cube.Pos]Block, *chunk.Chunk) {}
89-
func (NopViewer) ViewTime(int) {}
90+
func (NopViewer) ViewTime(int, bool) {}
9091
func (NopViewer) ViewEntityItems(Entity) {}
9192
func (NopViewer) ViewEntityArmour(Entity) {}
9293
func (NopViewer) ViewEntityAction(Entity, EntityAction) {}

server/world/world.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import (
44
"encoding/binary"
55
"errors"
66
"fmt"
7-
"github.com/df-mc/dragonfly/server/block/cube"
8-
"github.com/df-mc/dragonfly/server/event"
9-
"github.com/df-mc/dragonfly/server/internal/sliceutil"
10-
"github.com/df-mc/dragonfly/server/world/chunk"
11-
"github.com/df-mc/goleveldb/leveldb"
12-
"github.com/go-gl/mathgl/mgl64"
13-
"github.com/google/uuid"
147
"iter"
158
"maps"
169
"math/rand/v2"
1710
"slices"
1811
"sync"
1912
"sync/atomic"
2013
"time"
14+
15+
"github.com/df-mc/dragonfly/server/block/cube"
16+
"github.com/df-mc/dragonfly/server/event"
17+
"github.com/df-mc/dragonfly/server/internal/sliceutil"
18+
"github.com/df-mc/dragonfly/server/world/chunk"
19+
"github.com/df-mc/goleveldb/leveldb"
20+
"github.com/go-gl/mathgl/mgl64"
21+
"github.com/google/uuid"
2122
)
2223

2324
// World implements a Minecraft world. It manages all aspects of what players
@@ -597,11 +598,12 @@ func (w *World) SetTime(new int) {
597598
}
598599
w.set.Lock()
599600
w.set.Time = int64(new)
601+
timeCycle := w.set.TimeCycle
600602
w.set.Unlock()
601603

602604
viewers, _ := w.allViewers()
603605
for _, viewer := range viewers {
604-
viewer.ViewTime(new)
606+
viewer.ViewTime(new, timeCycle)
605607
}
606608
}
607609

@@ -619,6 +621,16 @@ func (w *World) StartTime() {
619621
w.enableTimeCycle(true)
620622
}
621623

624+
// TimeCycle returns whether time cycle is enabled.
625+
func (w *World) TimeCycle() bool {
626+
if w == nil {
627+
return false
628+
}
629+
w.set.Lock()
630+
defer w.set.Unlock()
631+
return w.set.TimeCycle
632+
}
633+
622634
// enableTimeCycle enables or disables the time cycling of the World.
623635
func (w *World) enableTimeCycle(v bool) {
624636
if w == nil {
@@ -1050,7 +1062,7 @@ func (w *World) addWorldViewer(l *Loader) {
10501062
w.viewers[l] = l.viewer
10511063
w.viewerMu.Unlock()
10521064

1053-
l.viewer.ViewTime(w.Time())
1065+
l.viewer.ViewTime(w.Time(), w.TimeCycle())
10541066
w.set.Lock()
10551067
raining, thundering := w.set.Raining, w.set.Raining && w.set.Thundering
10561068
w.set.Unlock()

0 commit comments

Comments
 (0)