Show live Minecraft weather in dashboard

This commit is contained in:
Tanner committed 2026-11-10 15:18:15 +00:00
1 parent ce3be28d0c
commit 95e6d5e275
7 files changed
+94 -2

No files matched your search

+33 -1
View File
@@ -13,6 +13,31 @@ from mosfet.protocol.managers import WorldState
from mosfet.vector import Point3D from mosfet.vector import Point3D
def classify_weather(rain_level, thunder_level):
"""Map protocol weather levels to a concise UI-friendly state."""
if rain_level is None and thunder_level is None:
return None
if thunder_level is not None and thunder_level > 0:
return 'thunderstorm'
if rain_level is not None and rain_level > 0:
return 'rain'
return 'clear'
def update_weather(global_state, packet):
"""Apply protocol-777 rain/thunder level game events."""
level = max(0.0, min(1.0, float(packet.value)))
if packet.event == 7:
global_state.rain_level = level
elif packet.event == 8:
global_state.thunder_level = level
else:
return False
global_state.weather = classify_weather(
global_state.get('rain_level'), global_state.get('thunder_level'))
return True
def component_text(value): def component_text(value):
"""Best-effort flattening of modern anonymous-NBT chat components.""" """Best-effort flattening of modern anonymous-NBT chat components."""
if value is None: if value is None:
@@ -84,6 +109,9 @@ def main(global_state=None, *, server=None, port=None, username=None,
global_state.info = packet global_state.info = packet
global_state.eid = packet.player_id global_state.eid = packet.player_id
global_state.command_lock = False global_state.command_lock = False
global_state.rain_level = None
global_state.thunder_level = None
global_state.weather = None
from mosfet.world import World from mosfet.world import World
from mosfet.commands import Commands from mosfet.commands import Commands
from mosfet.protocol.modern import ModernChatManager from mosfet.protocol.modern import ModernChatManager
@@ -112,8 +140,12 @@ def main(global_state=None, *, server=None, port=None, username=None,
@connection.listener(PlayGameEventPacket) @connection.listener(PlayGameEventPacket)
def on_game_event(packet): def on_game_event(packet):
if global_state is not None and packet.event == 3: if global_state is None:
return
if packet.event == 3:
global_state.game_mode = int(packet.value) global_state.game_mode = int(packet.value)
else:
update_weather(global_state, packet)
@connection.listener(TeleportPosition) @connection.listener(TeleportPosition)
def on_spawn(packet): def on_spawn(packet):
+35
View File
@@ -0,0 +1,35 @@
import unittest
from munch import Munch
from mosfet.connect import update_weather
class WeatherEventTests(unittest.TestCase):
def test_weather_levels_classify_clear_rain_and_thunderstorm(self):
state = Munch(rain_level=None, thunder_level=None)
self.assertTrue(update_weather(state, Munch(event=7, value=0.6)))
self.assertEqual(state.weather, 'rain')
self.assertEqual(state.rain_level, 0.6)
update_weather(state, Munch(event=8, value=0.8))
self.assertEqual(state.weather, 'thunderstorm')
update_weather(state, Munch(event=8, value=0.0))
self.assertEqual(state.weather, 'rain')
update_weather(state, Munch(event=7, value=0.0))
self.assertEqual(state.weather, 'clear')
def test_weather_levels_are_clamped_and_other_events_ignored(self):
state = Munch(rain_level=None, thunder_level=None)
self.assertFalse(update_weather(state, Munch(event=3, value=1.0)))
self.assertTrue(update_weather(state, Munch(event=7, value=2.0)))
self.assertEqual(state.rain_level, 1.0)
update_weather(state, Munch(event=7, value=-1.0))
self.assertEqual(state.rain_level, 0.0)
self.assertEqual(state.weather, 'clear')
if __name__ == '__main__':
unittest.main()
+15 -1
View File
@@ -45,7 +45,8 @@ class TestWebInventoryAPI(unittest.TestCase):
self.original = { self.original = {
key: self.g.get(key) for key in key: self.g.get(key) for key in
('connected', 'game', 'inv', 'chest_viewers', 'time', 'time_base', ('connected', 'game', 'inv', 'chest_viewers', 'time', 'time_base',
'chunks', 'pos', 'yaw', 'path') 'weather', 'rain_level', 'thunder_level', 'chunks', 'pos', 'yaw',
'path')
} }
self.game = FakeGame() self.game = FakeGame()
self.g.connected = True self.g.connected = True
@@ -117,6 +118,19 @@ class TestWebInventoryAPI(unittest.TestCase):
self.assertEqual(response.get_json()['time'], 12345.5) self.assertEqual(response.get_json()['time'], 12345.5)
self.assertEqual(response.get_json()['time_base'], 12345.5) self.assertEqual(response.get_json()['time_base'], 12345.5)
def test_global_api_exposes_weather_state(self):
self.g.weather = 'thunderstorm'
self.g.rain_level = 1.0
self.g.thunder_level = 0.8
response = self.client.get('/api/global')
self.assertEqual(response.status_code, 200)
payload = response.get_json()
self.assertEqual(payload['weather'], 'thunderstorm')
self.assertEqual(payload['rain_level'], 1.0)
self.assertEqual(payload['thunder_level'], 0.8)
def test_global_api_stringifies_tuple_keys(self): def test_global_api_stringifies_tuple_keys(self):
self.g.chest_viewers = {(12, 64, -7): 1} self.g.chest_viewers = {(12, 64, -7): 1}
+1
View File
@@ -101,6 +101,7 @@
<dl class="runtime-list"> <dl class="runtime-list">
<div><dt>Game mode</dt><dd id="game-mode">—</dd></div> <div><dt>Game mode</dt><dd id="game-mode">—</dd></div>
<div><dt>Time</dt><dd id="world-time">—</dd></div> <div><dt>Time</dt><dd id="world-time">—</dd></div>
<div><dt>Weather</dt><dd id="world-weather">—</dd></div>
<div><dt>Active job</dt><dd id="job-state">IDLE</dd></div> <div><dt>Active job</dt><dd id="job-state">IDLE</dd></div>
<div><dt>Path nodes</dt><dd id="path-length">0</dd></div> <div><dt>Path nodes</dt><dd id="path-length">0</dd></div>
<div><dt>Entities</dt><dd id="entity-count">0 mobs · 0 drops</dd></div> <div><dt>Entities</dt><dd id="entity-count">0 mobs · 0 drops</dd></div>
+1
View File
@@ -101,6 +101,7 @@
<dl class="runtime-list"> <dl class="runtime-list">
<div><dt>Game mode</dt><dd id="game-mode">—</dd></div> <div><dt>Game mode</dt><dd id="game-mode">—</dd></div>
<div><dt>Time</dt><dd id="world-time">—</dd></div> <div><dt>Time</dt><dd id="world-time">—</dd></div>
<div><dt>Weather</dt><dd id="world-weather">—</dd></div>
<div><dt>Active job</dt><dd id="job-state">IDLE</dd></div> <div><dt>Active job</dt><dd id="job-state">IDLE</dd></div>
<div><dt>Path nodes</dt><dd id="path-length">0</dd></div> <div><dt>Path nodes</dt><dd id="path-length">0</dd></div>
<div><dt>Entities</dt><dd id="entity-count">0 mobs · 0 drops</dd></div> <div><dt>Entities</dt><dd id="entity-count">0 mobs · 0 drops</dd></div>
+8
View File
@@ -80,6 +80,13 @@
return `${String(hour12).padStart(2, '0')}:${String(minute).padStart(2, '0')} ${period}`; return `${String(hour12).padStart(2, '0')}:${String(minute).padStart(2, '0')} ${period}`;
} }
function formatWeather(weather) {
const labels = {
clear: 'Clear', rain: 'Rain', thunderstorm: 'Thunderstorm'
};
return labels[weather] || '—';
}
function minimapBlockColor(name) { function minimapBlockColor(name) {
const block = String(name || '').replace(/^minecraft:/, '').toLowerCase(); const block = String(name || '').replace(/^minecraft:/, '').toLowerCase();
if (/water|bubble_column/.test(block)) return '#3979a5'; if (/water|bubble_column/.test(block)) return '#3979a5';
@@ -644,6 +651,7 @@
$('world-time').textContent = Number.isFinite(worldTime) $('world-time').textContent = Number.isFinite(worldTime)
? `${Math.floor(worldTime).toLocaleString()} t · ${formatWorldClock(worldTime)}` ? `${Math.floor(worldTime).toLocaleString()} t · ${formatWorldClock(worldTime)}`
: '—'; : '—';
$('world-weather').textContent = online ? formatWeather(state.weather) : '—';
if (lastMinimapData?.ready) { if (lastMinimapData?.ready) {
lastMinimapData.yaw = Number(state.yaw || 0); lastMinimapData.yaw = Number(state.yaw || 0);
drawMinimap(lastMinimapData); drawMinimap(lastMinimapData);
+1
View File
@@ -101,6 +101,7 @@
<dl class="runtime-list"> <dl class="runtime-list">
<div><dt>Game mode</dt><dd id="game-mode">—</dd></div> <div><dt>Game mode</dt><dd id="game-mode">—</dd></div>
<div><dt>Time</dt><dd id="world-time">—</dd></div> <div><dt>Time</dt><dd id="world-time">—</dd></div>
<div><dt>Weather</dt><dd id="world-weather">—</dd></div>
<div><dt>Active job</dt><dd id="job-state">IDLE</dd></div> <div><dt>Active job</dt><dd id="job-state">IDLE</dd></div>
<div><dt>Path nodes</dt><dd id="path-length">0</dd></div> <div><dt>Path nodes</dt><dd id="path-length">0</dd></div>
<div><dt>Entities</dt><dd id="entity-count">0 mobs · 0 drops</dd></div> <div><dt>Entities</dt><dd id="entity-count">0 mobs · 0 drops</dd></div>