EpicEnchants/src/main/java/com/songoda/epicenchants/effect/EffectExecutor.java

111 lines
4.1 KiB
Java
Raw Normal View History

package com.songoda.epicenchants.effect;
import com.songoda.epicenchants.enums.EventType;
2019-02-11 11:49:22 +01:00
import com.songoda.epicenchants.enums.TriggerType;
import com.songoda.epicenchants.objects.Condition;
import com.songoda.epicenchants.objects.LeveledModifier;
2019-02-19 14:23:20 +01:00
import com.songoda.epicenchants.utils.single.GeneralUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
2019-08-04 23:50:07 +02:00
import static com.songoda.epicenchants.effect.EffectExecutor.Who.OPPONENT;
import static com.songoda.epicenchants.effect.EffectExecutor.Who.USER;
public abstract class EffectExecutor {
2019-08-04 23:50:07 +02:00
private final ConfigurationSection section;
private final Set<TriggerType> triggerTypes;
private final Set<EffectExecutor> simultaneous;
private final Condition condition;
2019-02-19 14:23:20 +01:00
public EffectExecutor(ConfigurationSection section) {
this.section = section;
this.triggerTypes = GeneralUtils.parseTrigger(section.getString("trigger"));
this.condition = Condition.of(section.getString("condition"));
2019-04-04 14:40:11 +02:00
this.simultaneous = section.isConfigurationSection("simultaneous") ? section.getConfigurationSection("simultaneous").getKeys(false).stream()
.map(s -> "simultaneous." + s)
.map(section::getConfigurationSection)
.map(EffectManager::getEffect)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toSet()) : Collections.emptySet();
}
2019-03-26 15:32:53 +01:00
public void testAndRun(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType type, Event event, EventType eventType) {
2019-04-04 14:40:11 +02:00
testAndRun(user, opponent, level, type, event, eventType, false);
}
public void testAndRun(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType type, Event event, EventType eventType, boolean simul) {
if (!simul && !triggerTypes.contains(type)) {
return;
}
if (section.isString("chance") && !GeneralUtils.chance(LeveledModifier.of(section.getString("chance")).get(level, 100, user, opponent))) {
return;
}
if (!condition.get(user, opponent, level, event, false)) {
return;
}
if (this instanceof EffectEventExecutor) {
2019-03-26 15:32:53 +01:00
((EffectEventExecutor) this).execute(user, opponent, level, event, eventType);
} else {
execute(user, opponent, level, eventType);
}
2019-04-04 14:40:11 +02:00
simultaneous.forEach(e -> e.testAndRun(user, opponent, level, type, event, eventType, true));
}
2019-03-26 15:32:53 +01:00
public abstract void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType);
protected Who who() {
if (section.isString("who")) {
2019-03-26 15:32:53 +01:00
if (section.getString("who").equalsIgnoreCase("user")) return USER;
else if (section.getString("who").equalsIgnoreCase("opponent")) return OPPONENT;
}
2019-03-26 15:32:53 +01:00
return USER;
}
public LeveledModifier getAmount() {
return LeveledModifier.of(section.getString("amount"));
}
2019-03-26 15:32:53 +01:00
public void consume(Consumer<LivingEntity> playerConsumer, Player user, @Nullable LivingEntity opponent) {
if (triggerTypes.contains(TriggerType.HELD_ITEM) || triggerTypes.contains(TriggerType.STATIC_EFFECT)) {
2019-03-26 15:32:53 +01:00
playerConsumer.accept(user);
return;
}
switch (who()) {
2019-03-26 15:32:53 +01:00
case USER:
playerConsumer.accept(user);
break;
case OPPONENT:
if (opponent != null)
playerConsumer.accept(opponent);
}
}
2019-08-04 23:50:07 +02:00
public ConfigurationSection getSection() {
return this.section;
}
public Set<TriggerType> getTriggerTypes() {
return this.triggerTypes;
}
public enum Who {
2019-03-26 15:32:53 +01:00
USER, OPPONENT
}
}