Add support for :player at the end of player_ placeholders.

- Supports UUID
- Supports player name
- Will use issuing player if not found due to implementation limitations

Affects issues:
- Close #2130
This commit is contained in:
Aurora Lahtela 2022-05-28 08:52:55 +03:00
parent 79ff7d599f
commit 253cfd251b
4 changed files with 33 additions and 26 deletions

View File

@ -21,7 +21,7 @@ import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import org.jetbrains.annotations.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -39,13 +39,11 @@ public class Identifiers {
protected final DBSystem dbSystem;
private final UUIDUtility uuidUtility;
private final ErrorLogger errorLogger;
@Inject
public Identifiers(DBSystem dbSystem, UUIDUtility uuidUtility, ErrorLogger errorLogger) {
public Identifiers(DBSystem dbSystem, UUIDUtility uuidUtility) {
this.dbSystem = dbSystem;
this.uuidUtility = uuidUtility;
this.errorLogger = errorLogger;
}
/**
@ -104,6 +102,7 @@ public class Identifiers {
.orElseThrow(() -> new BadRequestException("Given 'player' was not found in the database."));
}
@Nullable
public UUID getPlayerUUID(String name) {
return uuidUtility.getUUIDOf(name);
}

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import net.playeranalytics.plugin.player.UUIDFetcher;
import org.jetbrains.annotations.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -76,6 +77,7 @@ public class UUIDUtility {
* @param playerName Player's name
* @return UUID of the player
*/
@Nullable
public UUID getUUIDOf(String playerName) {
if (playerName == null) throw new IllegalArgumentException("Player name can not be null!");
UUID uuid = getUUIDFromString(playerName);

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.placeholder;
import com.djrapitops.plan.commands.use.Arguments;
import com.djrapitops.plan.delivery.domain.container.PlayerContainer;
import com.djrapitops.plan.identification.Identifiers;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries;
@ -45,16 +46,19 @@ public final class PlanPlaceholders {
private final Map<String, PlayerPlaceholderLoader> playerPlaceholders;
private final Map<String, StaticPlaceholderLoader> staticPlaceholders;
private final Map<String, BiFunction<String, PlayerContainer, Serializable>> rawHandlers;
private final Map<String, Function<String, Serializable>> rawHandlers;
private final DBSystem dbSystem;
private final Identifiers identifiers;
@Inject
public PlanPlaceholders(
DBSystem dbSystem,
Set<Placeholders> placeholderRegistries
Set<Placeholders> placeholderRegistries,
Identifiers identifiers
) {
this.dbSystem = dbSystem;
this.identifiers = identifiers;
this.playerPlaceholders = new HashMap<>();
this.staticPlaceholders = new HashMap<>();
@ -81,7 +85,7 @@ public final class PlanPlaceholders {
playerPlaceholders.put(name, loader);
}
public void registerRaw(String name, BiFunction<String, PlayerContainer, Serializable> loader) {
public void registerRaw(String name, Function<String, Serializable> loader) {
rawHandlers.put(name, loader);
}
@ -93,38 +97,36 @@ public final class PlanPlaceholders {
return staticPlaceholders;
}
public String onPlaceholderRequest(UUID uuid, String placeholder, List<String> parameters) {
PlayerContainer player;
if (uuid != null) {
player = dbSystem.getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(uuid));
} 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 uuid 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}
*/
public String onPlaceholderRequest(PlayerContainer player, String placeholder, List<String> parameters) {
for (Entry<String, BiFunction<String, PlayerContainer, Serializable>> entry : rawHandlers.entrySet()) {
public String onPlaceholderRequest(UUID uuid, String placeholder, List<String> parameters) {
for (Entry<String, Function<String, Serializable>> entry : rawHandlers.entrySet()) {
if (placeholder.startsWith(entry.getKey())) {
return Objects.toString(entry.getValue().apply(placeholder, player));
return Objects.toString(entry.getValue().apply(placeholder));
}
}
Arguments arguments = new Arguments(parameters);
StaticPlaceholderLoader staticLoader = staticPlaceholders.get(placeholder);
if (staticLoader != null) {
return Objects.toString(staticLoader.apply(new Arguments(parameters)));
return Objects.toString(staticLoader.apply(arguments));
}
UUID playerUUID = arguments.get(0).flatMap(this::getPlayerUUIDForIdentifier).orElse(uuid);
PlayerContainer player;
if (uuid != null) {
player = dbSystem.getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
} else {
player = null;
}
PlayerPlaceholderLoader loader = playerPlaceholders.get(placeholder);
@ -135,6 +137,10 @@ public final class PlanPlaceholders {
return null;
}
private Optional<UUID> getPlayerUUIDForIdentifier(String identifier) {
return Optional.ofNullable(identifiers.getPlayerUUID(identifier));
}
public interface PlayerPlaceholderLoader extends BiFunction<PlayerContainer, List<String>, Serializable> {}
public interface StaticPlaceholderLoader extends Function<Arguments, Serializable> {}

View File

@ -52,11 +52,11 @@ public class WorldTimePlaceHolders implements Placeholders {
public void register(
PlanPlaceholders placeholders
) {
placeholders.registerRaw("worlds_playtime_total_", (input, p) -> {
placeholders.registerRaw("worlds_playtime_total_", placeholder -> {
// 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 = placeholder.substring("worlds_playtime_total_".length());
return getWorldPlaytime(worldName);
});