diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ErrorMessagePage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ErrorMessagePage.java index dc0459979..893ad5625 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ErrorMessagePage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ErrorMessagePage.java @@ -20,6 +20,8 @@ import com.djrapitops.plan.delivery.formatting.PlaceholderReplacer; import com.djrapitops.plan.delivery.rendering.html.Contributors; import com.djrapitops.plan.delivery.rendering.html.icon.Icon; import com.djrapitops.plan.settings.locale.Locale; +import com.djrapitops.plan.settings.theme.Theme; +import com.djrapitops.plan.utilities.java.UnaryChain; import com.djrapitops.plan.version.VersionChecker; /** @@ -35,30 +37,32 @@ public class ErrorMessagePage implements Page { private final String errorMsg; private final Locale locale; + private final Theme theme; private final VersionChecker versionChecker; public ErrorMessagePage( String template, Icon icon, String errorTitle, String errorMsg, - Locale locale, VersionChecker versionChecker + Locale locale, Theme theme, VersionChecker versionChecker ) { this.template = template; this.icon = icon; this.errorTitle = errorTitle; this.errorMsg = errorMsg; this.locale = locale; + this.theme = theme; this.versionChecker = versionChecker; } public ErrorMessagePage( String template, String errorTitle, String errorMsg, VersionChecker versionChecker, - Locale locale) { - this(template, Icon.called("exclamation-circle").build(), errorTitle, errorMsg, locale, versionChecker); + Locale locale, + Theme theme) { + this(template, Icon.called("exclamation-circle").build(), errorTitle, errorMsg, locale, theme, versionChecker); } @Override public String toHtml() { - PlaceholderReplacer placeholders = new PlaceholderReplacer(); placeholders.put("title", icon.toHtml() + " " + errorTitle); placeholders.put("titleText", errorTitle); @@ -66,6 +70,10 @@ public class ErrorMessagePage implements Page { placeholders.put("version", versionChecker.getUpdateButton().orElse(versionChecker.getCurrentVersionButton())); placeholders.put("updateModal", versionChecker.getUpdateModal()); placeholders.put("contributors", Contributors.generateContributorHtml()); - return locale.replaceLanguageInHtml(placeholders.apply(template)); + return UnaryChain.of(template) + .chain(theme::replaceThemeColors) + .chain(placeholders::apply) + .chain(locale::replaceLanguageInHtml) + .apply(); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/NetworkPage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/NetworkPage.java index 3e0d6e7c5..17e5b8b82 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/NetworkPage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/NetworkPage.java @@ -31,6 +31,7 @@ import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.theme.Theme; import com.djrapitops.plan.settings.theme.ThemeVal; import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.utilities.java.UnaryChain; import com.djrapitops.plan.version.VersionChecker; import org.apache.commons.lang3.StringUtils; @@ -102,15 +103,18 @@ public class NetworkPage implements Page { return new ServerPluginTabs(extensionData, formatters); }); - String html = locale.replaceLanguageInHtml(placeholders.apply(templateHtml)); - String nav = JSONCache.getOrCacheString(DataID.EXTENSION_NAV, serverUUID, () -> pluginTabs.get().getNav()); String tabs = JSONCache.getOrCacheString(DataID.EXTENSION_TABS, serverUUID, () -> pluginTabs.get().getTabs()); - placeholders = new PlaceholderReplacer(); - placeholders.put("navPluginsTabs", nav); - placeholders.put("tabsPlugins", StringUtils.remove(tabs, "${backButton}")); + PlaceholderReplacer pluginPlaceholders = new PlaceholderReplacer(); + pluginPlaceholders.put("navPluginsTabs", nav); + pluginPlaceholders.put("tabsPlugins", StringUtils.remove(tabs, "${backButton}")); - return placeholders.apply(html); + return UnaryChain.of(templateHtml) + .chain(theme::replaceThemeColors) + .chain(placeholders::apply) + .chain(locale::replaceLanguageInHtml) + .chain(pluginPlaceholders::apply) + .apply(); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java index f56a592e4..4b4b70b5a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PageFactory.java @@ -45,8 +45,6 @@ import java.util.*; /** * Factory for creating different {@link Page} objects. - *

- * TODO Set theme and locale stuff in here * * @author Rsl1122 */ @@ -101,7 +99,8 @@ public class PageFactory { } public PlayersPage playersPage() throws IOException { - return new PlayersPage(getResource("web/players.html"), versionChecker.get(), config.get(), locale.get(), serverInfo.get()); + return new PlayersPage(getResource("web/players.html"), versionChecker.get(), + config.get(), locale.get(), theme.get(), serverInfo.get()); } /** @@ -195,13 +194,13 @@ public class PageFactory { public Page errorPage(String title, String error) throws IOException { return new ErrorMessagePage( getResource("web/error.html"), title, error, - versionChecker.get(), locale.get()); + versionChecker.get(), locale.get(), theme.get()); } public Page errorPage(Icon icon, String title, String error) throws IOException { return new ErrorMessagePage( getResource("web/error.html"), icon, title, error, - locale.get(), versionChecker.get()); + locale.get(), theme.get(), versionChecker.get()); } public String getResource(String name) throws IOException { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayerPage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayerPage.java index 57614dac8..541cd000b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayerPage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayerPage.java @@ -113,6 +113,6 @@ public class PlayerPage implements Page { placeholders.put("navPluginsTabs", pluginTabs.getNav()); placeholders.put("pluginsTabs", pluginTabs.getTab()); - return placeholders.apply(html); + return placeholders.apply(theme.replaceThemeColors(html)); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayersPage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayersPage.java index 02d038b4b..1c468fc16 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayersPage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/PlayersPage.java @@ -23,6 +23,7 @@ import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.settings.config.paths.PluginSettings; import com.djrapitops.plan.settings.config.paths.ProxySettings; import com.djrapitops.plan.settings.locale.Locale; +import com.djrapitops.plan.settings.theme.Theme; import com.djrapitops.plan.version.VersionChecker; /** @@ -36,6 +37,7 @@ public class PlayersPage implements Page { private final VersionChecker versionChecker; private final PlanConfig config; private final Locale locale; + private final Theme theme; private final ServerInfo serverInfo; PlayersPage( @@ -43,12 +45,14 @@ public class PlayersPage implements Page { VersionChecker versionChecker, PlanConfig config, Locale locale, + Theme theme, ServerInfo serverInfo ) { this.templateHtml = templateHtml; this.versionChecker = versionChecker; this.config = config; this.locale = locale; + this.theme = theme; this.serverInfo = serverInfo; } @@ -65,6 +69,6 @@ public class PlayersPage implements Page { placeholders.put("networkName", config.get(PluginSettings.SERVER_NAME)); } - return locale.replaceLanguageInHtml(placeholders.apply(templateHtml)); + return locale.replaceLanguageInHtml(placeholders.apply(theme.replaceThemeColors(templateHtml))); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPage.java index 87915ab70..afc0d3a07 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/pages/ServerPage.java @@ -32,6 +32,7 @@ import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.theme.Theme; import com.djrapitops.plan.settings.theme.ThemeVal; import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.utilities.java.UnaryChain; import com.djrapitops.plan.version.VersionChecker; import java.util.List; @@ -97,15 +98,18 @@ public class ServerPage implements Page { return new ServerPluginTabs(extensionData, formatters); }); - String html = locale.replaceLanguageInHtml(placeholders.apply(templateHtml)); - String nav = JSONCache.getOrCacheString(DataID.EXTENSION_NAV, serverUUID, () -> pluginTabs.get().getNav()); String tabs = JSONCache.getOrCacheString(DataID.EXTENSION_TABS, serverUUID, () -> pluginTabs.get().getTabs()); - placeholders = new PlaceholderReplacer(); - placeholders.put("navPluginsTabs", nav); - placeholders.put("tabsPlugins", tabs); + PlaceholderReplacer pluginPlaceholders = new PlaceholderReplacer(); + pluginPlaceholders.put("navPluginsTabs", nav); + pluginPlaceholders.put("tabsPlugins", tabs); - return placeholders.apply(html); + return UnaryChain.of(templateHtml) + .chain(theme::replaceThemeColors) + .chain(placeholders::apply) + .chain(locale::replaceLanguageInHtml) + .chain(pluginPlaceholders::apply) + .apply(); } } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java index f333a9129..15f680137 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/ResponseFactory.java @@ -34,6 +34,7 @@ import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries; import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.utilities.java.Maps; +import com.djrapitops.plan.utilities.java.UnaryChain; import javax.inject.Inject; import javax.inject.Singleton; @@ -164,7 +165,10 @@ public class ResponseFactory { public Response javaScriptResponse(String fileName) { try { - String content = locale.replaceLanguageInJavascript(files.getCustomizableResourceOrDefault(fileName).asString()); + String content = UnaryChain.of(files.getCustomizableResourceOrDefault(fileName).asString()) + .chain(theme::replaceThemeColors) + .chain(locale::replaceLanguageInJavascript) + .apply(); return Response.builder() .setMimeType(MimeType.JS) .setContent(content) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/UnaryChain.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/UnaryChain.java new file mode 100644 index 000000000..fa53e9182 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/java/UnaryChain.java @@ -0,0 +1,52 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.utilities.java; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * Utility for combining multiple UnaryOperator modifications. + * + * @author Rsl1122 + */ +public class UnaryChain { + + private final T modifying; + private Function modifier; + + private UnaryChain(T modifying) { + this.modifying = modifying; + } + + public static UnaryChain of(V modifying) { + return new UnaryChain<>(modifying); + } + + public UnaryChain chain(UnaryOperator operation) { + if (modifier == null) { + modifier = operation; + } else { + modifier = modifier.andThen(operation); + } + return this; + } + + public T apply() { + return modifier.apply(modifying); + } +}