content: /*
   * BedWars1058 - A bed wars mini-game.
   * Copyright (C) 2021 Andrei Dascălu
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   *
   * Contact e-mail: andrew.dascalu@gmail.com
   */
  
  package com.andrei1058.bedwars.support.version.v1_16_R3;
  import com.andrei1058.bedwars.api.arena.team.ITeam;
  import com.andrei1058.bedwars.api.language.Language;
  import com.andrei1058.bedwars.api.language.Messages;
  import com.andrei1058.bedwars.support.version.common.VersionCommon;
  import net.minecraft.server.v1_16_R3.*;
  import org.apache.commons.lang.StringUtils;
  import org.bukkit.Location;
  import org.bukkit.craftbukkit.v1_16_R3.CraftWorld;
  import org.bukkit.craftbukkit.v1_16_R3.entity.CraftLivingEntity;
  import org.bukkit.entity.LivingEntity;
  import org.bukkit.event.entity.CreatureSpawnEvent;
  
  @SuppressWarnings("ALL")
  public class Silverfish extends EntitySilverfish {
  
      private ITeam team;
  
      private Silverfish(EntityTypes<? extends EntitySilverfish> entitytypes, net.minecraft.server.v1_16_R3.World world, ITeam bedWarsTeam) {
          super(entitytypes, world);
          this.team = bedWarsTeam;
      }
  
      @SuppressWarnings("unchecked")
      public Silverfish(EntityTypes entityTypes, net.minecraft.server.v1_16_R3.World world) {
          super(entityTypes, world);
      }
  
      @Override
      protected void initPathfinder() {
          this.goalSelector.a(1, new PathfinderGoalFloat(this));
          this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this,1.9D, false));
          this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this));
          this.goalSelector.a(3, new PathfinderGoalRandomStroll(this, 2D));
          this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 20, true, false, player -> {
              return ((EntityHuman)player).isAlive() && !team.wasMember(((EntityHuman)player).getUniqueID()) && !team.getArena().isReSpawning(((EntityHuman)player).getUniqueID())
                      && !team.getArena().isSpectator(((EntityHuman)player).getUniqueID());
          }));
          this.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget(this, IGolem.class, 20, true, false, golem -> {
              return ((IGolem)golem).getTeam() != team;
          }));
          this.targetSelector.a(4, new PathfinderGoalNearestAttackableTarget(this, Silverfish.class, 20, true, false, sf -> {
              return ((Silverfish)sf).getTeam() != team;
          }));
      }
  
      public ITeam getTeam() {
          return team;
      }
  
      public static LivingEntity spawn(Location loc, ITeam team, double speed, double health, int despawn, double damage) {
          WorldServer mcWorld = ((CraftWorld)loc.getWorld()).getHandle();
          Silverfish customEnt = new Silverfish(EntityTypes.SILVERFISH, mcWorld, team);
          customEnt.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
          customEnt.getAttributeInstance(GenericAttributes.MAX_HEALTH).setValue(health);
          customEnt.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
          customEnt.getAttributeInstance(GenericAttributes.ATTACK_DAMAGE).setValue(damage);
          customEnt.setPersistent();
          ((CraftLivingEntity)customEnt.getBukkitEntity()).setRemoveWhenFarAway(false);
          customEnt.setCustomNameVisible(true);
          mcWorld.addEntity(customEnt, CreatureSpawnEvent.SpawnReason.CUSTOM);
          customEnt.getBukkitEntity().setCustomName(Language.getDefaultLanguage().m(Messages.SHOP_UTILITY_NPC_SILVERFISH_NAME)
                  .replace("{despawn}", String.valueOf(despawn)
                          .replace("{health}", StringUtils.repeat(Language.getDefaultLanguage().m(Messages.FORMATTING_DESPAWNABLE_UTILITY_NPC_HEALTH)+" ", 10))
                          .replace("{TeamColor}", team.getColor().chat().toString())));
          return (LivingEntity) customEnt.getBukkitEntity();
      }
  
      @Override
      public void die() {
          super.die();
          team = null;
          VersionCommon.api.getVersionSupport().getDespawnablesList().remove(this.getUniqueID());
      }
  
      @Override
      public void die(DamageSource damagesource) {
          super.die(damagesource);
          team = null;
          VersionCommon.api.getVersionSupport().getDespawnablesList().remove(this.getUniqueID());
      }
  }
  