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 Function<MMOLineConfig, C> function;
// Configurable parameters
private 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,40 +6,24 @@ import java.util.List;
import net.Indyuce.mmoitems.api.player.PlayerData;
import io.lumine.mythic.lib.api.MMOLineConfig;
public class PermissionCondition extends Condition {
public class PermissionCondition extends GenericCondition {
private final List<String> permissions;
/**
* Permissions are super ugly to display so MI uses a string instead.
* 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) {
super("permission");
super("permission", config);
config.validate("list");
permissions = Arrays.asList(config.getString("list").split(","));
display = config.contains("display") ? config.getString("display") : "?";
}
@Override
public boolean isMet(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,20 +4,11 @@ import io.lumine.mythic.lib.api.MMOLineConfig;
import me.clip.placeholderapi.PlaceholderAPI;
import net.Indyuce.mmoitems.api.player.PlayerData;
public class PlaceholderCondition extends Condition {
public class PlaceholderCondition extends GenericCondition {
private final String value, placeholder, comparator, compareTo;
/**
* Permissions are super ugly to display so MI uses a string instead.
* 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) {
super("placeholder");
super("placeholder", config);
config.validate("placeholder");
value = config.getString("placeholder");
@ -25,7 +16,6 @@ public class PlaceholderCondition extends Condition {
placeholder = array[0];
comparator = array[1];
compareTo = array[2];
display = config.getString("display", "");
}
@Override
@ -51,11 +41,6 @@ public class PlaceholderCondition extends Condition {
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
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("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("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"));
@ -77,8 +77,7 @@ public class CraftingManager implements Reloadable {
ConfigFile language = new ConfigFile("/language", "crafting-stations");
for (LoadedCraftingObject<Condition> condition : getConditions())
if (condition.hasDisplay()) {
for (LoadedCraftingObject<Condition> condition : getConditions()) {
String path = "condition." + condition.getId();
if (!language.getConfig().contains(path)) {
language.getConfig().createSection(path);
@ -217,9 +216,10 @@ public class CraftingManager implements Reloadable {
* @param id Condition ID
* @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
* @since 6.7 A conditional display is required for all conditions
*/
public void registerCondition(String id, Function<MMOLineConfig, Condition> function, @Nullable ConditionalDisplay display) {
LoadedCraftingObject<Condition> obj = new LoadedCraftingObject<>(id, function, display);
public void registerCondition(String id, Function<MMOLineConfig, Condition> function, @NotNull ConditionalDisplay display) {
LoadedCraftingObject<Condition> obj = new LoadedCraftingObject<>(id, function, Objects.requireNonNull(display, "Conditional display cannot be null"));
conditions.put(obj.getId(), obj);
}