From 4371595ccfdbd938270be5472a0f0979e721b988 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Thu, 30 Apr 2020 21:23:02 +0300 Subject: [PATCH] Removed static usage from placeholders - Dagger used to reduce the amount of method parameters. Affects: - #1404 --- .../main/java/com/djrapitops/plan/Plan.java | 8 +- .../djrapitops/plan/PlanBukkitComponent.java | 5 + ...s.java => BukkitPlaceholderRegistrar.java} | 20 +-- .../placeholderapi/PlaceholderRegistrar.java | 36 ------ .../djrapitops/plan/PlanBungeeComponent.java | 2 + .../plan/modules/PlaceholderModule.java | 31 +++++ .../placeholder/OperatorPlaceholders.java | 22 +++- .../plan/placeholder/PlaceholderRegistry.java | 7 ++ .../plan/placeholder/PlanPlaceholders.java | 68 +++++----- ...aceHolder.java => PlayerPlaceHolders.java} | 74 ++++++----- .../plan/placeholder/ServerPlaceHolders.java | 73 ++++++----- ...ceHolder.java => SessionPlaceHolders.java} | 119 ++++++++++-------- ...Holder.java => WorldTimePlaceHolders.java} | 26 +++- .../utilities/dagger/PlanPluginComponent.java | 2 + .../utilities/dagger/PlanPluginModule.java | 1 - .../java/com/djrapitops/plan/PlanNukkit.java | 8 +- .../djrapitops/plan/PlanNukkitComponent.java | 5 + ...r.java => NukkitPlaceholderRegistrar.java} | 34 ++--- .../djrapitops/plan/PlanSpongeComponent.java | 2 + .../plan/PlanVelocityComponent.java | 2 + 20 files changed, 336 insertions(+), 209 deletions(-) rename Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/{BukkitPlanPlaceHolders.java => BukkitPlaceholderRegistrar.java} (83%) delete mode 100644 Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlaceholderRegistrar.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/modules/PlaceholderModule.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlaceholderRegistry.java rename Plan/common/src/main/java/com/djrapitops/plan/placeholder/{PlayerPlaceHolder.java => PlayerPlaceHolders.java} (76%) rename Plan/common/src/main/java/com/djrapitops/plan/placeholder/{SessionPlaceHolder.java => SessionPlaceHolders.java} (67%) rename Plan/common/src/main/java/com/djrapitops/plan/placeholder/{WorldTimePlaceHolder.java => WorldTimePlaceHolders.java} (76%) rename Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/{NKPlaceholderRegistrar.java => NukkitPlaceholderRegistrar.java} (77%) diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java index 6581bd0e4..e15984e12 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan; -import com.djrapitops.plan.addons.placeholderapi.PlaceholderRegistrar; +import com.djrapitops.plan.addons.placeholderapi.BukkitPlaceholderRegistrar; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.exceptions.EnableException; import com.djrapitops.plan.gathering.ServerShutdownSave; @@ -55,7 +55,7 @@ public class Plan extends BukkitPlugin implements PlanPlugin { system.enable(); registerMetrics(); - registerPlaceholderAPIExtension(); + registerPlaceholderAPIExtension(component.placeholders()); logger.debug("Verbose debug messages are enabled."); String benchTime = " (" + timings.end("Enable").map(Benchmark::toDurationString).orElse("-") + ")"; @@ -82,13 +82,13 @@ public class Plan extends BukkitPlugin implements PlanPlugin { } } - private void registerPlaceholderAPIExtension() { + private void registerPlaceholderAPIExtension(BukkitPlaceholderRegistrar placeholders) { if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { runnableFactory.create("Placeholders Registrar", new AbsRunnable() { @Override public void run() { try { - PlaceholderRegistrar.register(system, errorHandler); + placeholders.register(); } catch (Exception | NoClassDefFoundError | NoSuchMethodError failed) { logger.warn("Failed to register PlaceholderAPI placeholders: " + failed.toString()); } diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/PlanBukkitComponent.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/PlanBukkitComponent.java index bd53fc348..c805bed88 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/PlanBukkitComponent.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/PlanBukkitComponent.java @@ -16,9 +16,11 @@ */ package com.djrapitops.plan; +import com.djrapitops.plan.addons.placeholderapi.BukkitPlaceholderRegistrar; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.gathering.ServerShutdownSave; import com.djrapitops.plan.modules.APFModule; +import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.SystemObjectProvidingModule; import com.djrapitops.plan.modules.bukkit.BukkitPlanModule; import com.djrapitops.plan.modules.bukkit.BukkitServerPropertiesModule; @@ -38,6 +40,7 @@ import javax.inject.Singleton; BukkitPlanModule.class, SystemObjectProvidingModule.class, APFModule.class, + PlaceholderModule.class, BukkitServerPropertiesModule.class, BukkitSuperClassBindingModule.class @@ -48,6 +51,8 @@ public interface PlanBukkitComponent { PlanSystem system(); + BukkitPlaceholderRegistrar placeholders(); + ServerShutdownSave serverShutdownSave(); @Component.Builder diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlanPlaceHolders.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlaceholderRegistrar.java similarity index 83% rename from Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlanPlaceHolders.java rename to Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlaceholderRegistrar.java index 3dd6a7822..e03a6f513 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlanPlaceHolders.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlaceholderRegistrar.java @@ -26,6 +26,8 @@ import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.entity.Player; +import javax.inject.Inject; +import javax.inject.Singleton; import java.util.Collections; /** @@ -38,26 +40,30 @@ import java.util.Collections; * {@link ServerKeys#TPS},{@link ServerKeys#NAME}, * {@link ServerKeys#SERVER_UUID} *
  • {@link OperatorPlaceholders}: {@link ServerKeys#OPERATORS}
  • - *
  • {@link WorldTimePlaceHolder}: {@link ServerKeys#WORLD_TIMES}
  • - *
  • {@link SessionPlaceHolder}: {@link ServerKeys#SESSIONS}, + *
  • {@link WorldTimePlaceHolders}: {@link ServerKeys#WORLD_TIMES}
  • + *
  • {@link SessionPlaceHolders}: {@link ServerKeys#SESSIONS}, * {@link ServerKeys#PLAYERS},{@link ServerKeys#PING},{@link ServerKeys#ALL_TIME_PEAK_PLAYERS}, * {@link ServerKeys#RECENT_PEAK_PLAYERS}
  • * * * @author aidn5 */ -public class BukkitPlanPlaceHolders extends PlaceholderExpansion { +@Singleton +public class BukkitPlaceholderRegistrar extends PlaceholderExpansion { + public final ErrorHandler errorHandler; private final VersionChecker versionChecker; + private final PlanPlaceholders placeholders; - public BukkitPlanPlaceHolders( + @Inject + public BukkitPlaceholderRegistrar( + PlanPlaceholders placeholders, PlanSystem system, ErrorHandler errorHandler ) { + this.placeholders = placeholders; this.versionChecker = system.getVersionChecker(); this.errorHandler = errorHandler; - - PlanPlaceholders.init(system); } @Override @@ -94,7 +100,7 @@ public class BukkitPlanPlaceHolders extends PlaceholderExpansion { @Override public String onPlaceholderRequest(Player p, String params) { try { - String value = PlanPlaceholders.onPlaceholderRequest(p.getUniqueId(), params, Collections.emptyList()); + String value = placeholders.onPlaceholderRequest(p.getUniqueId(), params, Collections.emptyList()); if ("true".equals(value)) { //hack value = PlaceholderAPIPlugin.booleanTrue(); diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlaceholderRegistrar.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlaceholderRegistrar.java deleted file mode 100644 index 3019c93bc..000000000 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlaceholderRegistrar.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.addons.placeholderapi; - -import com.djrapitops.plan.PlanSystem; -import com.djrapitops.plugin.logging.error.ErrorHandler; - -/** - * Additional wrapper to register PlaceholderAPI placeholders. - * - * @author Rsl1122 - */ -public class PlaceholderRegistrar { - - private PlaceholderRegistrar() { - } - - public static void register(PlanSystem system, ErrorHandler errorHandler) { - new BukkitPlanPlaceHolders(system, errorHandler).register(); - } - -} \ No newline at end of file diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java index 81543eb90..8ca225841 100644 --- a/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/PlanBungeeComponent.java @@ -18,6 +18,7 @@ package com.djrapitops.plan; import com.djrapitops.plan.commands.PlanProxyCommand; import com.djrapitops.plan.modules.APFModule; +import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.ProxySuperClassBindingModule; import com.djrapitops.plan.modules.SystemObjectProvidingModule; import com.djrapitops.plan.modules.bungee.BungeeCommandModule; @@ -40,6 +41,7 @@ import javax.inject.Singleton; BungeeCommandModule.class, SystemObjectProvidingModule.class, APFModule.class, + PlaceholderModule.class, ProxySuperClassBindingModule.class, BungeeSuperClassBindingModule.class, diff --git a/Plan/common/src/main/java/com/djrapitops/plan/modules/PlaceholderModule.java b/Plan/common/src/main/java/com/djrapitops/plan/modules/PlaceholderModule.java new file mode 100644 index 000000000..b3e170b1e --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/modules/PlaceholderModule.java @@ -0,0 +1,31 @@ +package com.djrapitops.plan.modules; + +import com.djrapitops.plan.placeholder.*; +import dagger.Binds; +import dagger.Module; +import dagger.multibindings.IntoSet; + +@Module +public interface PlaceholderModule { + + @Binds + @IntoSet + PlaceholderRegistry bindOperatorPlaceholders(OperatorPlaceholders placeholders); + + @Binds + @IntoSet + PlaceholderRegistry bindPlayerPlaceHolders(PlayerPlaceHolders placeholders); + + @Binds + @IntoSet + PlaceholderRegistry bindServerPlaceHolders(ServerPlaceHolders placeholders); + + @Binds + @IntoSet + PlaceholderRegistry bindSessionPlaceHolders(SessionPlaceHolders placeholders); + + @Binds + @IntoSet + PlaceholderRegistry bindWorldTimePlaceHolders(WorldTimePlaceHolders placeholders); + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java index aea19e8f8..85169c003 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java @@ -20,15 +20,31 @@ import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries; +import javax.inject.Inject; +import javax.inject.Singleton; + /** * Placeholders about operators. * * @author aidn5, Rsl1122 */ -public class OperatorPlaceholders { +@Singleton +public class OperatorPlaceholders implements PlaceholderRegistry { - public static void register(DBSystem dbSystem, ServerInfo serverInfo) { - PlanPlaceholders.registerStatic("operators_total", + private final DBSystem dbSystem; + private final ServerInfo serverInfo; + + @Inject + public OperatorPlaceholders( + DBSystem dbSystem, ServerInfo serverInfo + ) { + this.dbSystem = dbSystem; + this.serverInfo = serverInfo; + } + + @Override + public void register(PlanPlaceholders placeholders) { + placeholders.registerStatic("operators_total", () -> dbSystem.getDatabase().query(PlayerCountQueries.operators(serverInfo.getServerUUID())) ); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlaceholderRegistry.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlaceholderRegistry.java new file mode 100644 index 000000000..b1cb72311 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlaceholderRegistry.java @@ -0,0 +1,7 @@ +package com.djrapitops.plan.placeholder; + +public interface PlaceholderRegistry { + + void register(PlanPlaceholders placeholders); + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlanPlaceholders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlanPlaceholders.java index d21122e50..3a197e0d4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlanPlaceholders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlanPlaceholders.java @@ -16,7 +16,6 @@ */ package com.djrapitops.plan.placeholder; -import com.djrapitops.plan.PlanSystem; import com.djrapitops.plan.delivery.domain.container.PlayerContainer; import com.djrapitops.plan.delivery.domain.keys.PlayerKeys; import com.djrapitops.plan.delivery.formatting.Formatters; @@ -26,6 +25,8 @@ import com.djrapitops.plan.settings.config.PlanConfig; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries; +import javax.inject.Inject; +import javax.inject.Singleton; import java.io.Serializable; import java.util.*; import java.util.Map.Entry; @@ -33,58 +34,64 @@ import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; +@Singleton public final class PlanPlaceholders { - private static final Map, Serializable>> placeholders = new HashMap<>(); - private static final Map, Serializable>> staticPlaceholders = new HashMap<>(); + private final Map playerPlaceholders; + private final Map staticPlaceholders; - private static final Map> rawHandlers = new HashMap<>(); + private final Map> rawHandlers; - private static DBSystem dbSystem; + private final DBSystem dbSystem; - public static void init(PlanSystem system) { - dbSystem = system.getDatabaseSystem(); + @Inject + public PlanPlaceholders( + PlanConfig config, + DBSystem dbSystem, + ServerInfo serverInfo, + Formatters formatters, + Set placeholderRegistries + ) { + this.dbSystem = dbSystem; - PlanConfig config = system.getConfigSystem().getConfig(); - ServerInfo serverInfo = system.getServerInfo(); - Formatters formatters = system.getDeliveryUtilities().getFormatters(); + this.playerPlaceholders = new HashMap<>(); + this.staticPlaceholders = new HashMap<>(); + this.rawHandlers = new HashMap<>(); - ServerPlaceHolders.register(dbSystem, serverInfo, formatters); - OperatorPlaceholders.register(dbSystem, serverInfo); - WorldTimePlaceHolder.register(dbSystem, serverInfo, formatters); - SessionPlaceHolder.register(config, dbSystem, serverInfo, formatters); - PlayerPlaceHolder.register(dbSystem, serverInfo, formatters); + for (PlaceholderRegistry registry : placeholderRegistries) { + registry.register(this); + } } - public static void registerStatic(String name, Supplier loader) { + public void registerStatic(String name, Supplier loader) { registerStatic(name, params -> loader.get()); } - public static void registerStatic(String name, Function, Serializable> loader) { + public void registerStatic(String name, StaticPlaceholderLoader loader) { staticPlaceholders.put(name, loader); } - public static void register(String name, Function loader) { + public void register(String name, Function loader) { register(name, (player, params) -> loader.apply(player)); } - public static void register(String name, BiFunction, Serializable> loader) { - placeholders.put(name, loader); + public void register(String name, PlayerPlaceholderLoader loader) { + playerPlaceholders.put(name, loader); } - public static void registerRaw(String name, BiFunction loader) { + public void registerRaw(String name, BiFunction loader) { rawHandlers.put(name, loader); } - public static Map, Serializable>> getPlaceholders() { - return placeholders; + public Map getPlaceholders() { + return playerPlaceholders; } - public static Map, Serializable>> getStaticPlaceholders() { + public Map getStaticPlaceholders() { return staticPlaceholders; } - public static String onPlaceholderRequest(UUID uuid, String placeholder, List parameters) { + public String onPlaceholderRequest(UUID uuid, String placeholder, List parameters) { PlayerContainer player; if (uuid != null) { @@ -106,23 +113,22 @@ public final class PlanPlaceholders { * @return the value of the placeholder if found, or empty {@link String} if no * value found but the placeholder is registered, * otherwise {@code null} - * @throws Exception if any error occurs */ - public static String onPlaceholderRequest(PlayerContainer player, String placeholder, List parameters) { + public String onPlaceholderRequest(PlayerContainer player, String placeholder, List parameters) { for (Entry> entry : rawHandlers.entrySet()) { if (placeholder.startsWith(entry.getKey())) { return Objects.toString(entry.getValue().apply(placeholder, player)); } } - Function, Serializable> staticLoader = staticPlaceholders.get(placeholder); + StaticPlaceholderLoader staticLoader = staticPlaceholders.get(placeholder); if (staticLoader != null) { return Objects.toString(staticLoader.apply(parameters)); } if (player != null) { - BiFunction, Serializable> loader = placeholders.get(placeholder); + PlayerPlaceholderLoader loader = playerPlaceholders.get(placeholder); if (loader != null) { return Objects.toString(loader.apply(player, parameters)); @@ -131,4 +137,8 @@ public final class PlanPlaceholders { return null; } + + public interface PlayerPlaceholderLoader extends BiFunction, Serializable> {} + + public interface StaticPlaceholderLoader extends Function, Serializable> {} } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolders.java similarity index 76% rename from Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java rename to Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolders.java index 43fd5ee83..13f631a14 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolders.java @@ -29,6 +29,9 @@ import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; import com.djrapitops.plan.utilities.Predicates; +import javax.inject.Inject; +import javax.inject.Singleton; + import static com.djrapitops.plan.utilities.MiscUtils.*; /** @@ -36,156 +39,171 @@ import static com.djrapitops.plan.utilities.MiscUtils.*; * * @author aidn5, Rsl1122 */ -public class PlayerPlaceHolder { +@Singleton +public class PlayerPlaceHolders implements PlaceholderRegistry { - public static void register( + private final DBSystem dbSystem; + private final ServerInfo serverInfo; + private final Formatters formatters; + + @Inject + public PlayerPlaceHolders( DBSystem dbSystem, ServerInfo serverInfo, Formatters formatters + ) { + this.dbSystem = dbSystem; + this.serverInfo = serverInfo; + this.formatters = formatters; + } + + @Override + public void register( + PlanPlaceholders placeholders ) { Formatter decimals = formatters.decimals(); Formatter year = formatters.yearLong(); Formatter time = formatters.timeAmount(); - PlanPlaceholders.register("player_banned", + placeholders.register("player_banned", player -> player.getValue(PlayerKeys.BANNED) .orElse(Boolean.FALSE) ); - PlanPlaceholders.register("player_operator", + placeholders.register("player_operator", player -> player.getValue(PlayerKeys.OPERATOR) .orElse(Boolean.FALSE) ); - PlanPlaceholders.register("player_sessions_count", + placeholders.register("player_sessions_count", player -> SessionsMutator.forContainer(player) .count() ); - PlanPlaceholders.register("player_kick_count", + placeholders.register("player_kick_count", player -> player.getValue(PlayerKeys.KICK_COUNT) .orElse(0) ); - PlanPlaceholders.register("player_death_count", + placeholders.register("player_death_count", player -> player.getValue(PlayerKeys.DEATH_COUNT) .orElse(0) ); - PlanPlaceholders.register("player_mob_kill_count", + placeholders.register("player_mob_kill_count", player -> player.getValue(PlayerKeys.MOB_KILL_COUNT) .orElse(0) ); - PlanPlaceholders.register("player_player_kill_count", + placeholders.register("player_player_kill_count", player -> player.getValue(PlayerKeys.PLAYER_KILL_COUNT) .orElse(0) ); - PlanPlaceholders.register("player_kill_death_ratio", + placeholders.register("player_kill_death_ratio", player -> PlayerVersusMutator.forContainer(player).toKillDeathRatio()); - PlanPlaceholders.register("player_ping_average_day", + placeholders.register("player_ping_average_day", player -> decimals.apply(PingMutator.forContainer(player) .filterBy(Predicates.within(dayAgo(), now())) .average()) + " ms" ); - PlanPlaceholders.register("player_ping_average_week", + placeholders.register("player_ping_average_week", player -> decimals.apply(PingMutator.forContainer(player) .filterBy(Predicates.within(weekAgo(), now())) .average()) + " ms" ); - PlanPlaceholders.register("player_ping_average_month", + placeholders.register("player_ping_average_month", player -> decimals.apply(PingMutator.forContainer(player) .filterBy(Predicates.within(monthAgo(), now())) .average()) + " ms" ); - PlanPlaceholders.register("player_lastseen", + placeholders.register("player_lastseen", player -> year.apply(player.getValue(PlayerKeys.LAST_SEEN) .orElse((long) 0)) ); - PlanPlaceholders.register("player_registered", + placeholders.register("player_registered", player -> year.apply(player.getValue(PlayerKeys.REGISTERED) .orElse((long) 0)) ); - PlanPlaceholders.register("player_time_active", + placeholders.register("player_time_active", player -> time.apply(SessionsMutator.forContainer(player) .toActivePlaytime()) ); - PlanPlaceholders.register("player_time_afk", + placeholders.register("player_time_afk", player -> time.apply(SessionsMutator.forContainer(player) .toAfkTime()) ); - PlanPlaceholders.register("player_time_total", + placeholders.register("player_time_total", player -> time.apply(SessionsMutator.forContainer(player) .toPlaytime()) ); - PlanPlaceholders.register("player_time_day", + placeholders.register("player_time_day", player -> time.apply(SessionsMutator.forContainer(player) .filterSessionsBetween(dayAgo(), now()) .toPlaytime()) ); - PlanPlaceholders.register("player_time_week", + placeholders.register("player_time_week", player -> time.apply(SessionsMutator.forContainer(player) .filterSessionsBetween(weekAgo(), now()) .toPlaytime()) ); - PlanPlaceholders.register("player_time_month", + placeholders.register("player_time_month", player -> time.apply(SessionsMutator.forContainer(player) .filterSessionsBetween(monthAgo(), now()) .toPlaytime()) ); - PlanPlaceholders.register("player_server_time_active", + placeholders.register("player_server_time_active", player -> time.apply(SessionsMutator.forContainer(player) .filterPlayedOnServer(serverInfo.getServerUUID()) .toActivePlaytime()) ); - PlanPlaceholders.register("player_server_time_afk", + placeholders.register("player_server_time_afk", player -> time.apply(SessionsMutator.forContainer(player) .filterPlayedOnServer(serverInfo.getServerUUID()) .toAfkTime()) ); - PlanPlaceholders.register("player_server_time_total", + placeholders.register("player_server_time_total", player -> time.apply(SessionsMutator.forContainer(player) .filterPlayedOnServer(serverInfo.getServerUUID()) .toPlaytime()) ); - PlanPlaceholders.register("player_server_time_day", + placeholders.register("player_server_time_day", player -> time.apply(SessionsMutator.forContainer(player) .filterSessionsBetween(dayAgo(), now()) .filterPlayedOnServer(serverInfo.getServerUUID()) .toPlaytime()) ); - PlanPlaceholders.register("player_server_time_week", + placeholders.register("player_server_time_week", player -> time.apply(SessionsMutator.forContainer(player) .filterSessionsBetween(weekAgo(), now()) .filterPlayedOnServer(serverInfo.getServerUUID()) .toPlaytime()) ); - PlanPlaceholders.register("player_server_time_month", + placeholders.register("player_server_time_month", player -> time.apply(SessionsMutator.forContainer(player) .filterSessionsBetween(monthAgo(), now()) .filterPlayedOnServer(serverInfo.getServerUUID()) .toPlaytime()) ); - PlanPlaceholders.register("player_favorite_server", + placeholders.register("player_favorite_server", player -> PerServerMutator.forContainer(player).favoriteServer() .flatMap(serverUUID -> dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverUUID))) .map(Server::getName) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java index 21f056642..0d40053a3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java @@ -23,6 +23,8 @@ import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.queries.objects.TPSQueries; +import javax.inject.Inject; +import javax.inject.Singleton; import java.util.UUID; import static com.djrapitops.plan.utilities.MiscUtils.*; @@ -32,12 +34,27 @@ import static com.djrapitops.plan.utilities.MiscUtils.*; * * @author aidn5, Rsl1122 */ -public class ServerPlaceHolders { +@Singleton +public class ServerPlaceHolders implements PlaceholderRegistry { - public static void register( + private final DBSystem dbSystem; + private final ServerInfo serverInfo; + private final Formatters formatters; + + @Inject + public ServerPlaceHolders( DBSystem dbSystem, ServerInfo serverInfo, Formatters formatters + ) { + this.dbSystem = dbSystem; + this.serverInfo = serverInfo; + this.formatters = formatters; + } + + @Override + public void register( + PlanPlaceholders placeholders ) { Formatter decimals = formatters.decimals(); Formatter percentage = formatters.percentage(); @@ -45,82 +62,82 @@ public class ServerPlaceHolders { Database database = dbSystem.getDatabase(); UUID serverUUID = serverInfo.getServerUUID(); - PlanPlaceholders.registerStatic("server_tps_day", + placeholders.registerStatic("server_tps_day", () -> decimals.apply(database.query(TPSQueries.averageTPS(dayAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("server_tps_week", + placeholders.registerStatic("server_tps_week", () -> decimals.apply(database.query(TPSQueries.averageTPS(weekAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("server_tps_month", + placeholders.registerStatic("server_tps_month", () -> decimals.apply(database.query(TPSQueries.averageTPS(monthAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("server_cpu_day", + placeholders.registerStatic("server_cpu_day", () -> percentage.apply(database.query(TPSQueries.averageCPU(dayAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("server_cpu_week", + placeholders.registerStatic("server_cpu_week", () -> percentage.apply(database.query(TPSQueries.averageCPU(weekAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("server_cpu_month", + placeholders.registerStatic("server_cpu_month", () -> percentage.apply(database.query(TPSQueries.averageCPU(monthAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("server_ram_day", + placeholders.registerStatic("server_ram_day", () -> database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID)) + " MB"); - PlanPlaceholders.registerStatic("server_ram_week", + placeholders.registerStatic("server_ram_week", () -> database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID)) + " MB"); - PlanPlaceholders.registerStatic("server_ram_month", + placeholders.registerStatic("server_ram_month", () -> database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID)) + " MB"); - PlanPlaceholders.registerStatic("server_chunks_day", + placeholders.registerStatic("server_chunks_day", () -> database.query(TPSQueries.averageChunks(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_chunks_week", + placeholders.registerStatic("server_chunks_week", () -> database.query(TPSQueries.averageChunks(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_chunks_month", + placeholders.registerStatic("server_chunks_month", () -> database.query(TPSQueries.averageChunks(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_entities_day", + placeholders.registerStatic("server_entities_day", () -> database.query(TPSQueries.averageEntities(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_entities_week", + placeholders.registerStatic("server_entities_week", () -> database.query(TPSQueries.averageEntities(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_entities_month", + placeholders.registerStatic("server_entities_month", () -> database.query(TPSQueries.averageEntities(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_max_free_disk_day", + placeholders.registerStatic("server_max_free_disk_day", () -> database.query(TPSQueries.maxFreeDisk(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_max_free_disk_week", + placeholders.registerStatic("server_max_free_disk_week", () -> database.query(TPSQueries.maxFreeDisk(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_max_free_disk_month", + placeholders.registerStatic("server_max_free_disk_month", () -> database.query(TPSQueries.maxFreeDisk(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_min_free_disk_day", + placeholders.registerStatic("server_min_free_disk_day", () -> database.query(TPSQueries.minFreeDisk(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_min_free_disk_week", + placeholders.registerStatic("server_min_free_disk_week", () -> database.query(TPSQueries.minFreeDisk(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_min_free_disk_month", + placeholders.registerStatic("server_min_free_disk_month", () -> database.query(TPSQueries.minFreeDisk(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_average_free_disk_day", + placeholders.registerStatic("server_average_free_disk_day", () -> database.query(TPSQueries.averageFreeDisk(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_average_free_disk_week", + placeholders.registerStatic("server_average_free_disk_week", () -> database.query(TPSQueries.averageFreeDisk(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_average_free_disk_month", + placeholders.registerStatic("server_average_free_disk_month", () -> database.query(TPSQueries.averageFreeDisk(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("server_name", + placeholders.registerStatic("server_name", () -> serverInfo.getServer().getName()); - PlanPlaceholders.registerStatic("server_uuid", + placeholders.registerStatic("server_uuid", serverInfo::getServerUUID); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolder.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolders.java similarity index 67% rename from Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolder.java rename to Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolders.java index bf276f2e2..6d8c03626 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolder.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolders.java @@ -30,6 +30,8 @@ import com.djrapitops.plan.storage.database.queries.objects.PingQueries; import com.djrapitops.plan.storage.database.queries.objects.SessionQueries; import com.djrapitops.plan.storage.database.queries.objects.TPSQueries; +import javax.inject.Inject; +import javax.inject.Singleton; import java.io.Serializable; import java.util.UUID; import java.util.function.Supplier; @@ -41,13 +43,30 @@ import static com.djrapitops.plan.utilities.MiscUtils.*; * * @author aidn5, Rsl1122 */ -public class SessionPlaceHolder { +@Singleton +public class SessionPlaceHolders implements PlaceholderRegistry { - public static void register( + private final PlanConfig config; + private final DBSystem dbSystem; + private final ServerInfo serverInfo; + private final Formatters formatters; + + @Inject + public SessionPlaceHolders( PlanConfig config, DBSystem dbSystem, ServerInfo serverInfo, Formatters formatters + ) { + this.config = config; + this.dbSystem = dbSystem; + this.serverInfo = serverInfo; + this.formatters = formatters; + } + + @Override + public void register( + PlanPlaceholders placeholders ) { int tzOffsetMs = config.getTimeZone().getOffset(System.currentTimeMillis()); Formatter timeAmount = formatters.timeAmount(); @@ -56,146 +75,146 @@ public class SessionPlaceHolder { Database database = dbSystem.getDatabase(); UUID serverUUID = serverInfo.getServerUUID(); - PlanPlaceholders.registerStatic("sessions_play_time_total", + placeholders.registerStatic("sessions_play_time_total", () -> timeAmount.apply(database.query(SessionQueries.playtime(0L, now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_play_time_day", + placeholders.registerStatic("sessions_play_time_day", () -> timeAmount.apply(database.query(SessionQueries.playtime(dayAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_play_time_week", + placeholders.registerStatic("sessions_play_time_week", () -> timeAmount.apply(database.query(SessionQueries.playtime(weekAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_play_time_month", + placeholders.registerStatic("sessions_play_time_month", () -> timeAmount.apply(database.query(SessionQueries.playtime(monthAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_active_time_total", + placeholders.registerStatic("sessions_active_time_total", () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_active_time_day", + placeholders.registerStatic("sessions_active_time_day", () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(dayAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_active_time_week", + placeholders.registerStatic("sessions_active_time_week", () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(weekAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_active_time_month", + placeholders.registerStatic("sessions_active_time_month", () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(monthAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_afk_time_total", + placeholders.registerStatic("sessions_afk_time_total", () -> timeAmount.apply(database.query(SessionQueries.afkTime(0L, now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_afk_time_day", + placeholders.registerStatic("sessions_afk_time_day", () -> timeAmount.apply(database.query(SessionQueries.afkTime(dayAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_afk_time_week", + placeholders.registerStatic("sessions_afk_time_week", () -> timeAmount.apply(database.query(SessionQueries.afkTime(weekAgo(), now(), serverUUID)))); - PlanPlaceholders.registerStatic("sessions_afk_time_month", + placeholders.registerStatic("sessions_afk_time_month", () -> timeAmount.apply(database.query(SessionQueries.afkTime(monthAgo(), now(), serverUUID)))); Supplier uniquePlayers = () -> database.query(PlayerCountQueries.newPlayerCount(0L, now(), serverUUID)); - PlanPlaceholders.registerStatic("sessions_unique_players_total", uniquePlayers); - PlanPlaceholders.registerStatic("sessions_new_players_total", uniquePlayers); + placeholders.registerStatic("sessions_unique_players_total", uniquePlayers); + placeholders.registerStatic("sessions_new_players_total", uniquePlayers); - PlanPlaceholders.registerStatic("sessions_unique_players_day", + placeholders.registerStatic("sessions_unique_players_day", () -> database.query(PlayerCountQueries.uniquePlayerCount(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_unique_players_week", + placeholders.registerStatic("sessions_unique_players_week", () -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_unique_players_month", + placeholders.registerStatic("sessions_unique_players_month", () -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_death_total", + placeholders.registerStatic("sessions_players_death_total", () -> database.query(KillQueries.deathCount(0L, now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_death_day", + placeholders.registerStatic("sessions_players_death_day", () -> database.query(KillQueries.deathCount(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_death_week", + placeholders.registerStatic("sessions_players_death_week", () -> database.query(KillQueries.deathCount(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_death_month", + placeholders.registerStatic("sessions_players_death_month", () -> database.query(KillQueries.deathCount(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_kill_total", + placeholders.registerStatic("sessions_players_kill_total", () -> database.query(KillQueries.playerKillCount(0L, now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_kill_day", + placeholders.registerStatic("sessions_players_kill_day", () -> database.query(KillQueries.playerKillCount(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_kill_week", + placeholders.registerStatic("sessions_players_kill_week", () -> database.query(KillQueries.playerKillCount(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_players_kill_month", + placeholders.registerStatic("sessions_players_kill_month", () -> database.query(KillQueries.playerKillCount(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_mob_kill_total", + placeholders.registerStatic("sessions_mob_kill_total", () -> database.query(KillQueries.mobKillCount(0L, now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_mob_kill_day", + placeholders.registerStatic("sessions_mob_kill_day", () -> database.query(KillQueries.mobKillCount(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_mob_kill_week", + placeholders.registerStatic("sessions_mob_kill_week", () -> database.query(KillQueries.mobKillCount(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_mob_kill_month", + placeholders.registerStatic("sessions_mob_kill_month", () -> database.query(KillQueries.mobKillCount(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_average_session_length_total", + placeholders.registerStatic("sessions_average_session_length_total", () -> getPlaytime(database, 0L, now(), serverUUID, timeAmount)); - PlanPlaceholders.registerStatic("sessions_average_session_length_day", + placeholders.registerStatic("sessions_average_session_length_day", () -> getPlaytime(database, dayAgo(), now(), serverUUID, timeAmount)); - PlanPlaceholders.registerStatic("sessions_average_session_length_week", + placeholders.registerStatic("sessions_average_session_length_week", () -> getPlaytime(database, weekAgo(), now(), serverUUID, timeAmount)); - PlanPlaceholders.registerStatic("sessions_average_session_length_month", + placeholders.registerStatic("sessions_average_session_length_month", () -> getPlaytime(database, monthAgo(), now(), serverUUID, timeAmount)); - PlanPlaceholders.registerStatic("sessions_average_unique_players_total", + placeholders.registerStatic("sessions_average_unique_players_total", () -> database.query(PlayerCountQueries.averageUniquePlayerCount(0L, now(), tzOffsetMs, serverUUID))); - PlanPlaceholders.registerStatic("sessions_average_unique_players_day", + placeholders.registerStatic("sessions_average_unique_players_day", () -> database.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo(), now(), tzOffsetMs, serverUUID))); - PlanPlaceholders.registerStatic("sessions_average_unique_players_week", + placeholders.registerStatic("sessions_average_unique_players_week", () -> database.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo(), now(), tzOffsetMs, serverUUID))); - PlanPlaceholders.registerStatic("sessions_average_unique_players_month", + placeholders.registerStatic("sessions_average_unique_players_month", () -> database.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo(), now(), tzOffsetMs, serverUUID))); - PlanPlaceholders.registerStatic("sessions_new_players_day", + placeholders.registerStatic("sessions_new_players_day", () -> database.query(PlayerCountQueries.newPlayerCount(dayAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_new_players_week", + placeholders.registerStatic("sessions_new_players_week", () -> database.query(PlayerCountQueries.newPlayerCount(weekAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("sessions_new_players_month", + placeholders.registerStatic("sessions_new_players_month", () -> database.query(PlayerCountQueries.newPlayerCount(monthAgo(), now(), serverUUID))); - PlanPlaceholders.registerStatic("ping_total", + placeholders.registerStatic("ping_total", () -> decimals.apply(database.query(PingQueries.averagePing(0L, now(), serverUUID))) + " ms"); - PlanPlaceholders.registerStatic("ping_day", + placeholders.registerStatic("ping_day", () -> decimals.apply(database.query(PingQueries.averagePing(dayAgo(), now(), serverUUID))) + " ms"); - PlanPlaceholders.registerStatic("ping_week", + placeholders.registerStatic("ping_week", () -> decimals.apply(database.query(PingQueries.averagePing(weekAgo(), now(), serverUUID))) + " ms"); - PlanPlaceholders.registerStatic("ping_month", + placeholders.registerStatic("ping_month", () -> decimals.apply(database.query(PingQueries.averagePing(monthAgo(), now(), serverUUID))) + " ms"); - PlanPlaceholders.registerStatic("sessions_peak_count", + placeholders.registerStatic("sessions_peak_count", () -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue).orElse(0)); - PlanPlaceholders.registerStatic("sessions_peak_date", + placeholders.registerStatic("sessions_peak_date", () -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(year).orElse("-")); - PlanPlaceholders.registerStatic("sessions_recent_peak_count", + placeholders.registerStatic("sessions_recent_peak_count", () -> database.query(TPSQueries.fetchPeakPlayerCount(serverUUID, dayAgo() * 2L)).map(DateObj::getValue).orElse(0)); - PlanPlaceholders.registerStatic("sessions_recent_peak_date", + placeholders.registerStatic("sessions_recent_peak_date", () -> database.query(TPSQueries.fetchPeakPlayerCount(serverUUID, dayAgo() * 2L)).map(year).orElse("-")); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolder.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolders.java similarity index 76% rename from Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolder.java rename to Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolders.java index 6333827f0..71ad961da 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolder.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolders.java @@ -23,21 +23,39 @@ import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.objects.WorldTimesQueries; +import javax.inject.Inject; +import javax.inject.Singleton; + /** * Placeholders about a world times. * * @author aidn5, Rsl1122 */ -public class WorldTimePlaceHolder { +@Singleton +public class WorldTimePlaceHolders implements PlaceholderRegistry { - public static void register( + private final DBSystem dbSystem; + private final ServerInfo serverInfo; + private final Formatters formatters; + + @Inject + public WorldTimePlaceHolders( DBSystem dbSystem, ServerInfo serverInfo, Formatters formatters + ) { + this.dbSystem = dbSystem; + this.serverInfo = serverInfo; + this.formatters = formatters; + } + + @Override + public void register( + PlanPlaceholders placeholders ) { Formatter timeAmount = formatters.timeAmount(); - PlanPlaceholders.registerRaw("worlds_playtime_total_", (input, p) -> { + placeholders.registerRaw("worlds_playtime_total_", (input, p) -> { // get world total play time // e.g. "plan_worlds_playtime_total_%worldname%" // where %worldname% is "world_nether" @@ -48,7 +66,7 @@ public class WorldTimePlaceHolder { return timeAmount.apply(worldTimes.getWorldPlaytime(worldName)); }); - PlanPlaceholders.registerStatic("worlds_playtime_total", params -> { + placeholders.registerStatic("worlds_playtime_total", params -> { if (params.isEmpty()) { return null; } diff --git a/Plan/common/src/test/java/utilities/dagger/PlanPluginComponent.java b/Plan/common/src/test/java/utilities/dagger/PlanPluginComponent.java index f37ef2dc5..82514dba3 100644 --- a/Plan/common/src/test/java/utilities/dagger/PlanPluginComponent.java +++ b/Plan/common/src/test/java/utilities/dagger/PlanPluginComponent.java @@ -20,6 +20,7 @@ import com.djrapitops.plan.PlanPlugin; import com.djrapitops.plan.PlanSystem; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.modules.APFModule; +import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.SystemObjectProvidingModule; import dagger.BindsInstance; import dagger.Component; @@ -36,6 +37,7 @@ import javax.inject.Singleton; PlanPluginModule.class, SystemObjectProvidingModule.class, APFModule.class, + PlaceholderModule.class, PluginServerPropertiesModule.class, PluginSuperClassBindingModule.class diff --git a/Plan/common/src/test/java/utilities/dagger/PlanPluginModule.java b/Plan/common/src/test/java/utilities/dagger/PlanPluginModule.java index f64bb940b..9924fe75e 100644 --- a/Plan/common/src/test/java/utilities/dagger/PlanPluginModule.java +++ b/Plan/common/src/test/java/utilities/dagger/PlanPluginModule.java @@ -49,5 +49,4 @@ public interface PlanPluginModule { @Binds ServerInfo bindServerInfo(ServerServerInfo serverServerInfo); - } \ No newline at end of file diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java index 15f6f26c4..5ae152d6d 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java @@ -16,7 +16,7 @@ */ package com.djrapitops.plan; -import com.djrapitops.plan.addons.placeholderapi.NKPlaceholderRegistrar; +import com.djrapitops.plan.addons.placeholderapi.NukkitPlaceholderRegistrar; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.exceptions.EnableException; import com.djrapitops.plan.gathering.ServerShutdownSave; @@ -52,7 +52,7 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { locale = system.getLocaleSystem().getLocale(); system.enable(); - registerPlaceholderAPI(); + registerPlaceholderAPI(component.placeholders()); logger.debug("Verbose debug messages are enabled."); String benchTime = " (" + timings.end("Enable").map(Benchmark::toDurationString).orElse("-") + ")"; @@ -119,13 +119,13 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { return system; } - private void registerPlaceholderAPI() { + private void registerPlaceholderAPI(NukkitPlaceholderRegistrar placeholders) { if (this.getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { runnableFactory.create("Placeholders Registrar", new AbsRunnable() { @Override public void run() { try { - NKPlaceholderRegistrar.register(system, errorHandler); + placeholders.register(); } catch (Exception | NoClassDefFoundError | NoSuchMethodError failed) { logger.warn("Failed to register PlaceholderAPI placeholders: " + failed.toString()); } diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java index 4180dc857..467fbf7ea 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkitComponent.java @@ -16,9 +16,11 @@ */ package com.djrapitops.plan; +import com.djrapitops.plan.addons.placeholderapi.NukkitPlaceholderRegistrar; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.gathering.ServerShutdownSave; import com.djrapitops.plan.modules.APFModule; +import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.SystemObjectProvidingModule; import com.djrapitops.plan.modules.nukkit.NukkitPlanModule; import com.djrapitops.plan.modules.nukkit.NukkitServerPropertiesModule; @@ -38,6 +40,7 @@ import javax.inject.Singleton; NukkitPlanModule.class, SystemObjectProvidingModule.class, APFModule.class, + PlaceholderModule.class, NukkitServerPropertiesModule.class, NukkitSuperClassBindingModule.class @@ -50,6 +53,8 @@ public interface PlanNukkitComponent { ServerShutdownSave serverShutdownSave(); + NukkitPlaceholderRegistrar placeholders(); + @Component.Builder interface Builder { diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NKPlaceholderRegistrar.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NukkitPlaceholderRegistrar.java similarity index 77% rename from Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NKPlaceholderRegistrar.java rename to Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NukkitPlaceholderRegistrar.java index 3e5422c67..a49f22d63 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NKPlaceholderRegistrar.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NukkitPlaceholderRegistrar.java @@ -27,24 +27,32 @@ import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQue import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.error.ErrorHandler; +import javax.inject.Inject; +import javax.inject.Singleton; import java.util.ArrayList; import java.util.UUID; -public class NKPlaceholderRegistrar { +@Singleton +public class NukkitPlaceholderRegistrar { - private final ErrorHandler errorHandler; + private final PlanPlaceholders placeholders; private final PlanSystem system; + private final ErrorHandler errorHandler; - public NKPlaceholderRegistrar(PlanSystem system, ErrorHandler errorHandler) { + @Inject + public NukkitPlaceholderRegistrar( + PlanPlaceholders placeholders, + PlanSystem system, + ErrorHandler errorHandler + ) { + this.placeholders = placeholders; this.system = system; this.errorHandler = errorHandler; } - private void register0() { - PlanPlaceholders.init(system); - + public void register() { PlaceholderAPI api = PlaceholderAPI.getInstance(); - PlanPlaceholders.getPlaceholders().forEach((name, loader) -> + placeholders.getPlaceholders().forEach((name, loader) -> api.visitorSensitivePlaceholder(name, (player, params) -> { try { return loader.apply(getPlayer(player), new ArrayList<>(params.getAll().values())); @@ -55,7 +63,7 @@ public class NKPlaceholderRegistrar { } )); - PlanPlaceholders.getStaticPlaceholders().forEach((name, loader) -> + placeholders.getStaticPlaceholders().forEach((name, loader) -> api.staticPlaceholder(name, params -> { try { return loader.apply(new ArrayList<>(params.getAll().values())); @@ -70,13 +78,9 @@ public class NKPlaceholderRegistrar { private PlayerContainer getPlayer(Player player) { UUID uuid = player.getUniqueId(); - PlayerContainer p = system.getDatabaseSystem().getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(uuid)); - SessionCache.getCachedSession(uuid).ifPresent(session -> p.putRawData(PlayerKeys.ACTIVE_SESSION, session)); + PlayerContainer container = system.getDatabaseSystem().getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(uuid)); + SessionCache.getCachedSession(uuid).ifPresent(session -> container.putRawData(PlayerKeys.ACTIVE_SESSION, session)); - return p; - } - - public static void register(PlanSystem system, ErrorHandler errorHandler) { - new NKPlaceholderRegistrar(system, errorHandler).register0(); + return container; } } diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSpongeComponent.java b/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSpongeComponent.java index b4d8ce724..7140d0df2 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSpongeComponent.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/PlanSpongeComponent.java @@ -19,6 +19,7 @@ package com.djrapitops.plan; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.gathering.ServerShutdownSave; import com.djrapitops.plan.modules.APFModule; +import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.SystemObjectProvidingModule; import com.djrapitops.plan.modules.sponge.SpongePlanModule; import com.djrapitops.plan.modules.sponge.SpongeServerPropertiesModule; @@ -38,6 +39,7 @@ import javax.inject.Singleton; SpongePlanModule.class, SystemObjectProvidingModule.class, APFModule.class, + PlaceholderModule.class, SpongeSuperClassBindingModule.class, SpongeServerPropertiesModule.class diff --git a/Plan/velocity/src/main/java/com/djrapitops/plan/PlanVelocityComponent.java b/Plan/velocity/src/main/java/com/djrapitops/plan/PlanVelocityComponent.java index b6b2b3eef..0cd898956 100644 --- a/Plan/velocity/src/main/java/com/djrapitops/plan/PlanVelocityComponent.java +++ b/Plan/velocity/src/main/java/com/djrapitops/plan/PlanVelocityComponent.java @@ -18,6 +18,7 @@ package com.djrapitops.plan; import com.djrapitops.plan.commands.PlanProxyCommand; import com.djrapitops.plan.modules.APFModule; +import com.djrapitops.plan.modules.PlaceholderModule; import com.djrapitops.plan.modules.ProxySuperClassBindingModule; import com.djrapitops.plan.modules.SystemObjectProvidingModule; import com.djrapitops.plan.modules.velocity.VelocityCommandModule; @@ -40,6 +41,7 @@ import javax.inject.Singleton; VelocityCommandModule.class, SystemObjectProvidingModule.class, APFModule.class, + PlaceholderModule.class, ProxySuperClassBindingModule.class, VelocitySuperClassBindingModule.class,