Skip to content

Commit

Permalink
Add abillity to freeze maps (they wont update) and persist stopped re…
Browse files Browse the repository at this point in the history
…nder-threads
  • Loading branch information
TBlueF committed May 16, 2021
1 parent 113d7f3 commit e90f329
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import de.bluecolored.bluemap.core.webserver.HttpRequestHandler;
import de.bluecolored.bluemap.core.webserver.WebServer;
import de.bluecolored.bluemap.core.world.World;
import org.spongepowered.configurate.gson.GsonConfigurationLoader;
import org.spongepowered.configurate.serialize.SerializationException;

import java.io.File;
import java.io.IOException;
Expand All @@ -72,6 +74,8 @@ public class Plugin {
private WebServerConfig webServerConfig;
private PluginConfig pluginConfig;

private PluginStatus pluginStatus;

private Map<UUID, World> worlds;
private Map<String, BmMap> maps;

Expand All @@ -82,7 +86,7 @@ public class Plugin {
private TimerTask saveTask;
private TimerTask metricsTask;

private Collection<RegionFileWatchService> regionFileWatchServices;
private Map<String, RegionFileWatchService> regionFileWatchServices;

private PlayerSkinUpdater skinUpdater;

Expand Down Expand Up @@ -118,6 +122,17 @@ public void load() throws IOException, ParseResourceException {
true
));

//load plugin status
try {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder()
.file(new File(getCoreConfig().getDataFolder(), "pluginStatus.json"))
.build();
pluginStatus = loader.load().get(PluginStatus.class);
} catch (SerializationException ex) {
Logger.global.logWarning("Failed to load pluginStatus.json (invalid format), creating a new one...");
pluginStatus = new PluginStatus();
}

//create and start webserver
if (webServerConfig.isWebserverEnabled()) {
FileUtils.mkDirs(webServerConfig.getWebRoot());
Expand Down Expand Up @@ -168,11 +183,17 @@ public void load() throws IOException, ParseResourceException {

//update all maps
for (BmMap map : maps.values()) {
renderManager.scheduleRenderTask(new MapUpdateTask(map));
if (pluginStatus.getMapStatus(map).isUpdateEnabled()) {
renderManager.scheduleRenderTask(new MapUpdateTask(map));
}
}

//start render-manager
renderManager.start(coreConfig.getRenderThreadCount());
if (pluginStatus.isRenderThreadsEnabled()) {
renderManager.start(coreConfig.getRenderThreadCount());
} else {
Logger.global.logInfo("Render-Threads are STOPPED! Use the command 'bluemap start' to start them.");
}

//update webapp and settings
blueMap.createOrUpdateWebApp(false);
Expand All @@ -194,12 +215,7 @@ public void load() throws IOException, ParseResourceException {
saveTask = new TimerTask() {
@Override
public void run() {
synchronized (Plugin.this) {
if (maps == null) return;
for (BmMap map : maps.values()) {
map.save();
}
}
save();
}
};
daemonTimer.schedule(saveTask, TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(2));
Expand All @@ -215,14 +231,10 @@ public void run() {
daemonTimer.scheduleAtFixedRate(metricsTask, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(30));

//watch map-changes
this.regionFileWatchServices = new ArrayList<>();
this.regionFileWatchServices = new HashMap<>();
for (BmMap map : maps.values()) {
try {
RegionFileWatchService watcher = new RegionFileWatchService(renderManager, map, false);
watcher.start();
regionFileWatchServices.add(watcher);
} catch (IOException ex) {
Logger.global.logError("Failed to create file-watcher for map: " + map.getId() + " (This map might not automatically update)", ex);
if (pluginStatus.getMapStatus(map).isUpdateEnabled()) {
startWatchingMap(map);
}
}

Expand All @@ -245,6 +257,8 @@ public void unload() {
try {
loadingLock.interruptAndLock();
synchronized (this) {
//save
save();

//disable api
if (api != null) api.unregister();
Expand All @@ -264,7 +278,7 @@ public void unload() {

//stop file-watchers
if (regionFileWatchServices != null) {
for (RegionFileWatchService watcher : regionFileWatchServices) {
for (RegionFileWatchService watcher : regionFileWatchServices.values()) {
watcher.close();
}
regionFileWatchServices.clear();
Expand All @@ -278,13 +292,6 @@ public void unload() {
if (webServer != null) webServer.close();
webServer = null;

//save renders
if (maps != null) {
for (BmMap map : maps.values()) {
map.save();
}
}

//clear resources and configs
blueMap = null;
worlds = null;
Expand All @@ -295,6 +302,8 @@ public void unload() {
webServerConfig = null;
pluginConfig = null;

pluginStatus = null;

//done
loaded = false;
}
Expand All @@ -308,6 +317,44 @@ public void reload() throws IOException, ParseResourceException {
load();
}

public synchronized void save() {
if (pluginStatus != null) {
try {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder()
.file(new File(getCoreConfig().getDataFolder(), "pluginStatus.json"))
.build();
loader.save(loader.createNode().set(PluginStatus.class, pluginStatus));
} catch (IOException ex) {
Logger.global.logError("Failed to save pluginStatus.json!", ex);
}
}

if (maps != null) {
for (BmMap map : maps.values()) {
map.save();
}
}
}

public synchronized void startWatchingMap(BmMap map) {
stopWatchingMap(map);

try {
RegionFileWatchService watcher = new RegionFileWatchService(renderManager, map, false);
watcher.start();
regionFileWatchServices.put(map.getId(), watcher);
} catch (IOException ex) {
Logger.global.logError("Failed to create file-watcher for map: " + map.getId() + " (This means the map might not automatically update)", ex);
}
}

public synchronized void stopWatchingMap(BmMap map) {
RegionFileWatchService watcher = regionFileWatchServices.remove(map.getId());
if (watcher != null) {
watcher.close();
}
}

public boolean flushWorldUpdates(UUID worldUUID) throws IOException {
return serverInterface.persistWorldChanges(worldUUID);
}
Expand All @@ -331,6 +378,10 @@ public WebServerConfig getWebServerConfig() {
public PluginConfig getPluginConfig() {
return pluginConfig;
}

public PluginStatus getPluginStatus() {
return pluginStatus;
}

public World getWorld(UUID uuid){
return worlds.get(uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,44 @@
*/
package de.bluecolored.bluemap.common.plugin;

import de.bluecolored.bluemap.core.map.BmMap;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("FieldMayBeFinal")
@ConfigSerializable
public class PluginStatus {

private boolean renderThreadsEnabled = true;
private Map<String, MapStatus> maps = new HashMap<>();

public boolean isRenderThreadsEnabled() {
return renderThreadsEnabled;
}

public void setRenderThreadsEnabled(boolean renderThreadsEnabled) {
this.renderThreadsEnabled = renderThreadsEnabled;
}

public MapStatus getMapStatus(BmMap map) {
return maps.computeIfAbsent(map.getId(), k -> new MapStatus());
}

@ConfigSerializable
public static class MapStatus {

private boolean updateEnabled = true;

public boolean isUpdateEnabled() {
return updateEnabled;
}

public void setUpdateEnabled(boolean update) {
this.updateEnabled = update;
}

}

}
Loading

0 comments on commit e90f329

Please sign in to comment.