All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sucy.skill.dynamic.target.TargetComponent Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.3.1-R1
Show newest version
package com.sucy.skill.dynamic.target;

import com.sucy.skill.SkillAPI;
import com.sucy.skill.api.target.TargetHelper;
import com.sucy.skill.dynamic.ComponentType;
import com.sucy.skill.dynamic.DynamicSkill;
import com.sucy.skill.dynamic.EffectComponent;
import com.sucy.skill.dynamic.TempEntity;
import com.sucy.skill.listener.MechanicListener;
import mc.promcteam.engine.mccore.config.parse.DataSection;
import org.bukkit.GameMode;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * ProSkillAPI © 2023
 * com.sucy.skill.dynamic.target.TargetComponent
 */
public abstract class TargetComponent extends EffectComponent {

    private static final   String ALLY   = "group";
    private static final   String WALL   = "wall";
    private static final   String CASTER = "caster";
    protected static final String MAX    = "max";

    boolean       everyone;
    boolean       allies;
    boolean       throughWall;
    IncludeCaster self;

    @Override
    public ComponentType getType() {
        return ComponentType.TARGET;
    }

    /**
     * Executes the component
     *
     * @param caster  caster of the skill
     * @param level   level of the skill
     * @param targets targets to apply to
     * @param force
     * @return true if applied to something, false otherwise
     */
    @Override
    public boolean execute(LivingEntity caster, int level, List targets, boolean force) {
        final List list = getTargets(caster, level, targets);
        return (!list.isEmpty() && executeChildren(caster, level, list, force));
    }

    @Override
    public void load(DynamicSkill skill, DataSection config) {
        super.load(skill, config);

        final String group = settings.getString(ALLY, "enemy").toLowerCase();
        everyone = group.equals("both");
        allies = group.equals("ally");
        throughWall = settings.getString(WALL, "false").equalsIgnoreCase("true");
        self = IncludeCaster.valueOf(settings.getString(CASTER, "false").toUpperCase().replace(' ', '_'));
    }

    abstract List getTargets(final LivingEntity caster,
                                           final int level,
                                           final List targets);

    /**
     * {@inheritDoc}
     */
    @Override
    public void playPreview(List onPreviewStop,
                            Player caster,
                            int level,
                            Supplier> targetSupplier) {
        Supplier> supplier = () -> getTargets(caster, level, targetSupplier.get());
        super.playPreview(onPreviewStop, caster, level, supplier);
        playChildrenPreviews(onPreviewStop, caster, level, supplier);
    }

    List determineTargets(final LivingEntity caster,
                                        final int level,
                                        final List from,
                                        final Function> conversion) {

        final double max = parseValues(caster, MAX, level, 99);

        final List list = new ArrayList<>();
        from.forEach(target -> {
            final List found = conversion.apply(target);
            int                      count = 0;

            for (LivingEntity entity : found) {
                if (count >= max) break;
                if (isValidTarget(caster, target, entity) || (self.equals(IncludeCaster.IN_AREA) && caster == entity)) {
                    list.add(entity);
                    count++;
                }
            }
        });
        if (self.equals(IncludeCaster.TRUE)) list.add(caster);
        return list;
    }

    boolean isValidTarget(final LivingEntity caster, final LivingEntity from, final LivingEntity target) {
        if (SkillAPI.getMeta(target, MechanicListener.ARMOR_STAND) != null) return false;
        if (target instanceof TempEntity) return true;
        if (target instanceof Player && (((Player) target).getGameMode() == GameMode.SPECTATOR
                || ((Player) target).getGameMode() == GameMode.CREATIVE)) return false;

        return target != caster && SkillAPI.getSettings().isValidTarget(target) && (throughWall
                || !TargetHelper.isObstructed(from.getEyeLocation(), target.getEyeLocation()))
                && (everyone || allies == SkillAPI.getSettings().isAlly(caster, target));
    }

    public enum IncludeCaster {
        TRUE, FALSE, IN_AREA
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy