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