Refactor placeholders and add support for nukkit PlaceholderAPI (#1404)

by Creeperface01
This commit is contained in:
Creeperface01 2020-04-23 09:35:15 +02:00 committed by Risto Lahtela
parent 4d09187a66
commit 0066813c28
18 changed files with 824 additions and 654 deletions

View File

@ -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()

View File

@ -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<AbstractPlanPlaceHolder> 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);
}

View File

@ -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();
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Double> decimals;
private Formatter<Long> year;
private Formatter<Long> 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;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Double> decimals;
private Formatter<Double> 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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Double> decimals;
private Formatter<Long> timeAmount;
private int tzOffsetMs;
private Formatter<DateHolder> 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);
}
}

View File

@ -14,33 +14,22 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
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()))
);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, BiFunction<PlayerContainer, List<String>, Serializable>> placeholders = new HashMap<>();
private static final Map<String, Function<List<String>, Serializable>> staticPlaceholders = new HashMap<>();
private static final Map<String, BiFunction<String, PlayerContainer, Serializable>> 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<Serializable> loader) {
registerStatic(name, params -> loader.get());
}
public static void registerStatic(String name, Function<List<String>, Serializable> loader) {
staticPlaceholders.put(name, loader);
}
public static void register(String name, Function<PlayerContainer, Serializable> loader) {
register(name, (player, params) -> loader.apply(player));
}
public static void register(String name, BiFunction<PlayerContainer, List<String>, Serializable> loader) {
placeholders.put(name, loader);
}
public static void registerRaw(String name, BiFunction<String, PlayerContainer, Serializable> loader) {
rawHandlers.put(name, loader);
}
public static Map<String, BiFunction<PlayerContainer, List<String>, Serializable>> getPlaceholders() {
return placeholders;
}
public static Map<String, Function<List<String>, Serializable>> getStaticPlaceholders() {
return staticPlaceholders;
}
public static String onPlaceholderRequest(UUID uuid, String placeholder, List<String> 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<String> parameters) {
for (Entry<String, BiFunction<String, PlayerContainer, Serializable>> entry : rawHandlers.entrySet()) {
if (placeholder.startsWith(entry.getKey())) {
return Objects.toString(entry.getValue().apply(placeholder, player));
}
}
Function<List<String>, Serializable> staticLoader = staticPlaceholders.get(placeholder);
if (staticLoader != null) {
return Objects.toString(staticLoader.apply(parameters));
}
if (player != null) {
BiFunction<PlayerContainer, List<String>, Serializable> loader = placeholders.get(placeholder);
if (loader != null) {
return Objects.toString(loader.apply(player, parameters));
}
}
return null;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Double> decimals = formatters.decimals();
Formatter<Long> year = formatters.yearLong();
Formatter<Long> 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("-")
);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Double> decimals = formatters.decimals();
Formatter<Double> 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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Long> timeAmount = formatters.timeAmount();
Formatter<DateHolder> year = formatters.year();
Formatter<Double> 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<Serializable> 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<Long> 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);
}
}

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
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<Long> timeAmount;
public WorldTimePlaceHolder(
public static void register(
DBSystem dbSystem,
ServerInfo serverInfo,
Formatters formatters
) {
super(serverInfo);
this.dbSystem = dbSystem;
timeAmount = formatters.timeAmount();
}
Formatter<Long> 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));
});
}
}

View File

@ -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));
}
}

View File

@ -3,6 +3,7 @@ author: Rsl1122
main: com.djrapitops.plan.PlanNukkit
version: "@version@"
api: ["1.0.5"]
softdepend: ["PlaceholderAPI"]
commands:
plan:

View File

@ -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')

View File

@ -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();
}
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}