Updated placeholder injection
This commit is contained in:
@@ -3,8 +3,8 @@ package com.willfp.eco.core.config.interfaces;
|
||||
import com.willfp.eco.core.config.BuildableConfig;
|
||||
import com.willfp.eco.core.config.ConfigType;
|
||||
import com.willfp.eco.core.config.TransientConfig;
|
||||
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
|
||||
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
|
||||
import com.willfp.eco.core.placeholder.StaticPlaceholder;
|
||||
import com.willfp.eco.util.NumberUtils;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@@ -471,7 +471,7 @@ public interface Config extends Cloneable, PlaceholderInjectable {
|
||||
*/
|
||||
default double getDoubleFromExpression(@NotNull String path,
|
||||
@Nullable Player player) {
|
||||
return NumberUtils.evaluateExpression(this.getString(path), player, this.getInjectedPlaceholders());
|
||||
return NumberUtils.evaluateExpression(this.getString(path), player, this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -539,12 +539,12 @@ public interface Config extends Cloneable, PlaceholderInjectable {
|
||||
Config clone();
|
||||
|
||||
@Override
|
||||
default void injectPlaceholders(@NotNull Iterable<StaticPlaceholder> placeholders) {
|
||||
default void addInjectablePlaceholder(@NotNull Iterable<InjectablePlaceholder> placeholders) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
default List<StaticPlaceholder> getInjectedPlaceholders() {
|
||||
default @NotNull List<InjectablePlaceholder> getPlaceholderInjections() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.willfp.eco.core.config.wrapper;
|
||||
|
||||
import com.willfp.eco.core.config.ConfigType;
|
||||
import com.willfp.eco.core.config.interfaces.Config;
|
||||
import com.willfp.eco.core.placeholder.StaticPlaceholder;
|
||||
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
|
||||
import com.willfp.eco.util.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -145,18 +145,13 @@ public abstract class ConfigWrapper<T extends Config> implements Config {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectPlaceholders(@NotNull final StaticPlaceholder... placeholders) {
|
||||
handle.injectPlaceholders(placeholders);
|
||||
public void addInjectablePlaceholder(@NotNull final Iterable<InjectablePlaceholder> placeholders) {
|
||||
handle.addInjectablePlaceholder(placeholders);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void injectPlaceholders(@NotNull final Iterable<StaticPlaceholder> placeholders) {
|
||||
handle.injectPlaceholders(placeholders);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StaticPlaceholder> getInjectedPlaceholders() {
|
||||
return handle.getInjectedPlaceholders();
|
||||
public @NotNull List<InjectablePlaceholder> getPlaceholderInjections() {
|
||||
return handle.getPlaceholderInjections();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,8 +4,11 @@ import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
import com.willfp.eco.core.Eco;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
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.PlayerPlaceholder;
|
||||
import com.willfp.eco.core.placeholder.PlayerStaticPlaceholder;
|
||||
import com.willfp.eco.core.placeholder.PlayerlessPlaceholder;
|
||||
import com.willfp.eco.core.placeholder.StaticPlaceholder;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -154,17 +157,47 @@ public final class PlaceholderManager {
|
||||
* @param player The player to translate the placeholders with respect to.
|
||||
* @param statics Extra static placeholders.
|
||||
* @return The text, translated.
|
||||
* @deprecated Use new static system.
|
||||
*/
|
||||
@Deprecated(since = "6.35.0", forRemoval = true)
|
||||
public static String translatePlaceholders(@NotNull final String text,
|
||||
@Nullable final Player player,
|
||||
@NotNull final List<StaticPlaceholder> statics) {
|
||||
return translatePlaceholders(text, player, new PlaceholderInjectable() {
|
||||
@Override
|
||||
public void clearInjectedPlaceholders() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<InjectablePlaceholder> getPlaceholderInjections() {
|
||||
return new ArrayList<>(statics);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate all placeholders with respect to a player.
|
||||
*
|
||||
* @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.
|
||||
* @return The text, translated.
|
||||
*/
|
||||
public static String translatePlaceholders(@NotNull final String text,
|
||||
@Nullable final Player player,
|
||||
@NotNull final PlaceholderInjectable context) {
|
||||
String processed = text;
|
||||
for (PlaceholderIntegration integration : REGISTERED_INTEGRATIONS) {
|
||||
processed = integration.translate(processed, player);
|
||||
}
|
||||
for (StaticPlaceholder placeholder : statics) {
|
||||
for (InjectablePlaceholder injection : context.getPlaceholderInjections()) {
|
||||
// Do I know this is a bad way of doing this? Yes.
|
||||
processed = processed.replace("%" + placeholder.getIdentifier() + "%", placeholder.getValue());
|
||||
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));
|
||||
}
|
||||
}
|
||||
return processed;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.willfp.eco.core.placeholder;
|
||||
|
||||
import com.willfp.eco.core.Eco;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
|
||||
/**
|
||||
* Placeholders that can be injected into {@link PlaceholderInjectable} objects.
|
||||
*/
|
||||
public sealed interface InjectablePlaceholder extends Placeholder permits PlayerStaticPlaceholder, StaticPlaceholder {
|
||||
@Override
|
||||
default EcoPlugin getPlugin() {
|
||||
return Eco.getHandler().getEcoPlugin();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import com.willfp.eco.core.EcoPlugin;
|
||||
/**
|
||||
* A placeholder represents a string that can hold a value.
|
||||
*/
|
||||
public sealed interface Placeholder permits PlayerPlaceholder, PlayerlessPlaceholder, StaticPlaceholder {
|
||||
public sealed interface Placeholder permits PlayerPlaceholder, PlayerlessPlaceholder, InjectablePlaceholder {
|
||||
/**
|
||||
* Get the plugin that holds the placeholder.
|
||||
*
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.willfp.eco.core.placeholder;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -22,7 +23,40 @@ public interface PlaceholderInjectable {
|
||||
*
|
||||
* @param placeholders The placeholders.
|
||||
*/
|
||||
void injectPlaceholders(@NotNull Iterable<StaticPlaceholder> placeholders);
|
||||
default void injectPlaceholders(@NotNull InjectablePlaceholder... placeholders) {
|
||||
this.addInjectablePlaceholder(List.of(placeholders));
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject placeholder.
|
||||
*
|
||||
* @param placeholders The placeholders.
|
||||
*/
|
||||
@Deprecated(since = "6.35.0", forRemoval = true)
|
||||
default void injectPlaceholders(@NotNull Iterable<StaticPlaceholder> placeholders) {
|
||||
List<InjectablePlaceholder> toInject = new ArrayList<>();
|
||||
for (StaticPlaceholder placeholder : placeholders) {
|
||||
toInject.add(placeholder);
|
||||
}
|
||||
this.addInjectablePlaceholder(toInject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject placeholders.
|
||||
* <p>
|
||||
* When implementing a PlaceholderInjectable object, override this method.
|
||||
*
|
||||
* @param placeholders The placeholders.
|
||||
*/
|
||||
default void addInjectablePlaceholder(@NotNull Iterable<InjectablePlaceholder> placeholders) {
|
||||
List<StaticPlaceholder> toInject = new ArrayList<>();
|
||||
for (InjectablePlaceholder placeholder : placeholders) {
|
||||
if (placeholder instanceof StaticPlaceholder staticPlaceholder) {
|
||||
toInject.add(staticPlaceholder);
|
||||
}
|
||||
}
|
||||
this.injectPlaceholders(toInject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear injected placeholders.
|
||||
@@ -33,6 +67,31 @@ public interface PlaceholderInjectable {
|
||||
* Get injected placeholders.
|
||||
*
|
||||
* @return Injected placeholders.
|
||||
* @deprecated Use getPlaceholderInjections.
|
||||
*/
|
||||
List<StaticPlaceholder> getInjectedPlaceholders();
|
||||
@Deprecated(since = "6.35.0", forRemoval = true)
|
||||
@NotNull
|
||||
default List<StaticPlaceholder> getInjectedPlaceholders() {
|
||||
List<StaticPlaceholder> found = new ArrayList<>();
|
||||
|
||||
for (InjectablePlaceholder placeholder : getPlaceholderInjections()) {
|
||||
if (placeholder instanceof StaticPlaceholder staticPlaceholder) {
|
||||
found.add(staticPlaceholder);
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get injected placeholders.
|
||||
* <p>
|
||||
* Override this method in implementations.
|
||||
*
|
||||
* @return Injected placeholders.
|
||||
*/
|
||||
@NotNull
|
||||
default List<InjectablePlaceholder> getPlaceholderInjections() {
|
||||
return new ArrayList<>(getInjectedPlaceholders());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.willfp.eco.core.placeholder;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A placeholder that cannot be registered, and exists purely in injection.
|
||||
*/
|
||||
public final class PlayerStaticPlaceholder implements InjectablePlaceholder {
|
||||
/**
|
||||
* The name of the placeholder.
|
||||
*/
|
||||
private final String identifier;
|
||||
|
||||
/**
|
||||
* The function to retrieve the output of the placeholder.
|
||||
*/
|
||||
private final Function<Player, String> function;
|
||||
|
||||
/**
|
||||
* Create a new player placeholder.
|
||||
*
|
||||
* @param identifier The identifier.
|
||||
* @param function The function to retrieve the value.
|
||||
*/
|
||||
public PlayerStaticPlaceholder(@NotNull final String identifier,
|
||||
@NotNull final Function<Player, String> function) {
|
||||
this.identifier = identifier;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of the placeholder.
|
||||
*
|
||||
* @param player The player.
|
||||
* @return The value.
|
||||
*/
|
||||
public String getValue(@NotNull final Player player) {
|
||||
return function.apply(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof PlayerStaticPlaceholder that)) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(this.getIdentifier(), that.getIdentifier());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.getIdentifier());
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.willfp.eco.core.placeholder;
|
||||
|
||||
import com.willfp.eco.core.Eco;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -11,7 +9,7 @@ import java.util.function.Supplier;
|
||||
/**
|
||||
* A placeholder that cannot be registered, and exists purely in injection.
|
||||
*/
|
||||
public final class StaticPlaceholder implements Placeholder {
|
||||
public final class StaticPlaceholder implements InjectablePlaceholder {
|
||||
/**
|
||||
* The name of the placeholder.
|
||||
*/
|
||||
@@ -43,11 +41,6 @@ public final class StaticPlaceholder implements Placeholder {
|
||||
return function.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public EcoPlugin getPlugin() {
|
||||
return Eco.getHandler().getEcoPlugin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return this.identifier;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.willfp.eco.util;
|
||||
|
||||
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
|
||||
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
|
||||
import com.willfp.eco.core.placeholder.StaticPlaceholder;
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -8,7 +10,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
@@ -252,7 +256,17 @@ public final class NumberUtils {
|
||||
*/
|
||||
public static double evaluateExpression(@NotNull final String expression,
|
||||
@Nullable final Player player) {
|
||||
return evaluateExpression(expression, player, Collections.emptyList());
|
||||
return evaluateExpression(expression, player, new PlaceholderInjectable() {
|
||||
@Override
|
||||
public void clearInjectedPlaceholders() {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<InjectablePlaceholder> getPlaceholderInjections() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,11 +276,41 @@ public final class NumberUtils {
|
||||
* @param player The player.
|
||||
* @param statics The static placeholders.
|
||||
* @return The value of the expression, or zero if invalid.
|
||||
* @deprecated Use new statics system.
|
||||
*/
|
||||
@Deprecated(since = "6.35.0", forRemoval = true)
|
||||
public static double evaluateExpression(@NotNull final String expression,
|
||||
@Nullable final Player player,
|
||||
@NotNull final Iterable<StaticPlaceholder> statics) {
|
||||
return crunch.evaluate(expression, player, statics);
|
||||
return crunch.evaluate(expression, player, new PlaceholderInjectable() {
|
||||
@Override
|
||||
public void clearInjectedPlaceholders() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<InjectablePlaceholder> getPlaceholderInjections() {
|
||||
List<InjectablePlaceholder> injections = new ArrayList<>();
|
||||
for (StaticPlaceholder placeholder : statics) {
|
||||
injections.add(placeholder);
|
||||
}
|
||||
return injections;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate an expression with respect to a player (for placeholders).
|
||||
*
|
||||
* @param expression The expression.
|
||||
* @param player The player.
|
||||
* @param context The injectable placeholders.
|
||||
* @return The value of the expression, or zero if invalid.
|
||||
*/
|
||||
public static double evaluateExpression(@NotNull final String expression,
|
||||
@Nullable final Player player,
|
||||
@NotNull final PlaceholderInjectable context) {
|
||||
return crunch.evaluate(expression, player, context);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -290,12 +334,12 @@ public final class NumberUtils {
|
||||
*
|
||||
* @param expression The expression.
|
||||
* @param player The player.
|
||||
* @param statics The statics.
|
||||
* @param injectable The injectable placeholders.
|
||||
* @return The value of the expression, or zero if invalid.
|
||||
*/
|
||||
double evaluate(@NotNull String expression,
|
||||
@Nullable Player player,
|
||||
@NotNull Iterable<StaticPlaceholder> statics);
|
||||
@NotNull PlaceholderInjectable injectable);
|
||||
}
|
||||
|
||||
private NumberUtils() {
|
||||
|
||||
Reference in New Issue
Block a user