Added PlaceholderContext as a unified way to parse placeholders

This commit is contained in:
Auxilor
2023-04-25 18:57:06 +01:00
parent 6ec80d30ad
commit 40463213ac
18 changed files with 474 additions and 301 deletions

View File

@@ -25,8 +25,8 @@ import com.willfp.eco.core.gui.menu.MenuType;
import com.willfp.eco.core.gui.slot.SlotBuilder;
import com.willfp.eco.core.gui.slot.functional.SlotProvider;
import com.willfp.eco.core.items.TestableItem;
import com.willfp.eco.core.math.MathContext;
import com.willfp.eco.core.packet.Packet;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import com.willfp.eco.core.proxy.ProxyFactory;
import com.willfp.eco.core.scheduling.Scheduler;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
@@ -531,7 +531,7 @@ public interface Eco {
* @return The value of the expression, or zero if invalid.
*/
double evaluate(@NotNull String expression,
@NotNull MathContext context);
@NotNull PlaceholderContext context);
/**
* Get the menu a player currently has open.

View File

@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* Wrapper class for placeholder integrations.
* Wrapper class for arguments integrations.
*/
public interface PlaceholderIntegration extends Integration {
/**

View File

@@ -2,20 +2,14 @@ package com.willfp.eco.core.integrations.placeholder;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.placeholder.AdditionalPlayer;
import com.willfp.eco.core.placeholder.DynamicPlaceholder;
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
import com.willfp.eco.core.placeholder.Placeholder;
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
import com.willfp.eco.core.placeholder.PlayerDynamicPlaceholder;
import com.willfp.eco.core.placeholder.PlayerPlaceholder;
import com.willfp.eco.core.placeholder.PlayerStaticPlaceholder;
import com.willfp.eco.core.placeholder.PlayerlessPlaceholder;
import com.willfp.eco.core.placeholder.StaticPlaceholder;
import com.willfp.eco.core.placeholder.RegistrablePlaceholder;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import com.willfp.eco.util.StringUtils;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,6 +21,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@@ -34,7 +29,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Class to handle placeholder integrations.
* Class to handle arguments integrations.
*/
public final class PlaceholderManager {
/**
@@ -43,7 +38,7 @@ public final class PlaceholderManager {
private static final Map<EcoPlugin, Map<Pattern, Placeholder>> REGISTERED_PLACEHOLDERS = new HashMap<>();
/**
* All registered placeholder integrations.
* All registered arguments integrations.
*/
private static final Set<PlaceholderIntegration> REGISTERED_INTEGRATIONS = new HashSet<>();
@@ -54,27 +49,13 @@ public final class PlaceholderManager {
.expireAfterWrite(1, TimeUnit.SECONDS)
.build();
/**
* Placeholder Cache.
*/
private static final LoadingCache<EntryWithPlayer, String> PLACEHOLDER_CACHE = Caffeine.newBuilder()
.expireAfterWrite(50, TimeUnit.MILLISECONDS)
.build(key -> key.entry.getValue(key.player));
/**
* Dynamic Placeholder Cache.
*/
private static final LoadingCache<DynamicEntryWithPlayer, String> DYNAMIC_PLACEHOLDER_CACHE = Caffeine.newBuilder()
.expireAfterWrite(50, TimeUnit.MILLISECONDS)
.build(key -> key.entry.getValue(key.args, key.player));
/**
* The default PlaceholderAPI pattern; brought in for compatibility.
*/
private static final Pattern PATTERN = Pattern.compile("%([^% ]+)%");
/**
* Empty injectable object.
* Empty injectableContext object.
*/
public static final PlaceholderInjectable EMPTY_INJECTABLE = new PlaceholderInjectable() {
@Override
@@ -105,15 +86,26 @@ public final class PlaceholderManager {
}
/**
* Register a placeholder.
* Register a arguments.
*
* @param placeholder The placeholder to register.
* @param placeholder The arguments to register.
* @deprecated Use {@link #registerPlaceholder(RegistrablePlaceholder)} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
public static void registerPlaceholder(@NotNull final Placeholder placeholder) {
if (placeholder instanceof StaticPlaceholder || placeholder instanceof PlayerStaticPlaceholder) {
throw new IllegalArgumentException("Static placeholders cannot be registered!");
if (!(placeholder instanceof RegistrablePlaceholder)) {
throw new IllegalArgumentException("Placeholder must be RegistrablePlaceholder!");
}
registerPlaceholder((RegistrablePlaceholder) placeholder);
}
/**
* Register a arguments.
*
* @param placeholder The arguments to register.
*/
public static void registerPlaceholder(@NotNull final RegistrablePlaceholder placeholder) {
Map<Pattern, Placeholder> pluginPlaceholders = REGISTERED_PLACEHOLDERS
.getOrDefault(placeholder.getPlugin(), new HashMap<>());
@@ -127,75 +119,59 @@ public final class PlaceholderManager {
*
* @param player The player to get the result from.
* @param identifier The placeholder identifier.
* @return The value of the placeholder.
* @deprecated Specify a plugin to get the result from.
*/
@Deprecated(since = "6.52.2", forRemoval = true)
@SuppressWarnings("unused")
public static String getResult(@Nullable final Player player,
@NotNull final String identifier) {
throw new UnsupportedOperationException("Please specify a plugin to get the result from!");
}
/**
* Get the result of a placeholder with respect to a player.
*
* @param player The player to get the result from.
* @param identifier The placeholder identifier.
* @param plugin The plugin for the placeholder.
* @return The value of the placeholder.
* @param plugin The plugin for the arguments.
* @return The value of the arguments.
*/
@NotNull
public static String getResult(@Nullable final Player player,
@NotNull final String identifier,
@NotNull final EcoPlugin plugin) {
Validate.notNull(plugin, "Plugin cannot be null!");
return Objects.requireNonNullElse(
getResult(
plugin,
identifier,
new PlaceholderContext(
player,
null,
EMPTY_INJECTABLE,
Collections.emptyList()
)
),
""
);
}
/**
* Get the result of a placeholder given a plugin and arguments.
*
* @param plugin The plugin for the placeholder.
* @param args The arguments.
* @return The value of the arguments.
*/
@Nullable
public static String getResult(@NotNull final EcoPlugin plugin,
@NotNull final String args,
@NotNull final PlaceholderContext context) {
// This is really janky, and it sucks, but it works so?
// Compensating for regex being slow so that's why we get it.
Placeholder placeholder = PLACEHOLDER_LOOKUP_CACHE.get(
new PlaceholderLookup(identifier, plugin),
new PlaceholderLookup(args, plugin),
(it) -> {
// I hate the streams API.
return REGISTERED_PLACEHOLDERS
.getOrDefault(plugin, new HashMap<>())
.entrySet()
.stream().filter(entry -> entry.getKey().matcher(identifier).matches())
.stream().filter(entry -> entry.getKey().matcher(args).matches())
.map(Map.Entry::getValue)
.findFirst();
}
).orElse(null);
if (placeholder == null) {
return "";
return null;
}
/*
This code here is *really* not very good. It's mega externalized logic hacked
together and made worse by the addition of dynamic placeholders. But it works,
and it means I don't have to rewrite the whole placeholder system. So it's
good enough for me.
*/
if (placeholder instanceof PlayerPlaceholder playerPlaceholder) {
if (player == null) {
return "";
} else {
return PLACEHOLDER_CACHE.get(new EntryWithPlayer(playerPlaceholder, player));
}
} else if (placeholder instanceof PlayerlessPlaceholder playerlessPlaceholder) {
return playerlessPlaceholder.getValue();
} else if (placeholder instanceof PlayerDynamicPlaceholder playerDynamicPlaceholder) {
if (player == null) {
return "";
} else {
return DYNAMIC_PLACEHOLDER_CACHE.get(new DynamicEntryWithPlayer(playerDynamicPlaceholder, identifier, player));
}
} else if (placeholder instanceof DynamicPlaceholder dynamicPlaceholder) {
return dynamicPlaceholder.getValue(identifier);
} else {
return "";
}
return placeholder.getValue(args, context);
}
/**
@@ -204,7 +180,10 @@ public final class PlaceholderManager {
* @param text The text that may contain placeholders to translate.
* @param player The player to translate the placeholders with respect to.
* @return The text, translated.
* @deprecated Use {@link #translatePlaceholders(String, PlaceholderContext)} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public static String translatePlaceholders(@NotNull final String text,
@Nullable final Player player) {
return translatePlaceholders(text, player, EMPTY_INJECTABLE);
@@ -215,9 +194,12 @@ public final class PlaceholderManager {
*
* @param text The text that may contain placeholders to translate.
* @param player The player to translate the placeholders with respect to.
* @param context The injectable context.
* @param context The injectableContext parseContext.
* @return The text, translated.
* @deprecated Use {@link #translatePlaceholders(String, PlaceholderContext)} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public static String translatePlaceholders(@NotNull final String text,
@Nullable final Player player,
@NotNull final PlaceholderInjectable context) {
@@ -229,14 +211,38 @@ public final class PlaceholderManager {
*
* @param text The text that may contain placeholders to translate.
* @param player The player to translate the placeholders with respect to.
* @param context The injectable context.
* @param context The injectableContext parseContext.
* @param additionalPlayers Additional players to translate placeholders for.
* @return The text, translated.
* @deprecated Use {@link #translatePlaceholders(String, PlaceholderContext)} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public static String translatePlaceholders(@NotNull final String text,
@Nullable final Player player,
@NotNull final PlaceholderInjectable context,
@NotNull final Collection<AdditionalPlayer> additionalPlayers) {
return translatePlaceholders(
text,
new PlaceholderContext(
player,
null,
context,
additionalPlayers
)
);
}
/**
* Translate all placeholders in a translation context.
*
* @param text The text that may contain placeholders to translate.
* @param context The translation context.
* @return The text, translated.
*/
@NotNull
public static String translatePlaceholders(@NotNull final String text,
@NotNull final PlaceholderContext context) {
String processed = text;
/*
@@ -260,19 +266,15 @@ public final class PlaceholderManager {
*/
for (InjectablePlaceholder injection : context.getPlaceholderInjections()) {
if (injection instanceof StaticPlaceholder placeholder) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue());
} else if (injection instanceof PlayerStaticPlaceholder placeholder && player != null) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue(player));
}
for (InjectablePlaceholder injection : context.injectableContext().getPlaceholderInjections()) {
processed = injection.tryTranslateQuickly(processed, context);
}
// Prevent running 2 scans if there are no additional players.
if (!additionalPlayers.isEmpty()) {
if (!context.additionalPlayers().isEmpty()) {
List<String> found = findPlaceholdersIn(text);
for (AdditionalPlayer additionalPlayer : additionalPlayers) {
for (AdditionalPlayer additionalPlayer : context.additionalPlayers()) {
for (String placeholder : found) {
String prefix = "%" + additionalPlayer.getIdentifier() + "_";
@@ -281,7 +283,7 @@ public final class PlaceholderManager {
placeholder,
translatePlaceholders(
"%" + StringUtils.removePrefix(prefix, placeholder),
additionalPlayer.getPlayer()
context.copyWithPlayer(additionalPlayer.getPlayer())
)
);
}
@@ -291,19 +293,16 @@ public final class PlaceholderManager {
// Only run jank code if there are no integrations.
if (REGISTERED_INTEGRATIONS.isEmpty()) {
processed = setWithoutIntegration(processed, player);
processed = setWithoutIntegration(processed, context.player());
}
for (PlaceholderIntegration integration : REGISTERED_INTEGRATIONS) {
processed = integration.translate(processed, player);
processed = integration.translate(processed, context.player());
}
// DON'T REMOVE THIS, IT'S NOT DUPLICATE CODE.
for (InjectablePlaceholder injection : context.getPlaceholderInjections()) {
// Do I know this is a bad way of doing this? Yes.
if (injection instanceof PlayerStaticPlaceholder placeholder && player != null) {
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue(player));
}
for (InjectablePlaceholder injection : context.injectableContext().getPlaceholderInjections()) {
processed = injection.tryTranslateQuickly(processed, context);
}
return processed;
@@ -424,17 +423,6 @@ public final class PlaceholderManager {
}
private record EntryWithPlayer(@NotNull PlayerPlaceholder entry,
@NotNull Player player) {
}
private record DynamicEntryWithPlayer(@NotNull PlayerDynamicPlaceholder entry,
@NotNull String args,
@NotNull Player player) {
}
private PlaceholderManager() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.core.math;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.AdditionalPlayer;
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -11,9 +12,9 @@ import java.util.Collection;
import java.util.Collections;
/**
* Represents a context to do math in.
* Represents a parseContext to do math in.
*
* @param injectableContext The PlaceholderInjectable context.
* @param injectableContext The PlaceholderInjectable parseContext.
* @param player The player.
* @param additionalPlayers The additional players.
*/
@@ -23,7 +24,7 @@ public record MathContext(
@NotNull Collection<AdditionalPlayer> additionalPlayers
) {
/**
* Empty math context.
* Empty math parseContext.
*/
public static final MathContext EMPTY = new MathContext(
PlaceholderManager.EMPTY_INJECTABLE,
@@ -32,9 +33,9 @@ public record MathContext(
);
/**
* Create MathContext of a PlaceholderInjectable context.
* Create MathContext of a PlaceholderInjectable parseContext.
*
* @param injectableContext The PlaceholderInjectable context.
* @param injectableContext The PlaceholderInjectable parseContext.
* @return The MathContext.
*/
public static MathContext of(@NotNull final PlaceholderInjectable injectableContext) {
@@ -48,7 +49,7 @@ public record MathContext(
/**
* Copy a MathContext with a player.
*
* @param context The context.
* @param context The parseContext.
* @param player The player.
* @return The new MathContext.
*/
@@ -60,4 +61,18 @@ public record MathContext(
context.additionalPlayers()
);
}
/**
* Convert to PlaceholderContext.
*
* @return The PlaceholderContext.
*/
public PlaceholderContext toPlaceholderContext() {
return new PlaceholderContext(
this.player,
null,
this.injectableContext,
this.additionalPlayers
);
}
}

View File

@@ -1,7 +1,7 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -10,26 +10,26 @@ import java.util.function.Function;
import java.util.regex.Pattern;
/**
* A placeholder that does not require a player and supports dynamic styles.
* A arguments that does not require a player and supports dynamic styles.
*/
public final class DynamicPlaceholder implements Placeholder {
public final class DynamicPlaceholder implements RegistrablePlaceholder {
/**
* The placeholder pattern.
* The arguments pattern.
*/
private final Pattern pattern;
/**
* The function to retrieve the output of the placeholder.
* The function to retrieve the output of the arguments.
*/
private final Function<String, String> function;
private final Function<@NotNull String, @Nullable String> function;
/**
* The plugin for the placeholder.
* The plugin for the arguments.
*/
private final EcoPlugin plugin;
/**
* Create a new dynamic placeholder.
* Create a new dynamic arguments.
*
* @param plugin The plugin.
* @param pattern The pattern.
@@ -37,31 +37,33 @@ public final class DynamicPlaceholder implements Placeholder {
*/
public DynamicPlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final Pattern pattern,
@NotNull final Function<String, String> function) {
@NotNull final Function<@NotNull String, @Nullable String> function) {
this.plugin = plugin;
this.pattern = pattern;
this.function = function;
}
/**
* Get the value of the placeholder.
*
* @param args The args.
* @return The value.
*/
@NotNull
public String getValue(@NotNull final String args) {
@Override
@Nullable
public String getValue(@NotNull final String args,
@NotNull final PlaceholderContext context) {
return function.apply(args);
}
/**
* Register the placeholder.
* Get the value of the arguments.
*
* @return The placeholder.
* @param args The args.
* @return The value.
* @deprecated Use {@link #getValue(String, PlaceholderContext)} instead.
*/
public DynamicPlaceholder register() {
PlaceholderManager.registerPlaceholder(this);
return this;
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public String getValue(@NotNull final String args) {
return Objects.requireNonNullElse(
function.apply(args),
""
);
}
@Override
@@ -69,18 +71,17 @@ public final class DynamicPlaceholder implements Placeholder {
return this.plugin;
}
@Override
@Deprecated
public @NotNull String getIdentifier() {
return "dynamic";
}
@NotNull
@Override
public Pattern getPattern() {
return this.pattern;
}
@Override
public @NotNull DynamicPlaceholder register() {
return (DynamicPlaceholder) RegistrablePlaceholder.super.register();
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
@@ -97,6 +98,6 @@ public final class DynamicPlaceholder implements Placeholder {
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier(), this.getPlugin());
return Objects.hash(this.getPattern(), this.getPlugin());
}
}

View File

@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
/**
* Placeholders that can be injected into {@link PlaceholderInjectable} objects.
*/
public sealed interface InjectablePlaceholder extends Placeholder permits PlayerStaticPlaceholder, StaticPlaceholder {
public interface InjectablePlaceholder extends Placeholder {
@Override
default @NotNull EcoPlugin getPlugin() {
return Eco.get().getEcoPlugin();

View File

@@ -1,17 +1,18 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
/**
* A placeholder represents a string that can hold a value.
*/
public sealed interface Placeholder permits PlayerPlaceholder, PlayerlessPlaceholder,
DynamicPlaceholder, PlayerDynamicPlaceholder, InjectablePlaceholder {
public interface Placeholder {
/**
* Get the plugin that holds the placeholder.
* Get the plugin that holds the arguments.
*
* @return The plugin.
*/
@@ -19,20 +20,45 @@ public sealed interface Placeholder permits PlayerPlaceholder, PlayerlessPlaceho
EcoPlugin getPlugin();
/**
* Get the identifier for the placeholder.
* Get the value of the arguments.
*
* @return The identifier.
* @param args The args.
* @param context The context.
* @return The value.
*/
@NotNull
String getIdentifier();
@Nullable
String getValue(@NotNull String args,
@NotNull PlaceholderContext context);
/**
* Get the pattern for the placeholder.
* Get the pattern for the arguments.
*
* @return The pattern.
*/
@NotNull
default Pattern getPattern() {
return Pattern.compile(this.getIdentifier());
Pattern getPattern();
/**
* Try to translate all instances of this placeholder in text quickly.
*
* @param text The text to translate.
* @param context The context.
* @return The translated text.
*/
default String tryTranslateQuickly(@NotNull final String text,
@NotNull final PlaceholderContext context) {
return text;
}
/**
* Get the identifier for the arguments.
*
* @return The identifier.
* @deprecated Some arguments may not have an identifier. Use {@link #getPattern()} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
default String getIdentifier() {
return this.getPattern().pattern();
}
}

View File

@@ -9,7 +9,7 @@ import java.util.List;
*/
public interface PlaceholderInjectable {
/**
* Inject placeholder.
* Inject arguments.
*
* @param placeholders The placeholders.
*/
@@ -18,7 +18,7 @@ public interface PlaceholderInjectable {
}
/**
* Inject placeholder.
* Inject arguments.
*
* @param placeholders The placeholders.
*/

View File

@@ -1,7 +1,7 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -11,26 +11,26 @@ import java.util.function.BiFunction;
import java.util.regex.Pattern;
/**
* A placeholder that does not require a player and supports dynamic styles.
* A arguments that does not require a player and supports dynamic styles.
*/
public final class PlayerDynamicPlaceholder implements Placeholder {
public final class PlayerDynamicPlaceholder implements RegistrablePlaceholder {
/**
* The placeholder pattern.
* The arguments pattern.
*/
private final Pattern pattern;
/**
* The function to retrieve the output of the placeholder.
* The function to retrieve the output of the arguments.
*/
private final BiFunction<String, Player, String> function;
private final BiFunction<@NotNull String, @NotNull Player, @Nullable String> function;
/**
* The plugin for the placeholder.
* The plugin for the arguments.
*/
private final EcoPlugin plugin;
/**
* Create a new dynamic placeholder.
* Create a new dynamic arguments.
*
* @param plugin The plugin.
* @param pattern The pattern.
@@ -38,33 +38,40 @@ public final class PlayerDynamicPlaceholder implements Placeholder {
*/
public PlayerDynamicPlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final Pattern pattern,
@NotNull final BiFunction<String, Player, String> function) {
@NotNull final BiFunction<@NotNull String, @NotNull Player, @Nullable String> function) {
this.plugin = plugin;
this.pattern = pattern;
this.function = function;
}
/**
* Get the value of the placeholder.
*
* @param args The args.
* @param player The player.
* @return The value.
*/
@NotNull
public String getValue(@NotNull final String args,
@NotNull final Player player) {
@Override
public @Nullable String getValue(@NotNull final String args,
@NotNull final PlaceholderContext context) {
Player player = context.player();
if (player == null) {
return null;
}
return function.apply(args, player);
}
/**
* Register the placeholder.
* Get the value of the arguments.
*
* @return The placeholder.
* @param args The args.
* @param player The player.
* @return The value.
* @deprecated Use {@link #getValue(String, PlaceholderContext)} instead.
*/
public PlayerDynamicPlaceholder register() {
PlaceholderManager.registerPlaceholder(this);
return this;
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public String getValue(@NotNull final String args,
@NotNull final Player player) {
return Objects.requireNonNullElse(
function.apply(args, player),
""
);
}
@Override
@@ -72,18 +79,17 @@ public final class PlayerDynamicPlaceholder implements Placeholder {
return this.plugin;
}
@Override
@Deprecated
public @NotNull String getIdentifier() {
return "dynamic";
}
@NotNull
@Override
public Pattern getPattern() {
return this.pattern;
}
@Override
public @NotNull PlayerDynamicPlaceholder register() {
return (PlayerDynamicPlaceholder) RegistrablePlaceholder.super.register();
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
@@ -100,6 +106,6 @@ public final class PlayerDynamicPlaceholder implements Placeholder {
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier(), this.getPlugin());
return Objects.hash(this.getPattern(), this.getPlugin());
}
}

View File

@@ -1,7 +1,7 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -11,31 +11,31 @@ import java.util.function.Function;
import java.util.regex.Pattern;
/**
* A placeholder that requires a player.
* A arguments that requires a player.
*/
public final class PlayerPlaceholder implements Placeholder {
public final class PlayerPlaceholder implements RegistrablePlaceholder {
/**
* The name of the placeholder.
* The name of the arguments.
*/
private final String identifier;
/**
* The placeholder pattern.
* The arguments pattern.
*/
private final Pattern pattern;
/**
* The function to retrieve the output of the placeholder given a player.
* The function to retrieve the output of the arguments given a player.
*/
private final Function<Player, String> function;
private final Function<@NotNull Player, @Nullable String> function;
/**
* The plugin for the placeholder.
* The plugin for the arguments.
*/
private final EcoPlugin plugin;
/**
* Create a new player placeholder.
* Create a new player arguments.
*
* @param plugin The plugin.
* @param identifier The identifier.
@@ -43,32 +43,39 @@ public final class PlayerPlaceholder implements Placeholder {
*/
public PlayerPlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final String identifier,
@NotNull final Function<Player, String> function) {
@NotNull final Function<@NotNull Player, @Nullable String> function) {
this.plugin = plugin;
this.identifier = identifier;
this.pattern = Pattern.compile(identifier);
this.function = function;
}
/**
* Get the value of the placeholder for a given player.
*
* @param player The player.
* @return The value.
*/
@NotNull
public String getValue(@NotNull final Player player) {
@Override
public @Nullable String getValue(@NotNull final String args,
@NotNull final PlaceholderContext context) {
Player player = context.player();
if (player == null) {
return null;
}
return function.apply(player);
}
/**
* Register the placeholder.
* Get the value of the arguments for a given player.
*
* @return The placeholder.
* @param player The player.
* @return The value.
* @deprecated Use {@link #getValue(String, PlaceholderContext)} instead.
*/
public PlayerPlaceholder register() {
PlaceholderManager.registerPlaceholder(this);
return this;
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public String getValue(@NotNull final Player player) {
return Objects.requireNonNullElse(
function.apply(player),
""
);
}
@Override
@@ -76,17 +83,17 @@ public final class PlayerPlaceholder implements Placeholder {
return this.plugin;
}
@Override
public @NotNull String getIdentifier() {
return this.identifier;
}
@NotNull
@Override
public Pattern getPattern() {
return this.pattern;
}
@Override
public @NotNull PlayerPlaceholder register() {
return (PlayerPlaceholder) RegistrablePlaceholder.super.register();
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
@@ -95,12 +102,12 @@ public final class PlayerPlaceholder implements Placeholder {
if (!(o instanceof PlayerPlaceholder that)) {
return false;
}
return Objects.equals(this.getIdentifier(), that.getIdentifier())
return Objects.equals(this.getPattern(), that.getPattern())
&& Objects.equals(this.getPlugin(), that.getPlugin());
}
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier(), this.getPlugin());
return Objects.hash(this.getPattern(), this.getPlugin());
}
}

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -13,47 +14,71 @@ import java.util.regex.Pattern;
*/
public final class PlayerStaticPlaceholder implements InjectablePlaceholder {
/**
* The name of the placeholder.
* The identifier.
*/
private final String identifier;
/**
* The placeholder pattern.
* The arguments pattern.
*/
private final Pattern pattern;
/**
* The function to retrieve the output of the placeholder.
* The function to retrieve the output of the arguments.
*/
private final Function<Player, String> function;
private final Function<@NotNull Player, @Nullable String> function;
/**
* Create a new player placeholder.
* Create a new player arguments.
*
* @param identifier The identifier.
* @param function The function to retrieve the value.
*/
public PlayerStaticPlaceholder(@NotNull final String identifier,
@NotNull final Function<Player, String> function) {
@NotNull final Function<@NotNull Player, @Nullable String> function) {
this.identifier = identifier;
this.pattern = Pattern.compile(identifier);
this.function = function;
}
@Override
public @Nullable String getValue(@NotNull final String args,
@NotNull final PlaceholderContext context) {
Player player = context.player();
if (player == null) {
return null;
}
return this.getValue(player);
}
/**
* Get the value of the placeholder.
* Get the value of the arguments.
*
* @param player The player.
* @return The value.
* @deprecated Use {@link #getValue(String, PlaceholderContext)} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public String getValue(@NotNull final Player player) {
return function.apply(player);
return Objects.requireNonNullElse(
function.apply(player),
""
);
}
@Override
public @NotNull String getIdentifier() {
return this.identifier;
public String tryTranslateQuickly(@NotNull final String text,
@NotNull final PlaceholderContext context) {
return text.replace(
"%" + this.identifier + "%",
Objects.requireNonNullElse(
this.getValue(identifier, context),
""
)
);
}
@NotNull
@@ -70,11 +95,11 @@ public final class PlayerStaticPlaceholder implements InjectablePlaceholder {
if (!(o instanceof PlayerStaticPlaceholder that)) {
return false;
}
return Objects.equals(this.getIdentifier(), that.getIdentifier());
return Objects.equals(this.getPattern(), that.getPattern());
}
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier());
return Objects.hash(this.getPattern());
}
}

View File

@@ -1,7 +1,7 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -10,31 +10,26 @@ import java.util.function.Supplier;
import java.util.regex.Pattern;
/**
* A placeholder that does not require a player.
* A arguments that does not require a player.
*/
public final class PlayerlessPlaceholder implements Placeholder {
public final class PlayerlessPlaceholder implements RegistrablePlaceholder {
/**
* The placeholder identifier.
*/
private final String identifier;
/**
* The placeholder pattern.
* The arguments pattern.
*/
private final Pattern pattern;
/**
* The function to retrieve the output of the placeholder.
* The function to retrieve the output of the arguments.
*/
private final Supplier<String> function;
private final Supplier<@Nullable String> function;
/**
* The plugin for the placeholder.
* The plugin for the arguments.
*/
private final EcoPlugin plugin;
/**
* Create a new player placeholder.
* Create a new player arguments.
*
* @param plugin The plugin.
* @param identifier The identifier.
@@ -42,30 +37,31 @@ public final class PlayerlessPlaceholder implements Placeholder {
*/
public PlayerlessPlaceholder(@NotNull final EcoPlugin plugin,
@NotNull final String identifier,
@NotNull final Supplier<String> function) {
@NotNull final Supplier<@Nullable String> function) {
this.plugin = plugin;
this.identifier = identifier;
this.pattern = Pattern.compile(identifier);
this.function = function;
}
/**
* Get the value of the placeholder.
*
* @return The value.
*/
public String getValue() {
@Override
public @Nullable String getValue(@NotNull final String args,
@NotNull final PlaceholderContext context) {
return function.get();
}
/**
* Register the placeholder.
* Get the value of the arguments.
*
* @return The placeholder.
* @return The value.
* @deprecated Use {@link #getValue(String, PlaceholderContext)} instead.
*/
public PlayerlessPlaceholder register() {
PlaceholderManager.registerPlaceholder(this);
return this;
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public String getValue() {
return Objects.requireNonNullElse(
this.function.get(),
""
);
}
@Override
@@ -73,17 +69,17 @@ public final class PlayerlessPlaceholder implements Placeholder {
return this.plugin;
}
@Override
public @NotNull String getIdentifier() {
return this.identifier;
}
@NotNull
@Override
public Pattern getPattern() {
return this.pattern;
}
@Override
public @NotNull PlayerlessPlaceholder register() {
return (PlayerlessPlaceholder) RegistrablePlaceholder.super.register();
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
@@ -92,12 +88,12 @@ public final class PlayerlessPlaceholder implements Placeholder {
if (!(o instanceof PlayerlessPlaceholder that)) {
return false;
}
return Objects.equals(this.getIdentifier(), that.getIdentifier())
return Objects.equals(this.getPattern(), that.getPattern())
&& Objects.equals(this.getPlugin(), that.getPlugin());
}
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier(), this.getPlugin());
return Objects.hash(this.getPattern(), this.getPlugin());
}
}

View File

@@ -0,0 +1,18 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import org.jetbrains.annotations.NotNull;
public interface RegistrablePlaceholder extends Placeholder {
/**
* Register the arguments.
*
* @return The arguments.
*/
@NotNull
default RegistrablePlaceholder register() {
PlaceholderManager.registerPlaceholder(this);
return this;
}
}

View File

@@ -1,5 +1,6 @@
package com.willfp.eco.core.placeholder;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -8,50 +9,62 @@ import java.util.function.Supplier;
import java.util.regex.Pattern;
/**
* A placeholder that cannot be registered, and exists purely in injection.
* A arguments that cannot be registered, and exists purely in injection.
*/
public final class StaticPlaceholder implements InjectablePlaceholder {
/**
* The name of the placeholder.
* The name of the arguments.
*/
private final String identifier;
/**
* The placeholder pattern.
* The arguments pattern.
*/
private final Pattern pattern;
/**
* The function to retrieve the output of the placeholder.
* The function to retrieve the output of the arguments.
*/
private final Supplier<String> function;
private final Supplier<@Nullable String> function;
/**
* Create a new player placeholder.
* Create a new player arguments.
*
* @param identifier The identifier.
* @param function The function to retrieve the value.
*/
public StaticPlaceholder(@NotNull final String identifier,
@NotNull final Supplier<String> function) {
@NotNull final Supplier<@Nullable String> function) {
this.identifier = identifier;
this.pattern = Pattern.compile(identifier);
this.function = function;
}
/**
* Get the value of the placeholder.
*
* @return The value.
*/
@NotNull
public String getValue() {
@Override
public @Nullable String getValue(@NotNull final String args,
@NotNull final PlaceholderContext context) {
return function.get();
}
/**
* Get the value of the arguments.
*
* @return The value.
* @deprecated Use {@link #getValue(String, PlaceholderContext)} instead.
*/
@Deprecated(since = "6.56.0", forRemoval = true)
@NotNull
public String getValue() {
return Objects.requireNonNullElse(
function.get(),
""
);
}
@Override
public @NotNull String getIdentifier() {
return this.identifier;
public String tryTranslateQuickly(@NotNull final String text,
@NotNull final PlaceholderContext context) {
return text.replace("%" + this.identifier + "%", this.getValue(this.identifier, context));
}
@NotNull
@@ -68,11 +81,11 @@ public final class StaticPlaceholder implements InjectablePlaceholder {
if (!(o instanceof StaticPlaceholder that)) {
return false;
}
return Objects.equals(this.getIdentifier(), that.getIdentifier());
return Objects.equals(this.getPattern(), that.getPattern());
}
@Override
public int hashCode() {
return Objects.hash(this.getIdentifier());
return Objects.hash(this.getPattern());
}
}

View File

@@ -0,0 +1,67 @@
package com.willfp.eco.core.placeholder.parsing;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.AdditionalPlayer;
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
/**
* Represents a context to translate placeholders in.
*
* @param player The player.
* @param itemStack The ItemStack.
* @param injectableContext The injectable context.
* @param additionalPlayers The additional players.
*/
public record PlaceholderContext(
@Nullable Player player,
@Nullable ItemStack itemStack,
@NotNull PlaceholderInjectable injectableContext,
@NotNull Collection<AdditionalPlayer> additionalPlayers
) {
/**
* An empty context.
*/
public static final PlaceholderContext EMPTY = new PlaceholderContext(
null,
null,
PlaceholderManager.EMPTY_INJECTABLE,
Collections.emptyList()
);
/**
* Create MathContext of a PlaceholderInjectable parseContext.
*
* @param injectableContext The PlaceholderInjectable parseContext.
* @return The MathContext.
*/
public static PlaceholderContext of(@NotNull final PlaceholderInjectable injectableContext) {
return new PlaceholderContext(
null,
null,
injectableContext,
Collections.emptyList()
);
}
/**
* Copy with a player.
*
* @param player The player.
* @return The new MathContext.
*/
public PlaceholderContext copyWithPlayer(@Nullable final Player player) {
return new PlaceholderContext(
player,
this.itemStack(),
this.injectableContext(),
this.additionalPlayers()
);
}
}

View File

@@ -5,6 +5,7 @@ import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.math.MathContext;
import com.willfp.eco.core.placeholder.AdditionalPlayer;
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
import com.willfp.eco.core.placeholder.parsing.PlaceholderContext;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -210,7 +211,7 @@ public final class NumberUtils {
* @return The value of the expression, or zero if invalid.
*/
public static double evaluateExpression(@NotNull final String expression) {
return evaluateExpression(expression, MathContext.EMPTY);
return evaluateExpression(expression, PlaceholderContext.EMPTY);
}
/**
@@ -230,7 +231,7 @@ public final class NumberUtils {
*
* @param expression The expression.
* @param player The player.
* @param context The injectable placeholders.
* @param context The injectableContext placeholders.
* @return The value of the expression, or zero if invalid.
*/
public static double evaluateExpression(@NotNull final String expression,
@@ -244,7 +245,7 @@ public final class NumberUtils {
*
* @param expression The expression.
* @param player The player.
* @param context The injectable placeholders.
* @param context The injectableContext placeholders.
* @param additionalPlayers Additional players to parse placeholders for.
* @return The value of the expression, or zero if invalid.
*/
@@ -252,22 +253,35 @@ public final class NumberUtils {
@Nullable final Player player,
@NotNull final PlaceholderInjectable context,
@NotNull final Collection<AdditionalPlayer> additionalPlayers) {
return Eco.get().evaluate(expression, new MathContext(
context,
return Eco.get().evaluate(expression, new PlaceholderContext(
player,
null,
context,
additionalPlayers
));
}
/**
* Evaluate an expression with respect to a player (for placeholders).
* Evaluate an expression in a context.
*
* @param expression The expression.
* @param context The math context.
* @param context The context.
* @return The value of the expression, or zero if invalid.
*/
public static double evaluateExpression(@NotNull final String expression,
@NotNull final MathContext context) {
return evaluateExpression(expression, context.toPlaceholderContext());
}
/**
* Evaluate an expression in a context.
*
* @param expression The expression.
* @param context The context.
* @return The value of the expression, or zero if invalid.
*/
public static double evaluateExpression(@NotNull final String expression,
@NotNull final PlaceholderContext context) {
return Eco.get().evaluate(expression, context);
}