Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private void loadConfig() {
public void onPing(ProxyPingEvent event) {
Component comp = manager.provide();
String legacy = LegacyComponentSerializer.legacySection().serialize(comp);
// TextComponent.fromLegacyText returns an array; wrap in a single component
event.getResponse().setDescriptionComponent(new TextComponent(TextComponent.fromLegacyText(legacy)));
event.getResponse().setDescriptionComponent(TextComponent.fromLegacyText(legacy));
}

// ServerInfoProvider
Expand Down
2 changes: 1 addition & 1 deletion bungee/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ motd:
- when: "event"
text: "<gold>Special event today — /warp event</gold>"
- when: "default"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_riga%</blue>"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_motd%</blue>"

weather:
enable: true
Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ motd:
- when: "event"
text: "<gold>Special event today — /warp event</gold>"
- when: "default"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_riga%</blue>"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_motd%</blue>"

weather:
enable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private String defaultPlaceholders(String text, MotdContext ctx) {
out = out.replace("%maxplayers%", Integer.toString(ctx.max));
out = out.replace("%tps%", String.format(Locale.US, "%.2f", ctx.tps));
if (ctx.weather != null) {
out = out.replace("%weather_" + config.weather.city.toLowerCase(Locale.ROOT) + "%", ctx.weather);
out = out.replace("%weather_motd%", ctx.weather);
out = out.replace("%weather_city%", ctx.weather);
}
out = out.replace("%discord_online%", Integer.toString(ctx.discordOnline));
Expand Down
21 changes: 11 additions & 10 deletions core/src/main/java/com/livemotdmanager/core/WeatherService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Fetches real world weather information using the OpenWeather API.
Expand All @@ -22,35 +19,38 @@ public class WeatherService {
private final boolean enabled;
private final String city;
private final String apiKey;
private final int updateIntervalMinutes;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private volatile String cached = "";
private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build();
private volatile long lastFetch = 0L;
private final long cacheDurationMs;

public WeatherService(MotdConfig.WeatherSettings settings) {
this.enabled = settings.enable;
this.city = settings.city;
this.apiKey = settings.apiKey;
this.updateIntervalMinutes = settings.updateIntervalMinutes;
this.cacheDurationMs = Math.max(1, settings.updateIntervalMinutes) * 60L * 1000L;
}

public void start() {
if (!enabled) return;
// Initial fetch for startup verification
update();
System.out.println("[LiveMotdManager] Weather API test: " + (cached.isEmpty() ? "unavailable" : cached));
scheduler.scheduleAtFixedRate(this::update, updateIntervalMinutes, updateIntervalMinutes, TimeUnit.MINUTES);
}

public void stop() {
scheduler.shutdownNow();
// no background tasks to stop
}

public String getCachedWeather() {
if (!enabled) return "";
long now = System.currentTimeMillis();
if (now - lastFetch > cacheDurationMs) {
update();
}
return cached;
}

private void update() {
private synchronized void update() {
if (!enabled || city == null || city.isEmpty() || apiKey == null || apiKey.isEmpty()) return;
try {
String encodedCity = URLEncoder.encode(city, StandardCharsets.UTF_8);
Expand All @@ -67,6 +67,7 @@ private void update() {
desc = desc.substring(0,1).toUpperCase(Locale.ROOT) + desc.substring(1);
}
cached = String.format(Locale.US, "%s %.1f°C", desc, temp);
lastFetch = System.currentTimeMillis();
} catch (Exception e) {
// ignore but keep cached
}
Expand Down
2 changes: 1 addition & 1 deletion spigot/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ motd:
- when: "event"
text: "<gold>Special event today — /warp event</gold>"
- when: "default"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_riga%</blue>"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_motd%</blue>"

weather:
enable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void loadConfig() {
@Subscribe
public void onPing(ProxyPingEvent event) {
Component comp = manager.provide();
event.getPing().asBuilder().description(comp).build();
event.setPing(event.getPing().asBuilder().description(comp).build());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion velocity/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ motd:
- when: "event"
text: "<gold>Special event today — /warp event</gold>"
- when: "default"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_riga%</blue>"
text: "<blue>Welcome to ChaosCraftMHX — %online%/%maxplayers% playing! Weather in Riga: %weather_motd%</blue>"

weather:
enable: true
Expand Down
5 changes: 3 additions & 2 deletions wiki/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ and the MiniMessage formatted `text` to display.

### Weather

Set `weather.enable` to `true` and specify `city`. Use `%weather_city%` or `%weather_<city>%`
placeholders in your templates.
Set `weather.enable` to `true` and specify `city`. Use `%weather_motd%` or `%weather_city%`
placeholders in your templates. The `weather.update-interval-minutes` option controls how often
new data is fetched (default 10 minutes).

### Discord

Expand Down
Loading