Search in sources :

Example 1 with Effect

use of com.easterlyn.effect.Effect in project Easterlyn by Easterlyn.

the class EasterlynEffects method getEffects.

/**
 * Gets all Effects and corresponding levels on the provided ItemStack(s).
 *
 * @param bypass whether or not the maximum level for an effect can be bypassed
 * @param items the item(s) to get effects from
 * @return the Effects and corresponding levels
 */
@NotNull
private Map<Effect, Integer> getEffects(boolean bypass, ItemStack... items) {
    Map<Effect, Integer> applicableEffects = new HashMap<>();
    for (ItemStack item : items) {
        if (item == null || item.getType() == Material.AIR || !item.hasItemMeta() || item.getItemMeta().getLore() == null) {
            continue;
        }
        for (String lore : item.getItemMeta().getLore()) {
            Pair<Effect, Integer> pair = getEffectFromLore(lore, false);
            if (pair == null) {
                continue;
            }
            int level = pair.getRight();
            if (applicableEffects.containsKey(pair.getLeft())) {
                level += applicableEffects.get(pair.getLeft());
            }
            if (!bypass && level > pair.getLeft().getMaxLevel()) {
                level = pair.getLeft().getMaxTotalLevel();
            }
            applicableEffects.put(pair.getLeft(), level);
        }
    }
    return applicableEffects;
}
Also used : HashMap(java.util.HashMap) Effect(com.easterlyn.effect.Effect) ItemStack(org.bukkit.inventory.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Effect

use of com.easterlyn.effect.Effect in project Easterlyn by Easterlyn.

the class EasterlynEffects method applyEffects.

/**
 * Applies all effects to the given LivingEntity.
 *
 * @param entity the LivingEntity
 */
private void applyEffects(@NotNull LivingEntity entity, @Nullable org.bukkit.event.Event event) {
    EntityEquipment equipment = entity.getEquipment();
    if (equipment == null) {
        return;
    }
    Map<Effect, Integer> effects = new HashMap<>();
    equipmentSlotMap.forEach((equipmentSlot, equipmentSlotFunction) -> getEffects(false, equipmentSlotFunction.apply(equipment)).forEach((effect, level) -> {
        if (!effect.getTarget().apply(equipmentSlot)) {
            return;
        }
        effects.compute(effect, (value, current) -> {
            if (current == null) {
                current = level;
            } else {
                current += level;
            }
            return current > effect.getMaxTotalLevel() ? effect.getMaxTotalLevel() : current;
        });
    }));
    effects.forEach((effect, level) -> effect.applyEffect(entity, level, event));
}
Also used : HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) Reflections(org.reflections.Reflections) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) PluginEnableEvent(org.bukkit.event.server.PluginEnableEvent) Map(java.util.Map) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) Material(org.bukkit.Material) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) NumberUtil(com.easterlyn.util.NumberUtil) Iterator(java.util.Iterator) FurnaceExtractEvent(org.bukkit.event.inventory.FurnaceExtractEvent) Set(java.util.Set) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) Pair(com.github.jikoo.planarwrappers.tuple.Pair) LivingEntity(org.bukkit.entity.LivingEntity) EconomyUtil(com.easterlyn.util.EconomyUtil) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) ItemStack(org.bukkit.inventory.ItemStack) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Effect(com.easterlyn.effect.Effect) Event(com.github.jikoo.planarwrappers.event.Event) EventPriority(org.bukkit.event.EventPriority) Modifier(java.lang.reflect.Modifier) ChatColor(org.bukkit.ChatColor) Pattern(java.util.regex.Pattern) EntityEquipment(org.bukkit.inventory.EntityEquipment) NotNull(org.jetbrains.annotations.NotNull) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) PlayerChangedWorldEvent(org.bukkit.event.player.PlayerChangedWorldEvent) HashMap(java.util.HashMap) EntityEquipment(org.bukkit.inventory.EntityEquipment) Effect(com.easterlyn.effect.Effect)

Example 3 with Effect

use of com.easterlyn.effect.Effect in project Easterlyn by Easterlyn.

the class EasterlynEffects method onEnable.

@Override
public void onEnable() {
    effects = new HashMap<>();
    equipmentSlotMap.put(EquipmentSlot.CHEST, EntityEquipment::getChestplate);
    equipmentSlotMap.put(EquipmentSlot.FEET, EntityEquipment::getBoots);
    equipmentSlotMap.put(EquipmentSlot.HAND, EntityEquipment::getItemInMainHand);
    equipmentSlotMap.put(EquipmentSlot.HEAD, EntityEquipment::getHelmet);
    equipmentSlotMap.put(EquipmentSlot.LEGS, EntityEquipment::getItemInOffHand);
    equipmentSlotMap.put(EquipmentSlot.OFF_HAND, EntityEquipment::getItemInOffHand);
    // TODO green thumb, autotorch, pshoooot
    Reflections reflections = new Reflections("com.easterlyn.effect", getClassLoader());
    Set<Class<? extends Effect>> allEffects = reflections.getSubTypesOf(Effect.class);
    for (Class<? extends Effect> effect : allEffects) {
        if (Modifier.isAbstract(effect.getModifiers())) {
            continue;
        }
        try {
            Constructor<? extends Effect> constructor = effect.getConstructor(this.getClass());
            Effect instance = constructor.newInstance(this);
            effects.put(instance.getName(), instance);
        } catch (IllegalAccessException | IllegalArgumentException | SecurityException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    // TODO event per effect, Effect#tick?
    // Register events for effects
    Event.register(BlockBreakEvent.class, event -> applyEffects(event.getPlayer(), event), this);
    Event.register(FurnaceExtractEvent.class, event -> applyEffects(event.getPlayer(), event), this);
    Event.register(EntityDamageEvent.class, event -> {
        if (event.getEntity() instanceof LivingEntity) {
            applyEffects((LivingEntity) event.getEntity(), event);
        }
        if (event instanceof EntityDamageByEntityEvent && ((EntityDamageByEntityEvent) event).getDamager() instanceof LivingEntity) {
            applyEffects((LivingEntity) ((EntityDamageByEntityEvent) event).getDamager(), event);
        }
    }, this);
    Event.register(PlayerInteractEvent.class, event -> applyEffects(event.getPlayer(), event), this, EventPriority.NORMAL, false);
    Event.register(PlayerChangedWorldEvent.class, event -> applyEffects(event.getPlayer(), event), this);
    RegisteredServiceProvider<EasterlynCore> registration = getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (registration != null) {
        register(registration.getProvider());
    }
    Event.register(PluginEnableEvent.class, event -> {
        if (event.getPlugin() instanceof EasterlynCore) {
            register((EasterlynCore) event.getPlugin());
        }
    }, this);
    getServer().getScheduler().runTaskTimer(this, () -> {
        for (Player player : getServer().getOnlinePlayers()) {
            applyEffects(player, null);
        }
    }, 20L, 20L);
}
Also used : Player(org.bukkit.entity.Player) InvocationTargetException(java.lang.reflect.InvocationTargetException) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityEquipment(org.bukkit.inventory.EntityEquipment) Effect(com.easterlyn.effect.Effect) Reflections(org.reflections.Reflections)

Example 4 with Effect

use of com.easterlyn.effect.Effect in project Easterlyn by Easterlyn.

the class EasterlynEffects method getEffectFromLore.

/**
 * Gets the Effect and level represented by a String in an ItemStack's lore.
 *
 * @param lore the String
 * @param ignoreCase if case should be ignored when matching Effect
 */
@Nullable
public Pair<Effect, Integer> getEffectFromLore(@NotNull String lore, boolean ignoreCase) {
    Matcher match = effectPattern.matcher(lore);
    if (!match.find()) {
        return null;
    }
    lore = ChatColor.stripColor(match.group(1));
    Effect effect = null;
    if (effects.containsKey(lore)) {
        effect = effects.get(lore);
    } else {
        if (!ignoreCase) {
            return null;
        }
        for (Map.Entry<String, Effect> entry : effects.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(lore)) {
                effect = entry.getValue();
                break;
            }
        }
        if (effect == null) {
            return null;
        }
    }
    try {
        return new Pair<>(effect, NumberUtil.intFromRoman(match.group(2)));
    } catch (NumberFormatException e) {
        return null;
    }
}
Also used : Matcher(java.util.regex.Matcher) Effect(com.easterlyn.effect.Effect) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.github.jikoo.planarwrappers.tuple.Pair) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Effect (com.easterlyn.effect.Effect)4 HashMap (java.util.HashMap)3 Pair (com.github.jikoo.planarwrappers.tuple.Pair)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 LivingEntity (org.bukkit.entity.LivingEntity)2 Player (org.bukkit.entity.Player)2 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)2 EntityEquipment (org.bukkit.inventory.EntityEquipment)2 ItemStack (org.bukkit.inventory.ItemStack)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 Reflections (org.reflections.Reflections)2 EconomyUtil (com.easterlyn.util.EconomyUtil)1 NumberUtil (com.easterlyn.util.NumberUtil)1 Event (com.github.jikoo.planarwrappers.event.Event)1 Constructor (java.lang.reflect.Constructor)1 Modifier (java.lang.reflect.Modifier)1 ArrayList (java.util.ArrayList)1