Cleaned up PlaceholderContext

This commit is contained in:
Auxilor
2023-04-29 13:22:18 +01:00
parent fa87cae81e
commit ad0223e1bb
5 changed files with 41 additions and 15 deletions

View File

@@ -118,8 +118,7 @@ public final class PlaceholderManager {
getResult(
plugin,
identifier,
new PlaceholderContext()
.copyWithPlayer(player)
new PlaceholderContext(player)
),
""
);

View File

@@ -1,7 +1,7 @@
package com.willfp.eco.core.placeholder.context;
import com.willfp.eco.core.integrations.placeholder.PlaceholderManager;
import com.willfp.eco.core.placeholder.AdditionalPlayer;
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
import com.willfp.eco.core.placeholder.PlaceholderInjectable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@@ -10,19 +10,40 @@ import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* Represents a context to translate placeholders in.
*/
public class PlaceholderContext {
/**
* An empty injectable.
*/
private static final PlaceholderInjectable EMPTY_INJECTABLE = new PlaceholderInjectable() {
@Override
public void addInjectablePlaceholder(@NotNull final Iterable<InjectablePlaceholder> placeholders) {
// Do nothing.
}
@Override
public void clearInjectedPlaceholders() {
// Do nothing.
}
@Override
public @NotNull List<InjectablePlaceholder> getPlaceholderInjections() {
return Collections.emptyList();
}
};
/**
* An empty context.
*/
public static final PlaceholderContext EMPTY = new PlaceholderContext(
null,
null,
PlaceholderManager.EMPTY_INJECTABLE,
null,
Collections.emptyList()
);
@@ -54,7 +75,16 @@ public class PlaceholderContext {
* Create an empty PlaceholderContext.
*/
public PlaceholderContext() {
this(null, null, PlaceholderManager.EMPTY_INJECTABLE, Collections.emptyList());
this(null, null, null, Collections.emptyList());
}
/**
* Create a PlaceholderContext for a player.
*
* @param player The player.
*/
public PlaceholderContext(@Nullable final Player player) {
this(player, null, null, Collections.emptyList());
}
/**
@@ -67,11 +97,11 @@ public class PlaceholderContext {
*/
public PlaceholderContext(@Nullable final Player player,
@Nullable final ItemStack itemStack,
@NotNull final PlaceholderInjectable injectableContext,
@Nullable final PlaceholderInjectable injectableContext,
@NotNull final Collection<AdditionalPlayer> additionalPlayers) {
this.player = player;
this.itemStack = itemStack;
this.injectableContext = injectableContext;
this.injectableContext = Objects.requireNonNullElse(injectableContext, EMPTY_INJECTABLE);
this.additionalPlayers = additionalPlayers;
}

View File

@@ -1,7 +1,6 @@
package com.willfp.eco.util;
import com.willfp.eco.core.Eco;
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.context.PlaceholderContext;
@@ -222,7 +221,7 @@ public final class NumberUtils {
*/
public static double evaluateExpression(@NotNull final String expression,
@Nullable final Player player) {
return evaluateExpression(expression, player, PlaceholderManager.EMPTY_INJECTABLE);
return evaluateExpression(expression, player, null);
}
/**
@@ -235,7 +234,7 @@ public final class NumberUtils {
*/
public static double evaluateExpression(@NotNull final String expression,
@Nullable final Player player,
@NotNull final PlaceholderInjectable context) {
@Nullable final PlaceholderInjectable context) {
return evaluateExpression(expression, player, context, new ArrayList<>());
}
@@ -250,7 +249,7 @@ public final class NumberUtils {
*/
public static double evaluateExpression(@NotNull final String expression,
@Nullable final Player player,
@NotNull final PlaceholderInjectable context,
@Nullable final PlaceholderInjectable context,
@NotNull final Collection<AdditionalPlayer> additionalPlayers) {
return Eco.get().evaluate(expression, new PlaceholderContext(
player,

View File

@@ -345,8 +345,7 @@ public final class StringUtils {
if (option == FormatOption.WITH_PLACEHOLDERS) {
return format(
message,
new PlaceholderContext()
.copyWithPlayer(player)
new PlaceholderContext(player)
);
}

View File

@@ -2,7 +2,6 @@
package com.willfp.eco.core.placeholder.context
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
@@ -13,6 +12,6 @@ import org.bukkit.inventory.ItemStack
fun placeholderContext(
player: Player? = null,
item: ItemStack? = null,
injectable: PlaceholderInjectable = PlaceholderManager.EMPTY_INJECTABLE,
injectable: PlaceholderInjectable? = null,
additionalPlayers: Collection<AdditionalPlayer> = emptyList()
): PlaceholderContext = PlaceholderContext(player, item, injectable, additionalPlayers)