Untitled

 avatar
unknown
plain_text
a month ago
7.1 kB
1
Indexable
package io.github.saimonovski.rageCraftCore.system.mob.zombie;

import io.github.saimonovski.rageCraftCore.RageCraftCore;
import io.github.saimonovski.rageCraftCore.system.mob.MobData;
import io.github.saimonovski.rageCraftCore.system.mob.ZombieSystem;
import io.github.saimonovski.rageCraftCore.utils.ListUtils;
import io.github.saimonovski.rageCraftCore.utils.LocationUtils;
import io.github.saimonovski.rageCraftCore.utils.LogUtil;
import io.github.saimonovski.rageCraftCore.utils.NBTUtil;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import org.bukkit.event.entity.EntityDeathEvent;


import java.util.*;

public class Hord {

    private Zombie leader;
   private final List<Zombie> zombies;
   private boolean isAttacking;
   private int range;
   private final UUID hordId;

    public Hord(Zombie leader, List<Zombie> zombies) {
        this.leader = leader;
        this.zombies = zombies;

        RageCraftCore.javaPlugin.getZombieSystem().getData().getZombieInstance().stream().forEach(instance -> {
            if(instance.isThisInstance(leader)){
                this.range = instance.getRange();
            }
        });


         this.hordId = UUID.randomUUID();
    }

    public void zombieDead(EntityDeathEvent event) {
        ZombieSystem system = RageCraftCore.javaPlugin.getZombieSystem();
        if(!(event.getEntity() instanceof Zombie zombie)) return;
        if (this.zombies.isEmpty()){
            RageCraftCore.javaPlugin.getZombieSystem().getPlugin().getZombieSystem().getHordManager().list.remove(this);
        }
        if(!event.getEntity().equals(leader)){
            LogUtil.debug("umarl czlonek hordy... Hord;zombieDead()");
            zombies.remove(zombie);
            RageCraftCore.javaPlugin.getZombieSystem().getData().getZombieInstance().stream().filter(in -> in.isThisInstance(zombie)).forEach(instance ->{
                event.getDrops().addAll(ListUtils.randomizeList(new Random().nextInt(0,100),system.getData().getDropFromZombie().get(instance.getLevel())));
             instance.dead(zombie);
            });
            return;
        }
        LogUtil.debug("umarl lider, trwa ustawianie nowego lidera... Hord;zombieDead()");
        if(zombies.remove(zombie)) {
            RageCraftCore.javaPlugin.getZombieSystem().getData().getZombieInstance().stream().filter(in -> in.isThisInstance(zombie)).forEach(instance ->{
                event.getDrops().addAll(ListUtils.randomizeList(new Random().nextInt(0,100),system.getData().getDropFromZombie().get(instance.getLevel())));
                LogUtil.debug("Drop ustawiony na: "+event.getDrops());
                instance.dead(zombie);
            });
            Collections.shuffle(zombies);
            leader = zombies.get(0);
        }
    }
    public void moveGroup() {
        LogUtil.debug("rozpoczecie przemieszczania grupy  - Hord;moveGroup()");
        if(isAttacking){
            LogUtil.debug("aktualnie zombie atakuja, przemieszczanie grupy anulowane - Hord;moveGroup()");
            return;
        }
        for (Zombie member : zombies) {
            if (!member.equals(leader)) {
                    member.setTarget(leader);
                LogUtil.debug("ustawiono cel "+member+" na "+leader);
            }
        }
    }
    public void attackPlayer(ZombieSystem system){
        if(new Random().nextBoolean()){
            return;
        }
        LogUtil.debug("rozpoczeto inicjalizacje atakowania gracza Hord;attackPlayer()");
        Bukkit.getScheduler().runTask(system.getPlugin(), () -> {
            leader.getNearbyEntities(range, range, range).stream().filter(entity -> entity instanceof Player).findAny()
                    .ifPresentOrElse(player -> {
                        LogUtil.debug("znaleziono gracza do zaatakowania trwa ustawianie celu. - Hord;attackPlayer()");
                        isAttacking = true;
                        leader.setTarget((LivingEntity) player);
                    },() -> isAttacking = false);
            if(!isAttacking){
                LogUtil.debug("nie znaleziono gracza do zaatakowania trwa pzemieszczanie grupy - Hord;attackPlayer()");
                zombies.forEach(z -> z.setTarget(leader));
                return;
            }
            LogUtil.debug("trwa ustawianie celu pozostalej grupy na gracza Hord;attackPlayer()");
            zombies.forEach(z -> z.setTarget(null));
            LogUtil.debug("wyslano task aby rozpoczac ponowny atak za 10 sekund Hord;attackPlayer()");
            Bukkit.getScheduler().runTaskLaterAsynchronously(system.getPlugin(), () -> attackPlayer(system), 20*10);
        });
    }
    public void attackBlock(){
        Random random = new Random();
        if(!random.nextBoolean()){
            return;
        }
        LogUtil.debug("starts attacking block... Hord;attackBlock()");
        if(isAttacking) return;
        zombies.forEach(zombie -> {
            Location loc1 = zombie.getLocation();
            Location loc2 = loc1.add(range, range, range);
            if(!random.nextBoolean()){
                LogUtil.debug(zombie+"anulowano planowana akcje;");
                return;
            }
            Location finalLocation = LocationUtils.getRandomLocation(loc1, loc2);
            LogUtil.debug("znaleziona lokacja to: "+finalLocation);
            Block block = finalLocation.getBlock();
            LogUtil.debug("sprawdzanie czy blok zostal postawiony przez gracza");
            if(!NBTUtil.isBlockPlacedByPlayer(block)) return;
            LogUtil.debug("sprawdzanie czy blok nie jest ciecza");
            if(block.isLiquid())return;
            LogUtil.debug("Niszczenie bloku");
            block.breakNaturally();
            Objects.requireNonNull(finalLocation.getWorld()).spawnParticle(Particle.EXPLOSION_NORMAL, finalLocation, 10, 10);
            LogUtil.debug("spawnowanie eksplozji");
            Objects.requireNonNull(finalLocation.getWorld()).createExplosion(finalLocation, 2, true, true); //siła eksplozji

        });
    }

    public void action(ZombieSystem system){
        LogUtil.debug("startowanie akcji hordy: Hord;action()");
        attackPlayer(system);
        attackBlock();
        moveGroup();
    }
    public List<Zombie> getMembers(){
        return zombies;
    }

    public Zombie getLeader() {
        return leader;
    }

    public UUID getId() {
        return hordId;
    }

    //static loader from database


    @Override
    public String toString() {
        List<String> zombieUUID = new ArrayList<>();
        zombies.forEach(zombie -> zombieUUID.add(zombie.getUniqueId().toString()));
        return "Hord{" +
                "leader=" + leader +
                ", zombies=" + zombieUUID +
                ", isAttacking=" + isAttacking +
                ", range=" + range +
                ", hordId=" + hordId +
                '}';
    }
}
Leave a Comment