com.sucy.skill.dynamic.condition.ValueTextCondition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of proskillapi Show documentation
Show all versions of proskillapi Show documentation
A Minecraft Bukkit plugin aiming to provide an easy code API and skill editor for all server owners to
create unique and fully custom classes and skills.
package com.sucy.skill.dynamic.condition;
import com.sucy.skill.dynamic.DynamicSkill;
import org.bukkit.entity.LivingEntity;
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.regex.Pattern;
/**
* A condition for dynamic skills that requires the text value to match the settings
*/
public class ValueTextCondition extends ConditionComponent {
private static final String MODE = "mode";
private static final String VALUE = "value";
private static final String EXPECT = "expect";
@Override
public String getKey() {
return "valuetext";
}
@Override
public boolean execute(LivingEntity caster, int level, List targets, boolean force) {
return test(caster, level, null) && executeChildren(caster, level, targets, force);
}
@Override
boolean test(final LivingEntity caster, final int level, final LivingEntity target) {
final CompareMode mode = CompareMode.valueOf(settings.getString(MODE).toUpperCase());
final Object value = DynamicSkill.getCastData(caster).get(settings.getString(VALUE));
final String expect = settings.getString(EXPECT);
if (value == null || expect == null) return false;
return mode.compare((String) value, expect);
}
private enum CompareMode {
REGEX((r, c) -> Pattern.compile(c).matcher(r).find()),
EXACTLY(Objects::equals),
CONTAIN(String::contains),
START(String::startsWith),
END(String::endsWith);
private final BiPredicate consumer;
CompareMode(BiPredicate consumer) {
this.consumer = consumer;
}
public boolean compare(String raw, String con) {
return consumer.test(raw, con);
}
}
}