From 0066813c2861ee20ce680bcced62a2e1ee1523d9 Mon Sep 17 00:00:00 2001 From: Creeperface01 Date: Thu, 23 Apr 2020 09:35:15 +0200 Subject: [PATCH] Refactor placeholders and add support for nukkit PlaceholderAPI (#1404) by Creeperface01 --- Plan/build.gradle | 1 + ...lders.java => BukkitPlanPlaceHolders.java} | 39 ++-- .../placeholderapi/PlaceholderRegistrar.java | 2 +- .../placeholders/AbstractPlanPlaceHolder.java | 75 ------- .../placeholders/PlayerPlaceHolder.java | 185 ---------------- .../placeholders/ServerPlaceHolders.java | 131 ----------- .../placeholders/SessionPlaceHolder.java | 197 ----------------- .../placeholder}/OperatorPlaceholders.java | 23 +- .../plan/placeholder/PlanPlaceholders.java | 134 ++++++++++++ .../plan/placeholder/PlayerPlaceHolder.java | 195 +++++++++++++++++ .../plan/placeholder/ServerPlaceHolders.java | 127 +++++++++++ .../plan/placeholder/SessionPlaceHolder.java | 207 ++++++++++++++++++ .../placeholder}/WorldTimePlaceHolder.java | 41 ++-- .../djrapitops/plan/utilities/MiscUtils.java | 18 ++ Plan/common/src/main/resources/nukkit.yml | 1 + Plan/nukkit/build.gradle | 1 + .../java/com/djrapitops/plan/PlanNukkit.java | 19 ++ .../NKPlaceholderRegistrar.java | 82 +++++++ 18 files changed, 824 insertions(+), 654 deletions(-) rename Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/{PlanPlaceHolders.java => BukkitPlanPlaceHolders.java} (65%) delete mode 100644 Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/AbstractPlanPlaceHolder.java delete mode 100644 Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/PlayerPlaceHolder.java delete mode 100644 Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/ServerPlaceHolders.java delete mode 100644 Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/SessionPlaceHolder.java rename Plan/{bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders => common/src/main/java/com/djrapitops/plan/placeholder}/OperatorPlaceholders.java (61%) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlanPlaceholders.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolder.java rename Plan/{bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders => common/src/main/java/com/djrapitops/plan/placeholder}/WorldTimePlaceHolder.java (64%) create mode 100644 Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NKPlaceholderRegistrar.java diff --git a/Plan/build.gradle b/Plan/build.gradle index 61c7f1430..65709dc92 100644 --- a/Plan/build.gradle +++ b/Plan/build.gradle @@ -91,6 +91,7 @@ subprojects { ext.guavaVersion = "28.0-jre" ext.bstatsVersion = "1.4" ext.placeholderapiVersion = "2.9.2" + ext.nkPlaceholderapiVersion = "1.3-SNAPSHOT" repositories { mavenCentral() diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlanPlaceHolders.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlanPlaceHolders.java similarity index 65% rename from Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlanPlaceHolders.java rename to Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlanPlaceHolders.java index 2edade0f4..3dd6a7822 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/PlanPlaceHolders.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/BukkitPlanPlaceHolders.java @@ -17,20 +17,16 @@ package com.djrapitops.plan.addons.placeholderapi; import com.djrapitops.plan.PlanSystem; -import com.djrapitops.plan.addons.placeholderapi.placeholders.*; import com.djrapitops.plan.delivery.domain.keys.ServerKeys; -import com.djrapitops.plan.delivery.formatting.Formatters; -import com.djrapitops.plan.identification.ServerInfo; -import com.djrapitops.plan.settings.config.PlanConfig; -import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.placeholder.*; import com.djrapitops.plan.version.VersionChecker; import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.error.ErrorHandler; +import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.Collection; +import java.util.Collections; /** * Placeholder expansion used to provide data from Plan. @@ -50,29 +46,18 @@ import java.util.Collection; * * @author aidn5 */ -public class PlanPlaceHolders extends PlaceholderExpansion { +public class BukkitPlanPlaceHolders extends PlaceholderExpansion { public final ErrorHandler errorHandler; - - private final Collection placeholders = new ArrayList<>(); private final VersionChecker versionChecker; - public PlanPlaceHolders( + public BukkitPlanPlaceHolders( PlanSystem system, ErrorHandler errorHandler ) { this.versionChecker = system.getVersionChecker(); this.errorHandler = errorHandler; - PlanConfig config = system.getConfigSystem().getConfig(); - DBSystem databaseSystem = system.getDatabaseSystem(); - ServerInfo serverInfo = system.getServerInfo(); - Formatters formatters = system.getDeliveryUtilities().getFormatters(); - - placeholders.add(new ServerPlaceHolders(databaseSystem, serverInfo, formatters)); - placeholders.add(new OperatorPlaceholders(databaseSystem, serverInfo)); - placeholders.add(new WorldTimePlaceHolder(databaseSystem, serverInfo, formatters)); - placeholders.add(new SessionPlaceHolder(config, databaseSystem, serverInfo, formatters)); - placeholders.add(new PlayerPlaceHolder(databaseSystem, serverInfo, formatters)); + PlanPlaceholders.init(system); } @Override @@ -109,13 +94,15 @@ public class PlanPlaceHolders extends PlaceholderExpansion { @Override public String onPlaceholderRequest(Player p, String params) { try { - for (AbstractPlanPlaceHolder placeholder : placeholders) { - String value = placeholder.onPlaceholderRequest(p, params); - if (value == null) continue; - - return value; + String value = PlanPlaceholders.onPlaceholderRequest(p.getUniqueId(), params, Collections.emptyList()); + if ("true".equals(value)) { //hack + value = PlaceholderAPIPlugin.booleanTrue(); + } else if ("false".equals(value)) { + value = PlaceholderAPIPlugin.booleanFalse(); } + + return value; } catch (Exception e) { errorHandler.log(L.WARN, getClass(), e); } 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 index 170f86999..3019c93bc 100644 --- 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 @@ -30,7 +30,7 @@ public class PlaceholderRegistrar { } public static void register(PlanSystem system, ErrorHandler errorHandler) { - new PlanPlaceHolders(system, errorHandler).register(); + new BukkitPlanPlaceHolders(system, errorHandler).register(); } } \ No newline at end of file diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/AbstractPlanPlaceHolder.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/AbstractPlanPlaceHolder.java deleted file mode 100644 index 04000f0ef..000000000 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/AbstractPlanPlaceHolder.java +++ /dev/null @@ -1,75 +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.placeholders; - -import com.djrapitops.plan.addons.placeholderapi.PlanPlaceHolders; -import com.djrapitops.plan.identification.ServerInfo; -import org.bukkit.entity.Player; - -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -/** - * abstract class used for plan placeholders list. This class contains most used - * methods (static methods and non-static one). It is also used as a universe - * interface for the Plan-placeholders instances - * {@link #onPlaceholderRequest(Player, String)}. Check {@link PlanPlaceHolders} - * to learn more how it is used. - * - * @author aidn5 - * @see PlanPlaceHolders - */ -public abstract class AbstractPlanPlaceHolder { - - protected final ServerInfo serverInfo; - - AbstractPlanPlaceHolder(ServerInfo serverInfo) { - this.serverInfo = serverInfo; - } - - static long now() { - return System.currentTimeMillis(); - } - - static long dayAgo() { - return now() - TimeUnit.DAYS.toMillis(1L); - } - - static long weekAgo() { - return now() - (TimeUnit.DAYS.toMillis(7L)); - } - - static long monthAgo() { - return now() - (TimeUnit.DAYS.toMillis(30L)); - } - - UUID serverUUID() { - return serverInfo.getServerUUID(); - } - - /** - * Look up the placeholder and check if it is registered in this instance. - * - * @param p the player who is viewing the placeholder - * @param params the placeholder to look up to. - * @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 abstract String onPlaceholderRequest(Player p, String params) throws Exception; -} diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/PlayerPlaceHolder.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/PlayerPlaceHolder.java deleted file mode 100644 index e3c24523d..000000000 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/PlayerPlaceHolder.java +++ /dev/null @@ -1,185 +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.placeholders; - -import com.djrapitops.plan.delivery.domain.container.PlayerContainer; -import com.djrapitops.plan.delivery.domain.keys.PlayerKeys; -import com.djrapitops.plan.delivery.domain.mutators.PerServerMutator; -import com.djrapitops.plan.delivery.domain.mutators.PingMutator; -import com.djrapitops.plan.delivery.domain.mutators.PlayerVersusMutator; -import com.djrapitops.plan.delivery.domain.mutators.SessionsMutator; -import com.djrapitops.plan.delivery.formatting.Formatter; -import com.djrapitops.plan.delivery.formatting.Formatters; -import com.djrapitops.plan.gathering.cache.SessionCache; -import com.djrapitops.plan.identification.Server; -import com.djrapitops.plan.identification.ServerInfo; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries; -import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; -import com.djrapitops.plan.utilities.Predicates; -import me.clip.placeholderapi.PlaceholderAPIPlugin; -import org.bukkit.entity.Player; - -import java.io.Serializable; -import java.util.UUID; - -/** - * Placeholders about a player. - * - * @author aidn5, Rsl1122 - */ -public class PlayerPlaceHolder extends AbstractPlanPlaceHolder { - - private final DBSystem dbSystem; - private final Formatter decimals; - private Formatter year; - private Formatter time; - - public PlayerPlaceHolder( - DBSystem dbSystem, - ServerInfo serverInfo, - Formatters formatters - ) { - super(serverInfo); - this.dbSystem = dbSystem; - time = formatters.timeAmount(); - year = formatters.yearLong(); - decimals = formatters.decimals(); - } - - @Override - public String onPlaceholderRequest(Player p, String params) { - Serializable got = get(params, p.getUniqueId()); - return got != null ? got.toString() : null; - } - - // Checkstyle.OFF: CyclomaticComplexity - - public Serializable get(String params, UUID playerUUID) { - PlayerContainer player = getPlayer(playerUUID); - - switch (params.toLowerCase()) { - case "player_banned": - return player.getValue(PlayerKeys.BANNED) - .orElse(Boolean.FALSE) ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse(); - case "player_operator": - return player.getValue(PlayerKeys.OPERATOR) - .orElse(Boolean.FALSE) ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse(); - - case "player_sessions_count": - return SessionsMutator.forContainer(player).count(); - - case "player_kick_count": - return player.getValue(PlayerKeys.KICK_COUNT).orElse(0); - case "player_death_count": - return player.getValue(PlayerKeys.DEATH_COUNT).orElse(0); - case "player_mob_kill_count": - return player.getValue(PlayerKeys.MOB_KILL_COUNT).orElse(0); - case "player_player_kill_count": - return player.getValue(PlayerKeys.PLAYER_KILL_COUNT).orElse(0); - case "player_kill_death_ratio": - return PlayerVersusMutator.forContainer(player).toKillDeathRatio(); - - case "player_ping_average_day": - return decimals.apply(PingMutator.forContainer(player) - .filterBy(Predicates.within(dayAgo(), now())) - .average()) + " ms"; - case "player_ping_average_week": - return decimals.apply(PingMutator.forContainer(player) - .filterBy(Predicates.within(weekAgo(), now())) - .average()) + " ms"; - case "player_ping_average_month": - return decimals.apply(PingMutator.forContainer(player) - .filterBy(Predicates.within(monthAgo(), now())) - .average()) + " ms"; - - case "player_lastseen": - return year.apply(player.getValue(PlayerKeys.LAST_SEEN).orElse((long) 0)); - case "player_registered": - return year.apply(player.getValue(PlayerKeys.REGISTERED).orElse((long) 0)); - - case "player_time_active": - return time.apply(SessionsMutator.forContainer(player) - .toActivePlaytime()); - case "player_time_afk": - return time.apply(SessionsMutator.forContainer(player) - .toAfkTime()); - - case "player_time_total": - return time.apply(SessionsMutator.forContainer(player) - .toPlaytime()); - case "player_time_day": - return time.apply(SessionsMutator.forContainer(player) - .filterSessionsBetween(dayAgo(), now()) - .toPlaytime()); - case "player_time_week": - return time.apply(SessionsMutator.forContainer(player) - .filterSessionsBetween(weekAgo(), now()) - .toPlaytime()); - case "player_time_month": - return time.apply(SessionsMutator.forContainer(player) - .filterSessionsBetween(monthAgo(), now()) - .toPlaytime()); - - case "player_server_time_active": - return time.apply(SessionsMutator.forContainer(player) - .filterPlayedOnServer(serverUUID()) - .toActivePlaytime()); - case "player_server_time_afk": - return time.apply(SessionsMutator.forContainer(player) - .filterPlayedOnServer(serverUUID()) - .toAfkTime()); - - case "player_server_time_total": - return time.apply(SessionsMutator.forContainer(player) - .filterPlayedOnServer(serverUUID()) - .toPlaytime()); - case "player_server_time_day": - return time.apply(SessionsMutator.forContainer(player) - .filterSessionsBetween(dayAgo(), now()) - .filterPlayedOnServer(serverUUID()) - .toPlaytime()); - case "player_server_time_week": - return time.apply(SessionsMutator.forContainer(player) - .filterSessionsBetween(weekAgo(), now()) - .filterPlayedOnServer(serverUUID()) - .toPlaytime()); - case "player_server_time_month": - return time.apply(SessionsMutator.forContainer(player) - .filterSessionsBetween(monthAgo(), now()) - .filterPlayedOnServer(serverUUID()) - .toPlaytime()); - - case "player_favorite_server": - return PerServerMutator.forContainer(player).favoriteServer() - .flatMap(serverUUID -> dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverUUID))) - .map(Server::getName) - .orElse("-"); - - default: - return null; - } - } - - // Checkstyle.ON: CyclomaticComplexity - - private PlayerContainer getPlayer(UUID playerUUID) { - PlayerContainer player = dbSystem.getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(playerUUID)); - SessionCache.getCachedSession(playerUUID).ifPresent(session -> player.putRawData(PlayerKeys.ACTIVE_SESSION, session)); - return player; - } -} diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/ServerPlaceHolders.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/ServerPlaceHolders.java deleted file mode 100644 index e3b203d80..000000000 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/ServerPlaceHolders.java +++ /dev/null @@ -1,131 +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.placeholders; - -import com.djrapitops.plan.delivery.formatting.Formatter; -import com.djrapitops.plan.delivery.formatting.Formatters; -import com.djrapitops.plan.identification.ServerInfo; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.Database; -import com.djrapitops.plan.storage.database.queries.objects.TPSQueries; -import org.bukkit.entity.Player; - -import java.io.Serializable; -import java.util.UUID; - -/** - * Placeholders about a servers. - * - * @author aidn5, Rsl1122 - */ -public class ServerPlaceHolders extends AbstractPlanPlaceHolder { - - private final DBSystem dbSystem; - private Formatter decimals; - private Formatter percentage; - - public ServerPlaceHolders( - DBSystem dbSystem, - ServerInfo serverInfo, - Formatters formatters - ) { - super(serverInfo); - this.dbSystem = dbSystem; - decimals = formatters.decimals(); - percentage = formatters.percentage(); - } - - @Override - public String onPlaceholderRequest(Player p, String params) { - Database database = dbSystem.getDatabase(); - UUID serverUUID = serverUUID(); - Serializable got = get(params, database, serverUUID); - return got != null ? got.toString() : null; - } - - // Checkstyle.OFF: CyclomaticComplexity - - public Serializable get(String params, Database database, UUID serverUUID) { - switch (params.toLowerCase()) { - case "server_tps_day": - return decimals.apply(database.query(TPSQueries.averageTPS(dayAgo(), now(), serverUUID))); - case "server_tps_week": - return decimals.apply(database.query(TPSQueries.averageTPS(weekAgo(), now(), serverUUID))); - case "server_tps_month": - return decimals.apply(database.query(TPSQueries.averageTPS(monthAgo(), now(), serverUUID))); - - case "server_cpu_day": - return percentage.apply(database.query(TPSQueries.averageCPU(dayAgo(), now(), serverUUID))); - case "server_cpu_week": - return percentage.apply(database.query(TPSQueries.averageCPU(weekAgo(), now(), serverUUID))); - case "server_cpu_month": - return percentage.apply(database.query(TPSQueries.averageCPU(monthAgo(), now(), serverUUID))); - - case "server_ram_day": - return database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID)) + " MB"; - case "server_ram_week": - return database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID)) + " MB"; - case "server_ram_month": - return database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID)) + " MB"; - - case "server_chunks_day": - return database.query(TPSQueries.averageChunks(dayAgo(), now(), serverUUID)); - case "server_chunks_week": - return database.query(TPSQueries.averageChunks(weekAgo(), now(), serverUUID)); - case "server_chunks_month": - return database.query(TPSQueries.averageChunks(monthAgo(), now(), serverUUID)); - - case "server_entities_day": - return database.query(TPSQueries.averageEntities(dayAgo(), now(), serverUUID)); - case "server_entities_week": - return database.query(TPSQueries.averageEntities(weekAgo(), now(), serverUUID)); - case "server_entities_month": - return database.query(TPSQueries.averageEntities(monthAgo(), now(), serverUUID)); - - case "server_max_free_disk_day": - return database.query(TPSQueries.maxFreeDisk(dayAgo(), now(), serverUUID)); - case "server_max_free_disk_week": - return database.query(TPSQueries.maxFreeDisk(weekAgo(), now(), serverUUID)); - case "server_max_free_disk_month": - return database.query(TPSQueries.maxFreeDisk(monthAgo(), now(), serverUUID)); - - case "server_min_free_disk_day": - return database.query(TPSQueries.minFreeDisk(dayAgo(), now(), serverUUID)); - case "server_min_free_disk_week": - return database.query(TPSQueries.minFreeDisk(weekAgo(), now(), serverUUID)); - case "server_min_free_disk_month": - return database.query(TPSQueries.minFreeDisk(monthAgo(), now(), serverUUID)); - - case "server_average_free_disk_day": - return database.query(TPSQueries.averageFreeDisk(dayAgo(), now(), serverUUID)); - case "server_average_free_disk_week": - return database.query(TPSQueries.averageFreeDisk(weekAgo(), now(), serverUUID)); - case "server_average_free_disk_month": - return database.query(TPSQueries.averageFreeDisk(monthAgo(), now(), serverUUID)); - - case "server_name": - return serverInfo.getServer().getName(); - case "server_uuid": - return serverInfo.getServerUUID(); - - default: - return null; - } - } - - // Checkstyle.ON: CyclomaticComplexity -} diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/SessionPlaceHolder.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/SessionPlaceHolder.java deleted file mode 100644 index f62107708..000000000 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/SessionPlaceHolder.java +++ /dev/null @@ -1,197 +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.placeholders; - -import com.djrapitops.plan.delivery.domain.DateHolder; -import com.djrapitops.plan.delivery.domain.DateObj; -import com.djrapitops.plan.delivery.formatting.Formatter; -import com.djrapitops.plan.delivery.formatting.Formatters; -import com.djrapitops.plan.identification.ServerInfo; -import com.djrapitops.plan.settings.config.PlanConfig; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.Database; -import com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries; -import com.djrapitops.plan.storage.database.queries.objects.KillQueries; -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 org.bukkit.entity.Player; - -import java.io.Serializable; -import java.util.UUID; - -/** - * Placeholders about a sessions. - * - * @author aidn5, Rsl1122 - */ -public class SessionPlaceHolder extends AbstractPlanPlaceHolder { - - private final DBSystem dbSystem; - private final Formatter decimals; - private Formatter timeAmount; - private int tzOffsetMs; - private Formatter year; - - public SessionPlaceHolder( - PlanConfig config, - DBSystem dbSystem, - ServerInfo serverInfo, - Formatters formatters - ) { - super(serverInfo); - this.dbSystem = dbSystem; - - tzOffsetMs = config.getTimeZone().getOffset(System.currentTimeMillis()); - timeAmount = formatters.timeAmount(); - year = formatters.year(); - decimals = formatters.decimals(); - } - - @Override - public String onPlaceholderRequest(Player p, String params) { - Serializable got = get(params); - return got != null ? got.toString() : null; - } - - // Checkstyle.OFF: CyclomaticComplexity - - public Serializable get(String params) { - Database database = dbSystem.getDatabase(); - UUID serverUUID = serverUUID(); - - switch (params.toLowerCase()) { - - case "sessions_play_time_total": - return timeAmount.apply(database.query(SessionQueries.playtime(0L, now(), serverUUID))); - case "sessions_play_time_day": - return timeAmount.apply(database.query(SessionQueries.playtime(dayAgo(), now(), serverUUID))); - case "sessions_play_time_week": - return timeAmount.apply(database.query(SessionQueries.playtime(weekAgo(), now(), serverUUID))); - case "sessions_play_time_month": - return timeAmount.apply(database.query(SessionQueries.playtime(monthAgo(), now(), serverUUID))); - - case "sessions_active_time_total": - return timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now(), serverUUID))); - case "sessions_active_time_day": - return timeAmount.apply(database.query(SessionQueries.activePlaytime(dayAgo(), now(), serverUUID))); - case "sessions_active_time_week": - return timeAmount.apply(database.query(SessionQueries.activePlaytime(weekAgo(), now(), serverUUID))); - case "sessions_active_time_month": - return timeAmount.apply(database.query(SessionQueries.activePlaytime(monthAgo(), now(), serverUUID))); - - case "sessions_afk_time_total": - return timeAmount.apply(database.query(SessionQueries.afkTime(0L, now(), serverUUID))); - case "sessions_afk_time_day": - return timeAmount.apply(database.query(SessionQueries.afkTime(dayAgo(), now(), serverUUID))); - case "sessions_afk_time_week": - return timeAmount.apply(database.query(SessionQueries.afkTime(weekAgo(), now(), serverUUID))); - case "sessions_afk_time_month": - return timeAmount.apply(database.query(SessionQueries.afkTime(monthAgo(), now(), serverUUID))); - - case "sessions_unique_players_total": - case "sessions_new_players_total": - return database.query(PlayerCountQueries.newPlayerCount(0L, now(), serverUUID)); - case "sessions_unique_players_day": - return database.query(PlayerCountQueries.uniquePlayerCount(dayAgo(), now(), serverUUID)); - case "sessions_unique_players_week": - return database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), serverUUID)); - case "sessions_unique_players_month": - return database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now(), serverUUID)); - - case "sessions_players_death_total": - return database.query(KillQueries.deathCount(0L, now(), serverUUID)); - case "sessions_players_death_day": - return database.query(KillQueries.deathCount(dayAgo(), now(), serverUUID)); - case "sessions_players_death_week": - return database.query(KillQueries.deathCount(weekAgo(), now(), serverUUID)); - case "sessions_players_death_month": - return database.query(KillQueries.deathCount(monthAgo(), now(), serverUUID)); - - case "sessions_players_kill_total": - return database.query(KillQueries.playerKillCount(0L, now(), serverUUID)); - case "sessions_players_kill_day": - return database.query(KillQueries.playerKillCount(dayAgo(), now(), serverUUID)); - case "sessions_players_kill_week": - return database.query(KillQueries.playerKillCount(weekAgo(), now(), serverUUID)); - case "sessions_players_kill_month": - return database.query(KillQueries.playerKillCount(monthAgo(), now(), serverUUID)); - - case "sessions_mob_kill_total": - return database.query(KillQueries.mobKillCount(0L, now(), serverUUID)); - case "sessions_mob_kill_day": - return database.query(KillQueries.mobKillCount(dayAgo(), now(), serverUUID)); - case "sessions_mob_kill_week": - return database.query(KillQueries.mobKillCount(weekAgo(), now(), serverUUID)); - case "sessions_mob_kill_month": - return database.query(KillQueries.mobKillCount(monthAgo(), now(), serverUUID)); - - case "sessions_average_session_length_total": - return getPlaytime(database, 0L, now(), serverUUID); - case "sessions_average_session_length_day": - return getPlaytime(database, dayAgo(), now(), serverUUID); - case "sessions_average_session_length_week": - return getPlaytime(database, weekAgo(), now(), serverUUID); - case "sessions_average_session_length_month": - return getPlaytime(database, monthAgo(), now(), serverUUID); - - case "sessions_average_unique_players_total": - return database.query(PlayerCountQueries.averageUniquePlayerCount(0L, now(), tzOffsetMs, serverUUID)); - case "sessions_average_unique_players_day": - return database.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo(), now(), tzOffsetMs, serverUUID)); - case "sessions_average_unique_players_week": - return database.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo(), now(), tzOffsetMs, serverUUID)); - case "sessions_average_unique_players_month": - return database.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo(), now(), tzOffsetMs, serverUUID)); - case "sessions_new_players_day": - return database.query(PlayerCountQueries.newPlayerCount(dayAgo(), now(), serverUUID)); - case "sessions_new_players_week": - return database.query(PlayerCountQueries.newPlayerCount(weekAgo(), now(), serverUUID)); - case "sessions_new_players_month": - return database.query(PlayerCountQueries.newPlayerCount(monthAgo(), now(), serverUUID)); - - case "ping_total": - return decimals.apply(database.query(PingQueries.averagePing(0L, now(), serverUUID))) + " ms"; - case "ping_day": - return decimals.apply(database.query(PingQueries.averagePing(dayAgo(), now(), serverUUID))) + " ms"; - case "ping_week": - return decimals.apply(database.query(PingQueries.averagePing(weekAgo(), now(), serverUUID))) + " ms"; - case "ping_month": - return decimals.apply(database.query(PingQueries.averagePing(monthAgo(), now(), serverUUID))) + " ms"; - - case "sessions_peak_count": - return database.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue).orElse(0); - case "sessions_peak_date": - return database.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(year).orElse("-"); - - case "sessions_recent_peak_count": - return database.query(TPSQueries.fetchPeakPlayerCount(serverUUID, dayAgo() * 2L)).map(DateObj::getValue).orElse(0); - case "sessions_recent_peak_date": - return database.query(TPSQueries.fetchPeakPlayerCount(serverUUID, dayAgo() * 2L)).map(year).orElse("-"); - - default: - return null; - } - } - // Checkstyle.ON: CyclomaticComplexity - - private String getPlaytime(Database database, long after, long before, UUID serverUUID) { - Long playtime = database.query(SessionQueries.playtime(after, before, serverUUID)); - Long sessionCount = database.query(SessionQueries.sessionCount(after, before, serverUUID)); - return timeAmount.apply(sessionCount != 0 ? playtime / sessionCount : playtime); - } -} diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/OperatorPlaceholders.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java similarity index 61% rename from Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/OperatorPlaceholders.java rename to Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java index c2b6898be..aea19e8f8 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/OperatorPlaceholders.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/OperatorPlaceholders.java @@ -14,33 +14,22 @@ * 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.placeholders; +package com.djrapitops.plan.placeholder; import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries; -import org.bukkit.entity.Player; /** * Placeholders about operators. * * @author aidn5, Rsl1122 */ -public class OperatorPlaceholders extends AbstractPlanPlaceHolder { +public class OperatorPlaceholders { - private final DBSystem dbSystem; - - public OperatorPlaceholders(DBSystem dbSystem, ServerInfo serverInfo) { - super(serverInfo); - this.dbSystem = dbSystem; - } - - @Override - public String onPlaceholderRequest(Player p, String params) throws Exception { - if ("operators_total".equalsIgnoreCase(params)) { - return dbSystem.getDatabase().query(PlayerCountQueries.operators(serverUUID())).toString(); - } - - return null; + public static void register(DBSystem dbSystem, ServerInfo serverInfo) { + PlanPlaceholders.registerStatic("operators_total", + () -> dbSystem.getDatabase().query(PlayerCountQueries.operators(serverInfo.getServerUUID())) + ); } } 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 new file mode 100644 index 000000000..d21122e50 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlanPlaceholders.java @@ -0,0 +1,134 @@ +/* + * 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.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; +import com.djrapitops.plan.gathering.cache.SessionCache; +import com.djrapitops.plan.identification.ServerInfo; +import com.djrapitops.plan.settings.config.PlanConfig; +import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries; + +import java.io.Serializable; +import java.util.*; +import java.util.Map.Entry; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +public final class PlanPlaceholders { + + private static final Map, Serializable>> placeholders = new HashMap<>(); + private static final Map, Serializable>> staticPlaceholders = new HashMap<>(); + + private static final Map> rawHandlers = new HashMap<>(); + + private static DBSystem dbSystem; + + public static void init(PlanSystem system) { + dbSystem = system.getDatabaseSystem(); + + PlanConfig config = system.getConfigSystem().getConfig(); + ServerInfo serverInfo = system.getServerInfo(); + Formatters formatters = system.getDeliveryUtilities().getFormatters(); + + 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); + } + + public static void registerStatic(String name, Supplier loader) { + registerStatic(name, params -> loader.get()); + } + + public static void registerStatic(String name, Function, Serializable> loader) { + staticPlaceholders.put(name, loader); + } + + public static 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 static void registerRaw(String name, BiFunction loader) { + rawHandlers.put(name, loader); + } + + public static Map, Serializable>> getPlaceholders() { + return placeholders; + } + + public static Map, Serializable>> getStaticPlaceholders() { + return staticPlaceholders; + } + + public static String onPlaceholderRequest(UUID uuid, String placeholder, List parameters) { + PlayerContainer player; + + if (uuid != null) { + player = dbSystem.getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(uuid)); + SessionCache.getCachedSession(uuid).ifPresent(session -> player.putRawData(PlayerKeys.ACTIVE_SESSION, session)); + } else { + player = null; + } + + return onPlaceholderRequest(player, placeholder, parameters); + } + + /** + * Look up the placeholder and check if it is registered. + * + * @param player the player who is viewing the placeholder + * @param placeholder the placeholder to look up to. + * @param parameters additional placeholder parameters + * @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) { + for (Entry> entry : rawHandlers.entrySet()) { + if (placeholder.startsWith(entry.getKey())) { + return Objects.toString(entry.getValue().apply(placeholder, player)); + } + } + + Function, Serializable> staticLoader = staticPlaceholders.get(placeholder); + + if (staticLoader != null) { + return Objects.toString(staticLoader.apply(parameters)); + } + + if (player != null) { + BiFunction, Serializable> loader = placeholders.get(placeholder); + + if (loader != null) { + return Objects.toString(loader.apply(player, parameters)); + } + } + + return null; + } +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java new file mode 100644 index 000000000..43fd5ee83 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/PlayerPlaceHolder.java @@ -0,0 +1,195 @@ +/* + * 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.placeholder; + +import com.djrapitops.plan.delivery.domain.keys.PlayerKeys; +import com.djrapitops.plan.delivery.domain.mutators.PerServerMutator; +import com.djrapitops.plan.delivery.domain.mutators.PingMutator; +import com.djrapitops.plan.delivery.domain.mutators.PlayerVersusMutator; +import com.djrapitops.plan.delivery.domain.mutators.SessionsMutator; +import com.djrapitops.plan.delivery.formatting.Formatter; +import com.djrapitops.plan.delivery.formatting.Formatters; +import com.djrapitops.plan.identification.Server; +import com.djrapitops.plan.identification.ServerInfo; +import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.storage.database.queries.objects.ServerQueries; +import com.djrapitops.plan.utilities.Predicates; + +import static com.djrapitops.plan.utilities.MiscUtils.*; + +/** + * Placeholders about a player. + * + * @author aidn5, Rsl1122 + */ +public class PlayerPlaceHolder { + + public static void register( + DBSystem dbSystem, + ServerInfo serverInfo, + Formatters formatters + ) { + Formatter decimals = formatters.decimals(); + Formatter year = formatters.yearLong(); + Formatter time = formatters.timeAmount(); + + PlanPlaceholders.register("player_banned", + player -> player.getValue(PlayerKeys.BANNED) + .orElse(Boolean.FALSE) + ); + + PlanPlaceholders.register("player_operator", + player -> player.getValue(PlayerKeys.OPERATOR) + .orElse(Boolean.FALSE) + ); + + PlanPlaceholders.register("player_sessions_count", + player -> SessionsMutator.forContainer(player) + .count() + ); + + PlanPlaceholders.register("player_kick_count", + player -> player.getValue(PlayerKeys.KICK_COUNT) + .orElse(0) + ); + + PlanPlaceholders.register("player_death_count", + player -> player.getValue(PlayerKeys.DEATH_COUNT) + .orElse(0) + ); + + PlanPlaceholders.register("player_mob_kill_count", + player -> player.getValue(PlayerKeys.MOB_KILL_COUNT) + .orElse(0) + ); + + PlanPlaceholders.register("player_player_kill_count", + player -> player.getValue(PlayerKeys.PLAYER_KILL_COUNT) + .orElse(0) + ); + + PlanPlaceholders.register("player_kill_death_ratio", + player -> PlayerVersusMutator.forContainer(player).toKillDeathRatio()); + + PlanPlaceholders.register("player_ping_average_day", + player -> decimals.apply(PingMutator.forContainer(player) + .filterBy(Predicates.within(dayAgo(), now())) + .average()) + " ms" + ); + + PlanPlaceholders.register("player_ping_average_week", + player -> decimals.apply(PingMutator.forContainer(player) + .filterBy(Predicates.within(weekAgo(), now())) + .average()) + " ms" + ); + + PlanPlaceholders.register("player_ping_average_month", + player -> decimals.apply(PingMutator.forContainer(player) + .filterBy(Predicates.within(monthAgo(), now())) + .average()) + " ms" + ); + + PlanPlaceholders.register("player_lastseen", + player -> year.apply(player.getValue(PlayerKeys.LAST_SEEN) + .orElse((long) 0)) + ); + + PlanPlaceholders.register("player_registered", + player -> year.apply(player.getValue(PlayerKeys.REGISTERED) + .orElse((long) 0)) + ); + + PlanPlaceholders.register("player_time_active", + player -> time.apply(SessionsMutator.forContainer(player) + .toActivePlaytime()) + ); + + PlanPlaceholders.register("player_time_afk", + player -> time.apply(SessionsMutator.forContainer(player) + .toAfkTime()) + ); + + PlanPlaceholders.register("player_time_total", + player -> time.apply(SessionsMutator.forContainer(player) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_time_day", + player -> time.apply(SessionsMutator.forContainer(player) + .filterSessionsBetween(dayAgo(), now()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_time_week", + player -> time.apply(SessionsMutator.forContainer(player) + .filterSessionsBetween(weekAgo(), now()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_time_month", + player -> time.apply(SessionsMutator.forContainer(player) + .filterSessionsBetween(monthAgo(), now()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_server_time_active", + player -> time.apply(SessionsMutator.forContainer(player) + .filterPlayedOnServer(serverInfo.getServerUUID()) + .toActivePlaytime()) + ); + + PlanPlaceholders.register("player_server_time_afk", + player -> time.apply(SessionsMutator.forContainer(player) + .filterPlayedOnServer(serverInfo.getServerUUID()) + .toAfkTime()) + ); + + PlanPlaceholders.register("player_server_time_total", + player -> time.apply(SessionsMutator.forContainer(player) + .filterPlayedOnServer(serverInfo.getServerUUID()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_server_time_day", + player -> time.apply(SessionsMutator.forContainer(player) + .filterSessionsBetween(dayAgo(), now()) + .filterPlayedOnServer(serverInfo.getServerUUID()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_server_time_week", + player -> time.apply(SessionsMutator.forContainer(player) + .filterSessionsBetween(weekAgo(), now()) + .filterPlayedOnServer(serverInfo.getServerUUID()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_server_time_month", + player -> time.apply(SessionsMutator.forContainer(player) + .filterSessionsBetween(monthAgo(), now()) + .filterPlayedOnServer(serverInfo.getServerUUID()) + .toPlaytime()) + ); + + PlanPlaceholders.register("player_favorite_server", + player -> PerServerMutator.forContainer(player).favoriteServer() + .flatMap(serverUUID -> dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverUUID))) + .map(Server::getName) + .orElse("-") + ); + } +} 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 new file mode 100644 index 000000000..21f056642 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/ServerPlaceHolders.java @@ -0,0 +1,127 @@ +/* + * 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.placeholder; + +import com.djrapitops.plan.delivery.formatting.Formatter; +import com.djrapitops.plan.delivery.formatting.Formatters; +import com.djrapitops.plan.identification.ServerInfo; +import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.storage.database.Database; +import com.djrapitops.plan.storage.database.queries.objects.TPSQueries; + +import java.util.UUID; + +import static com.djrapitops.plan.utilities.MiscUtils.*; + +/** + * Placeholders about a servers. + * + * @author aidn5, Rsl1122 + */ +public class ServerPlaceHolders { + + public static void register( + DBSystem dbSystem, + ServerInfo serverInfo, + Formatters formatters + ) { + Formatter decimals = formatters.decimals(); + Formatter percentage = formatters.percentage(); + + Database database = dbSystem.getDatabase(); + UUID serverUUID = serverInfo.getServerUUID(); + + PlanPlaceholders.registerStatic("server_tps_day", + () -> decimals.apply(database.query(TPSQueries.averageTPS(dayAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("server_tps_week", + () -> decimals.apply(database.query(TPSQueries.averageTPS(weekAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("server_tps_month", + () -> decimals.apply(database.query(TPSQueries.averageTPS(monthAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("server_cpu_day", + () -> percentage.apply(database.query(TPSQueries.averageCPU(dayAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("server_cpu_week", + () -> percentage.apply(database.query(TPSQueries.averageCPU(weekAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("server_cpu_month", + () -> percentage.apply(database.query(TPSQueries.averageCPU(monthAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("server_ram_day", + () -> database.query(TPSQueries.averageRAM(dayAgo(), now(), serverUUID)) + " MB"); + + PlanPlaceholders.registerStatic("server_ram_week", + () -> database.query(TPSQueries.averageRAM(weekAgo(), now(), serverUUID)) + " MB"); + + PlanPlaceholders.registerStatic("server_ram_month", + () -> database.query(TPSQueries.averageRAM(monthAgo(), now(), serverUUID)) + " MB"); + + PlanPlaceholders.registerStatic("server_chunks_day", + () -> database.query(TPSQueries.averageChunks(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_chunks_week", + () -> database.query(TPSQueries.averageChunks(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_chunks_month", + () -> database.query(TPSQueries.averageChunks(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_entities_day", + () -> database.query(TPSQueries.averageEntities(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_entities_week", + () -> database.query(TPSQueries.averageEntities(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_entities_month", + () -> database.query(TPSQueries.averageEntities(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_max_free_disk_day", + () -> database.query(TPSQueries.maxFreeDisk(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_max_free_disk_week", + () -> database.query(TPSQueries.maxFreeDisk(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_max_free_disk_month", + () -> database.query(TPSQueries.maxFreeDisk(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_min_free_disk_day", + () -> database.query(TPSQueries.minFreeDisk(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_min_free_disk_week", + () -> database.query(TPSQueries.minFreeDisk(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_min_free_disk_month", + () -> database.query(TPSQueries.minFreeDisk(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_average_free_disk_day", + () -> database.query(TPSQueries.averageFreeDisk(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_average_free_disk_week", + () -> database.query(TPSQueries.averageFreeDisk(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_average_free_disk_month", + () -> database.query(TPSQueries.averageFreeDisk(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("server_name", + () -> serverInfo.getServer().getName()); + + PlanPlaceholders.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/SessionPlaceHolder.java new file mode 100644 index 000000000..bf276f2e2 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/SessionPlaceHolder.java @@ -0,0 +1,207 @@ +/* + * 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.placeholder; + +import com.djrapitops.plan.delivery.domain.DateHolder; +import com.djrapitops.plan.delivery.domain.DateObj; +import com.djrapitops.plan.delivery.formatting.Formatter; +import com.djrapitops.plan.delivery.formatting.Formatters; +import com.djrapitops.plan.identification.ServerInfo; +import com.djrapitops.plan.settings.config.PlanConfig; +import com.djrapitops.plan.storage.database.DBSystem; +import com.djrapitops.plan.storage.database.Database; +import com.djrapitops.plan.storage.database.queries.analysis.PlayerCountQueries; +import com.djrapitops.plan.storage.database.queries.objects.KillQueries; +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 java.io.Serializable; +import java.util.UUID; +import java.util.function.Supplier; + +import static com.djrapitops.plan.utilities.MiscUtils.*; + +/** + * Placeholders about a sessions. + * + * @author aidn5, Rsl1122 + */ +public class SessionPlaceHolder { + + public static void register( + PlanConfig config, + DBSystem dbSystem, + ServerInfo serverInfo, + Formatters formatters + ) { + int tzOffsetMs = config.getTimeZone().getOffset(System.currentTimeMillis()); + Formatter timeAmount = formatters.timeAmount(); + Formatter year = formatters.year(); + Formatter decimals = formatters.decimals(); + Database database = dbSystem.getDatabase(); + UUID serverUUID = serverInfo.getServerUUID(); + + PlanPlaceholders.registerStatic("sessions_play_time_total", + () -> timeAmount.apply(database.query(SessionQueries.playtime(0L, now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_play_time_day", + () -> timeAmount.apply(database.query(SessionQueries.playtime(dayAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_play_time_week", + () -> timeAmount.apply(database.query(SessionQueries.playtime(weekAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_play_time_month", + () -> timeAmount.apply(database.query(SessionQueries.playtime(monthAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_active_time_total", + () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(0L, now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_active_time_day", + () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(dayAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_active_time_week", + () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(weekAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_active_time_month", + () -> timeAmount.apply(database.query(SessionQueries.activePlaytime(monthAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_afk_time_total", + () -> timeAmount.apply(database.query(SessionQueries.afkTime(0L, now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_afk_time_day", + () -> timeAmount.apply(database.query(SessionQueries.afkTime(dayAgo(), now(), serverUUID)))); + + PlanPlaceholders.registerStatic("sessions_afk_time_week", + () -> timeAmount.apply(database.query(SessionQueries.afkTime(weekAgo(), now(), serverUUID)))); + + PlanPlaceholders.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); + + PlanPlaceholders.registerStatic("sessions_unique_players_day", + () -> database.query(PlayerCountQueries.uniquePlayerCount(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_unique_players_week", + () -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_unique_players_month", + () -> database.query(PlayerCountQueries.uniquePlayerCount(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_death_total", + () -> database.query(KillQueries.deathCount(0L, now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_death_day", + () -> database.query(KillQueries.deathCount(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_death_week", + () -> database.query(KillQueries.deathCount(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_death_month", + () -> database.query(KillQueries.deathCount(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_kill_total", + () -> database.query(KillQueries.playerKillCount(0L, now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_kill_day", + () -> database.query(KillQueries.playerKillCount(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_kill_week", + () -> database.query(KillQueries.playerKillCount(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_players_kill_month", + () -> database.query(KillQueries.playerKillCount(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_mob_kill_total", + () -> database.query(KillQueries.mobKillCount(0L, now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_mob_kill_day", + () -> database.query(KillQueries.mobKillCount(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_mob_kill_week", + () -> database.query(KillQueries.mobKillCount(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_mob_kill_month", + () -> database.query(KillQueries.mobKillCount(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_average_session_length_total", + () -> getPlaytime(database, 0L, now(), serverUUID, timeAmount)); + + PlanPlaceholders.registerStatic("sessions_average_session_length_day", + () -> getPlaytime(database, dayAgo(), now(), serverUUID, timeAmount)); + + PlanPlaceholders.registerStatic("sessions_average_session_length_week", + () -> getPlaytime(database, weekAgo(), now(), serverUUID, timeAmount)); + + PlanPlaceholders.registerStatic("sessions_average_session_length_month", + () -> getPlaytime(database, monthAgo(), now(), serverUUID, timeAmount)); + + PlanPlaceholders.registerStatic("sessions_average_unique_players_total", + () -> database.query(PlayerCountQueries.averageUniquePlayerCount(0L, now(), tzOffsetMs, serverUUID))); + + PlanPlaceholders.registerStatic("sessions_average_unique_players_day", + () -> database.query(PlayerCountQueries.averageUniquePlayerCount(dayAgo(), now(), tzOffsetMs, serverUUID))); + + PlanPlaceholders.registerStatic("sessions_average_unique_players_week", + () -> database.query(PlayerCountQueries.averageUniquePlayerCount(weekAgo(), now(), tzOffsetMs, serverUUID))); + + PlanPlaceholders.registerStatic("sessions_average_unique_players_month", + () -> database.query(PlayerCountQueries.averageUniquePlayerCount(monthAgo(), now(), tzOffsetMs, serverUUID))); + + PlanPlaceholders.registerStatic("sessions_new_players_day", + () -> database.query(PlayerCountQueries.newPlayerCount(dayAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_new_players_week", + () -> database.query(PlayerCountQueries.newPlayerCount(weekAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("sessions_new_players_month", + () -> database.query(PlayerCountQueries.newPlayerCount(monthAgo(), now(), serverUUID))); + + PlanPlaceholders.registerStatic("ping_total", + () -> decimals.apply(database.query(PingQueries.averagePing(0L, now(), serverUUID))) + " ms"); + + PlanPlaceholders.registerStatic("ping_day", + () -> decimals.apply(database.query(PingQueries.averagePing(dayAgo(), now(), serverUUID))) + " ms"); + + PlanPlaceholders.registerStatic("ping_week", + () -> decimals.apply(database.query(PingQueries.averagePing(weekAgo(), now(), serverUUID))) + " ms"); + + PlanPlaceholders.registerStatic("ping_month", + () -> decimals.apply(database.query(PingQueries.averagePing(monthAgo(), now(), serverUUID))) + " ms"); + + PlanPlaceholders.registerStatic("sessions_peak_count", + () -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(DateObj::getValue).orElse(0)); + + PlanPlaceholders.registerStatic("sessions_peak_date", + () -> database.query(TPSQueries.fetchAllTimePeakPlayerCount(serverUUID)).map(year).orElse("-")); + + PlanPlaceholders.registerStatic("sessions_recent_peak_count", + () -> database.query(TPSQueries.fetchPeakPlayerCount(serverUUID, dayAgo() * 2L)).map(DateObj::getValue).orElse(0)); + + PlanPlaceholders.registerStatic("sessions_recent_peak_date", + () -> database.query(TPSQueries.fetchPeakPlayerCount(serverUUID, dayAgo() * 2L)).map(year).orElse("-")); + } + + private static String getPlaytime(Database database, long after, long before, UUID serverUUID, Formatter timeAmount) { + Long playtime = database.query(SessionQueries.playtime(after, before, serverUUID)); + Long sessionCount = database.query(SessionQueries.sessionCount(after, before, serverUUID)); + return timeAmount.apply(sessionCount != 0 ? playtime / sessionCount : playtime); + } +} diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/WorldTimePlaceHolder.java b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolder.java similarity index 64% rename from Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/WorldTimePlaceHolder.java rename to Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolder.java index 30708de07..6333827f0 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/placeholders/WorldTimePlaceHolder.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/placeholder/WorldTimePlaceHolder.java @@ -14,7 +14,7 @@ * 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.placeholders; +package com.djrapitops.plan.placeholder; import com.djrapitops.plan.delivery.formatting.Formatter; import com.djrapitops.plan.delivery.formatting.Formatters; @@ -22,45 +22,42 @@ import com.djrapitops.plan.gathering.domain.WorldTimes; import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.storage.database.DBSystem; import com.djrapitops.plan.storage.database.queries.objects.WorldTimesQueries; -import org.apache.commons.lang.StringUtils; -import org.bukkit.entity.Player; /** * Placeholders about a world times. * * @author aidn5, Rsl1122 */ -public class WorldTimePlaceHolder extends AbstractPlanPlaceHolder { +public class WorldTimePlaceHolder { - private final DBSystem dbSystem; - private Formatter timeAmount; - - public WorldTimePlaceHolder( + public static void register( DBSystem dbSystem, ServerInfo serverInfo, Formatters formatters ) { - super(serverInfo); - this.dbSystem = dbSystem; - timeAmount = formatters.timeAmount(); - } + Formatter timeAmount = formatters.timeAmount(); - @Override - public String onPlaceholderRequest(Player p, String params) throws Exception { - String string = params.toLowerCase(); - - if (StringUtils.startsWith(string, "worlds_playtime_total_")) { + PlanPlaceholders.registerRaw("worlds_playtime_total_", (input, p) -> { // get world total play time // e.g. "plan_worlds_playtime_total_%worldname%" // where %worldname% is "world_nether" + String worldName = input.substring("worlds_playtime_total_".length()); - String worldName = StringUtils.removeStart(string, "worlds_playtime_total_"); - WorldTimes worldTimes = dbSystem.getDatabase().query(WorldTimesQueries.fetchServerTotalWorldTimes(serverUUID())); + WorldTimes worldTimes = dbSystem.getDatabase().query(WorldTimesQueries.fetchServerTotalWorldTimes(serverInfo.getServerUUID())); return timeAmount.apply(worldTimes.getWorldPlaytime(worldName)); - } + }); - return null; + PlanPlaceholders.registerStatic("worlds_playtime_total", params -> { + if (params.isEmpty()) { + return null; + } + + String worldName = params.get(0); + + WorldTimes worldTimes = dbSystem.getDatabase().query(WorldTimesQueries.fetchServerTotalWorldTimes(serverInfo.getServerUUID())); + + return timeAmount.apply(worldTimes.getWorldPlaytime(worldName)); + }); } - } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java index 062b9fe62..0df415e04 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/MiscUtils.java @@ -20,6 +20,8 @@ import com.djrapitops.plan.settings.Permissions; import com.djrapitops.plugin.command.CommandUtils; import com.djrapitops.plugin.command.Sender; +import java.util.concurrent.TimeUnit; + /** * Utility method class containing various static methods. * @@ -83,4 +85,20 @@ public class MiscUtils { } } } + + public static long now() { + return System.currentTimeMillis(); + } + + public static long dayAgo() { + return now() - TimeUnit.DAYS.toMillis(1L); + } + + public static long weekAgo() { + return now() - (TimeUnit.DAYS.toMillis(7L)); + } + + public static long monthAgo() { + return now() - (TimeUnit.DAYS.toMillis(30L)); + } } diff --git a/Plan/common/src/main/resources/nukkit.yml b/Plan/common/src/main/resources/nukkit.yml index febf91870..b5e992023 100644 --- a/Plan/common/src/main/resources/nukkit.yml +++ b/Plan/common/src/main/resources/nukkit.yml @@ -3,6 +3,7 @@ author: Rsl1122 main: com.djrapitops.plan.PlanNukkit version: "@version@" api: ["1.0.5"] +softdepend: ["PlaceholderAPI"] commands: plan: diff --git a/Plan/nukkit/build.gradle b/Plan/nukkit/build.gradle index ec5f43e79..8b026cd29 100644 --- a/Plan/nukkit/build.gradle +++ b/Plan/nukkit/build.gradle @@ -4,6 +4,7 @@ dependencies { compileOnly "com.djrapitops:AbstractPluginFramework-api:$abstractPluginFrameworkVersion" compileOnly "cn.nukkit:nukkit:$nukkitVersion" + compileOnly "com.creeperface.nukkit.placeholderapi:PlaceholderAPI:$nkPlaceholderapiVersion" testCompile "cn.nukkit:nukkit:$nukkitVersion" testCompile project(path: ":common", configuration: 'testArtifacts') 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 f9995850a..15f6f26c4 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/PlanNukkit.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan; +import com.djrapitops.plan.addons.placeholderapi.NKPlaceholderRegistrar; import com.djrapitops.plan.commands.PlanCommand; import com.djrapitops.plan.exceptions.EnableException; import com.djrapitops.plan.gathering.ServerShutdownSave; @@ -25,6 +26,7 @@ import com.djrapitops.plan.settings.theme.PlanColorScheme; import com.djrapitops.plugin.NukkitPlugin; import com.djrapitops.plugin.benchmarking.Benchmark; import com.djrapitops.plugin.command.ColorScheme; +import com.djrapitops.plugin.task.AbsRunnable; import java.util.logging.Level; import java.util.logging.Logger; @@ -50,6 +52,8 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { locale = system.getLocaleSystem().getLocale(); system.enable(); + registerPlaceholderAPI(); + logger.debug("Verbose debug messages are enabled."); String benchTime = " (" + timings.end("Enable").map(Benchmark::toDurationString).orElse("-") + ")"; logger.info(locale.getString(PluginLang.ENABLED) + benchTime); @@ -114,4 +118,19 @@ public class PlanNukkit extends NukkitPlugin implements PlanPlugin { public PlanSystem getSystem() { return system; } + + private void registerPlaceholderAPI() { + if (this.getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { + runnableFactory.create("Placeholders Registrar", new AbsRunnable() { + @Override + public void run() { + try { + NKPlaceholderRegistrar.register(system, errorHandler); + } catch (Exception | NoClassDefFoundError | NoSuchMethodError failed) { + logger.warn("Failed to register PlaceholderAPI placeholders: " + failed.toString()); + } + } + }).runTask(); + } + } } \ No newline at end of file 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/NKPlaceholderRegistrar.java new file mode 100644 index 000000000..3e5422c67 --- /dev/null +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/addons/placeholderapi/NKPlaceholderRegistrar.java @@ -0,0 +1,82 @@ +/* + * 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 cn.nukkit.Player; +import com.creeperface.nukkit.placeholderapi.api.PlaceholderAPI; +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.gathering.cache.SessionCache; +import com.djrapitops.plan.placeholder.PlanPlaceholders; +import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries; +import com.djrapitops.plugin.logging.L; +import com.djrapitops.plugin.logging.error.ErrorHandler; + +import java.util.ArrayList; +import java.util.UUID; + +public class NKPlaceholderRegistrar { + + private final ErrorHandler errorHandler; + private final PlanSystem system; + + public NKPlaceholderRegistrar(PlanSystem system, ErrorHandler errorHandler) { + this.system = system; + this.errorHandler = errorHandler; + } + + private void register0() { + PlanPlaceholders.init(system); + + PlaceholderAPI api = PlaceholderAPI.getInstance(); + PlanPlaceholders.getPlaceholders().forEach((name, loader) -> + api.visitorSensitivePlaceholder(name, (player, params) -> { + try { + return loader.apply(getPlayer(player), new ArrayList<>(params.getAll().values())); + } catch (Exception e) { + errorHandler.log(L.WARN, getClass(), e); + return null; + } + } + )); + + PlanPlaceholders.getStaticPlaceholders().forEach((name, loader) -> + api.staticPlaceholder(name, params -> { + try { + return loader.apply(new ArrayList<>(params.getAll().values())); + } catch (Exception e) { + errorHandler.log(L.WARN, getClass(), e); + return null; + } + } + )); + } + + 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)); + + return p; + } + + public static void register(PlanSystem system, ErrorHandler errorHandler) { + new NKPlaceholderRegistrar(system, errorHandler).register0(); + } +}