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_8_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_8_R3.*;
  import org.apache.commons.lang3.StringUtils;
  import org.bukkit.Location;
  import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
  import org.bukkit.craftbukkit.v1_8_R3.util.UnsafeList;
  import org.bukkit.entity.LivingEntity;
  import org.bukkit.event.entity.CreatureSpawnEvent;
  
  import java.lang.reflect.Field;
  
  @SuppressWarnings("ALL")
  @Deprecated
  public class IGolem extends EntityIronGolem {
  
      private ITeam team;
  
      private IGolem(World world, ITeam team) {
          super(world);
          this.team = team;
          try {
              Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
              bField.setAccessible(true);
              Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
              cField.setAccessible(true);
              bField.set(this.goalSelector, new UnsafeList());
              bField.set(this.targetSelector, new UnsafeList());
              cField.set(this.goalSelector, new UnsafeList());
              cField.set(this.targetSelector, new UnsafeList());
          } catch (IllegalAccessException | NoSuchFieldException e1) {
              e1.printStackTrace();
          }
          this.setSize(1.4F, 2.9F);
          ((Navigation) this.getNavigation()).a(true);
          this.goalSelector.a(1, new PathfinderGoalFloat(this));
          this.goalSelector.a(2, new PathfinderGoalMeleeAttack(this, 1.5D, false));
          this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
          this.goalSelector.a(3, new PathfinderGoalRandomStroll(this, 1D));
          this.goalSelector.a(4, new PathfinderGoalRandomLookaround(this));
          this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, 20, true, false, player -> {
              if (player == null) return false;
              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 -> {
              if (golem == null) return false;
              return ((IGolem)golem).getTeam() != team;
          }));
          this.targetSelector.a(4, new PathfinderGoalNearestAttackableTarget(this, Silverfish.class, 20, true, false, sf -> {
              if (sf == null) return false;
              return ((Silverfish)sf).getTeam() != team;
          }));
      }
  
      public ITeam getTeam() {
          return team;
      }
  
      public static LivingEntity spawn(Location loc, ITeam bedWarsTeam, double speed, double health, int despawn) {
          WorldServer mcWorld = ((CraftWorld) loc.getWorld()).getHandle();
          IGolem customEnt = new IGolem(mcWorld, bedWarsTeam);
  
          customEnt.getAttributeInstance(GenericAttributes.maxHealth).setValue(health);
          customEnt.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(speed);
          customEnt.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
          ((CraftLivingEntity) customEnt.getBukkitEntity()).setRemoveWhenFarAway(false);
          customEnt.setCustomNameVisible(true);
  
          customEnt.setCustomName(Language.getDefaultLanguage().m(Messages.SHOP_UTILITY_NPC_IRON_GOLEM_NAME)
                  .replace("{despawn}", String.valueOf(despawn))
                  .replace("{health}", StringUtils.repeat(Language.getDefaultLanguage().m(Messages.FORMATTING_DESPAWNABLE_UTILITY_NPC_HEALTH) + " ", 10))
                  .replace("{TeamColor}", bedWarsTeam.getColor().chat().toString()));
          mcWorld.addEntity(customEnt, CreatureSpawnEvent.SpawnReason.CUSTOM);
          return (LivingEntity) customEnt.getBukkitEntity();
      }
  
      @Override
      protected void dropDeathLoot(boolean flag, int i) {
  
      }
  
      @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());
      }
  }
  