Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the live daylight cycle #571

Closed
wants to merge 2 commits into from
Closed
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
@@ -0,0 +1,78 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.common.live;

import com.google.gson.stream.JsonWriter;
import de.bluecolored.bluemap.common.serverinterface.Server;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
import java.util.function.Supplier;

public class LiveBrightnessDataSupplier implements Supplier<String> {

private final Server server;

public LiveBrightnessDataSupplier(Server server) {
this.server = server;
}

@Override
public String get() {
String dimensionName;
Key dimension;

try (StringWriter jsonString = new StringWriter();
JsonWriter json = new JsonWriter(jsonString)) {


json.beginObject();

json.name("brightness").beginArray();
for (Map.Entry<Key, Integer> entry : this.server.getSkyBrightness().entrySet()) {
dimension = entry.getKey();
dimensionName = dimension.getNamespace().equals("minecraft") ?
dimension.getValue() : dimension.getFormatted();
json.beginObject();
json.name("dimension").value(dimensionName);
json.name("val").value(entry.getValue());
json.endObject();
}
json.endArray();

json.endObject();

json.flush();
return jsonString.toString();
} catch (IOException ex) {
Logger.global.logError("Failed to write live/brightness json!", ex);
return "BlueMap - Exception handling this request";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import de.bluecolored.bluemap.core.util.Tristate;
import de.bluecolored.bluemap.core.world.World;
import de.bluecolored.bluemap.core.world.mca.MCAWorld;
import de.bluecolored.bluemap.core.util.Key;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

public interface Server {
Expand Down Expand Up @@ -99,6 +101,9 @@ default Optional<ServerWorld> getServerWorld(Object world) {
@DebugDump
Collection<Player> getOnlinePlayers();

@DebugDump
Map<Key, Integer> getSkyBrightness();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be something like a int getSkyBrightness() or maybe even int getWorldTime() in the ServerWorld interface instead


/**
* Registers a ServerEventListener, every method of this interface should be called on the specified events
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import de.bluecolored.bluemap.common.config.PluginConfig;
import de.bluecolored.bluemap.common.live.LiveMarkersDataSupplier;
import de.bluecolored.bluemap.common.live.LivePlayersDataSupplier;
import de.bluecolored.bluemap.common.live.LiveBrightnessDataSupplier;
import de.bluecolored.bluemap.common.serverinterface.Server;
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.map.BmMap;
Expand All @@ -43,16 +44,18 @@ public class MapRequestHandler extends RoutingRequestHandler {
public MapRequestHandler(BmMap map, Server serverInterface, PluginConfig pluginConfig, Predicate<UUID> playerFilter) {
this(map.getStorage(),
createPlayersDataSupplier(map, serverInterface, pluginConfig, playerFilter),
new LiveMarkersDataSupplier(map.getMarkerSets()));
new LiveMarkersDataSupplier(map.getMarkerSets()),
new LiveBrightnessDataSupplier(serverInterface));
}

public MapRequestHandler(MapStorage mapStorage) {
this(mapStorage, null, null);
this(mapStorage, null, null, null);
}

public MapRequestHandler(MapStorage mapStorage,
@Nullable Supplier<String> livePlayersDataSupplier,
@Nullable Supplier<String> liveMarkerDataSupplier) {
@Nullable Supplier<String> liveMarkerDataSupplier,
@Nullable Supplier<String> liveBrightnessDataSupplier) {

register(".*", new MapStorageRequestHandler(mapStorage));

Expand All @@ -67,6 +70,12 @@ public MapRequestHandler(MapStorage mapStorage,
new CachedRateLimitDataSupplier(liveMarkerDataSupplier,10000)
));
}

if (liveBrightnessDataSupplier != null) {
register("live/brightness\\.json", "", new JsonDataRequestHandler(
new CachedRateLimitDataSupplier(liveBrightnessDataSupplier,1000)
));
}
}

private static @Nullable LivePlayersDataSupplier createPlayersDataSupplier(BmMap map, Server serverInterface, PluginConfig pluginConfig, Predicate<UUID> playerFilter) {
Expand Down
1 change: 1 addition & 0 deletions BlueMapCommon/webapp/public/lang/en.conf
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
}
sunlight: "Sunlight"
ambientLight: "Ambient-Light"
liveDaylightCycle: "Live Daylight Cycle"
}
resolution: {
title: "Resolution"
Expand Down
1 change: 1 addition & 0 deletions BlueMapCommon/webapp/src/components/Menu/SettingsMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@update="mapViewer.uniforms.sunlightStrength.value = $event; $bluemap.mapViewer.redraw()">{{$t('lighting.sunlight')}}</Slider>
<Slider :value="mapViewer.uniforms.ambientLight.value" :min="0" :max="1" :step="0.01"
@update="mapViewer.uniforms.ambientLight.value = $event; $bluemap.mapViewer.redraw()">{{$t('lighting.ambientLight')}}</Slider>
<SwitchButton :on="appState.controls.liveDaylightCycle" @action="appState.controls.liveDaylightCycle = !appState.controls.liveDaylightCycle; $bluemap.saveUserSettings()">{{$t('lighting.liveDaylightCycle')}}</SwitchButton>
</Group>

<Group :title="$t('resolution.title')">
Expand Down
54 changes: 53 additions & 1 deletion BlueMapCommon/webapp/src/js/BlueMapApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export class BlueMapApp {
showZoomButtons: true,
invertMouse: false,
enableFreeFlight: false,
pauseTileLoading: false
pauseTileLoading: false,
liveDaylightCycle: false,
},
menu: this.mainMenu,
maps: [],
Expand All @@ -120,6 +121,7 @@ export class BlueMapApp {
this.mapViewer.markers.add(this.popupMarkerSet);

this.updateLoop = null;
this.updateDaylightCycle = null;

this.hashUpdateTimeout = null;
this.viewAnimation = null;
Expand Down Expand Up @@ -182,6 +184,8 @@ export class BlueMapApp {
// start app update loop
if(this.updateLoop) clearTimeout(this.updateLoop);
this.updateLoop = setTimeout(this.update, 1000);
if(this.updateDaylightCycle) clearTimeout(this.updateDaylightCycle);
this.updateDaylightCycle = setTimeout(this.updateDaylight, 1000);

// save user settings
this.saveUserSettings();
Expand All @@ -200,6 +204,24 @@ export class BlueMapApp {
this.updateLoop = setTimeout(this.update, 1000);
}

updateDaylight = () => {
if (!this.appState.controls.liveDaylightCycle) {
this.updateDaylightCycle = setTimeout(this.updateDaylight, 4000);
return;
}
if (!this.appState || !this.mapViewer.map) {
this.updateDaylightCycle = setTimeout(this.updateDaylight, 2000);
return;
}
this.updateBrightness().then(t => {
let timeout = Math.max(t, 2000);
this.updateDaylightCycle = setTimeout(this.updateDaylight, timeout);
}).catch(e => {
console.error(e);
this.updateDaylightCycle = setTimeout(this.updateDaylight, 3000);
});
}

async followPlayerMarkerWorld() {
/** @type {PlayerLike} */
let player = this.mapViewer.controlsManager.controls?.data.followingPlayer;
Expand Down Expand Up @@ -383,6 +405,34 @@ export class BlueMapApp {
});
}

async updateBrightness() {
/*
Fetch the brightness from the server and update the daylight cycle.
*/
return new Promise((resolve, reject) => {
let loader = new FileLoader();
loader.setResponseType("json");
loader.load(this.mapViewer.map.data.dataUrl + "live/brightness.json?" + generateCacheHash(),
fileData => {
if (!fileData || !fileData.brightness) {
reject("Failed to parse brightness.json");
}
let world_name = this.mapViewer.map.data.name.match(/\(([^)]+)\)/)[1].toLowerCase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map-name can be changed and is not at all guaranteed to include the world-id/dimension, so we can't use this to identify the dimension-key of a map

for (let idx=0; idx<fileData.brightness.length; idx++) {
let key = fileData.brightness[idx]['dimension']
if (key.toLowerCase() !== world_name) continue;
let brightness = 1 - (fileData.brightness[idx]['val'] / 11) * 0.6;
this.mapViewer.data.uniforms.sunlightStrength.value = brightness;
break;
}
resolve(2000);
}, () => {}, () => {
reject("Failed to load brightness.json");
}
)
});
}

initMarkerFileManager() {
if (this.markerFileManager)
this.markerFileManager.dispose();
Expand Down Expand Up @@ -612,6 +662,7 @@ export class BlueMapApp {
this.appState.controls.mouseSensitivity = this.loadUserSetting("mouseSensitivity", this.appState.controls.mouseSensitivity);
this.appState.controls.invertMouse = this.loadUserSetting("invertMouse", this.appState.controls.invertMouse);
this.appState.controls.pauseTileLoading = this.loadUserSetting("pauseTileLoading", this.appState.controls.pauseTileLoading);
this.appState.controls.liveDaylightCycle = this.loadUserSetting("liveDaylightCycle", this.appState.controls.liveDaylightCycle);
this.appState.controls.showZoomButtons = this.loadUserSetting("showZoomButtons", this.appState.controls.showZoomButtons);
this.updateControlsSettings();
this.setTheme(this.loadUserSetting("theme", this.appState.theme));
Expand All @@ -635,6 +686,7 @@ export class BlueMapApp {
this.saveUserSetting("mouseSensitivity", this.appState.controls.mouseSensitivity);
this.saveUserSetting("invertMouse", this.appState.controls.invertMouse);
this.saveUserSetting("pauseTileLoading", this.appState.controls.pauseTileLoading);
this.saveUserSetting("liveDaylightCycle", this.appState.controls.liveDaylightCycle);
this.saveUserSetting("showZoomButtons", this.appState.controls.showZoomButtons);
this.saveUserSetting("theme", this.appState.theme);
this.saveUserSetting("screenshotClipboard", this.appState.screenshot.clipboard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -238,4 +239,13 @@ private void updateSomePlayers() {
}
}

@Override
public Map<Key, Integer> getSkyBrightness() {
Map<Key, Integer> skyBrightness = new HashMap<>();
for (net.minecraft.server.world.ServerWorld world : serverInstance.getWorlds()) {
skyBrightness.put(worlds.get(world).getDimension(), world.getAmbientDarkness());
}
return skyBrightness;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -238,4 +239,13 @@ private void updateSomePlayers() {
}
}

@Override
public Map<Key, Integer> getSkyBrightness() {
Map<Key, Integer> skyBrightness = new HashMap<>();
for (net.minecraft.server.world.ServerWorld world : serverInstance.getWorlds()) {
skyBrightness.put(worlds.get(world).getDimension(), world.getAmbientDarkness());
}
return skyBrightness;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -238,4 +239,13 @@ private void updateSomePlayers() {
}
}

@Override
public Map<Key, Integer> getSkyBrightness() {
Map<Key, Integer> skyBrightness = new HashMap<>();
for (net.minecraft.server.world.ServerWorld world : serverInstance.getWorlds()) {
skyBrightness.put(worlds.get(world).getDimension(), world.getAmbientDarkness());
}
return skyBrightness;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -238,4 +239,13 @@ private void updateSomePlayers() {
}
}

@Override
public Map<Key, Integer> getSkyBrightness() {
Map<Key, Integer> skyBrightness = new HashMap<>();
for (net.minecraft.server.world.ServerWorld world : serverInstance.getWorlds()) {
skyBrightness.put(worlds.get(world).getDimension(), world.getAmbientDarkness());
}
return skyBrightness;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down Expand Up @@ -238,4 +239,13 @@ private void updateSomePlayers() {
}
}

@Override
public Map<Key, Integer> getSkyBrightness() {
Map<Key, Integer> skyBrightness = new HashMap<>();
for (net.minecraft.server.world.ServerWorld world : serverInstance.getWorlds()) {
skyBrightness.put(worlds.get(world).getDimension(), world.getAmbientDarkness());
}
return skyBrightness;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.bluecolored.bluemap.common.serverinterface.ServerWorld;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.util.Key;
import net.minecraft.SharedConstants;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -254,4 +255,14 @@ private void updateSomePlayers() {
}
}

@Override
public Map<Key, Integer> getSkyBrightness() {
Map<Key, Integer> skyBrightness = new HashMap<>();
Iterable<ServerLevel> levels = serverInstance.getAllLevels();
for (ServerLevel level : levels) {
skyBrightness.put(worlds.get(level).getDimension(), level.getSkyDarken());
}
return skyBrightness;
}

}
Loading
Loading