Fixed the 'placeholder' recipe condition

This commit is contained in:
Indyuce 2022-12-22 00:24:08 +01:00
parent 021813b57e
commit 192f997ff3

View File

@ -2,44 +2,56 @@ package net.Indyuce.mmoitems.api.crafting.condition;
import io.lumine.mythic.lib.api.MMOLineConfig;
import me.clip.placeholderapi.PlaceholderAPI;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.player.PlayerData;
import org.apache.commons.lang.Validate;
import java.util.logging.Level;
public class PlaceholderCondition extends GenericCondition {
private final String placeholder, comparator, compareTo;
private final String expression1, comparator, expression2;
public PlaceholderCondition(MMOLineConfig config) {
super("placeholder", config);
config.validate("placeholder");
config.validateKeys("placeholder");
String[] array = config.getString("placeholder").split("~");
Validate.isTrue(array.length == 3, "Please use exactly three times ~");
placeholder = array[0];
Validate.isTrue(array.length == 3, "Please use exactly twice ~");
expression1 = array[0];
comparator = array[1];
compareTo = array[2];
expression2 = array[2];
}
private static final double EQUALITY_THRESHOLD = Math.pow(10, -5);
@Override
public boolean isMet(PlayerData data) {
String placeholders = PlaceholderAPI.setPlaceholders(data.getPlayer(), placeholder);
switch (comparator) {
case "<":
return Double.valueOf(placeholders) < Double.valueOf(compareTo);
case "<=":
return Double.valueOf(placeholders) <= Double.valueOf(compareTo);
case ">":
return Double.valueOf(placeholders) > Double.valueOf(compareTo);
case ">=":
return Double.valueOf(placeholders) >= Double.valueOf(compareTo);
case "==":
case "=":
return Double.valueOf(placeholders) == Double.valueOf(compareTo);
case "!=":
return Double.valueOf(placeholders) != Double.valueOf(compareTo);
case "equals":
return placeholders.equals(compareTo);
final String unparsed1 = PlaceholderAPI.setPlaceholders(data.getPlayer(), expression1);
final String unparsed2 = PlaceholderAPI.setPlaceholders(data.getPlayer(), expression2);
try {
switch (comparator) {
case "<":
return Double.parseDouble(unparsed1) < Double.parseDouble(unparsed2);
case "<=":
return Double.parseDouble(unparsed1) <= Double.parseDouble(unparsed2);
case ">":
return Double.parseDouble(unparsed1) > Double.parseDouble(unparsed2);
case ">=":
return Double.parseDouble(unparsed1) >= Double.parseDouble(unparsed2);
case "==":
case "=":
return Math.abs(Double.parseDouble(unparsed1) - Double.parseDouble(unparsed2)) <= EQUALITY_THRESHOLD;
case "!=":
return Math.abs(Double.parseDouble(unparsed1) - Double.parseDouble(unparsed2)) > EQUALITY_THRESHOLD;
case "equals":
return unparsed1.equals(unparsed2);
default:
throw new RuntimeException("Comparator not recognized");
}
} catch (RuntimeException exception) {
MMOItems.plugin.getLogger().log(Level.WARNING, "Could not evaluate placeholder condition expression: " + exception.getMessage());
return false;
}
return false;
}
@Override