Generic conditions

This commit is contained in:
Jules 2022-02-25 12:44:44 +01:00
parent c2fe7eb5b6
commit 86f806136c
5 changed files with 92 additions and 96 deletions

View File

@ -17,6 +17,7 @@ public class LoadedCraftingObject<C> {
private final String id; private final String id;
private final Function<MMOLineConfig, C> function; private final Function<MMOLineConfig, C> function;
// Configurable parameters
private ConditionalDisplay display; private ConditionalDisplay display;
public LoadedCraftingObject(String id, Function<MMOLineConfig, C> function, ConditionalDisplay display) { public LoadedCraftingObject(String id, Function<MMOLineConfig, C> function, ConditionalDisplay display) {

View File

@ -0,0 +1,26 @@
package net.Indyuce.mmoitems.api.crafting.condition;
import io.lumine.mythic.lib.api.MMOLineConfig;
public abstract class GenericCondition extends Condition {
/**
* Permissions are super ugly to display so MI uses a string instead.
* This way 'Only for Mages' is used instead of 'class.mage'
* <p>
* One string can also replace multiple permissions.
* 'Magic Classes Only' instead of 'class.mage' and 'class.apprentice'
*/
public final String display;
public GenericCondition(String id, MMOLineConfig config) {
super(id);
this.display = config.getString("display", "<No display chosen>");
}
@Override
public String formatDisplay(String string) {
return string.replace("#display#", display);
}
}

View File

@ -6,41 +6,25 @@ import java.util.List;
import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerData;
import io.lumine.mythic.lib.api.MMOLineConfig; import io.lumine.mythic.lib.api.MMOLineConfig;
public class PermissionCondition extends Condition { public class PermissionCondition extends GenericCondition {
private final List<String> permissions; private final List<String> permissions;
/** public PermissionCondition(MMOLineConfig config) {
* Permissions are super ugly to display so MI uses a string instead. super("permission", config);
* This way 'Only for Mages' is used instead of 'class.mage'
*
* One string can also replace multiple permissions.
* 'Magic Classes Only' instead of 'class.mage' and 'class.apprentice'
*/
private final String display;
public PermissionCondition(MMOLineConfig config) { config.validate("list");
super("permission"); permissions = Arrays.asList(config.getString("list").split(","));
}
config.validate("list"); @Override
permissions = Arrays.asList(config.getString("list").split(",")); public boolean isMet(PlayerData data) {
display = config.contains("display") ? config.getString("display") : "?"; for (String permission : permissions)
} if (!data.getPlayer().hasPermission(permission))
return false;
return true;
}
@Override @Override
public boolean isMet(PlayerData data) { public void whenCrafting(PlayerData data) {
if(!data.isOnline()) return false; }
for (String permission : permissions)
if (!data.getPlayer().hasPermission(permission))
return false;
return true;
}
@Override
public String formatDisplay(String string) {
return string.replace("#perms#", String.join(", ", permissions)).replace("#display#", display);
}
@Override
public void whenCrafting(PlayerData data) {
}
} }

View File

@ -4,59 +4,44 @@ import io.lumine.mythic.lib.api.MMOLineConfig;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerData;
public class PlaceholderCondition extends Condition { public class PlaceholderCondition extends GenericCondition {
private final String value, placeholder, comparator, compareTo; private final String value, placeholder, comparator, compareTo;
/** public PlaceholderCondition(MMOLineConfig config) {
* Permissions are super ugly to display so MI uses a string instead. super("placeholder", config);
* This way 'Only for Mages' is used instead of 'class.mage'
*
* One string can also replace multiple permissions.
* 'Magic Classes Only' instead of 'class.mage' and 'class.apprentice'
*/
private final String display;
public PlaceholderCondition(MMOLineConfig config) { config.validate("placeholder");
super("placeholder"); value = config.getString("placeholder");
String[] array = value.split("~");
placeholder = array[0];
comparator = array[1];
compareTo = array[2];
}
config.validate("placeholder"); @Override
value = config.getString("placeholder"); public boolean isMet(PlayerData data) {
String[] array = value.split("~"); String placeholders = PlaceholderAPI.setPlaceholders(data.getPlayer(), placeholder);
placeholder = array[0]; switch (comparator) {
comparator = array[1]; case "<":
compareTo = array[2]; return Double.valueOf(placeholders) < Double.valueOf(compareTo);
display = config.getString("display", ""); 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);
}
return false;
}
@Override @Override
public boolean isMet(PlayerData data) { public void whenCrafting(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);
}
return false;
}
@Override
public String formatDisplay(String string) {
return string.replace("#placeholder#", "" + placeholder).replace("#display#", display);
}
@Override
public void whenCrafting(PlayerData data) {
}
} }

