2018-11-04 14:36:33 +01:00
|
|
|
package com.songoda.epicbosses.targeting;
|
2018-11-03 14:51:08 +01:00
|
|
|
|
2018-11-19 07:25:46 +01:00
|
|
|
import com.songoda.epicbosses.holder.IActiveHolder;
|
2018-11-03 14:51:08 +01:00
|
|
|
import com.songoda.epicbosses.managers.BossTargetManager;
|
|
|
|
import com.songoda.epicbosses.utils.ServerUtils;
|
|
|
|
import lombok.Getter;
|
|
|
|
import org.bukkit.entity.Creature;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.LivingEntity;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Charles Cullen
|
|
|
|
* @version 1.0.0
|
|
|
|
* @since 30-Oct-18
|
|
|
|
*/
|
2018-11-19 07:25:46 +01:00
|
|
|
public abstract class TargetHandler<Holder extends IActiveHolder> implements ITarget {
|
2018-11-03 14:51:08 +01:00
|
|
|
|
|
|
|
@Getter protected final BossTargetManager bossTargetManager;
|
2018-11-19 07:25:46 +01:00
|
|
|
@Getter protected final Holder holder;
|
2018-11-03 14:51:08 +01:00
|
|
|
|
2018-11-19 07:25:46 +01:00
|
|
|
public TargetHandler(Holder holder, BossTargetManager bossTargetManager) {
|
|
|
|
this.holder = holder;
|
2018-11-03 14:51:08 +01:00
|
|
|
this.bossTargetManager = bossTargetManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void runTargetCycle() {
|
2018-11-14 15:23:42 +01:00
|
|
|
ServerUtils.get().runLaterAsync(10L, () -> {
|
2018-11-03 14:51:08 +01:00
|
|
|
updateTarget();
|
|
|
|
|
2018-11-19 07:25:46 +01:00
|
|
|
if(!getHolder().isDead()) runTargetCycle();
|
2018-11-03 14:51:08 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected LivingEntity getBossEntity() {
|
2018-11-19 07:25:46 +01:00
|
|
|
for(LivingEntity livingEntity : getHolder().getLivingEntityMap().values()) {
|
2018-11-03 14:51:08 +01:00
|
|
|
if(livingEntity != null && !livingEntity.isDead()) return livingEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateTarget() {
|
|
|
|
LivingEntity boss = getBossEntity();
|
|
|
|
double radius = this.bossTargetManager.getTargetRadius();
|
|
|
|
|
|
|
|
if(boss == null) return;
|
|
|
|
|
|
|
|
List<LivingEntity> nearbyEntities = new ArrayList<>();
|
|
|
|
|
|
|
|
for(Entity entity : boss.getNearbyEntities(radius, radius, radius)) {
|
|
|
|
if(!(entity instanceof Player)) continue;
|
|
|
|
|
|
|
|
LivingEntity livingEntity = (LivingEntity) entity;
|
|
|
|
|
2018-11-19 07:25:46 +01:00
|
|
|
if(livingEntity instanceof Player) {
|
|
|
|
Player player = (Player) livingEntity;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-03 14:51:08 +01:00
|
|
|
nearbyEntities.add(livingEntity);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBoss(selectTarget(nearbyEntities));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateBoss(LivingEntity newTarget) {
|
2018-11-19 07:25:46 +01:00
|
|
|
getHolder().getLivingEntityMap().values().forEach(livingEntity -> {
|
2018-11-03 14:51:08 +01:00
|
|
|
if(livingEntity != null && !livingEntity.isDead()) {
|
|
|
|
((Creature) livingEntity).setTarget(newTarget);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|