2023-07-25 11:39:40 +02:00
|
|
|
package com.craftaro.epicenchants.effect;
|
2019-01-21 15:01:25 +01:00
|
|
|
|
2019-01-30 11:58:37 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2019-01-21 15:01:25 +01:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
2019-08-04 23:50:07 +02:00
|
|
|
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
|
|
|
|
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
|
2019-01-21 15:01:25 +01:00
|
|
|
|
|
|
|
public class EffectManager {
|
2019-01-24 12:00:57 +01:00
|
|
|
public static Optional<EffectExecutor> getEffect(ConfigurationSection section) {
|
|
|
|
if (section == null) {
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
|
2019-01-21 15:01:25 +01:00
|
|
|
try {
|
2019-01-24 12:00:57 +01:00
|
|
|
String formatted = UPPER_UNDERSCORE.to(UPPER_CAMEL, section.getName().toLowerCase()).replaceAll("-.*$", "");
|
2023-07-25 11:39:40 +02:00
|
|
|
Class<?> clazz = Class.forName("com.craftaro.epicenchants.effect.effects." + formatted);
|
2019-01-21 15:01:25 +01:00
|
|
|
Constructor<?> constructor = clazz.getConstructor(ConfigurationSection.class);
|
|
|
|
Object object = constructor.newInstance(section);
|
|
|
|
return Optional.of((EffectExecutor) object);
|
2023-06-25 13:50:27 +02:00
|
|
|
} catch (ReflectiveOperationException | ClassCastException ex) {
|
|
|
|
ex.printStackTrace();
|
2019-02-08 13:08:13 +01:00
|
|
|
Bukkit.getLogger().severe("Invalid effect: " + section.getName());
|
2019-01-21 15:01:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Optional.empty();
|
|
|
|
}
|
|
|
|
}
|