View File

@ -46,7 +46,7 @@ public class CraftingManager implements Reloadable {
// Conditions // Conditions
registerCondition("level", LevelCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires Level #level#", "&c" + AltChar.cross + " Requires Level #level#")); registerCondition("level", LevelCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires Level #level#", "&c" + AltChar.cross + " Requires Level #level#"));
registerCondition("permission", PermissionCondition::new, new ConditionalDisplay("&a" + AltChar.check + " #display#", "&c" + AltChar.cross + " #display#")); registerCondition("permission", PermissionCondition::new, new ConditionalDisplay("&a" + AltChar.check + " #display#", "&c" + AltChar.cross + " #display#"));
registerCondition("placeholder", PlaceholderCondition::new, null); registerCondition("placeholder", PlaceholderCondition::new, new ConditionalDisplay("&a" + AltChar.check + " #display#", "&c" + AltChar.cross + " #display#"));
registerCondition("mana", ManaCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires #mana# Mana", "&c" + AltChar.cross + " Requires #mana# Mana")); registerCondition("mana", ManaCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires #mana# Mana", "&c" + AltChar.cross + " Requires #mana# Mana"));
registerCondition("stamina", StaminaCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires #stamina# Stamina", "&c" + AltChar.cross + " Requires #stamina# Stamina")); registerCondition("stamina", StaminaCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires #stamina# Stamina", "&c" + AltChar.cross + " Requires #stamina# Stamina"));
registerCondition("food", FoodCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires #food# Food", "&c" + AltChar.cross + " Requires #food# Food")); registerCondition("food", FoodCondition::new, new ConditionalDisplay("&a" + AltChar.check + " Requires #food# Food", "&c" + AltChar.cross + " Requires #food# Food"));
@ -77,17 +77,16 @@ public class CraftingManager implements Reloadable {
ConfigFile language = new ConfigFile("/language", "crafting-stations"); ConfigFile language = new ConfigFile("/language", "crafting-stations");
for (LoadedCraftingObject<Condition> condition : getConditions()) for (LoadedCraftingObject<Condition> condition : getConditions()) {
if (condition.hasDisplay()) { String path = "condition." + condition.getId();
String path = "condition." + condition.getId(); if (!language.getConfig().contains(path)) {
if (!language.getConfig().contains(path)) { language.getConfig().createSection(path);
language.getConfig().createSection(path); condition.getDisplay().setup(language.getConfig().getConfigurationSection(path));
condition.getDisplay().setup(language.getConfig().getConfigurationSection(path));
}
condition.setDisplay(new ConditionalDisplay(language.getConfig().getConfigurationSection(path)));
} }
condition.setDisplay(new ConditionalDisplay(language.getConfig().getConfigurationSection(path)));
}
for (IngredientType ingredient : getIngredients()) { for (IngredientType ingredient : getIngredients()) {
String path = "ingredient." + ingredient.getId(); String path = "ingredient." + ingredient.getId();
if (!language.getConfig().contains(path)) { if (!language.getConfig().contains(path)) {
@ -217,9 +216,10 @@ public class CraftingManager implements Reloadable {
* @param id Condition ID * @param id Condition ID
* @param function Function that loads a condition from a line conf * @param function Function that loads a condition from a line conf
* @param display How it displays in the item lore, null if it should not * @param display How it displays in the item lore, null if it should not
* @since 6.7 A conditional display is required for all conditions
*/ */
public void registerCondition(String id, Function<MMOLineConfig, Condition> function, @Nullable ConditionalDisplay display) { public void registerCondition(String id, Function<MMOLineConfig, Condition> function, @NotNull ConditionalDisplay display) {
LoadedCraftingObject<Condition> obj = new LoadedCraftingObject<>(id, function, display); LoadedCraftingObject<Condition> obj = new LoadedCraftingObject<>(id, function, Objects.requireNonNull(display, "Conditional display cannot be null"));
conditions.put(obj.getId(), obj); conditions.put(obj.getId(), obj);
} }