mirror of
https://github.com/Xiao-MoMi/Custom-Fishing.git
synced 2026-01-04 15:41:35 +00:00
Fix placeholder renderer
This commit is contained in:
@@ -18,26 +18,23 @@
|
||||
package net.momirealms.customfishing.api.mechanic.misc.value;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import net.momirealms.customfishing.api.mechanic.misc.placeholder.BukkitPlaceholderManager;
|
||||
import net.momirealms.customfishing.common.helper.ExpressionHelper;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ExpressionMathValueImpl<T> implements MathValue<T> {
|
||||
|
||||
private final String raw;
|
||||
private final TextValue<T> raw;
|
||||
|
||||
public ExpressionMathValueImpl(String raw) {
|
||||
this.raw = raw;
|
||||
this.raw = TextValue.auto(raw);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double evaluate(Context<T> context) {
|
||||
Map<String, String> replacements = context.placeholderMap();
|
||||
String expression;
|
||||
if (context.getHolder() instanceof OfflinePlayer player) expression = BukkitPlaceholderManager.getInstance().parse(player, raw, replacements);
|
||||
else expression = BukkitPlaceholderManager.getInstance().parse(null, raw, replacements);
|
||||
return ExpressionHelper.evaluate(expression);
|
||||
return ExpressionHelper.evaluate(raw.render(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double evaluate(Context<T> context, boolean parseRawPlaceholders) {
|
||||
return ExpressionHelper.evaluate(raw.render(context, parseRawPlaceholders));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,17 @@ public interface MathValue<T> {
|
||||
*/
|
||||
double evaluate(Context<T> context);
|
||||
|
||||
/**
|
||||
* Evaluates the mathematical value within the given context.
|
||||
*
|
||||
* @param context the context in which the value is evaluated
|
||||
* @param parseRawPlaceholders whether to parse raw placeholders for instance %xxx%
|
||||
* @return the evaluated value as a double
|
||||
*/
|
||||
default double evaluate(Context<T> context, boolean parseRawPlaceholders) {
|
||||
return evaluate(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a MathValue based on a mathematical expression.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
package net.momirealms.customfishing.api.mechanic.misc.value;
|
||||
|
||||
import net.momirealms.customfishing.api.mechanic.context.Context;
|
||||
import net.momirealms.customfishing.api.mechanic.misc.placeholder.PlaceholderAPIUtils;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -40,6 +42,18 @@ public interface TextValue<T> {
|
||||
*/
|
||||
String render(Context<T> context);
|
||||
|
||||
/**
|
||||
* Renders the text value within the given context.
|
||||
*
|
||||
* @param context the context in which the text value is rendered
|
||||
* @param parseRawPlaceholders whether to parse raw placeholders for instance %xxx%
|
||||
* @return the rendered text as a String
|
||||
*/
|
||||
default String render(Context<T> context, boolean parseRawPlaceholders) {
|
||||
if (!parseRawPlaceholders || !(context.getHolder() instanceof OfflinePlayer player)) return render(context);
|
||||
return PlaceholderAPIUtils.parse(player, render(context));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a TextValue based on a placeholder text.
|
||||
* Placeholders can be dynamically replaced with context-specific values.
|
||||
|
||||
Reference in New Issue
Block a user