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,45 +2,57 @@ 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);
final String unparsed1 = PlaceholderAPI.setPlaceholders(data.getPlayer(), expression1);
final String unparsed2 = PlaceholderAPI.setPlaceholders(data.getPlayer(), expression2);
try {
switch (comparator) {
case "<":
return Double.valueOf(placeholders) < Double.valueOf(compareTo);
return Double.parseDouble(unparsed1) < Double.parseDouble(unparsed2);
case "<=":
return Double.valueOf(placeholders) <= Double.valueOf(compareTo);
return Double.parseDouble(unparsed1) <= Double.parseDouble(unparsed2);
case ">":
return Double.valueOf(placeholders) > Double.valueOf(compareTo);
return Double.parseDouble(unparsed1) > Double.parseDouble(unparsed2);
case ">=":
return Double.valueOf(placeholders) >= Double.valueOf(compareTo);
return Double.parseDouble(unparsed1) >= Double.parseDouble(unparsed2);
case "==":
case "=":
return Double.valueOf(placeholders) == Double.valueOf(compareTo);
return Math.abs(Double.parseDouble(unparsed1) - Double.parseDouble(unparsed2)) <= EQUALITY_THRESHOLD;
case "!=":
return Double.valueOf(placeholders) != Double.valueOf(compareTo);
return Math.abs(Double.parseDouble(unparsed1) - Double.parseDouble(unparsed2)) > EQUALITY_THRESHOLD;
case "equals":
return placeholders.equals(compareTo);
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;
}
}
@Override
public void whenCrafting(PlayerData data) {