From 192f997ff3cec6d2ba3221e93428aed9f7e2ad67 Mon Sep 17 00:00:00 2001 From: Indyuce Date: Thu, 22 Dec 2022 00:24:08 +0100 Subject: [PATCH] Fixed the 'placeholder' recipe condition --- .../condition/PlaceholderCondition.java | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/condition/PlaceholderCondition.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/condition/PlaceholderCondition.java index 79d58aab..3ec5580c 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/condition/PlaceholderCondition.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/crafting/condition/PlaceholderCondition.java @@ -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