Advance world clock between updates

This commit is contained in:
Tanner
2026-11-07 22:14:46 +00:00
parent a16b297db7
commit 9cbadbd551
4 changed files with 59 additions and 4 deletions
+24 -3
View File
@@ -179,9 +179,24 @@ class LegacyGame:
self.g.holding = (item.item_id if item and
getattr(item, 'present', False) else 0)
def _set_world_clock(self, total_ticks, partial_tick=0.0, rate=1.0):
self.g.time_base = float(total_ticks) + float(partial_tick)
self.g.clock_rate = float(rate)
self.g.time_update_received_at = time.monotonic()
self._advance_world_time()
def _advance_world_time(self):
received_at = self.g.get('time_update_received_at')
time_base = self.g.get('time_base')
if received_at is None or time_base is None:
return
elapsed_ticks = max(0.0, time.monotonic() - received_at) / utils.TICK
self.g.time = (time_base + elapsed_ticks *
self.g.get('clock_rate', 1.0)) % 24000
def handle_time_update(self, packet):
if hasattr(packet, 'time_of_day'):
self.g.time = packet.time_of_day % 24000
self._set_world_clock(packet.time_of_day)
return
updates = getattr(packet, 'clock_updates', ())
@@ -201,11 +216,12 @@ class LegacyGame:
'minecraft:') in (dimension, 'overworld')
]
update = preferred[0] if preferred else updates[0]
self.g.time = update['total_ticks'] % 24000
self.g.clock_id = update['clock_id']
self.g.clock_name = clock_names.get(update['clock_id'])
self.g.clock_rate = update['rate']
self.g.world_age = packet.age
self._set_world_clock(update['total_ticks'],
update.get('partial_tick', 0.0),
update.get('rate', 1.0))
def handle_set_slot(self, packet):
g = self.g
@@ -700,6 +716,10 @@ class Game(LegacyGame):
self.g.connection.register_packet_listener(
self.handle_set_held_slot, SetHeldSlotPacket)
self.g.setdefault('time_base', None)
self.g.setdefault('time_update_received_at', None)
self.g.setdefault('clock_rate', 1.0)
if not self.g.get('health_update_received', False):
self.g.health = None
self.g.food = None
@@ -1222,6 +1242,7 @@ class Game(LegacyGame):
self.g.drain_dropped), flush=True)
def tick(self):
self._advance_world_time()
self._tick_single_drop()
if self.g.get('dumping') is not None:
self._tick_dump()