Run the world clock so day and night happen
set_time was declared nowhere and sent never, so the client's sky was frozen wherever it started: the sun did not move, night did not fall, and nothing in the world had a time. 26.1.2 replaced the old (gameTime, dayTime, doDaylightCycle) triple with a registry of named clocks. The packet now carries a fixed-width game time plus a map from world clock to (totalTicks, partialTick, rate); the client advances each clock locally at its rate and drives the minecraft:day timeline -- a keyframe track over a 24000-tick period -- from the overworld clock. The clock registry is already among the 28 we sync, so the id is looked up rather than hardcoded and the server refuses to start if it is missing. The counter rides the entity tick loop because that is the only loop already running at 20 TPS. It belongs on the single authoritative tick the engine still needs; putting a sixth ticker beside the five that exist would make that worse. Broadcast every second, which is what vanilla does -- the client interpolates in between, so the resend only corrects drift. The clock also persists now. The world metadata file was written once and never touched again; it is atomically rewritable, carries gameTime and dayTime, and a file written before those fields existed still opens and resumes at dawn as it did. Saved every 30 seconds alongside the chunk autosave, and once more on shutdown after the final flush.
This commit is contained in:
parent
4db7638377
commit
57214fbd76
9 changed files with 386 additions and 15 deletions
77
internal/world/worldtime_test.go
Normal file
77
internal/world/worldtime_test.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package world
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestWorldTimeRoundTrip checks the clock survives a restart. Without it the
|
||||
// sky snaps back to dawn every time the server comes up, however long the world
|
||||
// has been running.
|
||||
func TestWorldTimeRoundTrip(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
store, err := NewStoreForSeed(dir, 4242)
|
||||
if err != nil {
|
||||
t.Fatalf("open store: %v", err)
|
||||
}
|
||||
if gameTime, dayTime := store.WorldTime(); gameTime != 0 || dayTime != 0 {
|
||||
t.Fatalf("a new world starts at %d/%d, want 0/0", gameTime, dayTime)
|
||||
}
|
||||
if err := store.SaveWorldTime(72_000, 18_000); err != nil {
|
||||
t.Fatalf("save time: %v", err)
|
||||
}
|
||||
store.Close()
|
||||
|
||||
reopened, err := NewStoreForSeed(dir, 4242)
|
||||
if err != nil {
|
||||
t.Fatalf("reopen store: %v", err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
gameTime, dayTime := reopened.WorldTime()
|
||||
if gameTime != 72_000 || dayTime != 18_000 {
|
||||
t.Errorf("reopened at %d/%d, want 72000/18000", gameTime, dayTime)
|
||||
}
|
||||
|
||||
// The seed guard has to survive the rewrite: it is the only thing stopping
|
||||
// a world from being regenerated with different terrain.
|
||||
if _, err := NewStoreForSeed(dir, 9999); err == nil {
|
||||
t.Error("reopening with a different seed was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorldMetadataWithoutClock accepts a metadata file written before the
|
||||
// clock was persisted, rather than rejecting the world.
|
||||
func TestWorldMetadataWithoutClock(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "region"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := json.Marshal(map[string]any{"format": 1, "seed": 7})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, worldMetadataFile), raw, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store, err := NewStoreForSeed(dir, 7)
|
||||
if err != nil {
|
||||
t.Fatalf("open a world written before the clock existed: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
if gameTime, dayTime := store.WorldTime(); gameTime != 0 || dayTime != 0 {
|
||||
t.Errorf("clock read %d/%d from a file that has none, want 0/0", gameTime, dayTime)
|
||||
}
|
||||
if err := store.SaveWorldTime(10, 20); err != nil {
|
||||
t.Fatalf("save time into an older world: %v", err)
|
||||
}
|
||||
reopened, err := NewStoreForSeed(dir, 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reopened.Close()
|
||||
if gameTime, dayTime := reopened.WorldTime(); gameTime != 10 || dayTime != 20 {
|
||||
t.Errorf("after upgrading the file the clock reads %d/%d, want 10/20", gameTime, dayTime)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue